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.util.ArrayList;
022import java.util.List;
023
024import org.apache.commons.lang3.StringUtils;
025import org.nuxeo.ecm.automation.core.Constants;
026import org.nuxeo.ecm.automation.core.annotations.Context;
027import org.nuxeo.ecm.automation.core.annotations.Operation;
028import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
029import org.nuxeo.ecm.automation.core.annotations.Param;
030import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector;
031import org.nuxeo.ecm.core.api.CoreSession;
032import org.nuxeo.ecm.core.api.CoreSession.CopyOption;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.api.DocumentRef;
035
036/**
037 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
038 */
039@Operation(id = CopyDocument.ID, label = "Copy", category = Constants.CAT_DOCUMENT, description = "Copy the input document into the given Folderish. The name parameter will be used as the copy name otherwise if not specified the original name will be preserved. The target Folderish can be specified as an absolute or relative path (relative to the input document) as an UID or by using an EL expression. Return the newly created document (the copy). The document is copied with its children recursively. The resetLifeCycle parameter allows to reset the life cycle of the copied document.")
040public class CopyDocument {
041
042    public static final String ID = "Document.Copy";
043
044    @Context
045    protected CoreSession session;
046
047    @Param(name = "target")
048    protected DocumentRef target; // the path or the ID
049
050    @Param(name = "name", required = false)
051    protected String name;
052
053    /**
054     * @since 11.1
055     */
056    @Param(name = "resetLifeCycle", required = false)
057    protected boolean resetLifeCycle;
058
059    @OperationMethod(collector = DocumentModelCollector.class)
060    public DocumentModel run(DocumentModel doc) {
061        String n = name;
062        if (StringUtils.isEmpty(name)) {
063            n = doc.getName();
064        }
065        List<CopyOption> options = new ArrayList<>();
066
067        if (resetLifeCycle) {
068            options.add(CopyOption.RESET_LIFE_CYCLE);
069        }
070        return session.copy(doc.getRef(), target, n, options.toArray(CopyOption[]::new));
071    }
072
073    @OperationMethod(collector = DocumentModelCollector.class)
074    public DocumentModel run(DocumentRef ref) {
075        return run(session.getDocument(ref));
076    }
077}