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 */ 019package org.nuxeo.ecm.automation.core.operations.services; 020 021import java.io.Serializable; 022import java.util.Map; 023 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026import org.nuxeo.ecm.automation.OperationContext; 027import org.nuxeo.ecm.automation.OperationException; 028import org.nuxeo.ecm.automation.core.Constants; 029import org.nuxeo.ecm.automation.core.annotations.Context; 030import org.nuxeo.ecm.automation.core.annotations.Operation; 031import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 032import org.nuxeo.ecm.automation.core.annotations.Param; 033import org.nuxeo.ecm.automation.core.util.PageProviderHelper; 034import org.nuxeo.ecm.automation.core.util.Properties; 035import org.nuxeo.ecm.automation.core.util.RecordSet; 036import org.nuxeo.ecm.automation.core.util.StringList; 037import org.nuxeo.ecm.core.api.CoreSession; 038import org.nuxeo.ecm.core.query.sql.NXQL; 039import org.nuxeo.ecm.platform.query.api.PageProvider; 040import org.nuxeo.ecm.platform.query.api.PageProviderDefinition; 041import org.nuxeo.ecm.platform.query.api.PageProviderService; 042 043/** 044 * Operation to execute a query or a named provider with support for Pagination 045 * 046 * @author Tiry (tdelprat@nuxeo.com) 047 * @since 5.7 048 */ 049@Operation(id = ResultSetPageProviderOperation.ID, category = Constants.CAT_FETCH, label = "QueryAndFetch", description = "Perform " 050 + "a named provider query on the repository. Result is paginated." 051 + "The result is returned as a RecordSet (QueryAndFetch) rather than as a List of Document" 052 + "The query result will become the input for the next operation.", addToStudio = false, aliases = { 053 "Resultset.PageProvider" }) 054public class ResultSetPageProviderOperation { 055 056 public static final String ID = "Repository.ResultSetPageProvider"; 057 058 public static final String DESC = "DESC"; 059 060 public static final String ASC = "ASC"; 061 062 public static final String CMIS = "CMIS"; 063 064 @Context 065 protected OperationContext context; 066 067 @Context 068 protected CoreSession session; 069 070 @Param(name = "providerName", required = false) 071 protected String providerName; 072 073 @Param(name = "language", required = false, widget = Constants.W_OPTION, values = { NXQL.NXQL, CMIS }) 074 protected String lang = NXQL.NXQL; 075 076 @Param(name = "currentPageIndex", alias = "page", required = false) 077 protected Integer page; 078 079 @Param(name = "pageSize", required = false) 080 protected Integer pageSize; 081 082 @Param(name = "queryParams", required = false) 083 protected StringList strParameters; 084 085 /** 086 * @since 5.7 087 */ 088 @Param(name = "maxResults", required = false) 089 protected String maxResults = "100"; 090 091 /** 092 * @since 6.0 093 */ 094 @Param(name = PageProviderService.NAMED_PARAMETERS, required = false, 095 description = "Named parameters to pass to the page provider to fill in query variables.") 096 protected Properties namedParameters; 097 098 /** 099 * @since 6.0 100 */ 101 @Param(name = "sortBy", required = false, description = "Sort by properties (separated by comma)") 102 protected StringList sortBy; 103 104 /** 105 * @since 6.0 106 */ 107 @Param(name = "sortOrder", required = false, description = "Sort order, ASC or DESC", 108 widget = Constants.W_OPTION, values = { ASC, DESC }) 109 protected StringList sortOrder; 110 111 @SuppressWarnings("unchecked") 112 @OperationMethod 113 public RecordSet run() throws OperationException { 114 PageProviderDefinition def = PageProviderHelper.getPageProviderDefinition(providerName); 115 116 Long targetPage = page != null ? page.longValue() : null; 117 Long targetPageSize = pageSize != null ? pageSize.longValue() : null; 118 119 PageProvider<Map<String, Serializable>> pp = (PageProvider<Map<String, Serializable>>) PageProviderHelper.getPageProvider( 120 session, def, namedParameters, sortBy, sortOrder, targetPageSize, targetPage, 121 strParameters != null ? strParameters.toArray(new String[0]) : null); 122 123 PaginableRecordSetImpl res = new PaginableRecordSetImpl(pp); 124 if (res.hasError()) { 125 throw new OperationException(res.getErrorMessage()); 126 } 127 return res; 128 } 129}