001/*
002 * (C) Copyright 2006-2011 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 */
019package org.nuxeo.ecm.automation.core.operations.document;
020
021import java.io.IOException;
022
023import org.nuxeo.ecm.automation.ConflictOperationException;
024import org.nuxeo.ecm.automation.core.Constants;
025import org.nuxeo.ecm.automation.core.annotations.Context;
026import org.nuxeo.ecm.automation.core.annotations.Operation;
027import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
028import org.nuxeo.ecm.automation.core.annotations.Param;
029import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector;
030import org.nuxeo.ecm.automation.core.util.DocumentHelper;
031import org.nuxeo.ecm.automation.core.util.Properties;
032import org.nuxeo.ecm.core.api.CoreSession;
033import org.nuxeo.ecm.core.api.DocumentModel;
034
035/**
036 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
037 */
038@Operation(id = UpdateDocument.ID, category = Constants.CAT_DOCUMENT, label = "Update Properties", description = "Set multiple properties on the input document. The properties are specified as <i>key=value</i> pairs separated by a new line. The key used for a property is the property xpath. To specify multi-line values you can use a \\ character followed by a new line. <p>Example:<pre>dc:title=The Document Title<br>dc:description=foo bar</pre>For updating a date, you will need to expose the value as ISO 8601 format, for instance : <p>Example:<pre>dc:title=The Document Title<br>dc:issued=@{org.nuxeo.ecm.core.schema.utils.DateParser.formatW3CDateTime(CurrentDate.date)}</pre><p>Returns back the updated document.<p>To update a multi-valued field with multiple values:<pre>custom:multivalued=a,b,c,d</pre>")
039public class UpdateDocument {
040
041    public static final String ID = "Document.Update";
042
043    @Context
044    protected CoreSession session;
045
046    @Param(name = "properties")
047    protected Properties properties;
048
049    @Param(name = "save", required = false, values = "true")
050    protected boolean save = true;
051
052    @Param(name = "changeToken", required = false)
053    protected String changeToken = null;
054
055    @OperationMethod(collector = DocumentModelCollector.class)
056    public DocumentModel run(DocumentModel doc) throws ConflictOperationException, IOException {
057
058        if (changeToken != null) {
059            // Check for dirty update
060            String repoToken = doc.getChangeToken();
061            if (!changeToken.equals(repoToken)) {
062                throw new ConflictOperationException(doc);
063            }
064        }
065
066        DocumentHelper.setProperties(session, doc, properties);
067        if (save) {
068            doc = session.saveDocument(doc);
069        }
070        return doc;
071    }
072
073}