001/* 002 * (C) Copyright 2008 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id: CreateDocumentRestlet.java 30586 2008-02-26 14:30:17Z ogrisel $ 018 */ 019 020package org.nuxeo.ecm.platform.ui.web.restAPI; 021 022import java.io.Serializable; 023 024import static org.jboss.seam.ScopeType.EVENT; 025 026import org.dom4j.Element; 027import org.dom4j.dom.DOMDocument; 028import org.dom4j.dom.DOMDocumentFactory; 029import org.jboss.seam.annotations.In; 030import org.jboss.seam.annotations.Name; 031import org.jboss.seam.annotations.Scope; 032import org.nuxeo.ecm.core.api.CoreSession; 033import org.nuxeo.ecm.core.api.DocumentModel; 034import org.nuxeo.ecm.core.api.IdRef; 035import org.nuxeo.ecm.core.api.NuxeoException; 036import org.nuxeo.ecm.core.api.pathsegment.PathSegmentService; 037import org.nuxeo.ecm.platform.ui.web.api.NavigationContext; 038import org.nuxeo.ecm.platform.ui.web.tag.fn.LiveEditConstants; 039import org.nuxeo.ecm.platform.util.RepositoryLocation; 040import org.nuxeo.runtime.api.Framework; 041import org.restlet.data.CharacterSet; 042import org.restlet.data.Form; 043import org.restlet.data.MediaType; 044import org.restlet.data.Request; 045import org.restlet.data.Response; 046import org.restlet.resource.Representation; 047import org.restlet.resource.StringRepresentation; 048 049/** 050 * Allow the creation of a new document of the specified document type 051 * 052 * @author Olivier Grisel <ogrisel@nuxeo.com> 053 */ 054@Name("createDocumentRestlet") 055@Scope(EVENT) 056public class CreateDocumentRestlet extends BaseNuxeoRestlet implements LiveEditConstants, Serializable { 057 058 private static final long serialVersionUID = -7223939557577366747L; 059 060 @In(create = true) 061 protected transient NavigationContext navigationContext; 062 063 protected CoreSession documentManager; 064 065 @Override 066 public void handle(Request req, Response res) { 067 String repo = (String) req.getAttributes().get("repo"); 068 if (repo == null || repo.equals("*")) { 069 handleError(res, "you must specify a repository"); 070 return; 071 } 072 073 DocumentModel parentDm; 074 try { 075 navigationContext.setCurrentServerLocation(new RepositoryLocation(repo)); 076 documentManager = navigationContext.getOrCreateDocumentManager(); 077 String parentDocRef = (String) req.getAttributes().get("parentdocid"); 078 if (parentDocRef != null) { 079 parentDm = documentManager.getDocument(new IdRef(parentDocRef)); 080 } else { 081 handleError(res, "you must specify a valid document IdRef for the parent document"); 082 return; 083 } 084 } catch (NuxeoException e) { 085 handleError(res, e); 086 return; 087 } 088 089 PathSegmentService pss = Framework.getService(PathSegmentService.class); 090 String docTypeName = getQueryParamValue(req, DOC_TYPE, DEFAULT_DOCTYPE); 091 String titleField = "dublincore:title"; 092 String title = getQueryParamValue(req, titleField, "New " + docTypeName); 093 094 try { 095 DocumentModel newDm = documentManager.createDocumentModel(docTypeName); 096 Form queryParameters = req.getResourceRef().getQueryAsForm(); 097 for (String paramName : queryParameters.getNames()) { 098 if (!DOC_TYPE.equals(paramName)) { 099 // treat all non doctype parameters as string fields 100 newDm.setPropertyValue(paramName, getQueryParamValue(req, paramName, null)); 101 // TODO: handle multi-valued parameters as StringList fields 102 } 103 // override the title for consistency 104 newDm.setPropertyValue(titleField, title); 105 } 106 // create the document in the repository 107 newDm.setPathInfo(parentDm.getPathAsString(), pss.generatePathSegment(newDm)); 108 newDm = documentManager.createDocument(newDm); 109 documentManager.save(); 110 111 // build the XML response document holding the ref 112 DOMDocumentFactory domFactory = new DOMDocumentFactory(); 113 DOMDocument resultDocument = (DOMDocument) domFactory.createDocument(); 114 Element docElement = resultDocument.addElement(documentTag); 115 docElement.addElement(docRepositoryTag).setText(newDm.getRepositoryName()); 116 docElement.addElement(docRefTag).setText(newDm.getRef().toString()); 117 docElement.addElement(docTitleTag).setText(newDm.getTitle()); 118 docElement.addElement(docPathTag).setText(newDm.getPathAsString()); 119 Representation rep = new StringRepresentation(resultDocument.asXML(), MediaType.APPLICATION_XML); 120 rep.setCharacterSet(CharacterSet.UTF_8); 121 res.setEntity(rep); 122 } catch (NuxeoException e) { 123 handleError(res, e); 124 } 125 } 126 127}