001/* 002 * (C) Copyright 2011-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.IOException; 022import java.io.Serializable; 023import java.util.ArrayList; 024import java.util.HashMap; 025import java.util.List; 026import java.util.Map; 027 028import org.apache.commons.lang3.StringUtils; 029import org.nuxeo.ecm.automation.OperationContext; 030import org.nuxeo.ecm.automation.core.Constants; 031import org.nuxeo.ecm.automation.core.annotations.Context; 032import org.nuxeo.ecm.automation.core.annotations.Operation; 033import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 034import org.nuxeo.ecm.automation.core.annotations.Param; 035import org.nuxeo.ecm.automation.core.util.DocumentHelper; 036import org.nuxeo.ecm.automation.core.util.Paginable; 037import org.nuxeo.ecm.automation.core.util.Properties; 038import org.nuxeo.ecm.automation.core.util.StringList; 039import org.nuxeo.ecm.core.api.CoreSession; 040import org.nuxeo.ecm.core.api.DocumentModel; 041import org.nuxeo.ecm.core.api.SortInfo; 042import org.nuxeo.ecm.core.query.sql.NXQL; 043import org.nuxeo.ecm.platform.audit.api.AuditPageProvider; 044import org.nuxeo.ecm.platform.audit.api.LogEntry; 045import org.nuxeo.ecm.platform.audit.api.LogEntryList; 046import org.nuxeo.ecm.platform.query.api.PageProvider; 047import org.nuxeo.ecm.platform.query.api.PageProviderService; 048import org.nuxeo.ecm.platform.query.core.GenericPageProviderDescriptor; 049import org.nuxeo.ecm.platform.query.nxql.CoreQueryDocumentPageProvider; 050 051/** 052 * Operation to execute a query or a named provider against Audit with support for Pagination 053 * 054 * @author Tiry (tdelprat@nuxeo.com) 055 * @since 5.8 056 */ 057@Operation(id = AuditPageProviderOperation.ID, category = Constants.CAT_FETCH, label = "Audit Query With Page Provider", description = "Perform " 058 + "a query or a named provider query against Audit logs. Result is " 059 + "paginated. The query result will become the input for the next " 060 + "operation. If no query or provider name is given, a query based on default Audit page provider will be executed.", addToStudio = false, aliases = { 061 "Audit.PageProvider" }) 062public class AuditPageProviderOperation { 063 064 public static final String ID = "Audit.QueryWithPageProvider"; 065 066 public static final String CURRENT_USERID_PATTERN = "$currentUser"; 067 068 public static final String CURRENT_REPO_PATTERN = "$currentRepository"; 069 070 private static final String SORT_PARAMETER_SEPARATOR = " "; 071 072 public static final String DESC = "DESC"; 073 074 public static final String ASC = "ASC"; 075 076 @Context 077 protected OperationContext context; 078 079 @Context 080 protected CoreSession session; 081 082 @Context 083 protected PageProviderService ppService; 084 085 @Param(name = "providerName", required = false) 086 protected String providerName; 087 088 @Param(name = "query", required = false) 089 protected String query; 090 091 @Param(name = "language", required = false, widget = Constants.W_OPTION, values = { NXQL.NXQL }) 092 protected String lang = NXQL.NXQL; 093 094 /** @deprecated since 6.0 use currentPageIndex instead. */ 095 @Param(name = "page", required = false) 096 @Deprecated 097 protected Integer page; 098 099 @Param(name = "currentPageIndex", required = false) 100 protected Integer currentPageIndex; 101 102 @Param(name = "pageSize", required = false) 103 protected Integer pageSize; 104 105 /** 106 * @deprecated since 6.0 use instead {@link #sortBy and @link #sortOrder}. 107 */ 108 @Deprecated 109 @Param(name = "sortInfo", required = false) 110 protected StringList sortInfoAsStringList; 111 112 @Param(name = "queryParams", required = false) 113 protected StringList strParameters; 114 115 @Param(name = "namedQueryParams", required = false) 116 protected Properties namedQueryParams; 117 118 /** 119 * @deprecated since 6.0, not used in operation. 120 */ 121 @Deprecated 122 @Param(name = "maxResults", required = false) 123 protected Integer maxResults = 100; 124 125 /** 126 * @since 6.0 127 */ 128 @Param(name = "sortBy", required = false, description = "Sort by " + "properties (separated by comma)") 129 protected String sortBy; 130 131 /** 132 * @since 6.0 133 */ 134 @Param(name = "sortOrder", required = false, description = "Sort order, " 135 + "ASC or DESC", widget = Constants.W_OPTION, values = { ASC, DESC }) 136 protected String sortOrder; 137 138 @SuppressWarnings("unchecked") 139 @OperationMethod 140 public Paginable<LogEntry> run() throws IOException { 141 142 List<SortInfo> sortInfos = null; 143 if (sortInfoAsStringList != null) { 144 sortInfos = new ArrayList<>(); 145 for (String sortInfoDesc : sortInfoAsStringList) { 146 SortInfo sortInfo; 147 if (sortInfoDesc.contains(SORT_PARAMETER_SEPARATOR)) { 148 String[] parts = sortInfoDesc.split(SORT_PARAMETER_SEPARATOR); 149 sortInfo = new SortInfo(parts[0], Boolean.parseBoolean(parts[1])); 150 } else { 151 sortInfo = new SortInfo(sortInfoDesc, true); 152 } 153 sortInfos.add(sortInfo); 154 } 155 } else { 156 // Sort Info Management 157 if (!StringUtils.isBlank(sortBy)) { 158 sortInfos = new ArrayList<>(); 159 String[] sorts = sortBy.split(","); 160 String[] orders = null; 161 if (!StringUtils.isBlank(sortOrder)) { 162 orders = sortOrder.split(","); 163 } 164 for (int i = 0; i < sorts.length; i++) { 165 String sort = sorts[i]; 166 boolean sortAscending = (orders != null && orders.length > i 167 && "asc".equals(orders[i].toLowerCase())); 168 sortInfos.add(new SortInfo(sort, sortAscending)); 169 } 170 } 171 } 172 173 Object[] parameters = null; 174 175 if (strParameters != null && !strParameters.isEmpty()) { 176 parameters = strParameters.toArray(new String[strParameters.size()]); 177 // expand specific parameters 178 for (int idx = 0; idx < parameters.length; idx++) { 179 String value = (String) parameters[idx]; 180 if (value.equals(CURRENT_USERID_PATTERN)) { 181 parameters[idx] = session.getPrincipal().getName(); 182 } else if (value.equals(CURRENT_REPO_PATTERN)) { 183 parameters[idx] = session.getRepositoryName(); 184 } 185 } 186 } 187 if (parameters == null) { 188 parameters = new Object[0]; 189 } 190 191 Map<String, Serializable> props = new HashMap<>(); 192 props.put(CoreQueryDocumentPageProvider.CORE_SESSION_PROPERTY, (Serializable) session); 193 194 if (query == null && (providerName == null || providerName.length() == 0)) { 195 // provide a defaut provider 196 providerName = "AUDIT_BROWSER"; 197 } 198 199 Long targetPage = null; 200 if (page != null) { 201 targetPage = page.longValue(); 202 } 203 if (currentPageIndex != null) { 204 targetPage = currentPageIndex.longValue(); 205 } 206 Long targetPageSize = null; 207 if (pageSize != null) { 208 targetPageSize = pageSize.longValue(); 209 } 210 211 if (query != null) { 212 213 AuditPageProvider app = new AuditPageProvider(); 214 app.setProperties(props); 215 GenericPageProviderDescriptor desc = new GenericPageProviderDescriptor(); 216 desc.setPattern(query); 217 app.setParameters(parameters); 218 app.setDefinition(desc); 219 app.setSortInfos(sortInfos); 220 app.setPageSize(targetPageSize); 221 app.setCurrentPage(targetPage); 222 return new LogEntryList(app); 223 } else { 224 225 DocumentModel searchDoc = null; 226 if (namedQueryParams != null && namedQueryParams.size() > 0) { 227 String docType = ppService.getPageProviderDefinition(providerName).getWhereClause().getDocType(); 228 searchDoc = session.createDocumentModel(docType); 229 DocumentHelper.setProperties(session, searchDoc, namedQueryParams); 230 } 231 232 PageProvider<LogEntry> pp = (PageProvider<LogEntry>) ppService.getPageProvider(providerName, searchDoc, 233 sortInfos, targetPageSize, targetPage, props, parameters); 234 // return new PaginablePageProvider<LogEntry>(pp); 235 return new LogEntryList(pp); 236 } 237 238 } 239}