001/* 002 * Copyright (c) 2006-2014 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Vladimir Pasquier <vpasquier@nuxeo.com> 011 */ 012package org.nuxeo.ecm.automation.core.operations.services.query; 013 014import java.io.Serializable; 015import java.util.ArrayList; 016import java.util.HashMap; 017import java.util.List; 018import java.util.Map; 019 020import org.apache.commons.lang.StringUtils; 021import org.nuxeo.ecm.automation.OperationException; 022import org.nuxeo.ecm.automation.core.Constants; 023import org.nuxeo.ecm.automation.core.annotations.Context; 024import org.nuxeo.ecm.automation.core.annotations.Operation; 025import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 026import org.nuxeo.ecm.automation.core.annotations.Param; 027import org.nuxeo.ecm.automation.core.operations.services.PaginableRecordSetImpl; 028import org.nuxeo.ecm.automation.core.util.Properties; 029import org.nuxeo.ecm.automation.core.util.RecordSet; 030import org.nuxeo.ecm.automation.core.util.StringList; 031import org.nuxeo.ecm.core.api.CoreSession; 032import org.nuxeo.ecm.core.api.SortInfo; 033import org.nuxeo.ecm.core.api.impl.SimpleDocumentModel; 034import org.nuxeo.ecm.core.query.sql.NXQL; 035import org.nuxeo.ecm.platform.query.api.PageProvider; 036import org.nuxeo.ecm.platform.query.api.PageProviderService; 037import org.nuxeo.ecm.platform.query.core.GenericPageProviderDescriptor; 038import org.nuxeo.ecm.platform.query.nxql.CoreQueryAndFetchPageProvider; 039import org.nuxeo.ecm.platform.query.nxql.CoreQueryDocumentPageProvider; 040 041/** 042 * @since 6.0 Result set query operation to perform queries on the repository. 043 */ 044@Operation(id = ResultSetPaginatedQuery.ID, category = Constants.CAT_FETCH, label = "ResultSet Query", description = "Perform a query on the " 045 + "repository. The result set returned will become the input for the " + "next operation.", since = "6.0", addToStudio = true, aliases = { "ResultSet.PaginatedQuery" }) 046public class ResultSetPaginatedQuery { 047 048 public static final String ID = "Repository.ResultSetQuery"; 049 050 public static final String CURRENT_USERID_PATTERN = "$currentUser"; 051 052 public static final String CURRENT_REPO_PATTERN = "$currentRepository"; 053 054 public static final String ASC = "ASC"; 055 056 public static final String DESC = "DESC"; 057 058 public static final String CMIS = "CMIS"; 059 060 @Context 061 protected CoreSession session; 062 063 @Context 064 protected PageProviderService pageProviderService; 065 066 @Param(name = "query", required = true, description = "The query to " + "perform.") 067 protected String query; 068 069 @Param(name = "language", required = false, description = "The query " + "language.", widget = Constants.W_OPTION, values = { 070 NXQL.NXQL, CMIS }) 071 protected String lang = NXQL.NXQL; 072 073 @Param(name = PageProviderService.NAMED_PARAMETERS, required = false, description = "Named parameters to pass to the page provider to " 074 + "fill in query variables.") 075 protected Properties namedParameters; 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 @SuppressWarnings("unchecked") 094 @OperationMethod 095 public RecordSet run() throws OperationException { 096 // Ordered parameters 097 Object[] orderedParameters = null; 098 if (strParameters != null && !strParameters.isEmpty()) { 099 orderedParameters = strParameters.toArray(new String[strParameters.size()]); 100 // expand specific parameters 101 for (int idx = 0; idx < orderedParameters.length; idx++) { 102 String value = (String) orderedParameters[idx]; 103 if (value.equals(CURRENT_USERID_PATTERN)) { 104 orderedParameters[idx] = session.getPrincipal().getName(); 105 } else if (value.equals(CURRENT_REPO_PATTERN)) { 106 orderedParameters[idx] = session.getRepositoryName(); 107 } 108 } 109 } 110 111 // Target query page 112 Long targetPage = null; 113 if (currentPageIndex != null) { 114 targetPage = currentPageIndex.longValue(); 115 } 116 // Target page size 117 Long targetPageSize = null; 118 if (pageSize != null) { 119 targetPageSize = pageSize.longValue(); 120 } 121 122 // Sort Info Management 123 List<SortInfo> sortInfoList = new ArrayList<>(); 124 if (!StringUtils.isBlank(sortBy)) { 125 String[] sorts = sortBy.split(","); 126 String[] orders = null; 127 if (!StringUtils.isBlank(sortOrder)) { 128 orders = sortOrder.split(","); 129 } 130 for (int i = 0; i < sorts.length; i++) { 131 String sort = sorts[i]; 132 boolean sortAscending = (orders != null && orders.length > i && "asc".equals(orders[i].toLowerCase())); 133 sortInfoList.add(new SortInfo(sort, sortAscending)); 134 } 135 } 136 137 Map<String, Serializable> props = new HashMap<String, Serializable>(); 138 props.put(CoreQueryDocumentPageProvider.CORE_SESSION_PROPERTY, (Serializable) session); 139 SimpleDocumentModel searchDocumentModel = null; 140 if (namedParameters != null && !namedParameters.isEmpty()) { 141 searchDocumentModel = new SimpleDocumentModel(); 142 searchDocumentModel.putContextData(PageProviderService.NAMED_PARAMETERS, namedParameters); 143 } 144 QueryAndFetchProviderDescriptor desc = new QueryAndFetchProviderDescriptor(); 145 desc.setPattern(query); 146 PageProvider<Map<String, Serializable>> pp = (PageProvider<Map<String, Serializable>>) pageProviderService.getPageProvider( 147 StringUtils.EMPTY, desc, searchDocumentModel, sortInfoList, targetPageSize, targetPage, props, 148 orderedParameters); 149 PaginableRecordSetImpl res = new PaginableRecordSetImpl(pp); 150 if (res.hasError()) { 151 throw new OperationException(res.getErrorMessage()); 152 } 153 return res; 154 } 155 156 @SuppressWarnings("unchecked") 157 final class QueryAndFetchProviderDescriptor extends GenericPageProviderDescriptor { 158 private static final long serialVersionUID = 1L; 159 160 public QueryAndFetchProviderDescriptor() { 161 super(); 162 try { 163 klass = (Class<PageProvider<?>>) Class.forName(CoreQueryAndFetchPageProvider.class.getName()); 164 } catch (ClassNotFoundException e) { 165 166 } 167 } 168 } 169 170}