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.ArrayList; 023import java.util.HashMap; 024import java.util.List; 025import java.util.Map; 026 027import org.apache.commons.lang3.StringUtils; 028import org.nuxeo.ecm.automation.OperationException; 029import org.nuxeo.ecm.automation.core.Constants; 030import org.nuxeo.ecm.automation.core.annotations.Context; 031import org.nuxeo.ecm.automation.core.annotations.Operation; 032import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 033import org.nuxeo.ecm.automation.core.annotations.Param; 034import org.nuxeo.ecm.automation.core.operations.services.PaginableRecordSetImpl; 035import org.nuxeo.ecm.automation.core.util.Properties; 036import org.nuxeo.ecm.automation.core.util.RecordSet; 037import org.nuxeo.ecm.automation.core.util.StringList; 038import org.nuxeo.ecm.core.api.CoreSession; 039import org.nuxeo.ecm.core.api.DocumentModel; 040import org.nuxeo.ecm.core.api.SortInfo; 041import org.nuxeo.ecm.core.query.sql.NXQL; 042import org.nuxeo.ecm.platform.query.api.PageProvider; 043import org.nuxeo.ecm.platform.query.api.PageProviderService; 044import org.nuxeo.ecm.platform.query.core.GenericPageProviderDescriptor; 045import org.nuxeo.ecm.platform.query.nxql.CoreQueryAndFetchPageProvider; 046import org.nuxeo.ecm.platform.query.nxql.CoreQueryDocumentPageProvider; 047 048/** 049 * @since 6.0 Result set query operation to perform queries on the repository. 050 */ 051@Operation(id = ResultSetPaginatedQuery.ID, category = Constants.CAT_FETCH, label = "ResultSet Query", description = "Perform a query on the " 052 + "repository. The result set returned will become the input for the " 053 + "next operation.", since = "6.0", addToStudio = true, aliases = { "ResultSet.PaginatedQuery" }) 054public class ResultSetPaginatedQuery { 055 056 public static final String ID = "Repository.ResultSetQuery"; 057 058 public static final String CURRENT_USERID_PATTERN = "$currentUser"; 059 060 public static final String CURRENT_REPO_PATTERN = "$currentRepository"; 061 062 public static final String ASC = "ASC"; 063 064 public static final String DESC = "DESC"; 065 066 public static final String CMIS = "CMIS"; 067 068 @Context 069 protected CoreSession session; 070 071 @Context 072 protected PageProviderService pageProviderService; 073 074 @Param(name = "query", required = true, description = "The query to " + "perform.") 075 protected String query; 076 077 @Param(name = "language", required = false, description = "The query " 078 + "language.", widget = Constants.W_OPTION, values = { NXQL.NXQL, CMIS }) 079 protected String lang = NXQL.NXQL; 080 081 @Param(name = PageProviderService.NAMED_PARAMETERS, required = false, description = "Named parameters to pass to the page provider to " 082 + "fill in query variables.") 083 protected Properties namedParameters; 084 085 @Param(name = "currentPageIndex", required = false, description = "Target listing page.") 086 protected Integer currentPageIndex; 087 088 @Param(name = "pageSize", required = false, description = "Entries number" + " per page.") 089 protected Integer pageSize; 090 091 @Param(name = "queryParams", required = false, description = "Ordered " + "query parameters.") 092 protected StringList strParameters; 093 094 @Param(name = "sortBy", required = false, description = "Sort by " + "properties (separated by comma)") 095 protected String sortBy; 096 097 @Param(name = "sortOrder", required = false, description = "Sort order, " 098 + "ASC or DESC", widget = Constants.W_OPTION, values = { ASC, DESC }) 099 protected String sortOrder; 100 101 @SuppressWarnings("unchecked") 102 @OperationMethod 103 public RecordSet run() throws OperationException { 104 // Ordered parameters 105 Object[] orderedParameters = null; 106 if (strParameters != null && !strParameters.isEmpty()) { 107 orderedParameters = strParameters.toArray(new String[strParameters.size()]); 108 // expand specific parameters 109 for (int idx = 0; idx < orderedParameters.length; idx++) { 110 String value = (String) orderedParameters[idx]; 111 if (value.equals(CURRENT_USERID_PATTERN)) { 112 orderedParameters[idx] = session.getPrincipal().getName(); 113 } else if (value.equals(CURRENT_REPO_PATTERN)) { 114 orderedParameters[idx] = session.getRepositoryName(); 115 } 116 } 117 } 118 119 // Target query page 120 Long targetPage = null; 121 if (currentPageIndex != null) { 122 targetPage = currentPageIndex.longValue(); 123 } 124 // Target page size 125 Long targetPageSize = null; 126 if (pageSize != null) { 127 targetPageSize = pageSize.longValue(); 128 } 129 130 // Sort Info Management 131 List<SortInfo> sortInfoList = new ArrayList<>(); 132 if (!StringUtils.isBlank(sortBy)) { 133 String[] sorts = sortBy.split(","); 134 String[] orders = null; 135 if (!StringUtils.isBlank(sortOrder)) { 136 orders = sortOrder.split(","); 137 } 138 for (int i = 0; i < sorts.length; i++) { 139 String sort = sorts[i]; 140 boolean sortAscending = (orders != null && orders.length > i && "asc".equals(orders[i].toLowerCase())); 141 sortInfoList.add(new SortInfo(sort, sortAscending)); 142 } 143 } 144 145 Map<String, Serializable> props = new HashMap<>(); 146 props.put(CoreQueryDocumentPageProvider.CORE_SESSION_PROPERTY, (Serializable) session); 147 DocumentModel searchDocumentModel = DocumentPaginatedQuery.getSearchDocumentModel(session, namedParameters); 148 QueryAndFetchProviderDescriptor desc = new QueryAndFetchProviderDescriptor(); 149 desc.setPattern(query); 150 PageProvider<Map<String, Serializable>> pp = (PageProvider<Map<String, Serializable>>) pageProviderService.getPageProvider( 151 StringUtils.EMPTY, desc, searchDocumentModel, sortInfoList, targetPageSize, targetPage, props, 152 orderedParameters); 153 PaginableRecordSetImpl res = new PaginableRecordSetImpl(pp); 154 if (res.hasError()) { 155 throw new OperationException(res.getErrorMessage()); 156 } 157 return res; 158 } 159 160 @SuppressWarnings("unchecked") 161 final class QueryAndFetchProviderDescriptor extends GenericPageProviderDescriptor { 162 private static final long serialVersionUID = 1L; 163 164 public QueryAndFetchProviderDescriptor() { 165 super(); 166 try { 167 klass = (Class<PageProvider<?>>) Class.forName(CoreQueryAndFetchPageProvider.class.getName()); 168 } catch (ClassNotFoundException e) { 169 170 } 171 } 172 } 173 174}