001/*
002 * (C) Copyright 2006-2018 Nuxeo (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 *     Thierry Delprat
018 *     Marwane Kalam-Alami
019 */
020package org.nuxeo.ecm.automation.core.operations.services;
021
022import org.nuxeo.ecm.automation.OperationContext;
023import org.nuxeo.ecm.automation.OperationException;
024import org.nuxeo.ecm.automation.core.Constants;
025import org.nuxeo.ecm.automation.core.annotations.Context;
026import org.nuxeo.ecm.automation.core.annotations.Operation;
027import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
028import org.nuxeo.ecm.automation.core.annotations.Param;
029import org.nuxeo.ecm.automation.core.util.Properties;
030import org.nuxeo.ecm.automation.core.util.StringList;
031import org.nuxeo.ecm.automation.jaxrs.io.documents.PaginableDocumentModelListImpl;
032import org.nuxeo.ecm.core.api.CoreSession;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.query.sql.NXQL;
035import org.nuxeo.ecm.platform.actions.ActionContext;
036import org.nuxeo.ecm.platform.query.api.PageProvider;
037import org.nuxeo.ecm.platform.query.api.PageProviderDefinition;
038import org.nuxeo.ecm.platform.query.api.PageProviderService;
039import org.nuxeo.ecm.automation.core.util.PageProviderHelper;
040
041/**
042 * Operation to execute a query or a named provider with support for Pagination.
043 *
044 * @author Tiry (tdelprat@nuxeo.com)
045 * @since 5.4.2
046 */
047@Operation(id = DocumentPageProviderOperation.ID, category = Constants.CAT_FETCH, label = "PageProvider", description = "Perform "
048        + "a named provider query on the repository. Result is paginated. The query result will become the input for "
049        + "the next operation.", aliases = { "Document.PageProvider" })
050public class DocumentPageProviderOperation {
051
052    public static final String ID = "Repository.PageProvider";
053
054    public static final String ASC = "ASC";
055
056    public static final String DESC = "DESC";
057
058    @Context
059    protected OperationContext context;
060
061    @Context
062    protected CoreSession session;
063
064    @Param(name = "providerName", required = true)
065    protected String providerName;
066
067    @Param(name = "language", required = false, widget = Constants.W_OPTION, values = { NXQL.NXQL })
068    protected String lang = NXQL.NXQL;
069
070    @Param(name = "currentPageIndex", alias = "page", required = false)
071    protected Integer currentPageIndex;
072
073    @Param(name = "pageSize", required = false)
074    protected Integer pageSize;
075
076    @Param(name = "queryParams", alias = "searchTerm", required = false)
077    protected StringList strParameters;
078
079    @Param(name = "documentLinkBuilder", required = false)
080    protected String documentLinkBuilder;
081
082    /**
083     * @since 6.0
084     */
085    @Param(name = PageProviderService.NAMED_PARAMETERS, required = false, description = "Named parameters to pass to the page provider to "
086            + "fill in query variables.")
087    protected Properties namedParameters;
088
089    /**
090     * @since 6.0
091     */
092    @Param(name = "sortBy", required = false, description = "Sort by " + "properties (separated by comma)")
093    protected StringList sortBy;
094
095    /**
096     * @since 6.0
097     */
098    @Param(name = "sortOrder", required = false, description = "Sort order, "
099            + "ASC or DESC", widget = Constants.W_OPTION, values = { ASC, DESC })
100    protected StringList sortOrder;
101
102    /**
103     * @since 8.4
104     */
105    @Param(name = "quickFilters", required = false, description = "Quick filter " + "properties (separated by comma)")
106    protected StringList quickFilters;
107
108    /**
109     * @since 9.1
110     */
111    @Param(name = "highlights", required = false, description = "Highlight properties (separated by comma)")
112    protected StringList highlights;
113
114    @SuppressWarnings("unchecked")
115    @OperationMethod
116    public PaginableDocumentModelListImpl run() throws OperationException {
117        PageProviderDefinition def = PageProviderHelper.getPageProviderDefinition(providerName);
118
119        Long targetPage = currentPageIndex != null ? currentPageIndex.longValue() : null;
120        Long targetPageSize = pageSize != null ? pageSize.longValue() : null;
121
122        Object[] parameters = strParameters != null ? strParameters.toArray(new String[0]) : null;
123        ActionContext actionContext = (ActionContext) context.get(GetActions.SEAM_ACTION_CONTEXT);
124        if (actionContext != null) {
125            parameters = PageProviderHelper.resolveELParameters(def, parameters);
126        }
127
128        PageProvider<DocumentModel> pp = (PageProvider<DocumentModel>) PageProviderHelper.getPageProvider(session, def,
129                namedParameters, sortBy, sortOrder, targetPageSize, targetPage, highlights, quickFilters, parameters);
130
131        PaginableDocumentModelListImpl res = new PaginableDocumentModelListImpl(pp, documentLinkBuilder);
132
133        if (res.hasError()) {
134            throw new OperationException(res.getErrorMessage());
135        }
136        return res;
137    }
138}