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.annotations.Param;
031import org.nuxeo.ecm.automation.core.operations.services.DocumentPageProviderOperation;
032import org.nuxeo.ecm.automation.core.util.StringList;
033import org.nuxeo.ecm.automation.jaxrs.io.documents.PaginableDocumentModelListImpl;
034import org.nuxeo.ecm.collections.api.CollectionConstants;
035
036/**
037 * Class for the operation to get the collections matching the searching terms.
038 *
039 * @since 5.9.4
040 */
041@Operation(id = GetCollectionsOperation.ID, category = Constants.CAT_DOCUMENT, label = "Get collections", description = "Get the list of all the collections visible by the currentUser. "
042        + "This is returning a list of collections.", aliases = { "Collection.GetCollections" })
043public class GetCollectionsOperation {
044
045    public static final String ID = "User.GetCollections";
046
047    @Param(name = "searchTerm")
048    protected String searchTerm;
049
050    @Context
051    protected OperationContext ctx;
052
053    @Context
054    protected AutomationService service;
055
056    @OperationMethod
057    public PaginableDocumentModelListImpl run() throws OperationException {
058
059        Map<String, Object> vars = ctx.getVars();
060        StringList sl = new StringList();
061        sl.add(searchTerm + (searchTerm.endsWith("%") ? "" : "%"));
062        sl.add(DocumentPageProviderOperation.CURRENT_USERID_PATTERN);
063        vars.put("queryParams", sl);
064        vars.put("providerName", CollectionConstants.COLLECTION_PAGE_PROVIDER);
065
066        OperationContext subctx = new OperationContext(ctx.getCoreSession(), vars);
067
068        OperationChain chain = new OperationChain("operation");
069        OperationParameters oparams = new OperationParameters(DocumentPageProviderOperation.ID, vars);
070        chain.add(oparams);
071        return (PaginableDocumentModelListImpl) service.run(subctx, chain);
072    }
073}