Search in sources :

Example 1 with DataValueAudit

use of org.hisp.dhis.datavalue.DataValueAudit in project dhis2-core by dhis2.

the class AuditController method getAggregateDataValueAudit.

@RequestMapping(value = "dataValue", method = RequestMethod.GET)
@ResponseBody
public RootNode getAggregateDataValueAudit(@RequestParam(required = false, defaultValue = "") List<String> ds, @RequestParam(required = false, defaultValue = "") List<String> de, @RequestParam(required = false, defaultValue = "") List<String> pe, @RequestParam(required = false, defaultValue = "") List<String> ou, @RequestParam(required = false) String co, @RequestParam(required = false) String cc, @RequestParam(required = false) AuditType auditType, @RequestParam(required = false) boolean skipPaging, @RequestParam(required = false, defaultValue = "50") int pageSize, @RequestParam(required = false, defaultValue = "1") int page) throws WebMessageException {
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    if (fields.isEmpty()) {
        fields.addAll(Preset.ALL.getFields());
    }
    List<DataElement> dataElements = new ArrayList<>();
    dataElements.addAll(getDataElements(de));
    dataElements.addAll(getDataElementsByDataSet(ds));
    List<Period> periods = getPeriods(pe);
    List<OrganisationUnit> organisationUnits = getOrganisationUnit(ou);
    DataElementCategoryOptionCombo categoryOptionCombo = getCategoryOptionCombo(co);
    DataElementCategoryOptionCombo attributeOptionCombo = getAttributeOptionCombo(cc);
    List<DataValueAudit> dataValueAudits;
    Pager pager = null;
    if (skipPaging) {
        dataValueAudits = dataValueAuditService.getDataValueAudits(dataElements, periods, organisationUnits, categoryOptionCombo, attributeOptionCombo, auditType);
    } else {
        int total = dataValueAuditService.countDataValueAudits(dataElements, periods, organisationUnits, categoryOptionCombo, attributeOptionCombo, auditType);
        pager = new Pager(page, total, pageSize);
        dataValueAudits = dataValueAuditService.getDataValueAudits(dataElements, periods, organisationUnits, categoryOptionCombo, attributeOptionCombo, auditType, pager.getOffset(), pager.getPageSize());
    }
    RootNode rootNode = NodeUtils.createMetadata();
    if (pager != null) {
        rootNode.addChild(NodeUtils.createPager(pager));
    }
    CollectionNode trackedEntityAttributeValueAudits = rootNode.addChild(new CollectionNode("dataValueAudits", true));
    trackedEntityAttributeValueAudits.addChildren(fieldFilterService.filter(DataValueAudit.class, dataValueAudits, fields).getChildren());
    return rootNode;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) RootNode(org.hisp.dhis.node.types.RootNode) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) CollectionNode(org.hisp.dhis.node.types.CollectionNode) DataElement(org.hisp.dhis.dataelement.DataElement) Pager(org.hisp.dhis.common.Pager) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) DataValueAudit(org.hisp.dhis.datavalue.DataValueAudit) TrackedEntityDataValueAudit(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValueAudit) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with DataValueAudit

use of org.hisp.dhis.datavalue.DataValueAudit in project dhis2-core by dhis2.

the class HibernateDataValueAuditStore method getDataValueAuditPredicates.

/**
 * Returns a list of Predicates generated from given parameters. Returns an
 * empty list if given Period does not exist in database.
 *
 * @param builder the {@link CriteriaBuilder}.
 * @param dataElements the list of data elements.
 * @param periods the list of periods.
 * @param organisationUnits the list of organisation units.
 * @param categoryOptionCombo the category option combo.
 * @param attributeOptionCombo the attribute option combo.
 * @param auditType the audit type.
 */
private List<Function<Root<DataValueAudit>, Predicate>> getDataValueAuditPredicates(CriteriaBuilder builder, List<DataElement> dataElements, List<Period> periods, List<OrganisationUnit> organisationUnits, CategoryOptionCombo categoryOptionCombo, CategoryOptionCombo attributeOptionCombo, AuditType auditType) {
    List<Period> storedPeriods = new ArrayList<>();
    if (periods != null && !periods.isEmpty()) {
        for (Period period : periods) {
            Period storedPeriod = periodStore.reloadPeriod(period);
            if (storedPeriod != null) {
                storedPeriods.add(storedPeriod);
            }
        }
    }
    List<Function<Root<DataValueAudit>, Predicate>> predicates = new ArrayList<>();
    if (!storedPeriods.isEmpty()) {
        predicates.add(root -> root.get("period").in(storedPeriods));
    } else if (periods != null && !periods.isEmpty()) {
        return predicates;
    }
    if (dataElements != null && !dataElements.isEmpty()) {
        predicates.add(root -> root.get("dataElement").in(dataElements));
    }
    if (organisationUnits != null && !organisationUnits.isEmpty()) {
        predicates.add(root -> root.get("organisationUnit").in(organisationUnits));
    }
    if (categoryOptionCombo != null) {
        predicates.add(root -> builder.equal(root.get("categoryOptionCombo"), categoryOptionCombo));
    }
    if (attributeOptionCombo != null) {
        predicates.add(root -> builder.equal(root.get("attributeOptionCombo"), attributeOptionCombo));
    }
    if (auditType != null) {
        predicates.add(root -> builder.equal(root.get("auditType"), auditType));
    }
    return predicates;
}
Also used : Function(java.util.function.Function) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) DataValueAudit(org.hisp.dhis.datavalue.DataValueAudit)

Example 3 with DataValueAudit

use of org.hisp.dhis.datavalue.DataValueAudit in project dhis2-core by dhis2.

the class HibernateDataValueAuditStore method getDataValueAudits.

@Override
public List<DataValueAudit> getDataValueAudits(List<DataElement> dataElements, List<Period> periods, List<OrganisationUnit> organisationUnits, CategoryOptionCombo categoryOptionCombo, CategoryOptionCombo attributeOptionCombo, AuditType auditType) {
    CriteriaBuilder builder = getSession().getCriteriaBuilder();
    List<Function<Root<DataValueAudit>, Predicate>> predicates = getDataValueAuditPredicates(builder, dataElements, periods, organisationUnits, categoryOptionCombo, attributeOptionCombo, auditType);
    if (!predicates.isEmpty()) {
        return getList(builder, newJpaParameters().addPredicate(root -> builder.and(predicates.stream().map(p -> p.apply(root)).collect(Collectors.toList()).toArray(new Predicate[predicates.size()]))).addOrder(root -> builder.desc(root.get("created"))));
    } else {
        return new ArrayList<>();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) HibernateGenericStore(org.hisp.dhis.hibernate.HibernateGenericStore) DataValueAuditStore(org.hisp.dhis.datavalue.DataValueAuditStore) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) SessionFactory(org.hibernate.SessionFactory) DataValueAudit(org.hisp.dhis.datavalue.DataValueAudit) AuditType(org.hisp.dhis.common.AuditType) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataElement(org.hisp.dhis.dataelement.DataElement) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) List(java.util.List) Lists(com.google.common.collect.Lists) Predicate(javax.persistence.criteria.Predicate) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) PeriodStore(org.hisp.dhis.period.PeriodStore) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) DataValue(org.hisp.dhis.datavalue.DataValue) Repository(org.springframework.stereotype.Repository) Root(javax.persistence.criteria.Root) Period(org.hisp.dhis.period.Period) Transactional(org.springframework.transaction.annotation.Transactional) Function(java.util.function.Function) ArrayList(java.util.ArrayList) DataValueAudit(org.hisp.dhis.datavalue.DataValueAudit)

Example 4 with DataValueAudit

use of org.hisp.dhis.datavalue.DataValueAudit in project dhis2-core by dhis2.

the class FileResourceCleanUpJobTest method testRetention.

@Test
void testRetention() {
    when(fileResourceContentStore.fileResourceContentExists(any(String.class))).thenReturn(true);
    systemSettingManager.saveSystemSetting(SettingKey.FILE_RESOURCE_RETENTION_STRATEGY, FileResourceRetentionStrategy.THREE_MONTHS);
    content = "filecontentA".getBytes(StandardCharsets.UTF_8);
    dataValueA = createFileResourceDataValue('A', content);
    assertNotNull(fileResourceService.getFileResource(dataValueA.getValue()));
    content = "filecontentB".getBytes(StandardCharsets.UTF_8);
    dataValueB = createFileResourceDataValue('B', content);
    assertNotNull(fileResourceService.getFileResource(dataValueB.getValue()));
    content = "fileResourceC".getBytes(StandardCharsets.UTF_8);
    FileResource fileResource = createFileResource('C', content);
    dataValueB.setValue(fileResource.getUid());
    dataValueService.updateDataValue(dataValueB);
    fileResource.setAssigned(true);
    DataValueAudit audit = dataValueAuditService.getDataValueAudits(dataValueB).get(0);
    audit.setCreated(getDate(2000, 1, 1));
    dataValueAuditStore.updateDataValueAudit(audit);
    cleanUpJob.execute(null, NoopJobProgress.INSTANCE);
    assertNotNull(fileResourceService.getFileResource(dataValueA.getValue()));
    assertTrue(fileResourceService.getFileResource(dataValueA.getValue()).isAssigned());
    assertNull(dataValueService.getDataValue(dataValueA.getDataElement(), dataValueA.getPeriod(), dataValueA.getSource(), null));
    assertNull(fileResourceService.getFileResource(dataValueB.getValue()));
}
Also used : DataValueAudit(org.hisp.dhis.datavalue.DataValueAudit) Test(org.junit.jupiter.api.Test)

Example 5 with DataValueAudit

use of org.hisp.dhis.datavalue.DataValueAudit in project dhis2-core by dhis2.

the class DataValueSetServiceTest method testImportDataValueSetXmlPreheatCache.

@Test
void testImportDataValueSetXmlPreheatCache() throws Exception {
    in = new ClassPathResource("datavalueset/dataValueSetA.xml").getInputStream();
    ImportOptions importOptions = new ImportOptions().setPreheatCache(true);
    ImportSummary summary = dataValueSetService.importDataValueSetXml(in, importOptions);
    assertNotNull(summary);
    assertNotNull(summary.getImportCount());
    assertEquals(ImportStatus.SUCCESS, summary.getStatus());
    assertHasNoConflicts(summary);
    Collection<DataValue> dataValues = mockDataValueBatchHandler.getInserts();
    Collection<DataValueAudit> auditValues = mockDataValueAuditBatchHandler.getInserts();
    assertNotNull(dataValues);
    assertEquals(3, dataValues.size());
    assertTrue(dataValues.contains(new DataValue(deA, peA, ouA, ocDef, ocDef)));
    assertEquals("10002", ((List<DataValue>) dataValues).get(1).getValue());
    assertEquals("10003", ((List<DataValue>) dataValues).get(2).getValue());
    CompleteDataSetRegistration registration = registrationService.getCompleteDataSetRegistration(dsA, peA, ouA, ocDef);
    assertNotNull(registration);
    assertEquals(dsA, registration.getDataSet());
    assertEquals(peA, registration.getPeriod());
    assertEquals(ouA, registration.getSource());
    assertEquals(getDate(2012, 1, 9), registration.getDate());
    assertEquals(0, auditValues.size());
}
Also used : DataValue(org.hisp.dhis.datavalue.DataValue) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) List(java.util.List) CompleteDataSetRegistration(org.hisp.dhis.dataset.CompleteDataSetRegistration) ClassPathResource(org.springframework.core.io.ClassPathResource) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) DataValueAudit(org.hisp.dhis.datavalue.DataValueAudit) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

DataValueAudit (org.hisp.dhis.datavalue.DataValueAudit)18 DataValue (org.hisp.dhis.datavalue.DataValue)11 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)7 Period (org.hisp.dhis.period.Period)7 Test (org.junit.jupiter.api.Test)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)6 DataElement (org.hisp.dhis.dataelement.DataElement)6 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)6 ClassPathResource (org.springframework.core.io.ClassPathResource)6 Function (java.util.function.Function)4 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)4 AuditType (org.hisp.dhis.common.AuditType)4 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)3 Lists (com.google.common.collect.Lists)3 Collectors (java.util.stream.Collectors)3 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)3 Predicate (javax.persistence.criteria.Predicate)3 Root (javax.persistence.criteria.Root)3