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 static org.jboss.seam.ScopeType.EVENT;
027
028import org.dom4j.Element;
029import org.dom4j.dom.DOMDocument;
030import org.dom4j.dom.DOMDocumentFactory;
031import org.jboss.seam.annotations.In;
032import org.jboss.seam.annotations.Name;
033import org.jboss.seam.annotations.Scope;
034import org.nuxeo.ecm.core.api.CoreSession;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.core.api.IdRef;
037import org.nuxeo.ecm.core.api.NuxeoException;
038import org.nuxeo.ecm.core.api.pathsegment.PathSegmentService;
039import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
040import org.nuxeo.ecm.platform.ui.web.tag.fn.LiveEditConstants;
041import org.nuxeo.ecm.platform.util.RepositoryLocation;
042import org.nuxeo.runtime.api.Framework;
043import org.restlet.data.CharacterSet;
044import org.restlet.data.Form;
045import org.restlet.data.MediaType;
046import org.restlet.data.Request;
047import org.restlet.data.Response;
048import org.restlet.resource.Representation;
049import org.restlet.resource.StringRepresentation;
050
051/**
052 * Allow the creation of a new document of the specified document type
053 *
054 * @author Olivier Grisel <ogrisel@nuxeo.com>
055 */
056@Name("createDocumentRestlet")
057@Scope(EVENT)
058public class CreateDocumentRestlet extends BaseNuxeoRestlet implements LiveEditConstants, Serializable {
059
060    private static final long serialVersionUID = -7223939557577366747L;
061
062    @In(create = true)
063    protected transient NavigationContext navigationContext;
064
065    protected CoreSession documentManager;
066
067    @Override
068    public void handle(Request req, Response res) {
069        String repo = (String) req.getAttributes().get("repo");
070        if (repo == null || repo.equals("*")) {
071            handleError(res, "you must specify a repository");
072            return;
073        }
074
075        DocumentModel parentDm;
076        try {
077            navigationContext.setCurrentServerLocation(new RepositoryLocation(repo));
078            documentManager = navigationContext.getOrCreateDocumentManager();
079            String parentDocRef = (String) req.getAttributes().get("parentdocid");
080            if (parentDocRef != null) {
081                parentDm = documentManager.getDocument(new IdRef(parentDocRef));
082            } else {
083                handleError(res, "you must specify a valid document IdRef for the parent document");
084                return;
085            }
086        } catch (NuxeoException e) {
087            handleError(res, e);
088            return;
089        }
090
091        PathSegmentService pss = Framework.getService(PathSegmentService.class);
092        String docTypeName = getQueryParamValue(req, DOC_TYPE, DEFAULT_DOCTYPE);
093        String titleField = "dublincore:title";
094        String title = getQueryParamValue(req, titleField, "New " + docTypeName);
095
096        try {
097            DocumentModel newDm = documentManager.createDocumentModel(docTypeName);
098            Form queryParameters = req.getResourceRef().getQueryAsForm();
099            for (String paramName : queryParameters.getNames()) {
100                if (!DOC_TYPE.equals(paramName)) {
101                    // treat all non doctype parameters as string fields
102                    newDm.setPropertyValue(paramName, getQueryParamValue(req, paramName, null));
103                    // TODO: handle multi-valued parameters as StringList fields
104                }
105                // override the title for consistency
106                newDm.setPropertyValue(titleField, title);
107            }
108            // create the document in the repository
109            newDm.setPathInfo(parentDm.getPathAsString(), pss.generatePathSegment(newDm));
110            newDm = documentManager.createDocument(newDm);
111            documentManager.save();
112
113            // build the XML response document holding the ref
114            DOMDocumentFactory domFactory = new DOMDocumentFactory();
115            DOMDocument resultDocument = (DOMDocument) domFactory.createDocument();
116            Element docElement = resultDocument.addElement(documentTag);
117            docElement.addElement(docRepositoryTag).setText(newDm.getRepositoryName());
118            docElement.addElement(docRefTag).setText(newDm.getRef().toString());
119            docElement.addElement(docTitleTag).setText(newDm.getTitle());
120            docElement.addElement(docPathTag).setText(newDm.getPathAsString());
121            Representation rep = new StringRepresentation(resultDocument.asXML(), MediaType.APPLICATION_XML);
122            rep.setCharacterSet(CharacterSet.UTF_8);
123            res.setEntity(rep);
124        } catch (NuxeoException e) {
125            handleError(res, e);
126        }
127    }
128
129}