Search in sources :

Example 26 with Program

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");
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) ProgramStage(org.hisp.dhis.program.ProgramStage) DateTime(org.joda.time.DateTime) HashSet(java.util.HashSet)

Example 27 with Program

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;
}
Also used : Program(org.hisp.dhis.program.Program) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) ProgramStage(org.hisp.dhis.program.ProgramStage) Date(java.util.Date) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 28 with Program

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);
}
Also used : NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) PatientAttribute(org.hisp.dhis.api.mobile.model.PatientAttribute) HashSet(java.util.HashSet) Program(org.hisp.dhis.program.Program) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) PatientAttribute(org.hisp.dhis.api.mobile.model.PatientAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException)

Example 29 with Program

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;
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) ArrayList(java.util.ArrayList) ProgramType(org.hisp.dhis.program.ProgramType)

Example 30 with Program

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));
}
Also used : EventQueryParams(org.hisp.dhis.analytics.event.EventQueryParams) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) DhisConvenienceTest.createProgram(org.hisp.dhis.DhisConvenienceTest.createProgram) HashMap(java.util.HashMap) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) Period(org.hisp.dhis.period.Period) ListGrid(org.hisp.dhis.system.grid.ListGrid) DataElement(org.hisp.dhis.dataelement.DataElement) DhisConvenienceTest.createDataElement(org.hisp.dhis.DhisConvenienceTest.createDataElement) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) ProgramDataElementDimensionItem(org.hisp.dhis.program.ProgramDataElementDimensionItem) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) Test(org.junit.jupiter.api.Test)

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