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.api.versioning.VersioningService;
029import org.nuxeo.ecm.webengine.forms.FormData;
030import org.nuxeo.ecm.webengine.model.WebContext;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
035 */
036public class DocumentHelper {
037
038    // Utility class.
039    private DocumentHelper() {
040    }
041
042    public static DocumentModel createDocument(WebContext context, DocumentModel parent, String name) {
043        FormData form = context.getForm();
044        String type = form.getDocumentType();
045        if (type == null) {
046            throw new NuxeoException("Invalid argument exception. No doc type specified");
047        }
048
049        try {
050            PathSegmentService pss = Framework.getService(PathSegmentService.class);
051            CoreSession session = context.getCoreSession();
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            e.addInfo("Failed to create document: " + name);
065            throw e;
066        }
067    }
068
069    public static DocumentModel updateDocument(WebContext ctx, DocumentModel doc) {
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    }
077
078}