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 041import javax.servlet.http.HttpServletRequest; 042import java.io.Serializable; 043import java.util.Map; 044 045/** 046 * Operation to execute a query or a named provider with support for Pagination. 047 * 048 * @author Tiry (tdelprat@nuxeo.com) 049 * @since 5.4.2 050 */ 051@Operation(id = DocumentPageProviderOperation.ID, category = Constants.CAT_FETCH, label = "PageProvider", description = "Perform " 052 + "a named provider query on the repository. Result is paginated. The query result will become the input for " 053 + "the next operation.", aliases = { "Document.PageProvider" }) 054public class DocumentPageProviderOperation { 055 056 public static final String ID = "Repository.PageProvider"; 057 058 public static final String ASC = "ASC"; 059 060 public static final String DESC = "DESC"; 061 062 @Context 063 protected OperationContext context; 064 065 @Context 066 protected CoreSession session; 067 068 @Param(name = "providerName", required = true) 069 protected String providerName; 070 071 @Param(name = "language", required = false, widget = Constants.W_OPTION, values = { NXQL.NXQL }) 072 protected String lang = NXQL.NXQL; 073 074 @Param(name = "currentPageIndex", alias = "page", required = false) 075 protected Integer currentPageIndex; 076 077 @Param(name = "pageSize", required = false) 078 protected Integer pageSize; 079 080 /* 081 * @since 11.4 082 */ 083 @Param(name = "offset", required = false) 084 protected Integer offset; 085 086 @Param(name = "queryParams", alias = "searchTerm", required = false) 087 protected StringList strParameters; 088 089 @Param(name = "documentLinkBuilder", required = false) 090 protected String documentLinkBuilder; 091 092 /** 093 * @since 6.0 094 */ 095 @Param(name = PageProviderService.NAMED_PARAMETERS, required = false, description = "Named parameters to pass to the page provider to " 096 + "fill in query variables.") 097 protected Properties namedParameters; 098 099 /** 100 * @since 6.0 101 */ 102 @Param(name = "sortBy", required = false, description = "Sort by " + "properties (separated by comma)") 103 protected StringList sortBy; 104 105 /** 106 * @since 6.0 107 */ 108 @Param(name = "sortOrder", required = false, description = "Sort order, " 109 + "ASC or DESC", widget = Constants.W_OPTION, values = { ASC, DESC }) 110 protected StringList sortOrder; 111 112 /** 113 * @since 8.4 114 */ 115 @Param(name = "quickFilters", required = false, description = "Quick filter " + "properties (separated by comma)") 116 protected StringList quickFilters; 117 118 /** 119 * @since 9.1 120 */ 121 @Param(name = "highlights", required = false, description = "Highlight properties (separated by comma)") 122 protected StringList highlights; 123 124 @SuppressWarnings("unchecked") 125 @OperationMethod 126 public PaginableDocumentModelListImpl run() throws OperationException { 127 PageProviderDefinition def = PageProviderHelper.getPageProviderDefinition(providerName); 128 129 Long targetPage = currentPageIndex != null ? currentPageIndex.longValue() : null; 130 Long targetPageSize = pageSize != null ? pageSize.longValue() : null; 131 Long currentOffset = offset != null ? offset.longValue() : null; 132 133 Object[] parameters = strParameters != null ? strParameters.toArray(new String[0]) : null; 134 ActionContext actionContext = (ActionContext) context.get(GetActions.SEAM_ACTION_CONTEXT); 135 if (actionContext != null) { 136 parameters = PageProviderHelper.resolveELParameters(def, parameters); 137 } 138 PageProvider<DocumentModel> pp = (PageProvider<DocumentModel>) PageProviderHelper.getPageProvider(session, def, 139 namedParameters, sortBy, sortOrder, targetPageSize, targetPage, currentOffset, highlights, quickFilters, 140 parameters); 141 142 HttpServletRequest request = (HttpServletRequest) context.get("request"); 143 if (request != null) { 144 String skipAggregates = request.getHeader(PageProvider.SKIP_AGGREGATES_PROP); 145 if (skipAggregates != null) { 146 Map<String, Serializable> props = pp.getProperties(); 147 props.put(PageProvider.SKIP_AGGREGATES_PROP, 148 Boolean.parseBoolean(request.getHeader(PageProvider.SKIP_AGGREGATES_PROP))); 149 pp.setProperties(props); 150 } 151 } 152 153 PaginableDocumentModelListImpl res = new PaginableDocumentModelListImpl(pp, documentLinkBuilder); 154 155 if (res.hasError()) { 156 throw new OperationException(res.getErrorMessage()); 157 } 158 return res; 159 } 160}