001/* 002 * (C) Copyright 2008 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id: CreateDocumentRestlet.java 30586 2008-02-26 14:30:17Z ogrisel $ 020 */ 021 022package org.nuxeo.ecm.platform.ui.web.restAPI; 023 024import java.io.Serializable; 025 026import org.dom4j.Element; 027import org.dom4j.dom.DOMDocument; 028import org.dom4j.dom.DOMDocumentFactory; 029import org.nuxeo.ecm.core.api.CloseableCoreSession; 030import org.nuxeo.ecm.core.api.CoreInstance; 031import org.nuxeo.ecm.core.api.DocumentModel; 032import org.nuxeo.ecm.core.api.IdRef; 033import org.nuxeo.ecm.core.api.NuxeoException; 034import org.nuxeo.ecm.core.api.pathsegment.PathSegmentService; 035import org.nuxeo.ecm.platform.ui.web.tag.fn.LiveEditConstants; 036import org.nuxeo.runtime.api.Framework; 037import org.restlet.Request; 038import org.restlet.Response; 039import org.restlet.data.CharacterSet; 040import org.restlet.data.Form; 041import org.restlet.data.MediaType; 042import org.restlet.representation.Representation; 043import org.restlet.representation.StringRepresentation; 044 045/** 046 * Allow the creation of a new document of the specified document type 047 * 048 * @author Olivier Grisel <[email protected]> 049 */ 050public class CreateDocumentRestlet extends BaseNuxeoRestlet implements LiveEditConstants, Serializable { 051 052 private static final long serialVersionUID = -7223939557577366747L; 053 054 @Override 055 public void handle(Request req, Response res) { 056 logDeprecation(); 057 String repo = (String) req.getAttributes().get("repo"); 058 if (repo == null || repo.equals("*")) { 059 handleError(res, "you must specify a repository"); 060 return; 061 } 062 063 DocumentModel parentDm; 064 try (CloseableCoreSession documentManager = CoreInstance.openCoreSession(repo)) { 065 String parentDocRef = (String) req.getAttributes().get("parentdocid"); 066 if (parentDocRef != null) { 067 parentDm = documentManager.getDocument(new IdRef(parentDocRef)); 068 } else { 069 handleError(res, "you must specify a valid document IdRef for the parent document"); 070 return; 071 } 072 073 PathSegmentService pss = Framework.getService(PathSegmentService.class); 074 String docTypeName = getQueryParamValue(req, DOC_TYPE, DEFAULT_DOCTYPE); 075 String titleField = "dublincore:title"; 076 String title = getQueryParamValue(req, titleField, "New " + docTypeName); 077 078 DocumentModel newDm = documentManager.createDocumentModel(docTypeName); 079 Form queryParameters = req.getResourceRef().getQueryAsForm(); 080 for (String paramName : queryParameters.getNames()) { 081 if (!DOC_TYPE.equals(paramName)) { 082 // treat all non doctype parameters as string fields 083 newDm.setPropertyValue(paramName, getQueryParamValue(req, paramName, null)); 084 // TODO: handle multi-valued parameters as StringList fields 085 } 086 // override the title for consistency 087 newDm.setPropertyValue(titleField, title); 088 } 089 // create the document in the repository 090 newDm.setPathInfo(parentDm.getPathAsString(), pss.generatePathSegment(newDm)); 091 newDm = documentManager.createDocument(newDm); 092 documentManager.save(); 093 094 // build the XML response document holding the ref 095 DOMDocumentFactory domFactory = new DOMDocumentFactory(); 096 DOMDocument resultDocument = (DOMDocument) domFactory.createDocument(); 097 Element docElement = resultDocument.addElement(documentTag); 098 docElement.addElement(docRepositoryTag).setText(newDm.getRepositoryName()); 099 docElement.addElement(docRefTag).setText(newDm.getRef().toString()); 100 docElement.addElement(docTitleTag).setText(newDm.getTitle()); 101 docElement.addElement(docPathTag).setText(newDm.getPathAsString()); 102 Representation rep = new StringRepresentation(resultDocument.asXML(), MediaType.APPLICATION_XML); 103 rep.setCharacterSet(CharacterSet.UTF_8); 104 res.setEntity(rep); 105 } catch (NuxeoException e) { 106 handleError(res, e); 107 } 108 } 109 110}