001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.core.operations.document;
013
014import org.nuxeo.ecm.automation.core.Constants;
015import org.nuxeo.ecm.automation.core.annotations.Context;
016import org.nuxeo.ecm.automation.core.annotations.Operation;
017import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
018import org.nuxeo.ecm.automation.core.annotations.Param;
019import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector;
020import org.nuxeo.ecm.core.api.CoreSession;
021import org.nuxeo.ecm.core.api.DocumentModel;
022import org.nuxeo.ecm.core.api.DocumentRef;
023
024/**
025 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
026 */
027@Operation(id = CopyDocument.ID, label = "Copy", category = Constants.CAT_DOCUMENT, description = "Copy the input document into the given folder. The name parameter will be used as the copy name otherwise if not specified the original name will be preserved. The target folder 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).")
028public class CopyDocument {
029
030    public static final String ID = "Document.Copy";
031
032    @Context
033    protected CoreSession session;
034
035    @Param(name = "target")
036    protected DocumentRef target; // the path or the ID
037
038    @Param(name = "name", required = false)
039    protected String name;
040
041    @OperationMethod(collector = DocumentModelCollector.class)
042    public DocumentModel run(DocumentModel doc) {
043        String n = name;
044        if (name == null || name.length() == 0) {
045            n = doc.getName();
046        }
047        return session.copy(doc.getRef(), target, n);
048    }
049
050    @OperationMethod(collector = DocumentModelCollector.class)
051    public DocumentModel run(DocumentRef ref) {
052        String n = name;
053        if (name == null || name.length() == 0) {
054            n = session.getDocument(ref).getName();
055        }
056        return session.copy(ref, target, n);
057    }
058}