[opencms-dev] Issues programmatically creating resources

Deiverson Silveira deiverson at solutioncms.com
Tue Jun 28 14:47:13 CEST 2011


Thank you very much for code!

2011/6/28 Yves Glodt <yg at mind.lu>

> Thanks go to Michael, I got it working with his suggestion. Here my final
> code:
>
>
>
> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" session="false" %>
> <%@ page import="java.io.*, java.lang.*, java.util.*, java.text.*" %>
> <%@ page import="javax.servlet.*, javax.servlet.http.*" %>
> <%@ page import="org.opencms.*, org.opencms.main.*, org.opencms.flex.*, org.opencms.jsp.*, org.opencms.file.*, org.opencms.file.types.*" %>
> <%@ page import="org.apache.commons.lang.*" %>
> <%@ page import="org.apache.commons.fileupload.*, org.apache.commons.fileupload.disk.*, org.apache.commons.fileupload.
> servlet.*" %>
> <%
>
> if (!request.getMethod().equals("POST")) {
> 	response.sendRedirect("http://my.site.biz/");
> 	return;
> }
>
> FileItemFactory factory = new DiskFileItemFactory();
> ServletFileUpload upload = new ServletFileUpload(factory);
>
> List<FileItem> items = null;
>
> try {
> 	items = upload.parseRequest(request);
> } catch (FileUploadException e) {
> 	out.println("Error parsing request");
> 	System.out.println(request.getRemoteAddr()+": Import: Error parsing request.");
> 	return;
> }
>
> Iterator<FileItem> iter = items.iterator();
>
> Integer no_dossier = null;
> String no_article = null;
> String filename = null;
> byte[] bytes = null;
>
> while (iter.hasNext()) {
> 	FileItem item = (FileItem) iter.next();
>
> 	if (item.isFormField()) {
>
> 		if (item.getFieldName().equals("no_dossier")) {
> 			no_dossier = Integer.parseInt(item.getString());
> 		}
>
> 		if (item.getFieldName().equals("no_article")) {
> 			no_article = item.getString();
> 		}
>
> 	} else {
> 		filename = item.getName();
> 		bytes = item.get();
> 	}
> }
>
> try {
>
> 	CmsObject cms = OpenCms.initCmsObject("Guest");
> 	cms.loginUser("contentcreator", "thepassword");
>
> 	CmsProject project = cms.readProject("Offline");
> 	cms.getRequestContext().setCurrentProject(project);
>
> 	cms.getRequestContext().setSiteRoot("/sites/my.site.biz/");
> 	
>
> 	if (!cms.existsResource("/images")) {
> 		System.out.println("Creating /images");
> 		cms.createResource("/images", CmsResourceTypeFolder.RESOURCE_TYPE_ID);
> 	}
>
> 	if (!cms.existsResource("/images/articles")) {
> 		System.out.println("Creating /images/articles");
> 		cms.createResource("/images/articles", CmsResourceTypeFolder.RESOURCE_TYPE_ID);
> 	}
>
> 	if (!cms.existsResource("/images/articles/"+no_dossier)) {
> 		System.out.println("Creating /images/articles/"+no_dossier);
> 		cms.createResource("/images/articles/"+no_dossier, CmsResourceTypeFolder.RESOURCE_TYPE_ID);
> 	}
>
> 	if (!cms.existsResource("/images/articles/"+no_dossier+"/"+no_article)) {
> 		System.out.println("Creating /images/articles/"+no_dossier+"/"+no_article);
> 		cms.createResource("/images/articles/"+no_dossier+"/"+no_article, CmsResourceTypeFolder.RESOURCE_TYPE_ID);
> 	}
>
> 	String newResname = cms.getRequestContext().getFileTranslator().translateResource("/images/articles/"+no_dossier+"/"+no_article+"/" + filename);
> 	int resTypeId = OpenCms.getResourceManager().getDefaultTypeForName(filename).getTypeId();
> 	cms.createResource(newResname, resTypeId, bytes, null);
>
> 	//cms.unlockResource("/images/articles");
> 	//cms.unlockResource("/images/articles/"+no_dossier);
> 	//cms.unlockResource("/images/articles/"+no_dossier+"/"+no_article);
>
> 	cms.publishResource("/images/articles");
> 	//OpenCms.getPublishManager().publishResource(cms, folder);
> 	//OpenCms.getPublishManager().waitWhileRunning();
>
> } catch (Exception e) {
> 	out.println("Error: "+e.getMessage() );
> }
>
> %>
>
>
>
-- 
*
www.solutioncms.com
The Brazilian
Official Provider OpenCms
*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20110628/6c820771/attachment.htm>


More information about the opencms-dev mailing list