Merged 5.2.N-AUDIT-API (5.2.2) to 5.2.N (5.2.2)

137897 anechifor: REPO-2619 - adding implementation of listAuditEntries


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@137962 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Jan Vonka
2017-07-12 12:09:36 +00:00
parent bbfd3a75a6
commit 72ca042f3b
4 changed files with 405 additions and 2 deletions

View File

@@ -25,23 +25,40 @@
*/
package org.alfresco.rest.api.impl;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.rest.antlr.WhereClauseParser;
import org.alfresco.rest.api.Audit;
import org.alfresco.rest.api.model.AuditApp;
import org.alfresco.rest.api.model.AuditEntry;
import org.alfresco.rest.api.model.UserInfo;
import org.alfresco.rest.framework.core.exceptions.DisabledServiceException;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Paging;
import org.alfresco.rest.framework.resource.parameters.Parameters;
import org.alfresco.rest.framework.resource.parameters.SortColumn;
import org.alfresco.rest.framework.resource.parameters.where.Query;
import org.alfresco.rest.framework.resource.parameters.where.QueryHelper;
import org.alfresco.rest.workflow.api.impl.MapBasedQueryWalker;
import org.alfresco.service.cmr.audit.AuditQueryParameters;
import org.alfresco.service.cmr.audit.AuditService;
import org.alfresco.service.cmr.audit.AuditService.AuditApplication;
import org.alfresco.service.cmr.audit.AuditService.AuditQueryCallback;
import org.alfresco.util.Pair;
/**
* Handles audit (applications & entries)
@@ -50,7 +67,23 @@ import org.alfresco.service.cmr.audit.AuditService.AuditApplication;
*/
public class AuditImpl implements Audit
{
private final static String DISABLED = "Audit is disabled system-wide";
private final static int MAX_ITEMS_AUDIT_ENTRIES = 100;
// list of equals filter's auditEntry (via where clause)
private final static Set<String> LIST_AUDIT_ENTRY_EQUALS_QUERY_PROPERTIES = new HashSet<>(
Arrays.asList(new String[] { CREATED_BY_USER, VALUES_KEY, VALUES_VALUE }));
// map of sort parameters for the moment one createdAt
private final static Map<String, String> SORT_PARAMS_TO_NAMES;
static
{
Map<String, String> aMap = new HashMap<>(1);
aMap.put(CREATED_AT, CREATED_AT);
SORT_PARAMS_TO_NAMES = Collections.unmodifiableMap(aMap);
}
private AuditService auditService;
@@ -140,6 +173,197 @@ public class AuditImpl implements Audit
return CollectionWithPagingInfo.asPaged(paging, auditApps, hasMoreItems, totalItems);
}
@Override
public CollectionWithPagingInfo<AuditEntry> listAuditEntries(String auditAppId, Parameters parameters)
{
checkEnabled();
// adding orderBy property
Pair<String, Boolean> sortProp = getAuditEntrySortProp(parameters);
Boolean forward = true;
if ((sortProp != null) && (sortProp.getFirst().equals(CREATED_AT)))
forward = sortProp.getSecond();
// Parse where clause properties.
List<AuditEntry> entriesAfterWhereQuery = new ArrayList<AuditEntry>();
Query q = parameters.getQuery();
if (q != null)
{
// filtering via "where" clause
AuditEntryQueryWalker propertyWalker = new AuditEntryQueryWalker();
QueryHelper.walk(q, propertyWalker);
entriesAfterWhereQuery = getQueryResultAuditEntries(auditAppId, propertyWalker, MAX_ITEMS_AUDIT_ENTRIES, forward);
}
// paging
Paging paging = parameters.getPaging();
int skipCount = paging.getSkipCount();
int maxItems = paging.getMaxItems();
int max = skipCount + maxItems; // to detect hasMoreItems
int totalItems = entriesAfterWhereQuery.size();
if (skipCount >= totalItems)
{
List<AuditEntry> empty = Collections.emptyList();
return CollectionWithPagingInfo.asPaged(paging, empty, false, totalItems);
}
else
{
int end = Math.min(max, totalItems);
boolean hasMoreItems = totalItems > end;
entriesAfterWhereQuery = entriesAfterWhereQuery.subList(skipCount, end);
return CollectionWithPagingInfo.asPaged(paging, entriesAfterWhereQuery, hasMoreItems, totalItems);
}
}
/**
*
* @param parameters
* @return
* @throws InvalidArgumentException
*/
private Pair<String, Boolean> getAuditEntrySortProp(Parameters parameters)
{
Pair<String, Boolean> sortProp = null;
List<SortColumn> sortCols = parameters.getSorting();
if ((sortCols != null) && (sortCols.size() > 0))
{
if (sortCols.size() > 1)
{
throw new InvalidArgumentException("Multiple sort fields not allowed.");
}
SortColumn sortCol = sortCols.get(0);
String sortPropName = SORT_PARAMS_TO_NAMES.get(sortCol.column);
if (sortPropName == null)
{
throw new InvalidArgumentException("Invalid sort field: " + sortCol.column);
}
sortProp = new Pair<>(sortPropName, (sortCol.asc ? Boolean.TRUE : Boolean.FALSE));
}
return sortProp;
}
/**
*
* @author anechifor
*
*/
private static class AuditEntryQueryWalker extends MapBasedQueryWalker
{
private Long fromTime;
private Long toTime;
public AuditEntryQueryWalker()
{
super(LIST_AUDIT_ENTRY_EQUALS_QUERY_PROPERTIES, null);
}
@Override
public void and()
{
// allow AND, e.g. isRoot=true AND zones in ('BLAH')
}
@Override
public void between(String propertyName, String firstValue, String secondValue, boolean negated)
{
if (propertyName.equals(CREATED_AT))
{
fromTime = new Long(firstValue);
toTime = new Long(secondValue);
}
}
public Long getFromTime()
{
return fromTime;
}
public Long getToTime()
{
return toTime;
}
public String getCreatedByUser()
{
return getProperty(CREATED_BY_USER, WhereClauseParser.EQUALS, String.class);
}
public String getValuesKey()
{
return getProperty(VALUES_KEY, WhereClauseParser.EQUALS, String.class);
}
public String getValuesValue()
{
return getProperty(VALUES_VALUE, WhereClauseParser.EQUALS, String.class);
}
}
/**
*
* @param auditAppId
* @param propertyWalker
* @param maxItem
* @param forward
* @return
*/
public List<AuditEntry> getQueryResultAuditEntries(String auditAppId, AuditEntryQueryWalker propertyWalker, int maxItem, Boolean forward)
{
final List<AuditEntry> results = new ArrayList<AuditEntry>();
AuditApplication auditApplication = findAuditAppById(auditAppId);
if (auditApplication != null)
{
String auditApplicationName = auditApplication.getName();
// Execute the query
AuditQueryParameters params = new AuditQueryParameters();
// used to orderBY by field createdAt
params.setForward(forward);
params.setApplicationName(auditApplicationName);
params.setUser(propertyWalker.getCreatedByUser());
params.setFromTime(propertyWalker.getFromTime());
params.setToTime(propertyWalker.getToTime());
params.addSearchKey(propertyWalker.getValuesKey(), propertyWalker.getValuesValue());
// create the callback for auditQuery method
final AuditQueryCallback callback = new AuditQueryCallback()
{
public boolean valuesRequired()
{
return true;
}
public boolean handleAuditEntryError(Long entryId, String errorMsg, Throwable error)
{
throw new AlfrescoRuntimeException("Failed to retrieve audit data.", error);
}
public boolean handleAuditEntry(Long entryId, String applicationName, String user, long time, Map<String, Serializable> values)
{
AuditEntry auditEntry = new AuditEntry(entryId, new Long(auditAppId), new UserInfo(null, user, null), new Date(time), values);
results.add(auditEntry);
return true;
}
};
auditService.auditQuery(callback, params, maxItem);
}
return results;
}
@Override
public AuditApp update(String auditAppId, AuditApp auditApp, Parameters parameters)
{
@@ -166,4 +390,5 @@ public class AuditImpl implements Audit
return new AuditApp(auditApplication.getKey().substring(1), auditApplication.getName(), auditApp.getIsEnabled());
}
}