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 java.util.Map;
020
021import org.nuxeo.ecm.automation.AutomationService;
022import org.nuxeo.ecm.automation.OperationChain;
023import org.nuxeo.ecm.automation.OperationContext;
024import org.nuxeo.ecm.automation.OperationException;
025import org.nuxeo.ecm.automation.OperationParameters;
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.operations.services.DocumentPageProviderOperation;
031import org.nuxeo.ecm.automation.jaxrs.io.documents.PaginableDocumentModelListImpl;
032import org.nuxeo.ecm.collections.api.CollectionConstants;
033import org.nuxeo.ecm.core.api.DocumentModel;
034
035/**
036 * Class for the operation to get the list of documents from a Collection.
037 *
038 * @since 5.9.4
039 */
040@Operation(id = GetDocumentsFromCollectionOperation.ID, category = Constants.CAT_DOCUMENT, label = "Get documents from collection", description = "Get the list "
041        + "of documents visible by the currentUser in a collection. This is returning a list of documents.")
042public class GetDocumentsFromCollectionOperation {
043
044    public static final String ID = "Collection.GetDocumentsFromCollection";
045
046    @Context
047    protected OperationContext ctx;
048
049    @Context
050    protected AutomationService service;
051
052    @OperationMethod
053    public PaginableDocumentModelListImpl run(DocumentModel collection) throws OperationException {
054        Map<String, Object> vars = ctx.getVars();
055        vars.put("searchTerm", collection.getId());
056        vars.put("providerName", CollectionConstants.COLLECTION_CONTENT_PAGE_PROVIDER);
057
058        OperationContext subctx = new OperationContext(ctx.getCoreSession(), vars);
059
060        OperationChain chain = new OperationChain("operation");
061        OperationParameters oparams = new OperationParameters(DocumentPageProviderOperation.ID, vars);
062        chain.add(oparams);
063        return (PaginableDocumentModelListImpl) service.run(subctx, chain);
064    }
065}