001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     <a href="mailto:glefevre@nuxeo.com">Gildas</a>
016 */
017package org.nuxeo.ecm.collections.core.automation;
018
019import org.nuxeo.ecm.automation.core.Constants;
020import org.nuxeo.ecm.automation.core.annotations.Context;
021import org.nuxeo.ecm.automation.core.annotations.Operation;
022import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
023import org.nuxeo.ecm.automation.core.annotations.Param;
024import org.nuxeo.ecm.collections.api.CollectionManager;
025import org.nuxeo.ecm.core.api.CoreSession;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.DocumentModelList;
028
029/**
030 * Class for the operation to add a list of documents in a Collection.
031 *
032 * @since 5.9.4
033 */
034@Operation(id = AddToCollectionOperation.ID, category = Constants.CAT_DOCUMENT, label = "Add document to collection", description = "Add a list of documents in a collection. "
035        + "No value is returned.", aliases = { "Collection.AddToCollection" })
036public class AddToCollectionOperation {
037
038    public static final String ID = "Document.AddToCollection";
039
040    @Context
041    protected CoreSession session;
042
043    @Context
044    protected CollectionManager collectionManager;
045
046    @Param(name = "collection")
047    protected DocumentModel collection;
048
049    @OperationMethod
050    public DocumentModelList run(DocumentModelList docs) {
051        for (DocumentModel doc : docs) {
052            collectionManager.addToCollection(collection, doc, session);
053        }
054
055        return docs;
056    }
057
058    @OperationMethod()
059    public DocumentModel run(DocumentModel doc) {
060        collectionManager.addToCollection(collection, doc, session);
061
062        return doc;
063    }
064}