use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.
the class TrackedEntityDataValueStoreTest method setUpTest.
@Override
public void setUpTest() {
OrganisationUnit organisationUnit = createOrganisationUnit('A');
organisationUnitService.addOrganisationUnit(organisationUnit);
dataElementA = createDataElement('A');
dataElementB = createDataElement('B');
dataElementService.addDataElement(dataElementA);
dataElementService.addDataElement(dataElementB);
entityInstance = createTrackedEntityInstance('A', organisationUnit);
entityInstanceService.addTrackedEntityInstance(entityInstance);
Program program = createProgram('A', new HashSet<>(), organisationUnit);
programService.addProgram(program);
ProgramStage stageA = createProgramStage('A', 0);
stageA.setProgram(program);
programStageService.saveProgramStage(stageA);
ProgramStage stageB = createProgramStage('B', 0);
stageB.setProgram(program);
programStageService.saveProgramStage(stageB);
Set<ProgramStage> programStages = new HashSet<>();
programStages.add(stageA);
programStages.add(stageB);
program.setProgramStages(programStages);
programService.updateProgram(program);
DateTime yesterDate = DateTime.now();
yesterDate.withTimeAtStartOfDay();
yesterDate.minusDays(1);
yesterday = yesterDate.toDate();
DateTime tomorrowDate = DateTime.now();
tomorrowDate.withTimeAtStartOfDay();
tomorrowDate.plusDays(1);
tomorrow = tomorrowDate.toDate();
programInstance = programInstanceService.enrollTrackedEntityInstance(entityInstance, program, yesterday, yesterday, organisationUnit);
stageInstanceA = programStageInstanceService.createProgramStageInstance(programInstance, stageA, yesterday, yesterday, organisationUnit);
stageInstanceB = programStageInstanceService.createProgramStageInstance(programInstance, stageB, yesterday, yesterday, organisationUnit);
dataValueA = new TrackedEntityDataValue(stageInstanceA, dataElementA, "A");
dataValueB = new TrackedEntityDataValue(stageInstanceA, dataElementB, "B");
dataValueC = new TrackedEntityDataValue(stageInstanceB, dataElementA, "C");
dataValueD = new TrackedEntityDataValue(stageInstanceB, dataElementB, "D");
}
use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.
the class ActivityReportingServiceImpl method saveSingleEventWithoutRegistration.
@Override
public String saveSingleEventWithoutRegistration(org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStage mobileProgramStage, int orgUnitId) throws NotAllowedException {
ProgramStage programStage = programStageService.getProgramStage(mobileProgramStage.getId());
Program program = programStage.getProgram();
ProgramInstance programInstance = new ProgramInstance();
programInstance.setEnrollmentDate(new Date());
programInstance.setIncidentDate(new Date());
programInstance.setProgram(program);
programInstance.setStatus(ProgramStatus.COMPLETED);
programInstanceService.addProgramInstance(programInstance);
ProgramStageInstance programStageInstance = new ProgramStageInstance();
programStageInstance.setProgramInstance(programInstance);
programStageInstance.setProgramStage(programStage);
programStageInstance.setDueDate(new Date());
programStageInstance.setExecutionDate(new Date());
programStageInstance.setStatus(EventStatus.COMPLETED);
programStageInstance.setOrganisationUnit(organisationUnitService.getOrganisationUnit(orgUnitId));
programStageInstanceService.addProgramStageInstance(programStageInstance);
for (org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStageDataElement mobileDataElement : mobileProgramStage.getDataElements()) {
TrackedEntityDataValue trackedEntityDataValue = new TrackedEntityDataValue();
trackedEntityDataValue.setDataElement(dataElementService.getDataElement(mobileDataElement.getId()));
String value = mobileDataElement.getValue();
if (value != null && !value.trim().equals("")) {
trackedEntityDataValue.setValue(value);
trackedEntityDataValue.setProgramStageInstance(programStageInstance);
trackedEntityDataValue.setProvidedElsewhere(false);
trackedEntityDataValue.setLastUpdated(new Date());
dataValueService.saveTrackedEntityDataValue(trackedEntityDataValue);
}
}
return SINGLE_EVENT_WITHOUT_REGISTRATION_UPLOADED;
}
use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.
the class ActivityReportingServiceImpl method registerRelative.
@Override
public Patient registerRelative(Patient patient, int orgUnitId, String programId) throws NotAllowedException {
TrackedEntityInstance patientWeb = new TrackedEntityInstance();
patientWeb.setOrganisationUnit(organisationUnitService.getOrganisationUnit(orgUnitId));
Set<TrackedEntityAttribute> patientAttributeSet = new HashSet<>();
Set<TrackedEntityAttributeValue> patientAttributeValues = new HashSet<>();
Collection<org.hisp.dhis.api.mobile.model.PatientAttribute> attributesMobile = patient.getAttributes();
if (attributesMobile != null) {
for (org.hisp.dhis.api.mobile.model.PatientAttribute paAtt : attributesMobile) {
TrackedEntityAttribute patientAttribute = attributeService.getTrackedEntityAttributeByName(paAtt.getName());
patientAttributeSet.add(patientAttribute);
TrackedEntityAttributeValue patientAttributeValue = new TrackedEntityAttributeValue();
patientAttributeValue.setEntityInstance(patientWeb);
patientAttributeValue.setAttribute(patientAttribute);
patientAttributeValue.setValue(paAtt.getValue());
patientAttributeValues.add(patientAttributeValue);
}
}
patientWeb.setTrackedEntity(trackedEntityService.getTrackedEntityByName(patient.getTrackedEntityName()));
if (patient.getIdToAddRelative() != 0) {
TrackedEntityInstance relative = entityInstanceService.getTrackedEntityInstance(patient.getIdToAddRelative());
if (relative == null) {
throw new NotAllowedException("relative does not exist");
}
patientId = entityInstanceService.createTrackedEntityInstance(patientWeb, relative.getUid(), patient.getRelTypeIdToAdd(), patientAttributeValues);
} else {
patientId = entityInstanceService.createTrackedEntityInstance(patientWeb, null, null, patientAttributeValues);
}
TrackedEntityInstance newTrackedEntityInstance = entityInstanceService.getTrackedEntityInstance(this.patientId);
String errorMsg = null;
try {
for (org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramInstance mobileProgramInstance : patient.getEnrollmentPrograms()) {
Date incidentDate = DateUtils.getMediumDate(mobileProgramInstance.getDateOfIncident());
enrollProgram(patientId + "-" + mobileProgramInstance.getProgramId(), mobileProgramInstance.getProgramStageInstances(), incidentDate);
}
Program program = programService.getProgram(Integer.parseInt(programId));
String[] errorCode = entityInstanceService.validateTrackedEntityInstance(newTrackedEntityInstance, program).split("_");
int code = Integer.parseInt(errorCode[0]);
if (code >= 1) {
entityInstanceService.deleteTrackedEntityInstance(newTrackedEntityInstance);
if (code == TrackedEntityInstanceService.ERROR_DUPLICATE_IDENTIFIER) {
errorMsg = "Duplicate value of " + attributeService.getTrackedEntityAttribute(Integer.parseInt(errorCode[1])).getDisplayName();
} else {
errorMsg = "Validation error";
}
}
} catch (Exception e) {
e.printStackTrace();
}
if (errorMsg != null) {
throw new NotAllowedException(errorMsg);
}
if (patient.getEnrollmentRelationship() != null) {
org.hisp.dhis.api.mobile.model.LWUITmodel.Relationship enrollmentRelationship = patient.getEnrollmentRelationship();
enrollmentRelationship.setPersonBId(newTrackedEntityInstance.getId());
addRelationship(enrollmentRelationship, orgUnitId);
}
return getPatientModel(newTrackedEntityInstance);
}
use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.
the class ActivityReportingServiceImpl method getAllProgramByOrgUnit.
@Override
public org.hisp.dhis.api.mobile.model.LWUITmodel.Program getAllProgramByOrgUnit(int orgUnitId, String type) throws NotAllowedException {
String programsInfo = "";
OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(orgUnitId);
Set<Program> tempPrograms = null;
ProgramType programType = ProgramType.fromValue(type);
if (programType == ProgramType.WITHOUT_REGISTRATION) {
tempPrograms = programService.getUserPrograms(ProgramType.WITHOUT_REGISTRATION);
} else // ProgramType.WITH_REGISTRATION
{
tempPrograms = programService.getUserPrograms(ProgramType.WITH_REGISTRATION);
}
List<Program> programs = new ArrayList<>();
for (Program program : tempPrograms) {
if (program.getOrganisationUnits().contains(organisationUnit)) {
programs.add(program);
}
}
if (programs.size() != 0) {
if (programs.size() == 1) {
Program program = programs.get(0);
return getMobileProgramWithoutData(program);
} else {
for (Program program : programs) {
if (program.getOrganisationUnits().contains(organisationUnit)) {
programsInfo += program.getId() + "/" + program.getName() + "$";
}
}
throw new NotAllowedException(programsInfo);
}
} else {
throw NotAllowedException.NO_PROGRAM_FOUND;
}
}
use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.
the class AnalyticsServiceProgramDataElementTest method verifyProgramDataElementInQueryCallsEventsAnalytics.
/**
* This test verifies that a call to the Analytics Service with a Data
* Element of type Program Data Element, triggers a call to the Event
* Analytics Service
*/
@Test
void verifyProgramDataElementInQueryCallsEventsAnalytics() {
ArgumentCaptor<EventQueryParams> capturedParams = ArgumentCaptor.forClass(EventQueryParams.class);
DataElement de1 = createDataElement('A');
Program pr1 = createProgram('P');
ProgramDataElementDimensionItem pded1 = new ProgramDataElementDimensionItem(pr1, de1);
DataQueryParams params = DataQueryParams.newBuilder().withAggregationType(AnalyticsAggregationType.AVERAGE).withPeriod(new Period(YearlyPeriodType.getPeriodFromIsoString("2017W10"))).withDataElements(newArrayList(pded1)).withIgnoreLimit(true).withFilters(Collections.singletonList(new BaseDimensionalObject("ou", DimensionType.ORGANISATION_UNIT, null, DISPLAY_NAME_ORGUNIT, ImmutableList.of(new OrganisationUnit("bbb", "bbb", "OU_2", null, null, "c2"))))).build();
initMock(params);
Map<String, Object> emptyData = new HashMap<>();
when(analyticsManager.getAggregatedDataValues(any(DataQueryParams.class), eq(AnalyticsTableType.DATA_VALUE), eq(0))).thenReturn(CompletableFuture.completedFuture(emptyData));
when(eventAnalyticsService.getAggregatedEventData(any(EventQueryParams.class))).thenReturn(new ListGrid());
target.getAggregatedDataValueGrid(params);
verify(eventAnalyticsService).getAggregatedEventData(capturedParams.capture());
EventQueryParams data = capturedParams.getValue();
assertThat(data.hasValueDimension(), is(false));
assertThat(data.getItems(), hasSize(1));
assertThat(data.getItems().get(0).getItemId(), is(de1.getUid()));
assertThat(data.getDimensions(), hasSize(1));
assertThat(data.getDimensions().get(0).getDimensionType(), is(DimensionType.PERIOD));
assertThat(data.getFilters(), hasSize(1));
assertThat(data.getFilters().get(0).getDimensionType(), is(DimensionType.ORGANISATION_UNIT));
assertThat(data.getAggregationType(), is(AnalyticsAggregationType.AVERAGE));
}
Aggregations