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.Serializable;
022
023import org.nuxeo.ecm.automation.OperationException;
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.core.api.CoreSession;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.model.Property;
033import org.nuxeo.ecm.core.schema.types.SimpleType;
034import org.nuxeo.ecm.core.schema.types.Type;
035
036/**
037 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
038 */
039@Operation(id = SetDocumentProperty.ID, category = Constants.CAT_DOCUMENT, label = "Update Property", description = "Set a single property value on the input document. The property is specified using its xpath. The document is automatically saved if 'save' parameter is true. If you unset the 'save' you need to save it later using Save Document operation. Return the modified document.")
040public class SetDocumentProperty {
041
042    public static final String ID = "Document.SetProperty";
043
044    @Context
045    protected CoreSession session;
046
047    @Param(name = "xpath")
048    protected String xpath;
049
050    @Param(name = "value", required = false)
051    protected Serializable value;
052
053    @Param(name = "save", required = false, values = "true")
054    protected boolean save = true;
055
056    @OperationMethod(collector = DocumentModelCollector.class)
057    public DocumentModel run(DocumentModel doc) throws OperationException {
058        Property p = doc.getProperty(xpath);
059        Type type = p.getField().getType();
060        if (!type.isSimpleType()) {
061            throw new OperationException("Only scalar types can be set using update operation");
062        }
063        if (value == null) {
064            p.setValue(null);
065        } else if (value.getClass() == String.class) {
066            p.setValue(((SimpleType) type).getPrimitiveType().decode((String) value));
067        } else {
068            p.setValue(value);
069        }
070        if (save) {
071            doc = session.saveDocument(doc);
072        }
073
074        return doc;
075    }
076
077}