001/*
002 * (C) Copyright 2006-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 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.core.rest;
023
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.NuxeoException;
027import org.nuxeo.ecm.core.api.pathsegment.PathSegmentService;
028import org.nuxeo.ecm.core.versioning.VersioningService;
029import org.nuxeo.ecm.webengine.WebException;
030import org.nuxeo.ecm.webengine.forms.FormData;
031import org.nuxeo.ecm.webengine.model.WebContext;
032import org.nuxeo.runtime.api.Framework;
033
034/**
035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
036 */
037public class DocumentHelper {
038
039    // Utility class.
040    private DocumentHelper() {
041    }
042
043    public static DocumentModel createDocument(WebContext context, DocumentModel parent, String name) {
044        try {
045            PathSegmentService pss = Framework.getService(PathSegmentService.class);
046            CoreSession session = context.getCoreSession();
047            FormData form = context.getForm();
048            String type = form.getDocumentType();
049            if (type == null) {
050                throw new WebException("Invalid argument exception. No doc type specified");
051            }
052            DocumentModel newDoc = session.createDocumentModel(type);
053            form.fillDocument(newDoc);
054            if (name != null) {
055                newDoc.setPropertyValue("dc:title", name);
056            }
057            newDoc.setPathInfo(parent.getPathAsString(), pss.generatePathSegment(newDoc));
058            newDoc = session.createDocument(newDoc);
059            newDoc.setPropertyValue("dc:title", newDoc.getName());
060            session.saveDocument(newDoc);
061            session.save();
062            return newDoc;
063        } catch (NuxeoException e) {
064            throw WebException.wrap("Failed to create document: " + name, e);
065        }
066    }
067
068    public static DocumentModel updateDocument(WebContext ctx, DocumentModel doc) {
069        try {
070            FormData form = ctx.getForm();
071            form.fillDocument(doc);
072            doc.putContextData(VersioningService.VERSIONING_OPTION, form.getVersioningOption());
073            doc = ctx.getCoreSession().saveDocument(doc);
074            ctx.getCoreSession().save();
075            return doc;
076        } catch (NuxeoException e) {
077            throw WebException.wrap("Failed to update document", e);
078        }
079    }
080
081}