001/* 002 * (C) Copyright 2006-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 * 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.lang.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.util.Properties; 035import org.nuxeo.ecm.automation.core.util.StringList; 036import org.nuxeo.ecm.automation.jaxrs.io.documents.PaginableDocumentModelListImpl; 037import org.nuxeo.ecm.core.api.CoreSession; 038import org.nuxeo.ecm.core.api.DocumentModel; 039import org.nuxeo.ecm.core.api.DocumentModelList; 040import org.nuxeo.ecm.core.api.SortInfo; 041import org.nuxeo.ecm.core.api.impl.SimpleDocumentModel; 042import org.nuxeo.ecm.core.query.sql.NXQL; 043import org.nuxeo.ecm.platform.query.api.PageProvider; 044import org.nuxeo.ecm.platform.query.api.PageProviderService; 045import org.nuxeo.ecm.platform.query.core.CoreQueryPageProviderDescriptor; 046import org.nuxeo.ecm.platform.query.nxql.CoreQueryDocumentPageProvider; 047 048/** 049 * @since 6.0 Document query operation to perform queries on the repository. 050 */ 051@Operation(id = DocumentPaginatedQuery.ID, category = Constants.CAT_FETCH, label = "Query", description = "Perform a query on the repository. " 052 + "The document list returned will become the input for the next " + "operation.", since = "6.0", addToStudio = true, aliases = { "Document.Query" }) 053public class DocumentPaginatedQuery { 054 055 public static final String ID = "Repository.Query"; 056 057 public static final String CURRENT_USERID_PATTERN = "$currentUser"; 058 059 public static final String CURRENT_REPO_PATTERN = "$currentRepository"; 060 061 public static final String DESC = "DESC"; 062 063 public static final String ASC = "ASC"; 064 065 @Context 066 protected CoreSession session; 067 068 @Context 069 protected PageProviderService pageProviderService; 070 071 @Param(name = "query", required = true, description = "The query to " + "perform.") 072 protected String query; 073 074 @Param(name = "language", required = false, description = "The query " + "language.", widget = Constants.W_OPTION, values = { NXQL.NXQL }) 075 protected String lang = NXQL.NXQL; 076 077 @Param(name = "currentPageIndex", required = false, description = "Target listing page.") 078 protected Integer currentPageIndex; 079 080 @Param(name = "pageSize", required = false, description = "Entries number" + " per page.") 081 protected Integer pageSize; 082 083 @Param(name = "queryParams", required = false, description = "Ordered " + "query parameters.") 084 protected StringList strParameters; 085 086 @Param(name = "sortBy", required = false, description = "Sort by " + "properties (separated by comma)") 087 protected String sortBy; 088 089 @Param(name = "sortOrder", required = false, description = "Sort order, " + "ASC or DESC", widget = Constants.W_OPTION, values = { 090 ASC, DESC }) 091 protected String sortOrder; 092 093 @Param(name = PageProviderService.NAMED_PARAMETERS, required = false, description = "Named parameters to pass to the page provider to " 094 + "fill in query variables.") 095 protected Properties namedParameters; 096 097 @SuppressWarnings("unchecked") 098 @OperationMethod 099 public DocumentModelList run() throws OperationException { 100 // Ordered parameters 101 Object[] orderedParameters = null; 102 if (strParameters != null && !strParameters.isEmpty()) { 103 orderedParameters = strParameters.toArray(new String[strParameters.size()]); 104 // expand specific parameters 105 for (int idx = 0; idx < orderedParameters.length; idx++) { 106 String value = (String) orderedParameters[idx]; 107 if (value.equals(CURRENT_USERID_PATTERN)) { 108 orderedParameters[idx] = session.getPrincipal().getName(); 109 } else if (value.equals(CURRENT_REPO_PATTERN)) { 110 orderedParameters[idx] = session.getRepositoryName(); 111 } 112 } 113 } 114 // Target query page 115 Long targetPage = null; 116 if (currentPageIndex != null) { 117 targetPage = currentPageIndex.longValue(); 118 } 119 // Target page size 120 Long targetPageSize = null; 121 if (pageSize != null) { 122 targetPageSize = pageSize.longValue(); 123 } 124 125 // Sort Info Management 126 List<SortInfo> sortInfoList = new ArrayList<>(); 127 if (!StringUtils.isBlank(sortBy)) { 128 String[] sorts = sortBy.split(","); 129 String[] orders = null; 130 if (!StringUtils.isBlank(sortOrder)) { 131 orders = sortOrder.split(","); 132 } 133 for (int i = 0; i < sorts.length; i++) { 134 String sort = sorts[i]; 135 boolean sortAscending = (orders != null && orders.length > i && "asc".equals(orders[i].toLowerCase())); 136 sortInfoList.add(new SortInfo(sort, sortAscending)); 137 } 138 } 139 140 Map<String, Serializable> props = new HashMap<String, Serializable>(); 141 props.put(CoreQueryDocumentPageProvider.CORE_SESSION_PROPERTY, (Serializable) session); 142 SimpleDocumentModel searchDocumentModel = null; 143 if (namedParameters != null && !namedParameters.isEmpty()) { 144 searchDocumentModel = new SimpleDocumentModel(); 145 searchDocumentModel.putContextData(PageProviderService.NAMED_PARAMETERS, namedParameters); 146 } 147 CoreQueryPageProviderDescriptor desc = new CoreQueryPageProviderDescriptor(); 148 desc.setPattern(query); 149 PaginableDocumentModelListImpl res = new PaginableDocumentModelListImpl( 150 (PageProvider<DocumentModel>) pageProviderService.getPageProvider(StringUtils.EMPTY, desc, 151 searchDocumentModel, sortInfoList, targetPageSize, targetPage, props, orderedParameters), null); 152 if (res.hasError()) { 153 throw new OperationException(res.getErrorMessage()); 154 } 155 return res; 156 157 } 158}