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);
}
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;
}
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;
}
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;
}
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);
}
Aggregations