Search in sources :

Example 31 with Program

use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.

the class JdbcEnrollmentAnalyticsTableManager method populateTable.

@Override
protected void populateTable(AnalyticsTableUpdateParams params, AnalyticsTablePartition partition) {
    final Program program = partition.getMasterTable().getProgram();
    String fromClause = "from programinstance pi " + "inner join program pr on pi.programid=pr.programid " + "left join trackedentityinstance tei on pi.trackedentityinstanceid=tei.trackedentityinstanceid " + "and tei.deleted is false " + "inner join organisationunit ou on pi.organisationunitid=ou.organisationunitid " + "left join _orgunitstructure ous on pi.organisationunitid=ous.organisationunitid " + "left join _organisationunitgroupsetstructure ougs on pi.organisationunitid=ougs.organisationunitid " + "and (cast(date_trunc('month', pi.enrollmentdate) as date)=ougs.startdate or ougs.startdate is null) " + "left join _dateperiodstructure dps on cast(pi.enrollmentdate as date)=dps.dateperiod " + "where pr.programid=" + program.getId() + " " + "and pi.organisationunitid is not null " + "and pi.lastupdated <= '" + getLongDateString(params.getStartTime()) + "' " + "and pi.incidentdate is not null " + "and pi.deleted is false ";
    populateTableInternal(partition, getDimensionColumns(program), fromClause);
}
Also used : Program(org.hisp.dhis.program.Program) DateUtils.getLongDateString(org.hisp.dhis.util.DateUtils.getLongDateString)

Example 32 with Program

use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.

the class JdbcEnrollmentAnalyticsTableManager method getRegularAnalyticsTables.

/**
 * Creates a list of {@link AnalyticsTable} for each program.
 *
 * @param params the {@link AnalyticsTableUpdateParams}.
 * @return a list of {@link AnalyticsTableUpdateParams}.
 */
private List<AnalyticsTable> getRegularAnalyticsTables(AnalyticsTableUpdateParams params) {
    List<AnalyticsTable> tables = new UniqueArrayList<>();
    List<Program> programs = idObjectManager.getAllNoAcl(Program.class);
    for (Program program : programs) {
        AnalyticsTable table = new AnalyticsTable(getAnalyticsTableType(), getDimensionColumns(program), Lists.newArrayList(), program);
        tables.add(table);
    }
    return tables;
}
Also used : Program(org.hisp.dhis.program.Program) AnalyticsTable(org.hisp.dhis.analytics.AnalyticsTable) UniqueArrayList(org.hisp.dhis.commons.collection.UniqueArrayList)

Example 33 with Program

use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.

the class JdbcEventAnalyticsTableManager method getRegularAnalyticsTables.

/**
 * Creates a list of {@link AnalyticsTable} for each program. The tables
 * contain a partition for each year for which events exist.
 *
 * @param params the {@link AnalyticsTableUpdateParams}.
 * @return a list of {@link AnalyticsTableUpdateParams}.
 */
private List<AnalyticsTable> getRegularAnalyticsTables(AnalyticsTableUpdateParams params) {
    List<AnalyticsTable> tables = new ArrayList<>();
    Calendar calendar = PeriodType.getCalendar();
    List<Program> programs = params.isSkipPrograms() ? idObjectManager.getAllNoAcl(Program.class) : idObjectManager.getAllNoAcl(Program.class).stream().filter(p -> !params.getSkipPrograms().contains(p.getUid())).collect(Collectors.toList());
    for (Program program : programs) {
        List<Integer> dataYears = getDataYears(params, program);
        Collections.sort(dataYears);
        AnalyticsTable table = new AnalyticsTable(getAnalyticsTableType(), getDimensionColumns(program), Lists.newArrayList(), program);
        for (Integer year : dataYears) {
            table.addPartitionTable(year, PartitionUtils.getStartDate(calendar, year), PartitionUtils.getEndDate(calendar, year));
        }
        if (table.hasPartitionTables()) {
            tables.add(table);
        }
    }
    return tables;
}
Also used : Program(org.hisp.dhis.program.Program) AnalyticsTable(org.hisp.dhis.analytics.AnalyticsTable) Calendar(org.hisp.dhis.calendar.Calendar) ArrayList(java.util.ArrayList)

Example 34 with Program

use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.

the class EnrollmentSMSListener method postProcess.

@Override
protected SmsResponse postProcess(IncomingSms sms, SmsSubmission submission) throws SMSProcessingException {
    EnrollmentSmsSubmission subm = (EnrollmentSmsSubmission) submission;
    Date enrollmentDate = subm.getEnrollmentDate();
    Date incidentDate = subm.getIncidentDate();
    Uid teiUid = subm.getTrackedEntityInstance();
    Uid progid = subm.getTrackerProgram();
    Uid tetid = subm.getTrackedEntityType();
    Uid ouid = subm.getOrgUnit();
    Uid enrollmentid = subm.getEnrollment();
    OrganisationUnit orgUnit = organisationUnitService.getOrganisationUnit(ouid.getUid());
    Program program = programService.getProgram(progid.getUid());
    if (program == null) {
        throw new SMSProcessingException(SmsResponse.INVALID_PROGRAM.set(progid));
    }
    TrackedEntityType entityType = trackedEntityTypeService.getTrackedEntityType(tetid.getUid());
    if (entityType == null) {
        throw new SMSProcessingException(SmsResponse.INVALID_TETYPE.set(tetid));
    }
    if (!programService.hasOrgUnit(program, orgUnit)) {
        throw new SMSProcessingException(SmsResponse.OU_NOTIN_PROGRAM.set(ouid, progid));
    }
    TrackedEntityInstance entityInstance;
    boolean teiExists = teiService.trackedEntityInstanceExists(teiUid.getUid());
    if (teiExists) {
        log.info(String.format("Given TEI [%s] exists. Updating...", teiUid));
        entityInstance = teiService.getTrackedEntityInstance(teiUid.getUid());
    } else {
        log.info(String.format("Given TEI [%s] does not exist. Creating...", teiUid));
        entityInstance = new TrackedEntityInstance();
        entityInstance.setUid(teiUid.getUid());
        entityInstance.setOrganisationUnit(orgUnit);
        entityInstance.setTrackedEntityType(entityType);
    }
    Set<TrackedEntityAttributeValue> attributeValues = getSMSAttributeValues(subm, entityInstance);
    if (teiExists) {
        updateAttributeValues(attributeValues, entityInstance.getTrackedEntityAttributeValues());
        entityInstance.setTrackedEntityAttributeValues(attributeValues);
        teiService.updateTrackedEntityInstance(entityInstance);
    } else {
        teiService.createTrackedEntityInstance(entityInstance, attributeValues);
    }
    TrackedEntityInstance tei = teiService.getTrackedEntityInstance(teiUid.getUid());
    // TODO: Unsure about this handling for enrollments, this needs to be
    // checked closely
    ProgramInstance enrollment;
    boolean enrollmentExists = programInstanceService.programInstanceExists(enrollmentid.getUid());
    if (enrollmentExists) {
        enrollment = programInstanceService.getProgramInstance(enrollmentid.getUid());
        // Update these dates in case they've changed
        enrollment.setEnrollmentDate(enrollmentDate);
        enrollment.setIncidentDate(incidentDate);
    } else {
        enrollment = programInstanceService.enrollTrackedEntityInstance(tei, program, enrollmentDate, incidentDate, orgUnit, enrollmentid.getUid());
    }
    if (enrollment == null) {
        throw new SMSProcessingException(SmsResponse.ENROLL_FAILED.set(teiUid, progid));
    }
    enrollment.setStatus(getCoreProgramStatus(subm.getEnrollmentStatus()));
    enrollment.setGeometry(convertGeoPointToGeometry(subm.getCoordinates()));
    programInstanceService.updateProgramInstance(enrollment);
    // We now check if the enrollment has events to process
    User user = userService.getUser(subm.getUserId().getUid());
    List<Object> errorUIDs = new ArrayList<>();
    if (subm.getEvents() != null) {
        for (SmsEvent event : subm.getEvents()) {
            errorUIDs.addAll(processEvent(event, user, enrollment, sms));
        }
    }
    enrollment.setStatus(getCoreProgramStatus(subm.getEnrollmentStatus()));
    enrollment.setGeometry(convertGeoPointToGeometry(subm.getCoordinates()));
    programInstanceService.updateProgramInstance(enrollment);
    if (!errorUIDs.isEmpty()) {
        return SmsResponse.WARN_DVERR.setList(errorUIDs);
    }
    if (attributeValues == null || attributeValues.isEmpty()) {
        // TODO: Is this correct handling?
        return SmsResponse.WARN_AVEMPTY;
    }
    return SmsResponse.SUCCESS;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) User(org.hisp.dhis.user.User) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ArrayList(java.util.ArrayList) SmsEvent(org.hisp.dhis.smscompression.models.SmsEvent) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) Uid(org.hisp.dhis.smscompression.models.Uid) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) EnrollmentSmsSubmission(org.hisp.dhis.smscompression.models.EnrollmentSmsSubmission)

Example 35 with Program

use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.

the class TrackedEntityAttributeStoreTest method setUpTest.

@Override
public void setUpTest() {
    attributeW = createTrackedEntityAttribute('W');
    attributeY = createTrackedEntityAttribute('Y');
    attributeZ = createTrackedEntityAttribute('Z', ValueType.NUMBER);
    List<TrackedEntityAttribute> attributesA = new ArrayList<>();
    attributesA.add(attributeW);
    attributesA.add(attributeY);
    Program program = createProgram('A');
    programService.addProgram(program);
    TrackedEntityType trackedEntityTypeA = createTrackedEntityType('A');
    trackedEntityTypeA.setPublicAccess(AccessStringHelper.FULL);
    trackedEntityTypeService.addTrackedEntityType(trackedEntityTypeA);
    TrackedEntityType trackedEntityTypeB = createTrackedEntityType('B');
    trackedEntityTypeB.setPublicAccess(AccessStringHelper.FULL);
    trackedEntityTypeService.addTrackedEntityType(trackedEntityTypeB);
    // Create 20 Tracked Entity Attributes (named A .. O)
    IntStream.range(A, T).mapToObj(i -> Character.toString((char) i)).forEach(c -> attributeService.addTrackedEntityAttribute(createTrackedEntityAttribute(c.charAt(0), ValueType.TEXT)));
    // Transform the Tracked Entity Attributes into a List of
    // TrackedEntityTypeAttribute
    List<TrackedEntityTypeAttribute> teatList = IntStream.range(A, T).mapToObj(i -> Character.toString((char) i)).map(s -> new TrackedEntityTypeAttribute(trackedEntityTypeA, attributeService.getTrackedEntityAttributeByName("Attribute" + s))).collect(Collectors.toList());
    // Assign 10 TrackedEntityTypeAttribute to Tracked Entity Type A
    trackedEntityTypeA.setTrackedEntityTypeAttributes(teatList.subList(0, 10));
    trackedEntityTypeService.updateTrackedEntityType(trackedEntityTypeA);
    // Assign 10 TrackedEntityTypeAttribute to Tracked Entity Type B
    trackedEntityTypeB.setTrackedEntityTypeAttributes(teatList.subList(10, 20));
    trackedEntityTypeService.updateTrackedEntityType(trackedEntityTypeB);
    programB = createProgram('B');
    programService.addProgram(programB);
    List<ProgramTrackedEntityAttribute> pteaList = IntStream.range(A, T).mapToObj(i -> Character.toString((char) i)).map(s -> new ProgramTrackedEntityAttribute(programB, attributeService.getTrackedEntityAttributeByName("Attribute" + s))).collect(Collectors.toList());
    programB.setProgramAttributes(pteaList);
    programService.updateProgram(programB);
}
Also used : IntStream(java.util.stream.IntStream) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) AccessStringHelper(org.hisp.dhis.security.acl.AccessStringHelper) ValueType(org.hisp.dhis.common.ValueType) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Set(java.util.Set) Autowired(org.springframework.beans.factory.annotation.Autowired) Collectors(java.util.stream.Collectors) Program(org.hisp.dhis.program.Program) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) DhisSpringTest(org.hisp.dhis.DhisSpringTest) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Map(java.util.Map) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ProgramService(org.hisp.dhis.program.ProgramService) Program(org.hisp.dhis.program.Program) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) ArrayList(java.util.ArrayList)

Aggregations

Program (org.hisp.dhis.program.Program)344 Test (org.junit.jupiter.api.Test)160 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)123 ProgramStage (org.hisp.dhis.program.ProgramStage)109 ProgramInstance (org.hisp.dhis.program.ProgramInstance)79 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)68 Date (java.util.Date)47 Collectors (java.util.stream.Collectors)44 User (org.hisp.dhis.user.User)44 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)42 TrackedEntityType (org.hisp.dhis.trackedentity.TrackedEntityType)42 BeforeEach (org.junit.jupiter.api.BeforeEach)42 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)41 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)41 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)40 DhisConvenienceTest.createProgram (org.hisp.dhis.DhisConvenienceTest.createProgram)39 CategoryCombo (org.hisp.dhis.category.CategoryCombo)39 DataElement (org.hisp.dhis.dataelement.DataElement)39 Event (org.hisp.dhis.tracker.domain.Event)38 Enrollment (org.hisp.dhis.tracker.domain.Enrollment)37