Search in sources :

Example 11 with ProgramStageInstance

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

the class AbstractEnrollmentService method getEnrollment.

@Override
public Enrollment getEnrollment(ProgramInstance programInstance, TrackedEntityInstanceParams params) {
    Enrollment enrollment = new Enrollment();
    enrollment.setEnrollment(programInstance.getUid());
    if (programInstance.getEntityInstance() != null) {
        enrollment.setTrackedEntity(programInstance.getEntityInstance().getTrackedEntity().getUid());
        enrollment.setTrackedEntityInstance(programInstance.getEntityInstance().getUid());
    }
    if (programInstance.getOrganisationUnit() != null) {
        enrollment.setOrgUnit(programInstance.getOrganisationUnit().getUid());
        enrollment.setOrgUnitName(programInstance.getOrganisationUnit().getName());
    }
    if (programInstance.getProgram().getCaptureCoordinates()) {
        Coordinate coordinate = null;
        if (programInstance.getLongitude() != null && programInstance.getLatitude() != null) {
            coordinate = new Coordinate(programInstance.getLongitude(), programInstance.getLatitude());
            try {
                List<Double> list = OBJECT_MAPPER.readValue(coordinate.getCoordinateString(), new TypeReference<List<Double>>() {
                });
                coordinate.setLongitude(list.get(0));
                coordinate.setLatitude(list.get(1));
            } catch (IOException ignored) {
            }
        }
        if (coordinate != null && coordinate.isValid()) {
            enrollment.setCoordinate(coordinate);
        }
    }
    enrollment.setCreated(DateUtils.getIso8601NoTz(programInstance.getCreated()));
    enrollment.setCreatedAtClient(DateUtils.getIso8601NoTz(programInstance.getCreatedAtClient()));
    enrollment.setLastUpdated(DateUtils.getIso8601NoTz(programInstance.getLastUpdated()));
    enrollment.setLastUpdatedAtClient(DateUtils.getIso8601NoTz(programInstance.getLastUpdatedAtClient()));
    enrollment.setProgram(programInstance.getProgram().getUid());
    enrollment.setStatus(EnrollmentStatus.fromProgramStatus(programInstance.getStatus()));
    enrollment.setEnrollmentDate(programInstance.getEnrollmentDate());
    enrollment.setIncidentDate(programInstance.getIncidentDate());
    enrollment.setFollowup(programInstance.getFollowup());
    enrollment.setCompletedDate(programInstance.getEndDate());
    enrollment.setCompletedBy(programInstance.getCompletedBy());
    List<TrackedEntityComment> comments = programInstance.getComments();
    for (TrackedEntityComment comment : comments) {
        Note note = new Note();
        note.setValue(comment.getCommentText());
        note.setStoredBy(comment.getCreator());
        if (comment.getCreatedDate() != null) {
            note.setStoredDate(comment.getCreatedDate().toString());
        }
        enrollment.getNotes().add(note);
    }
    if (params.isIncludeEvents()) {
        for (ProgramStageInstance programStageInstance : programInstance.getProgramStageInstances()) {
            enrollment.getEvents().add(eventService.getEvent(programStageInstance));
        }
    }
    return enrollment;
}
Also used : TrackedEntityComment(org.hisp.dhis.trackedentitycomment.TrackedEntityComment) Coordinate(org.hisp.dhis.dxf2.events.event.Coordinate) Note(org.hisp.dhis.dxf2.events.event.Note) List(java.util.List) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 12 with ProgramStageInstance

use of org.hisp.dhis.program.ProgramStageInstance 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 13 with ProgramStageInstance

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

the class AbstractEventService method updateEvent.

private ImportSummary updateEvent(Event event, User user, boolean singleValue, ImportOptions importOptions) {
    if (importOptions == null) {
        importOptions = new ImportOptions();
    }
    ImportSummary importSummary = new ImportSummary(event.getEvent());
    ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance(event.getEvent());
    if (programStageInstance == null) {
        importSummary.getConflicts().add(new ImportConflict("Invalid Event ID.", event.getEvent()));
        return importSummary.incrementIgnored();
    }
    OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), event.getOrgUnit());
    if (organisationUnit == null) {
        organisationUnit = programStageInstance.getOrganisationUnit();
    }
    Date executionDate = new Date();
    if (event.getEventDate() != null) {
        executionDate = DateUtils.parseDate(event.getEventDate());
        programStageInstance.setExecutionDate(executionDate);
    }
    Date dueDate = new Date();
    if (event.getDueDate() != null) {
        dueDate = DateUtils.parseDate(event.getDueDate());
    }
    String storedBy = getStoredBy(event, null, user);
    programStageInstance.setStoredBy(storedBy);
    String completedBy = getCompletedBy(event, null, user);
    if (event.getStatus() == EventStatus.ACTIVE) {
        programStageInstance.setStatus(EventStatus.ACTIVE);
        programStageInstance.setCompletedBy(null);
        programStageInstance.setCompletedDate(null);
    } else if (programStageInstance.getStatus() != event.getStatus() && event.getStatus() == EventStatus.COMPLETED) {
        programStageInstance.setStatus(EventStatus.COMPLETED);
        programStageInstance.setCompletedBy(completedBy);
        programStageInstance.setCompletedDate(executionDate);
        if (programStageInstance.isCompleted()) {
            programStageInstanceService.completeProgramStageInstance(programStageInstance, importOptions.isSkipNotifications(), i18nManager.getI18nFormat());
        }
    } else if (event.getStatus() == EventStatus.SKIPPED) {
        programStageInstance.setStatus(EventStatus.SKIPPED);
    } else if (event.getStatus() == EventStatus.SCHEDULE) {
        programStageInstance.setStatus(EventStatus.SCHEDULE);
    }
    programStageInstance.setDueDate(dueDate);
    programStageInstance.setOrganisationUnit(organisationUnit);
    if (!singleValue) {
        if (programStageInstance.getProgramStage().getCaptureCoordinates()) {
            if (event.getCoordinate() != null && event.getCoordinate().isValid()) {
                programStageInstance.setLatitude(event.getCoordinate().getLatitude());
                programStageInstance.setLongitude(event.getCoordinate().getLongitude());
            } else {
                programStageInstance.setLatitude(null);
                programStageInstance.setLongitude(null);
            }
        }
    }
    Program program = getProgram(importOptions.getIdSchemes().getProgramIdScheme(), event.getProgram());
    validateExpiryDays(event, program, programStageInstance);
    if ((event.getAttributeCategoryOptions() != null && program.getCategoryCombo() != null) || event.getAttributeOptionCombo() != null) {
        IdScheme idScheme = importOptions.getIdSchemes().getCategoryOptionIdScheme();
        DataElementCategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo(program.getCategoryCombo(), event.getAttributeCategoryOptions(), event.getAttributeOptionCombo(), idScheme);
        if (attributeOptionCombo == null) {
            importSummary.getConflicts().add(new ImportConflict("Invalid attribute option combo identifier:", event.getAttributeCategoryOptions()));
            return importSummary.incrementIgnored();
        }
        programStageInstance.setAttributeOptionCombo(attributeOptionCombo);
    }
    programStageInstance.setDeleted(event.isDeleted());
    programStageInstanceService.updateProgramStageInstance(programStageInstance);
    updateTrackedEntityInstance(programStageInstance);
    saveTrackedEntityComment(programStageInstance, event, storedBy);
    Set<TrackedEntityDataValue> dataValues = new HashSet<>(dataValueService.getTrackedEntityDataValues(programStageInstance));
    Map<String, TrackedEntityDataValue> existingDataValues = getDataElementDataValueMap(dataValues);
    for (DataValue value : event.getDataValues()) {
        DataElement dataElement = getDataElement(importOptions.getIdSchemes().getDataElementIdScheme(), value.getDataElement());
        TrackedEntityDataValue dataValue = dataValueService.getTrackedEntityDataValue(programStageInstance, dataElement);
        if (!validateDataValue(dataElement, value.getValue(), importSummary)) {
            continue;
        }
        if (dataValue != null) {
            if (StringUtils.isEmpty(value.getValue()) && dataElement.isFileType() && !StringUtils.isEmpty(dataValue.getValue())) {
                fileResourceService.deleteFileResource(dataValue.getValue());
            }
            dataValue.setValue(value.getValue());
            dataValue.setProvidedElsewhere(value.getProvidedElsewhere());
            dataValueService.updateTrackedEntityDataValue(dataValue);
            dataValues.remove(dataValue);
        } else {
            TrackedEntityDataValue existingDataValue = existingDataValues.get(value.getDataElement());
            saveDataValue(programStageInstance, event.getStoredBy(), dataElement, value.getValue(), value.getProvidedElsewhere(), existingDataValue, null);
        }
    }
    if (!singleValue) {
        dataValues.forEach(dataValueService::deleteTrackedEntityDataValue);
    }
    importSummary.setStatus(importSummary.getConflicts().isEmpty() ? ImportStatus.SUCCESS : ImportStatus.WARNING);
    return importSummary;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) IdScheme(org.hisp.dhis.common.IdScheme) Date(java.util.Date) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict) HashSet(java.util.HashSet)

Example 14 with ProgramStageInstance

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

the class JacksonEventService method addEvents.

private ImportSummaries addEvents(List<Event> events, TaskId taskId, ImportOptions importOptions) {
    ImportSummaries importSummaries = new ImportSummaries();
    notifier.clear(taskId).notify(taskId, "Importing events");
    Clock clock = new Clock(log).startClock();
    List<Event> create = new ArrayList<>();
    List<Event> update = new ArrayList<>();
    List<String> delete = new ArrayList<>();
    if (importOptions.getImportStrategy().isCreate()) {
        create.addAll(events);
    } else if (importOptions.getImportStrategy().isCreateAndUpdate()) {
        for (Event event : events) {
            if (StringUtils.isEmpty(event.getEvent())) {
                create.add(event);
            } else {
                ProgramStageInstance programStageInstance = manager.getObject(ProgramStageInstance.class, importOptions.getIdSchemes().getProgramStageInstanceIdScheme(), event.getEvent());
                if (programStageInstance == null) {
                    create.add(event);
                } else {
                    update.add(event);
                }
            }
        }
    } else if (importOptions.getImportStrategy().isUpdate()) {
        update.addAll(events);
    } else if (importOptions.getImportStrategy().isDelete()) {
        delete.addAll(events.stream().map(Event::getEvent).collect(Collectors.toList()));
    }
    importSummaries.addImportSummaries(addEvents(create, importOptions));
    importSummaries.addImportSummaries(updateEvents(update, false));
    importSummaries.addImportSummaries(deleteEvents(delete));
    if (taskId != null) {
        notifier.notify(taskId, NotificationLevel.INFO, "Import done. Completed in " + clock.time() + ".", true).addTaskSummary(taskId, importSummaries);
    } else {
        clock.logTime("Import done");
    }
    return importSummaries;
}
Also used : ArrayList(java.util.ArrayList) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) Clock(org.hisp.dhis.system.util.Clock) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 15 with ProgramStageInstance

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

the class AbstractEventService method createProgramStageInstance.

private ProgramStageInstance createProgramStageInstance(Event event, ProgramStage programStage, ProgramInstance programInstance, OrganisationUnit organisationUnit, Date dueDate, Date executionDate, int status, Coordinate coordinate, String completedBy, String programStageInstanceIdentifier, DataElementCategoryOptionCombo aoc, ImportOptions importOptions) {
    ProgramStageInstance programStageInstance = new ProgramStageInstance();
    if (importOptions.getIdSchemes().getProgramStageInstanceIdScheme().equals(IdScheme.UID)) {
        programStageInstance.setUid(CodeGenerator.isValidUid(programStageInstanceIdentifier) ? programStageInstanceIdentifier : CodeGenerator.generateUid());
    } else if (importOptions.getIdSchemes().getProgramStageInstanceIdScheme().equals(IdScheme.CODE)) {
        programStageInstance.setUid(CodeGenerator.generateUid());
        programStageInstance.setCode(programStageInstanceIdentifier);
    }
    updateProgramStageInstance(event, programStage, programInstance, organisationUnit, dueDate, executionDate, status, coordinate, completedBy, programStageInstance, aoc, importOptions);
    return programStageInstance;
}
Also used : ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Aggregations

ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)23 ArrayList (java.util.ArrayList)10 ProgramInstance (org.hisp.dhis.program.ProgramInstance)9 Date (java.util.Date)8 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)7 ProgramStage (org.hisp.dhis.program.ProgramStage)7 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)7 Program (org.hisp.dhis.program.Program)6 DataElement (org.hisp.dhis.dataelement.DataElement)5 TrackedEntityDataValue (org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue)5 ProgramStageDataElement (org.hisp.dhis.program.ProgramStageDataElement)4 HashSet (java.util.HashSet)3 List (java.util.List)3 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)3 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)3 Calendar (java.util.Calendar)2 HashMap (java.util.HashMap)2 NotAllowedException (org.hisp.dhis.api.mobile.NotAllowedException)2 Activity (org.hisp.dhis.api.mobile.model.Activity)2 ActivityPlan (org.hisp.dhis.api.mobile.model.ActivityPlan)2