Search in sources :

Example 1 with COMPLETEDBY

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.COMPLETEDBY 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 2 with COMPLETEDBY

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.COMPLETEDBY in project dhis2-core by dhis2.

the class AbstractEventService method saveEvent.

private ImportSummary saveEvent(Program program, ProgramInstance programInstance, ProgramStage programStage, ProgramStageInstance programStageInstance, OrganisationUnit organisationUnit, Event event, User user, ImportOptions importOptions) {
    Assert.notNull(program, "Program cannot be null");
    Assert.notNull(programInstance, "Program instance cannot be null");
    Assert.notNull(programStage, "Program stage cannot be null");
    ImportSummary importSummary = new ImportSummary(event.getEvent());
    if (importOptions == null) {
        importOptions = new ImportOptions();
    }
    boolean existingEvent = programStageInstance != null;
    boolean dryRun = importOptions.isDryRun();
    // = new Date();
    Date executionDate = null;
    if (event.getEventDate() != null) {
        executionDate = DateUtils.parseDate(event.getEventDate());
    }
    Date dueDate = new Date();
    if (event.getDueDate() != null) {
        dueDate = DateUtils.parseDate(event.getDueDate());
    }
    String storedBy = getStoredBy(event, importSummary, user);
    String completedBy = getCompletedBy(event, importSummary, user);
    DataElementCategoryOptionCombo aoc = null;
    if ((event.getAttributeCategoryOptions() != null && program.getCategoryCombo() != null) || event.getAttributeOptionCombo() != null) {
        IdScheme idScheme = importOptions.getIdSchemes().getCategoryOptionIdScheme();
        try {
            aoc = inputUtils.getAttributeOptionCombo(program.getCategoryCombo(), event.getAttributeCategoryOptions(), event.getAttributeOptionCombo(), idScheme);
        } catch (IllegalQueryException ex) {
            importSummary.getConflicts().add(new ImportConflict(ex.getMessage(), event.getAttributeCategoryOptions()));
        }
    } else {
        aoc = categoryService.getDefaultDataElementCategoryOptionCombo();
    }
    if (!dryRun) {
        if (programStageInstance == null) {
            programStageInstance = createProgramStageInstance(event, programStage, programInstance, organisationUnit, dueDate, executionDate, event.getStatus().getValue(), event.getCoordinate(), completedBy, event.getEvent(), aoc, importOptions);
        } else {
            updateProgramStageInstance(event, programStage, programInstance, organisationUnit, dueDate, executionDate, event.getStatus().getValue(), event.getCoordinate(), completedBy, programStageInstance, aoc, importOptions);
        }
        updateTrackedEntityInstance(programStageInstance);
        saveTrackedEntityComment(programStageInstance, event, storedBy);
        importSummary.setReference(programStageInstance.getUid());
    }
    Map<String, TrackedEntityDataValue> dataElementValueMap = Maps.newHashMap();
    if (existingEvent) {
        dataElementValueMap = getDataElementDataValueMap(dataValueService.getTrackedEntityDataValues(programStageInstance));
    }
    for (DataValue dataValue : event.getDataValues()) {
        DataElement dataElement;
        if (dataElementValueMap.containsKey(dataValue.getDataElement())) {
            dataElement = dataElementValueMap.get(dataValue.getDataElement()).getDataElement();
        } else {
            dataElement = getDataElement(importOptions.getIdSchemes().getDataElementIdScheme(), dataValue.getDataElement());
        }
        if (dataElement != null) {
            if (validateDataValue(dataElement, dataValue.getValue(), importSummary)) {
                String dataValueStoredBy = dataValue.getStoredBy() != null ? dataValue.getStoredBy() : storedBy;
                if (!dryRun) {
                    TrackedEntityDataValue existingDataValue = dataElementValueMap.get(dataValue.getDataElement());
                    saveDataValue(programStageInstance, dataValueStoredBy, dataElement, dataValue.getValue(), dataValue.getProvidedElsewhere(), existingDataValue, importSummary);
                }
            }
        } else {
            importSummary.getConflicts().add(new ImportConflict("dataElement", dataValue.getDataElement() + " is not a valid data element"));
            importSummary.getImportCount().incrementIgnored();
        }
    }
    importSummary.setStatus(importSummary.getConflicts().isEmpty() ? ImportStatus.SUCCESS : ImportStatus.WARNING);
    return importSummary;
}
Also used : TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) 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) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 3 with COMPLETEDBY

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.COMPLETEDBY in project dhis2-core by dhis2.

the class EnrollmentRowCallbackHandler method getEnrollment.

private Enrollment getEnrollment(ResultSet rs) throws SQLException {
    Enrollment enrollment = new Enrollment();
    enrollment.setEnrollment(rs.getString(getColumnName(UID)));
    MapperGeoUtils.resolveGeometry(rs.getBytes(getColumnName(GEOMETRY))).ifPresent(enrollment::setGeometry);
    enrollment.setTrackedEntityType(rs.getString(getColumnName(TEI_TYPE_UID)));
    enrollment.setTrackedEntityInstance(rs.getString(getColumnName(TEI_UID)));
    enrollment.setOrgUnit(rs.getString(getColumnName(ORGUNIT_UID)));
    enrollment.setOrgUnitName(rs.getString(getColumnName(ORGUNIT_NAME)));
    enrollment.setCreated(DateUtils.getIso8601NoTz(rs.getTimestamp(getColumnName(CREATED))));
    enrollment.setCreatedAtClient(DateUtils.getIso8601NoTz(rs.getTimestamp(getColumnName(CREATEDCLIENT))));
    setUserInfoSnapshot(rs, getColumnName(CREATED_BY), enrollment::setCreatedByUserInfo);
    enrollment.setLastUpdated(DateUtils.getIso8601NoTz(rs.getTimestamp(getColumnName(UPDATED))));
    enrollment.setLastUpdatedAtClient(DateUtils.getIso8601NoTz(rs.getTimestamp(getColumnName(UPDATEDCLIENT))));
    setUserInfoSnapshot(rs, getColumnName(LAST_UPDATED_BY), enrollment::setLastUpdatedByUserInfo);
    enrollment.setProgram(rs.getString(getColumnName(PROGRAM_UID)));
    enrollment.setStatus(EnrollmentStatus.fromStatusString(rs.getString(getColumnName(STATUS))));
    enrollment.setEnrollmentDate(rs.getTimestamp(getColumnName(ENROLLMENTDATE)));
    enrollment.setIncidentDate(rs.getTimestamp(getColumnName(INCIDENTDATE)));
    final boolean followup = rs.getBoolean(getColumnName(FOLLOWUP));
    enrollment.setFollowup(rs.wasNull() ? null : followup);
    enrollment.setCompletedDate(rs.getTimestamp(getColumnName(COMPLETED)));
    enrollment.setCompletedBy(rs.getString(getColumnName(COMPLETEDBY)));
    enrollment.setStoredBy(rs.getString(getColumnName(STOREDBY)));
    enrollment.setDeleted(rs.getBoolean(getColumnName(DELETED)));
    enrollment.setId(rs.getLong(getColumnName(ID)));
    return enrollment;
}
Also used : Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment)

Aggregations

Date (java.util.Date)2 IdScheme (org.hisp.dhis.common.IdScheme)2 DataElement (org.hisp.dhis.dataelement.DataElement)2 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)2 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)2 ImportConflict (org.hisp.dhis.dxf2.importsummary.ImportConflict)2 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)2 ProgramStageDataElement (org.hisp.dhis.program.ProgramStageDataElement)2 TrackedEntityDataValue (org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue)2 HashSet (java.util.HashSet)1 IllegalQueryException (org.hisp.dhis.common.IllegalQueryException)1 Enrollment (org.hisp.dhis.dxf2.events.enrollment.Enrollment)1 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)1 Program (org.hisp.dhis.program.Program)1 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)1