001/*
002 * (C) Copyright 2016 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 *     Miguel Nixo
018 *     Ricardo Dias
019 *     Bertrand Chauvin
020 *     Thibaud Arguillere
021 */
022package org.nuxeo.ecm.automation.core.operations.document;
023
024import org.nuxeo.ecm.automation.OperationContext;
025import org.nuxeo.ecm.automation.OperationException;
026import org.nuxeo.ecm.automation.core.Constants;
027import org.nuxeo.ecm.automation.core.annotations.Context;
028import org.nuxeo.ecm.automation.core.annotations.Operation;
029import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
030import org.nuxeo.ecm.automation.core.annotations.Param;
031import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector;
032import org.nuxeo.ecm.core.api.CoreSession;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.api.IdRef;
035import org.nuxeo.ecm.core.api.PathRef;
036
037/**
038 * @since 8.3
039 */
040@Operation(id = CopySchema.ID, category = Constants.CAT_DOCUMENT, label = "Copy Schema",
041    description = "Copy all the properties from the schema of the source into the input document. Either sourceId or sourcePath parameter should be filled. When both are filled, sourceId will be used. If saveDocument is true, the document is saved. If save is true, the session is saved (setting save to true and saveDocument to false has no effect, the session will not be saved)")
042public class CopySchema {
043
044    public static final String ID = "Document.CopySchema";
045
046    @Context
047    protected OperationContext context;
048
049    @Context
050    protected CoreSession session;
051    
052    @Param(name = "schema")
053    protected String schema;
054
055    @Param(name = "sourceId", required = false)
056    protected String sourceId;
057
058    @Param(name = "sourcePath", required = false)
059    protected String sourcePath;
060
061    @Param(name = "save", required = false, values = { "true" })
062    protected boolean save = true;
063
064    @Param(name = "saveDocument", required = false, values = { "true" })
065    protected boolean saveDocument = true;
066
067    private DocumentModel getDocumentFromIdOrPath() throws OperationException {
068        if (sourceId != null) {
069            return session.getDocument(new IdRef(sourceId));
070        } else if (sourcePath != null) {
071            return session.getDocument(new PathRef(sourcePath));
072        } else {
073            throw new OperationException("No document id or path was provided");
074        }
075    }
076
077    private void copySchemaProperties(DocumentModel source, DocumentModel target) {
078        target.setProperties(schema, source.getProperties(schema));
079    }
080
081    @OperationMethod(collector = DocumentModelCollector.class)
082    public DocumentModel run(DocumentModel target) throws OperationException {
083        DocumentModel source = getDocumentFromIdOrPath();
084        copySchemaProperties(source, target);
085
086        if (saveDocument) {
087            target = session.saveDocument(target);
088            if (save) {
089                session.save();
090            }
091        }
092        return target;
093    }
094}