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