Search in sources :

Example 1 with TrackedEntityInstanceAudit

use of org.hisp.dhis.audit.payloads.TrackedEntityInstanceAudit in project dhis2-core by dhis2.

the class DefaultTrackedEntityInstanceService method getTrackedEntityInstancesGrid.

// TODO lower index on attribute value?
@Override
@Transactional(readOnly = true)
public Grid getTrackedEntityInstancesGrid(TrackedEntityInstanceQueryParams params) {
    decideAccess(params);
    validate(params);
    validateSearchScope(params, true);
    handleAttributes(params);
    // ---------------------------------------------------------------------
    // Conform parameters
    // ---------------------------------------------------------------------
    params.conform();
    params.handleCurrentUserSelectionMode();
    // ---------------------------------------------------------------------
    // Grid headers
    // ---------------------------------------------------------------------
    Grid grid = new ListGrid();
    grid.addHeader(new GridHeader(TRACKED_ENTITY_INSTANCE_ID, "Instance"));
    grid.addHeader(new GridHeader(CREATED_ID, "Created"));
    grid.addHeader(new GridHeader(LAST_UPDATED_ID, "Last updated"));
    grid.addHeader(new GridHeader(ORG_UNIT_ID, "Organisation unit"));
    grid.addHeader(new GridHeader(ORG_UNIT_NAME, "Organisation unit name"));
    grid.addHeader(new GridHeader(TRACKED_ENTITY_ID, "Tracked entity type"));
    grid.addHeader(new GridHeader(INACTIVE_ID, "Inactive"));
    grid.addHeader(new GridHeader(POTENTIAL_DUPLICATE, "Potential duplicate"));
    if (params.isIncludeDeleted()) {
        grid.addHeader(new GridHeader(DELETED, "Deleted", ValueType.BOOLEAN, false, false));
    }
    for (QueryItem item : params.getAttributes()) {
        grid.addHeader(new GridHeader(item.getItem().getUid(), item.getItem().getName()));
    }
    List<Map<String, String>> entities = trackedEntityInstanceStore.getTrackedEntityInstancesGrid(params);
    // ---------------------------------------------------------------------
    // Grid rows
    // ---------------------------------------------------------------------
    String accessedBy = currentUserService.getCurrentUsername();
    Map<String, TrackedEntityType> trackedEntityTypes = new HashMap<>();
    if (params.hasTrackedEntityType()) {
        trackedEntityTypes.put(params.getTrackedEntityType().getUid(), params.getTrackedEntityType());
    }
    if (params.hasProgram() && params.getProgram().getTrackedEntityType() != null) {
        trackedEntityTypes.put(params.getProgram().getTrackedEntityType().getUid(), params.getProgram().getTrackedEntityType());
    }
    Set<String> tes = new HashSet<>();
    for (Map<String, String> entity : entities) {
        if (params.getUser() != null && !params.getUser().isSuper() && params.hasProgram() && (params.getProgram().getAccessLevel().equals(AccessLevel.PROTECTED) || params.getProgram().getAccessLevel().equals(AccessLevel.CLOSED))) {
            TrackedEntityInstance tei = trackedEntityInstanceStore.getByUid(entity.get(TRACKED_ENTITY_INSTANCE_ID));
            if (!trackerOwnershipAccessManager.hasAccess(params.getUser(), tei, params.getProgram())) {
                continue;
            }
        }
        grid.addRow();
        grid.addValue(entity.get(TRACKED_ENTITY_INSTANCE_ID));
        grid.addValue(entity.get(CREATED_ID));
        grid.addValue(entity.get(LAST_UPDATED_ID));
        grid.addValue(entity.get(ORG_UNIT_ID));
        grid.addValue(entity.get(ORG_UNIT_NAME));
        grid.addValue(entity.get(TRACKED_ENTITY_ID));
        grid.addValue(entity.get(INACTIVE_ID));
        grid.addValue(entity.get(POTENTIAL_DUPLICATE));
        if (params.isIncludeDeleted()) {
            grid.addValue(entity.get(DELETED));
        }
        tes.add(entity.get(TRACKED_ENTITY_ID));
        TrackedEntityType te = trackedEntityTypes.get(entity.get(TRACKED_ENTITY_ID));
        if (te == null) {
            te = trackedEntityTypeService.getTrackedEntityType(entity.get(TRACKED_ENTITY_ID));
            trackedEntityTypes.put(entity.get(TRACKED_ENTITY_ID), te);
        }
        if (te != null && te.isAllowAuditLog() && accessedBy != null) {
            TrackedEntityInstanceAudit trackedEntityInstanceAudit = new TrackedEntityInstanceAudit(entity.get(TRACKED_ENTITY_INSTANCE_ID), accessedBy, AuditType.SEARCH);
            trackedEntityInstanceAuditService.addTrackedEntityInstanceAudit(trackedEntityInstanceAudit);
        }
        for (QueryItem item : params.getAttributes()) {
            grid.addValue(entity.get(item.getItemId()));
        }
    }
    Map<String, Object> metaData = new HashMap<>();
    if (params.isPaging()) {
        int count = 0;
        if (params.isTotalPages()) {
            count = trackedEntityInstanceStore.getTrackedEntityInstanceCountForGrid(params);
        }
        Pager pager = new Pager(params.getPageWithDefault(), count, params.getPageSizeWithDefault());
        metaData.put(PAGER_META_KEY, pager);
    }
    if (!params.isSkipMeta()) {
        Map<String, String> names = new HashMap<>();
        for (String te : tes) {
            TrackedEntityType entity = trackedEntityTypes.get(te);
            names.put(te, entity != null ? entity.getDisplayName() : null);
        }
        metaData.put(META_DATA_NAMES_KEY, names);
    }
    grid.setMetaData(metaData);
    return grid;
}
Also used : QueryItem(org.hisp.dhis.common.QueryItem) HashMap(java.util.HashMap) ListGrid(org.hisp.dhis.system.grid.ListGrid) Grid(org.hisp.dhis.common.Grid) ListGrid(org.hisp.dhis.system.grid.ListGrid) GridHeader(org.hisp.dhis.common.GridHeader) Pager(org.hisp.dhis.common.Pager) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet) TrackedEntityInstanceAudit(org.hisp.dhis.audit.payloads.TrackedEntityInstanceAudit) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with TrackedEntityInstanceAudit

use of org.hisp.dhis.audit.payloads.TrackedEntityInstanceAudit in project dhis2-core by dhis2.

the class AuditController method getTrackedEnityInstanceAudit.

@GetMapping("trackedEntityInstance")
@ResponseBody
public RootNode getTrackedEnityInstanceAudit(@RequestParam(required = false, defaultValue = "") List<String> tei, @RequestParam(required = false, defaultValue = "") List<String> user, @RequestParam(required = false) AuditType auditType, @RequestParam(required = false) Date startDate, @RequestParam(required = false) Date endDate, @RequestParam(required = false) Boolean skipPaging, @RequestParam(required = false) Boolean paging, @RequestParam(required = false, defaultValue = "50") int pageSize, @RequestParam(required = false, defaultValue = "1") int page) {
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    if (fields.isEmpty()) {
        fields.addAll(Preset.ALL.getFields());
    }
    TrackedEntityInstanceAuditQueryParams params = new TrackedEntityInstanceAuditQueryParams();
    params.setTrackedEntityInstances(new HashSet<>(tei));
    params.setUsers(new HashSet<>(user));
    params.setAuditType(auditType);
    params.setStartDate(startDate);
    params.setEndDate(endDate);
    params.setSkipPaging(PagerUtils.isSkipPaging(skipPaging, paging));
    List<TrackedEntityInstanceAudit> teiAudits;
    Pager pager = null;
    if (!params.isSkipPaging()) {
        int total = trackedEntityInstanceAuditService.getTrackedEntityInstanceAuditsCount(params);
        pager = new Pager(page, total, pageSize);
        params.setFirst(pager.getOffset());
        params.setMax(pager.getPageSize());
    }
    teiAudits = trackedEntityInstanceAuditService.getTrackedEntityInstanceAudits(params);
    RootNode rootNode = NodeUtils.createMetadata();
    if (pager != null) {
        rootNode.addChild(NodeUtils.createPager(pager));
    }
    CollectionNode trackedEntityInstanceAudits = rootNode.addChild(new CollectionNode("trackedEntityInstanceAudits", true));
    trackedEntityInstanceAudits.addChildren(fieldFilterService.toCollectionNode(TrackedEntityInstanceAudit.class, new FieldFilterParams(teiAudits, fields)).getChildren());
    return rootNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) Pager(org.hisp.dhis.common.Pager) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) TrackedEntityInstanceAuditQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceAuditQueryParams) CollectionNode(org.hisp.dhis.node.types.CollectionNode) TrackedEntityInstanceAudit(org.hisp.dhis.audit.payloads.TrackedEntityInstanceAudit) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with TrackedEntityInstanceAudit

use of org.hisp.dhis.audit.payloads.TrackedEntityInstanceAudit in project dhis2-core by dhis2.

the class DefaultTrackedEntityInstanceService method addTrackedEntityInstanceAudit.

private void addTrackedEntityInstanceAudit(TrackedEntityInstance trackedEntityInstance, String user, AuditType auditType) {
    if (user != null && trackedEntityInstance != null && trackedEntityInstance.getTrackedEntityType() != null && trackedEntityInstance.getTrackedEntityType().isAllowAuditLog()) {
        TrackedEntityInstanceAudit trackedEntityInstanceAudit = new TrackedEntityInstanceAudit(trackedEntityInstance.getUid(), user, auditType);
        trackedEntityInstanceAuditService.addTrackedEntityInstanceAudit(trackedEntityInstanceAudit);
    }
}
Also used : TrackedEntityInstanceAudit(org.hisp.dhis.audit.payloads.TrackedEntityInstanceAudit)

Example 4 with TrackedEntityInstanceAudit

use of org.hisp.dhis.audit.payloads.TrackedEntityInstanceAudit in project dhis2-core by dhis2.

the class HibernateTrackedEntityInstanceAuditStore method getTrackedEntityInstanceAudits.

@Override
public List<TrackedEntityInstanceAudit> getTrackedEntityInstanceAudits(TrackedEntityInstanceAuditQueryParams params) {
    CriteriaBuilder builder = getCriteriaBuilder();
    JpaQueryParameters<TrackedEntityInstanceAudit> jpaParameters = newJpaParameters().addPredicates(getTrackedEntityInstanceAuditPredicates(params, builder)).addOrder(root -> builder.desc(root.get("created")));
    if (!params.isSkipPaging()) {
        jpaParameters.setFirstResult(params.getFirst()).setMaxResults(params.getMax());
    }
    return getList(builder, jpaParameters);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) TrackedEntityInstanceAudit(org.hisp.dhis.audit.payloads.TrackedEntityInstanceAudit)

Aggregations

TrackedEntityInstanceAudit (org.hisp.dhis.audit.payloads.TrackedEntityInstanceAudit)4 Pager (org.hisp.dhis.common.Pager)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 Grid (org.hisp.dhis.common.Grid)1 GridHeader (org.hisp.dhis.common.GridHeader)1 QueryItem (org.hisp.dhis.common.QueryItem)1 FieldFilterParams (org.hisp.dhis.fieldfilter.FieldFilterParams)1 CollectionNode (org.hisp.dhis.node.types.CollectionNode)1 RootNode (org.hisp.dhis.node.types.RootNode)1 ListGrid (org.hisp.dhis.system.grid.ListGrid)1 TrackedEntityInstanceAuditQueryParams (org.hisp.dhis.trackedentity.TrackedEntityInstanceAuditQueryParams)1 Transactional (org.springframework.transaction.annotation.Transactional)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1