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 *      Vladimir Pasquier <vpasquier@nuxeo.com>
018 */
019package org.nuxeo.ecm.automation.core.operations.services.query;
020
021import java.io.Serializable;
022import java.util.Collections;
023import java.util.Map;
024
025import org.nuxeo.ecm.automation.OperationException;
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.util.PageProviderHelper;
032import org.nuxeo.ecm.automation.core.operations.services.PaginableRecordSetImpl;
033import org.nuxeo.ecm.automation.core.util.Properties;
034import org.nuxeo.ecm.automation.core.util.RecordSet;
035import org.nuxeo.ecm.automation.core.util.StringList;
036import org.nuxeo.ecm.core.api.CoreSession;
037import org.nuxeo.ecm.core.query.sql.NXQL;
038import org.nuxeo.ecm.platform.query.api.PageProvider;
039import org.nuxeo.ecm.platform.query.api.PageProviderDefinition;
040import org.nuxeo.ecm.platform.query.api.PageProviderService;
041
042/**
043 * @since 6.0 Result set query operation to perform queries on the repository.
044 */
045@Operation(id = ResultSetPaginatedQuery.ID, category = Constants.CAT_FETCH, label = "ResultSet Query", description =
046            "Perform a query on the repository. The result set returned will become the input for the next operation. " +
047            "If no query or provider name is given, a query returning all the documents that the user has access to " +
048                    "will be executed.", since = "6.0", addToStudio = true, aliases = { "ResultSet.PaginatedQuery" })
049public class ResultSetPaginatedQuery {
050
051    public static final String ID = "Repository.ResultSetQuery";
052
053    public static final String ASC = "ASC";
054
055    public static final String DESC = "DESC";
056
057    public static final String CMIS = "CMIS";
058
059    @Context
060    protected CoreSession session;
061
062    @Param(name = "query", required = true, description = "The query to " + "perform.")
063    protected String query;
064
065    @Param(name = "language", required = false, description = "The query "
066            + "language.", widget = Constants.W_OPTION, values = { NXQL.NXQL, CMIS })
067    protected String lang = NXQL.NXQL;
068
069    @Param(name = PageProviderService.NAMED_PARAMETERS, required = false, description = "Named parameters to pass to the page provider to "
070            + "fill in query variables.")
071    protected Properties namedParameters;
072
073    @Param(name = "currentPageIndex", required = false, description = "Target listing page.")
074    protected Integer currentPageIndex;
075
076    @Param(name = "pageSize", required = false, description = "Entries number" + " per page.")
077    protected Integer pageSize;
078
079    @Param(name = "queryParams", required = false, description = "Ordered " + "query parameters.")
080    protected StringList strParameters;
081
082    @Param(name = "sortBy", required = false, description = "Sort by " + "properties (separated by comma)")
083    protected StringList sortBy;
084
085    @Param(name = "sortOrder", required = false, description = "Sort order, "
086            + "ASC or DESC", widget = Constants.W_OPTION, values = { ASC, DESC })
087    protected StringList sortOrder;
088
089    /**
090     * @since 10.3
091     */
092    @Param(name = "maxResults", required = false)
093    protected Integer maxResults;
094
095    @SuppressWarnings("unchecked")
096    @OperationMethod
097    public RecordSet run() throws OperationException {
098        if (query == null) {
099            // provide a defaut query
100            query = "SELECT * from Document";
101        }
102        Map<String, String> properties = null;
103        if (maxResults != null) {
104            properties = Collections.singletonMap("maxResults", maxResults.toString());
105        }
106        PageProviderDefinition def = PageProviderHelper.getQueryAndFetchProviderDefinition(query, properties);
107
108        Long targetPage = currentPageIndex != null ? currentPageIndex.longValue() : null;
109        Long targetPageSize = pageSize != null ? pageSize.longValue() : null;
110
111        PageProvider<Map<String, Serializable>> pp = (PageProvider<Map<String, Serializable>>) PageProviderHelper.getPageProvider(
112                session, def, namedParameters, sortBy, sortOrder, targetPageSize, targetPage,
113                strParameters != null ? strParameters.toArray(new String[0]) : null);
114
115        PaginableRecordSetImpl res = new PaginableRecordSetImpl(pp);
116        if (res.hasError()) {
117            throw new OperationException(res.getErrorMessage());
118        }
119        return res;
120    }
121}