Search in sources :

Example 1 with AuditEntry

use of org.alfresco.rest.api.model.AuditEntry in project alfresco-remote-api by Alfresco.

the class AuditImpl method getQueryResultAuditEntries.

/**
 * @param auditAppId
 * @param propertyWalker
 * @param includeParams
 * @param maxItem
 * @param forward
 * @return
 */
public List<AuditEntry> getQueryResultAuditEntries(AuditService.AuditApplication auditApplication, AuditEntryQueryWalker propertyWalker, List<String> includeParam, int maxItem, Boolean forward) {
    final List<AuditEntry> results = new ArrayList<>();
    final String auditAppId = auditApplication.getKey().substring(1);
    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());
    Long fromId = propertyWalker.getFromId();
    Long toId = propertyWalker.getToId();
    validateWhereBetween(auditAppId, fromId, toId);
    Long fromTime = propertyWalker.getFromTime();
    Long toTime = propertyWalker.getToTime();
    validateWhereBetween(auditAppId, fromTime, toTime);
    params.setFromTime(fromTime);
    params.setToTime(toTime);
    params.setFromId(fromId);
    params.setToId(toId);
    if (propertyWalker.getValuesKey() != null && propertyWalker.getValuesValue() != null) {
        params.addSearchKey(propertyWalker.getValuesKey(), propertyWalker.getValuesValue());
    }
    final Map<String, UserInfo> mapUserInfo = new HashMap<>(10);
    // create the callback for auditQuery method
    final AuditQueryCallback callback = new AuditQueryCallback() {

        public boolean valuesRequired() {
            return ((includeParam != null) && (includeParam.contains(PARAM_INCLUDE_VALUES)));
        }

        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 userName, long time, Map<String, Serializable> values) {
            UserInfo userInfo = Node.lookupUserInfo(userName, mapUserInfo, personService);
            AuditEntry auditEntry = new AuditEntry(entryId, auditAppId, userInfo, new Date(time), values);
            results.add(auditEntry);
            return true;
        }
    };
    auditService.auditQuery(callback, params, maxItem);
    return results;
}
Also used : HashMap(java.util.HashMap) AuditQueryParameters(org.alfresco.service.cmr.audit.AuditQueryParameters) ArrayList(java.util.ArrayList) UserInfo(org.alfresco.rest.api.model.UserInfo) Date(java.util.Date) AuditEntry(org.alfresco.rest.api.model.AuditEntry) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) AuditQueryCallback(org.alfresco.service.cmr.audit.AuditService.AuditQueryCallback) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with AuditEntry

use of org.alfresco.rest.api.model.AuditEntry in project alfresco-remote-api by Alfresco.

the class AuditImpl method getQueryResultAuditEntriesByNodeRef.

private List<AuditEntry> getQueryResultAuditEntriesByNodeRef(NodeRef nodeRef, AuditEntriesByNodeIdQueryWalker propertyWalker, List<String> includeParam, boolean forward, int limit) {
    final List<AuditEntry> results = new ArrayList<>();
    String auditAppId = "alfresco-access";
    String auditApplicationName = AuthenticationUtil.runAs(new RunAsWork<String>() {

        public String doWork() throws Exception {
            return findAuditAppByIdOr404(auditAppId).getName();
        }
    }, AuthenticationUtil.getSystemUserName());
    // create the callback for auditQuery method
    final AuditQueryCallback callback = new AuditQueryCallback() {

        public boolean valuesRequired() {
            return ((includeParam != null) && (includeParam.contains(PARAM_INCLUDE_VALUES)));
        }

        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 userName, long time, Map<String, Serializable> values) {
            UserInfo userInfo = Node.lookupUserInfo(userName, new HashMap<>(0), personService);
            AuditEntry auditEntry = new AuditEntry(entryId, auditAppId, userInfo, new Date(time), values);
            results.add(auditEntry);
            return true;
        }
    };
    // resolve the path of the node - note: this will also check read permission for current user
    final String nodePath = ISO9075.decode(nodeService.getPath(nodeRef).toPrefixString(namespaceService));
    Long fromTime = propertyWalker.getFromTime();
    Long toTime = propertyWalker.getToTime();
    validateWhereBetween(nodeRef.getId(), fromTime, toTime);
    AuthenticationUtil.runAs(new RunAsWork<Object>() {

        public Object doWork() throws Exception {
            // QueryParameters
            AuditQueryParameters pathParams = new AuditQueryParameters();
            // used to orderBY by field createdAt
            pathParams.setForward(forward);
            pathParams.setUser(propertyWalker.getCreatedByUser());
            pathParams.setFromTime(fromTime);
            pathParams.setToTime(toTime);
            pathParams.setApplicationName(auditApplicationName);
            pathParams.addSearchKey("/" + auditAppId + "/transaction/path", nodePath);
            auditService.auditQuery(callback, pathParams, limit);
            AuditQueryParameters copyFromPathParams = new AuditQueryParameters();
            // used to orderBY by field createdAt
            copyFromPathParams.setForward(forward);
            copyFromPathParams.setUser(propertyWalker.getCreatedByUser());
            copyFromPathParams.setFromTime(fromTime);
            copyFromPathParams.setToTime(toTime);
            copyFromPathParams.setApplicationName(auditApplicationName);
            copyFromPathParams.addSearchKey("/" + auditAppId + "/transaction/copy/from/path", nodePath);
            auditService.auditQuery(callback, copyFromPathParams, limit);
            AuditQueryParameters moveFromPathParams = new AuditQueryParameters();
            // used to orderBY by field createdAt
            moveFromPathParams.setForward(forward);
            moveFromPathParams.setUser(propertyWalker.getCreatedByUser());
            moveFromPathParams.setFromTime(fromTime);
            moveFromPathParams.setToTime(toTime);
            moveFromPathParams.setApplicationName(auditApplicationName);
            moveFromPathParams.addSearchKey("/" + auditAppId + "/transaction/move/from/path", nodePath);
            auditService.auditQuery(callback, moveFromPathParams, limit);
            return null;
        }
    }, AuthenticationUtil.getSystemUserName());
    return results;
}
Also used : AuditQueryParameters(org.alfresco.service.cmr.audit.AuditQueryParameters) ArrayList(java.util.ArrayList) UserInfo(org.alfresco.rest.api.model.UserInfo) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) DisabledServiceException(org.alfresco.rest.framework.core.exceptions.DisabledServiceException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) Date(java.util.Date) AuditEntry(org.alfresco.rest.api.model.AuditEntry) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) AuditQueryCallback(org.alfresco.service.cmr.audit.AuditService.AuditQueryCallback) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with AuditEntry

use of org.alfresco.rest.api.model.AuditEntry in project alfresco-remote-api by Alfresco.

the class AuditImpl method listAuditEntriesByNodeId.

@Override
public CollectionWithPagingInfo<AuditEntry> listAuditEntriesByNodeId(String nodeId, Parameters parameters) {
    AuthenticationUtil.runAs(new RunAsWork<Void>() {

        public Void doWork() throws Exception {
            checkEnabled();
            return null;
        }
    }, AuthenticationUtil.getSystemUserName());
    // note: node read permission is checked later - see nodeService.getPath
    NodeRef nodeRef = nodes.validateNode(nodeId);
    List<AuditEntry> entriesAudit = new ArrayList<>();
    // adding orderBy property
    Pair<String, Boolean> sortProp = getAuditEntrySortProp(parameters);
    Boolean forward = true;
    if ((sortProp != null) && (sortProp.getFirst().equals(CREATED_AT))) {
        forward = sortProp.getSecond();
    }
    // paging
    Paging paging = parameters.getPaging();
    int skipCount = paging.getSkipCount();
    int maxItems = paging.getMaxItems();
    // to detect hasMoreItems
    int limit = skipCount + maxItems + 1;
    Query q = parameters.getQuery();
    if (q != null) {
        // filtering via "where" clause
        AuditEntriesByNodeIdQueryWalker propertyWalker = new AuditEntriesByNodeIdQueryWalker();
        QueryHelper.walk(q, propertyWalker);
        entriesAudit = getQueryResultAuditEntriesByNodeRef(nodeRef, propertyWalker, parameters.getInclude(), forward, limit);
    }
    // clear null elements
    entriesAudit.removeAll(Collections.singleton(null));
    int totalItems = entriesAudit.size();
    if (skipCount >= totalItems) {
        List<AuditEntry> empty = Collections.emptyList();
        return CollectionWithPagingInfo.asPaged(paging, empty, false, totalItems);
    } else {
        int end = Math.min(limit - 1, totalItems);
        boolean hasMoreItems = totalItems > end;
        entriesAudit = entriesAudit.subList(skipCount, end);
        return CollectionWithPagingInfo.asPaged(paging, entriesAudit, hasMoreItems, totalItems);
    }
}
Also used : Query(org.alfresco.rest.framework.resource.parameters.where.Query) Paging(org.alfresco.rest.framework.resource.parameters.Paging) ArrayList(java.util.ArrayList) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) DisabledServiceException(org.alfresco.rest.framework.core.exceptions.DisabledServiceException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) AuditEntry(org.alfresco.rest.api.model.AuditEntry)

Example 4 with AuditEntry

use of org.alfresco.rest.api.model.AuditEntry in project alfresco-remote-api by Alfresco.

the class AuditImpl method listAuditEntries.

@Override
public CollectionWithPagingInfo<AuditEntry> listAuditEntries(String auditAppId, Parameters parameters) {
    checkEnabled();
    AuditService.AuditApplication auditApplication = findAuditAppByIdOr404(auditAppId);
    // 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> entriesAudit = new ArrayList<>();
    Query q = parameters.getQuery();
    // paging
    Paging paging = parameters.getPaging();
    int skipCount = paging.getSkipCount();
    int maxItems = paging.getMaxItems();
    // to detect hasMoreItems
    int limit = skipCount + maxItems + 1;
    if (q != null) {
        // filtering via "where" clause
        AuditEntryQueryWalker propertyWalker = new AuditEntryQueryWalker();
        QueryHelper.walk(q, propertyWalker);
        entriesAudit = getQueryResultAuditEntries(auditApplication, propertyWalker, parameters.getInclude(), limit, forward);
    }
    // clear null elements
    entriesAudit.removeAll(Collections.singleton(null));
    int totalRetrievedItems = entriesAudit.size();
    int end = Math.min(limit - 1, totalRetrievedItems);
    boolean hasMoreItems = totalRetrievedItems > end;
    String omitTotalItemsParameter = parameters.getParameter("omitTotalItems");
    boolean omitTotalItems = (null != omitTotalItemsParameter) && Boolean.parseBoolean(omitTotalItemsParameter);
    Integer totalItems;
    if (omitTotalItems) {
        totalItems = null;
    } else {
        totalItems = hasMoreItems ? getAuditEntriesCountByApp(auditApplication) : totalRetrievedItems;
    }
    entriesAudit = (skipCount >= totalRetrievedItems) ? Collections.emptyList() : entriesAudit.subList(skipCount, end);
    return CollectionWithPagingInfo.asPaged(paging, entriesAudit, hasMoreItems, totalItems);
}
Also used : Query(org.alfresco.rest.framework.resource.parameters.where.Query) Paging(org.alfresco.rest.framework.resource.parameters.Paging) ArrayList(java.util.ArrayList) AuditApplication(org.alfresco.service.cmr.audit.AuditService.AuditApplication) AuditEntry(org.alfresco.rest.api.model.AuditEntry) AuditService(org.alfresco.service.cmr.audit.AuditService)

Example 5 with AuditEntry

use of org.alfresco.rest.api.model.AuditEntry in project alfresco-remote-api by Alfresco.

the class AuditImpl method getAuditEntry.

@Override
public AuditEntry getAuditEntry(String auditAppId, long auditEntryId, Parameters parameters) {
    checkEnabled();
    AuditService.AuditApplication auditApplication = findAuditAppByIdOr404(auditAppId);
    // Execute the query
    AuditQueryParameters params = new AuditQueryParameters();
    params.setApplicationName(auditApplication.getName());
    params.setFromId(auditEntryId);
    params.setToId(auditEntryId + 1);
    List<String> includeParam = new ArrayList<>();
    if (parameters != null) {
        includeParam.addAll(parameters.getInclude());
    }
    // Add values for single get
    includeParam.add(PARAM_INCLUDE_VALUES);
    final List<AuditEntry> results = new ArrayList<>();
    // create the callback for auditQuery method
    final AuditQueryCallback callback = new AuditQueryCallback() {

        public boolean valuesRequired() {
            return ((includeParam != null) && (includeParam.contains(PARAM_INCLUDE_VALUES)));
        }

        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 userName, long time, Map<String, Serializable> values) {
            UserInfo userInfo = Node.lookupUserInfo(userName, new HashMap<>(0), personService);
            AuditEntry auditEntry = new AuditEntry(entryId, auditAppId, userInfo, new Date(time), values);
            results.add(auditEntry);
            return true;
        }
    };
    auditService.auditQuery(callback, params, 1);
    if (results.size() != 1) {
        throw new EntityNotFoundException("" + auditEntryId);
    }
    return results.get(0);
}
Also used : AuditQueryParameters(org.alfresco.service.cmr.audit.AuditQueryParameters) ArrayList(java.util.ArrayList) UserInfo(org.alfresco.rest.api.model.UserInfo) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) Date(java.util.Date) AuditApplication(org.alfresco.service.cmr.audit.AuditService.AuditApplication) AuditEntry(org.alfresco.rest.api.model.AuditEntry) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) AuditQueryCallback(org.alfresco.service.cmr.audit.AuditService.AuditQueryCallback) AuditService(org.alfresco.service.cmr.audit.AuditService) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ArrayList (java.util.ArrayList)5 AuditEntry (org.alfresco.rest.api.model.AuditEntry)5 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)4 Date (java.util.Date)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 UserInfo (org.alfresco.rest.api.model.UserInfo)3 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)3 AuditQueryParameters (org.alfresco.service.cmr.audit.AuditQueryParameters)3 AuditQueryCallback (org.alfresco.service.cmr.audit.AuditService.AuditQueryCallback)3 DisabledServiceException (org.alfresco.rest.framework.core.exceptions.DisabledServiceException)2 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)2 Paging (org.alfresco.rest.framework.resource.parameters.Paging)2 Query (org.alfresco.rest.framework.resource.parameters.where.Query)2 AuditService (org.alfresco.service.cmr.audit.AuditService)2 AuditApplication (org.alfresco.service.cmr.audit.AuditService.AuditApplication)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1