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