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