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