[opencms-dev] Programatically loading Structured Content

Roman Uhlig roman.uhlig at knve.de
Wed Nov 15 12:19:42 CET 2006


Here are some snippets of our code which might help answering your questions:

1. Dynamically switching projects

CmsJspLoginBean cms = new CmsJspLoginBean(pageContext, request, response);
CmsObject cmso = cms.getCmsObject();
cmso.getRequestContext().setCurrentProject(cmso.readProject("MyProject"));

2. Creating resources

int m_restype_folder_id;
int m_restype_customxmlpage_id;

CmsResourceManager res_manager = OpenCms.getResourceManager();
try {
	m_restype_folder_id = res_manager.getResourceType("folder").getTypeId();
	m_restype_customxmlpage_id =
res_manager.getResourceType("MyXmltype").getTypeId();
} catch (CmsLoaderException e) {
	log.error(e.getMessage());
}

CmsJspLoginBean cms = new CmsJspLoginBean(pageContext, request, response);
CmsObject cmso = cms.getCmsObject();

cmso.createResource(s_uri_folder, m_restype_folder_id);
cmso.writePropertyObject(s_uri_folder, new CmsProperty("Title", "New
Folder", null));
cmso.writePropertyObject(s_uri_folder, new CmsProperty("NavText", "New
Folder", null));
cmso.unlockResource(s_uri_folder);
cmso.publishResource(s_uri_folder);

cmso.createResource(s_uri, m_restype_customxmlpage_id);
cmso.writePropertyObject(s_uri, new CmsProperty("Title", "New XmlContent",
null));
cmso.unlockResource(s_uri);
cmso.publishResource(s_uri);


Feel free to ask if you have difficulties with parts of the code.

Roman



Robert Read-3 wrote:
> 
> Hi All,
> 
> I am attempting to write an interface that will load data rows from an
> external system as Structured Content via the OpenCms API.
> 
> So far I have done this (the lines prefixed '+' are my actual code lines):
> 
> + CmsDefaultUsers cdu = new CmsDefaultUsers();
> + String guestUser = cdu.getUserGuest();
> + CmsObject cmso = OpenCms.initCmsObject(guestUser);
> + cmso.loginUser("User", "password");
> 	 :
>        :
> loop over all rows {
> 		:
>        do stuff to format rows ready to create a resource
> 		:
> 		:
>        + resource = cmso.createResource(cp.getCatalogueNumber(), 600,
> content, propertyList);
> 
> 	 + file = new CmsFile(resource);
> 
> 	 + file.setContents(content);  // is this required?
> 
> 	 // persist document as CmsFile to VFS
> 	 + cmso.writeFile(file);
> 	
> } // end loop
> 
> 
> On the line that calls cmso.createResource(...) I get the following
> exception: 
> 
> 
> org.opencms.file.CmsVfsException: Error creating the resource "/ 789A".
> 	at
> org.opencms.file.CmsVfsException.createException(CmsVfsException.java:79)
> 	at org.opencms.db.CmsDbContext.throwException(CmsDbContext.java:213)
> 	at org.opencms.db.CmsDbContext.report(CmsDbContext.java:197)
> 	at
> org.opencms.db.CmsSecurityManager.createResource(CmsSecurityManager.java:1094)
> 	at
> org.opencms.file.types.A_CmsResourceType.createResource(A_CmsResourceType.java:291)
> 	at
> org.opencms.file.types.CmsResourceTypeXmlContent.createResource(CmsResourceTypeXmlContent.java:110)
> 	at org.opencms.file.CmsObject.createResource(CmsObject.java:621)
> 	at nz.ac.auckland.onlinecalendar.util.Loader.load(Loader.java:102)
> 	at nz.ac.auckland.onlinecalendar.util.Loader.<init>(Loader.java:32)
> 	at
> org.apache.jsp.course_005fload_005faction_jsp._jspService(course_005fload_005faction_jsp.java:44)
> 	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
> 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
> 	at
> org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
> 	at
> org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
> 	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
> 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
> 	at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
> 	at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
> 	at
> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
> 	at
> org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
> 	at
> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
> 	at
> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
> 	at
> org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
> 	at
> org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
> 	at
> org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
> 	at 
> 
> org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
> 	at
> org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
> 	at
> org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
> 	at
> org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
> 	at java.lang.Thread.run(Unknown Source)
> Caused by: org.opencms.file.CmsVfsException: This operation is not allowed
> in the "Online" project.
> 	at
> org.opencms.db.CmsSecurityManager.checkOfflineProject(CmsSecurityManager.java:533)
> 	at
> org.opencms.db.CmsSecurityManager.createResource(CmsSecurityManager.java:1091)
> 	... 26 more
> 
> 
> 
> Right near the bottom of the stack trace it says 'This operation is not
> allowed in the "Online" project.' - which makes perfect sense.  
> 
> So I am thinking...
> 1) I need to create an "Offline Project" to work on
> 2) create my resource in that project 
> 
> 
> Does anyone have any simple ideas of how to achieve this or what steps I
> may have missed?
> 
> Cheers,
> Robert
> 
> 
> Robert Read
> Analyst-Programmer
> Group Applications
> Information Technology Systems & Services
> University of Auckland
> Telephone: +64 9 373 7599 ext 82475
>  
> Email: r.read at auckland.ac.nz
> Web:  http://www.auckland.ac.nz/
> 
> ________________________________________
> The information contained in this email message is intended only for the
> named recipient(s). 
> If you are not the intended recipient(s), you must not peruse, use,
> disseminate, distribute or copy this email or attachments. 
> If you receive this email in error please immediately notify the sender
> and delete the original message.
> 
> 
> _______________________________________________
> 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
> 
> 

-- 
View this message in context: http://www.nabble.com/Programatically-loading-Structured-Content-tf2629797.html#a7355999
Sent from the OpenCMS - Dev mailing list archive at Nabble.com.




More information about the opencms-dev mailing list