001/* 002 * (C) Copyright 2011 Nuxeo SA (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.lang.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 = { "Audit.PageProvider" }) 061public class AuditPageProviderOperation { 062 063 public static final String ID = "Audit.QueryWithPageProvider"; 064 065 public static final String CURRENT_USERID_PATTERN = "$currentUser"; 066 067 public static final String CURRENT_REPO_PATTERN = "$currentRepository"; 068 069 private static final String SORT_PARAMETER_SEPARATOR = " "; 070 071 public static final String DESC = "DESC"; 072 073 public static final String ASC = "ASC"; 074 075 @Context 076 protected OperationContext context; 077 078 @Context 079 protected CoreSession session; 080 081 @Context 082 protected PageProviderService ppService; 083 084 @Param(name = "providerName", required = false) 085 protected String providerName; 086 087 @Param(name = "query", required = false) 088 protected String query; 089 090 @Param(name = "language", required = false, widget = Constants.W_OPTION, values = { NXQL.NXQL }) 091 protected String lang = NXQL.NXQL; 092 093 /** @deprecated since 6.0 use currentPageIndex instead. */ 094 @Param(name = "page", required = false) 095 @Deprecated 096 protected Integer page; 097 098 @Param(name = "currentPageIndex", required = false) 099 protected Integer currentPageIndex; 100 101 @Param(name = "pageSize", required = false) 102 protected Integer pageSize; 103 104 /** 105 * @deprecated since 6.0 use instead {@link #sortBy and @link #sortOrder}. 106 */ 107 @Deprecated 108 @Param(name = "sortInfo", required = false) 109 protected StringList sortInfoAsStringList; 110 111 @Param(name = "queryParams", required = false) 112 protected StringList strParameters; 113 114 @Param(name = "namedQueryParams", required = false) 115 protected Properties namedQueryParams; 116 117 /** 118 * @deprecated since 6.0, not used in operation. 119 */ 120 @Deprecated 121 @Param(name = "maxResults", required = false) 122 protected Integer maxResults = 100; 123 124 /** 125 * @since 6.0 126 */ 127 @Param(name = "sortBy", required = false, description = "Sort by " + "properties (separated by comma)") 128 protected String sortBy; 129 130 /** 131 * @since 6.0 132 */ 133 @Param(name = "sortOrder", required = false, description = "Sort order, " + "ASC or DESC", widget = Constants.W_OPTION, values = { 134 ASC, DESC }) 135 protected String sortOrder; 136 137 @SuppressWarnings("unchecked") 138 @OperationMethod 139 public Paginable<LogEntry> run() throws IOException { 140 141 List<SortInfo> sortInfos = null; 142 if (sortInfoAsStringList != null) { 143 sortInfos = new ArrayList<SortInfo>(); 144 for (String sortInfoDesc : sortInfoAsStringList) { 145 SortInfo sortInfo; 146 if (sortInfoDesc.contains(SORT_PARAMETER_SEPARATOR)) { 147 String[] parts = sortInfoDesc.split(SORT_PARAMETER_SEPARATOR); 148 sortInfo = new SortInfo(parts[0], Boolean.parseBoolean(parts[1])); 149 } else { 150 sortInfo = new SortInfo(sortInfoDesc, true); 151 } 152 sortInfos.add(sortInfo); 153 } 154 } else { 155 // Sort Info Management 156 if (!StringUtils.isBlank(sortBy)) { 157 sortInfos = new ArrayList<>(); 158 String[] sorts = sortBy.split(","); 159 String[] orders = null; 160 if (!StringUtils.isBlank(sortOrder)) { 161 orders = sortOrder.split(","); 162 } 163 for (int i = 0; i < sorts.length; i++) { 164 String sort = sorts[i]; 165 boolean sortAscending = (orders != null && orders.length > i && "asc".equals(orders[i].toLowerCase())); 166 sortInfos.add(new SortInfo(sort, sortAscending)); 167 } 168 } 169 } 170 171 Object[] parameters = null; 172 173 if (strParameters != null && !strParameters.isEmpty()) { 174 parameters = strParameters.toArray(new String[strParameters.size()]); 175 // expand specific parameters 176 for (int idx = 0; idx < parameters.length; idx++) { 177 String value = (String) parameters[idx]; 178 if (value.equals(CURRENT_USERID_PATTERN)) { 179 parameters[idx] = session.getPrincipal().getName(); 180 } else if (value.equals(CURRENT_REPO_PATTERN)) { 181 parameters[idx] = session.getRepositoryName(); 182 } 183 } 184 } 185 if (parameters == null) { 186 parameters = new Object[0]; 187 } 188 189 Map<String, Serializable> props = new HashMap<String, Serializable>(); 190 props.put(CoreQueryDocumentPageProvider.CORE_SESSION_PROPERTY, (Serializable) session); 191 192 if (query == null && (providerName == null || providerName.length() == 0)) { 193 // provide a defaut provider 194 providerName = "AUDIT_BROWSER"; 195 } 196 197 Long targetPage = null; 198 if (page != null) { 199 targetPage = page.longValue(); 200 } 201 if (currentPageIndex != null) { 202 targetPage = currentPageIndex.longValue(); 203 } 204 Long targetPageSize = null; 205 if (pageSize != null) { 206 targetPageSize = pageSize.longValue(); 207 } 208 209 if (query != null) { 210 211 AuditPageProvider app = new AuditPageProvider(); 212 app.setProperties(props); 213 GenericPageProviderDescriptor desc = new GenericPageProviderDescriptor(); 214 desc.setPattern(query); 215 app.setParameters(parameters); 216 app.setDefinition(desc); 217 app.setSortInfos(sortInfos); 218 app.setPageSize(targetPageSize); 219 app.setCurrentPage(targetPage); 220 return new LogEntryList(app); 221 } else { 222 223 DocumentModel searchDoc = null; 224 if (namedQueryParams != null && namedQueryParams.size() > 0) { 225 String docType = ppService.getPageProviderDefinition(providerName).getWhereClause().getDocType(); 226 searchDoc = session.createDocumentModel(docType); 227 DocumentHelper.setProperties(session, searchDoc, namedQueryParams); 228 } 229 230 PageProvider<LogEntry> pp = (PageProvider<LogEntry>) ppService.getPageProvider(providerName, searchDoc, 231 sortInfos, targetPageSize, targetPage, props, parameters); 232 // return new PaginablePageProvider<LogEntry>(pp); 233 return new LogEntryList(pp); 234 } 235 236 } 237}