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