001/*
002 * (C) Copyright 2006-2009 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 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020/**
021 * This RESTLET allows to update document properties
022 * @author jthimonier
023 */
024package org.nuxeo.ecm.platform.ui.web.restAPI;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.dom4j.Element;
029import org.dom4j.dom.DOMDocument;
030import org.dom4j.dom.DOMDocumentFactory;
031import org.nuxeo.ecm.core.api.NuxeoException;
032import org.nuxeo.ecm.platform.ui.web.tag.fn.LiveEditConstants;
033import org.restlet.data.CharacterSet;
034import org.restlet.data.Form;
035import org.restlet.data.MediaType;
036import org.restlet.data.Request;
037import org.restlet.data.Response;
038
039public class UpdateDocumentRestlet extends BaseStatelessNuxeoRestlet implements LiveEditConstants {
040
041    private static final Log log = LogFactory.getLog(UpdateDocumentRestlet.class);
042
043    @Override
044    protected void doHandleStatelessRequest(Request req, Response res) {
045        String repoId = (String) req.getAttributes().get("repo");
046        String docId = (String) req.getAttributes().get("docid");
047
048        DOMDocumentFactory domFactory = new DOMDocumentFactory();
049        DOMDocument result = (DOMDocument) domFactory.createDocument();
050
051        // init repo and document
052        boolean initOk = initRepositoryAndTargetDocument(res, repoId, docId);
053        if (!initOk) {
054            return;
055        }
056
057        try {
058            Form queryParameters = req.getResourceRef().getQueryAsForm();
059            for (String paramName : queryParameters.getNames()) {
060                if (!DOC_TYPE.equals(paramName)) {
061                    // treat all non doctype parameters as string fields
062                    targetDocument.setPropertyValue(paramName, getQueryParamValue(req, paramName, null));
063                }
064            }
065            session.saveDocument(targetDocument);
066            session.save();
067
068            // build the XML response document holding the ref
069            Element docElement = result.addElement(documentTag);
070            docElement.addElement(docRefTag).setText("Document " + docId + " has been updated");
071            res.setEntity(result.asXML(), MediaType.TEXT_XML);
072            res.getEntity().setCharacterSet(CharacterSet.UTF_8);
073        } catch (NuxeoException e) {
074            log.error(e.getMessage(), e);
075            handleError(res, e);
076        }
077    }
078
079}