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