[opencms-dev] How to make CmsObject use offline project instead ofonline (createSibling problem)?
Jonathan Woods
jonathan.woods at scintillance.com
Tue May 23 15:18:31 CEST 2006
Jani -
The notion of the 'current project' is part of the CMS session state, just
as is the 'current site root'. The session state is modelled in
CmsRequestContext, which itself is accessible from an instance of CmsObject,
so you can set the current project thus:
CmsProject project = _cms.readProject(<name of the offline project>);
/*
or use the method variation which takes an int project id; is either 3 or 4
for
Offline; I forget which, and I forget how to obtain this safely for any
default installation...
CmsProject = _cms.readProject(3);
*/
_cms.getRequestContext().setCurrentProject(project);
Are you sure it's wise to stash away a reference to the CmsObject given to
your class on initialisation? I've seen this approach used by other people,
but the truth is that in general you don't know (i) what's been done to it
in the meantime, nor (ii) whether it is being used by other threads at the
same time as you. There is no reason why you can't instantiate your own
CmsObject and use that to carry out operations you want, because it's a
bean-like thing. This is the approach used in CmsShell, which (given only
parameters about the configuration of a running OpenCms instance and access
to its classes) can do just about anything. Here's some code that would do
this:
// Get instances of CmsObject from static method on class OpenCms.
// Means shown is standard way of initialising without needing to know
credentials.
CmsObject cmsObject =
OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserGuest());
// Then log in the CmsObject instance as the Admin user.
cmsObject.loginUser("Admin", "admin password here");
/* Other stuff necessary - including setting current project and current
site root; then you can use cmsObject freely.
*/
Jon
-----Original Message-----
From: opencms-dev-bounces at opencms.org
[mailto:opencms-dev-bounces at opencms.org] On Behalf Of Jani Kankaanpää
Sent: 23 May 2006 13:37
To: opencms-dev at opencms.org
Subject: [opencms-dev] How to make CmsObject use offline project instead
ofonline (createSibling problem)?
Hi,
I'm trying to create a sibling (with a different locale) automatically when
a user creates a new html resource for a site in offline mode. The problem
is that by default OpenCms object uses the project in online mode and I
don't know how to make it use offline project instead. Currently, when I try
to create a sibling, I get CmsVfsResourceNotFoundException. When I try
cmsObject.existsResource(myResource.getRootPath()), it returns false (for
unpublished resources).
Here's the relevant part of the code:
---
public class MyActionClass implements I_CmsModuleAction {
private CmsObject cms = null;
public void initialize(CmsObject _cms, CmsConfigurationManager arg1,
CmsModule arg2) {
OpenCms.addCmsEventListener(this);
cms = _cms;
}
public void cmsEvent(CmsEvent event) {
// Do stuff to figure out that a new resource was created
// and we want to create a sibling of it
String src = "/sites/default/mysite/index.html";
String dest = "/sites/default/mysite/fi/index.html";
List props = new ArrayList();
try {
cms.createSibling(src, dest, props);
} catch (Exception e) {
// we get
org.opencms.file.CmsVfsResourceNotFoundException
// The VFS folder '/sites/default/mysite/fi' DOES exist
}
}
}
---
Any ideas? How can I force CmsObject to do createSibling, existsResource,
and other actions on offline project instead of online project?
Br, Jani
_______________________________________________
This mail is sent to you from the opencms-dev mailing list To change your
list options, or to unsubscribe from the list, please visit
http://lists.opencms.org/mailman/listinfo/opencms-dev
More information about the opencms-dev
mailing list