001/*
002 * (C) Copyright 2018 Nuxeo (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 *     Mariana Cedica
018 */
019package org.nuxeo.adobe.cc;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.List;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.ecm.automation.core.Constants;
028import org.nuxeo.ecm.automation.core.annotations.Context;
029import org.nuxeo.ecm.automation.core.annotations.Operation;
030import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
031import org.nuxeo.ecm.automation.core.annotations.Param;
032import org.nuxeo.ecm.core.api.CoreSession;
033import org.nuxeo.ecm.core.api.DocumentModel;
034
035/**
036 * @since 10.2
037 */
038@Operation(id = CompoundAttach.ID, category = Constants.CAT_DOCUMENT, label = "Attach compound docs", description = "Attach compund documents.")
039public class CompoundAttach {
040
041    public static final String ID = "CompoundDocument.Attach";
042
043    private static final Log log = LogFactory.getLog(CompoundAttach.class);
044
045    public static final String COMPOUND_DOC_FACET = "CompoundDocument";
046
047    public static final String COMPOUND_DOC_DOCS_PROPERTY = "compound:docs";
048
049    @Context
050    protected CoreSession session;
051
052    @Param(name = "compoundDocs")
053    protected List<String> compoundDocs = new ArrayList<>();
054
055    @Param(name = "save", required = false)
056    protected Boolean save = true;
057
058    @OperationMethod
059    public DocumentModel run(DocumentModel doc) {
060        if (!doc.hasFacet(COMPOUND_DOC_FACET)) {
061            doc.addFacet(COMPOUND_DOC_FACET);
062        }
063        doc.setPropertyValue(COMPOUND_DOC_DOCS_PROPERTY, (Serializable) compoundDocs);
064        if (save) {
065            doc = session.saveDocument(doc);
066        }
067        return doc;
068    }
069}