Search in sources :

Example 6 with TrackedEntityDataValue

use of org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue in project dhis2-core by dhis2.

the class AbstractEventService method convertProgramStageInstance.

private Event convertProgramStageInstance(ProgramStageInstance programStageInstance) {
    if (programStageInstance == null) {
        return null;
    }
    Event event = new Event();
    event.setEvent(programStageInstance.getUid());
    if (programStageInstance.getProgramInstance().getEntityInstance() != null) {
        event.setTrackedEntityInstance(programStageInstance.getProgramInstance().getEntityInstance().getUid());
    }
    event.setFollowup(programStageInstance.getProgramInstance().getFollowup());
    event.setEnrollmentStatus(EnrollmentStatus.fromProgramStatus(programStageInstance.getProgramInstance().getStatus()));
    event.setStatus(programStageInstance.getStatus());
    event.setEventDate(DateUtils.getIso8601NoTz(programStageInstance.getExecutionDate()));
    event.setDueDate(DateUtils.getIso8601NoTz(programStageInstance.getDueDate()));
    event.setStoredBy(programStageInstance.getStoredBy());
    event.setCompletedBy(programStageInstance.getCompletedBy());
    event.setCompletedDate(DateUtils.getIso8601NoTz(programStageInstance.getCompletedDate()));
    event.setCreated(DateUtils.getIso8601NoTz(programStageInstance.getCreated()));
    event.setCreatedAtClient(DateUtils.getIso8601NoTz(programStageInstance.getCreatedAtClient()));
    event.setLastUpdated(DateUtils.getIso8601NoTz(programStageInstance.getLastUpdated()));
    event.setLastUpdatedAtClient(DateUtils.getIso8601NoTz(programStageInstance.getLastUpdatedAtClient()));
    UserCredentials userCredentials = currentUserService.getCurrentUser().getUserCredentials();
    OrganisationUnit ou = programStageInstance.getOrganisationUnit();
    if (ou != null) {
        if (!organisationUnitService.isInUserHierarchy(ou)) {
            if (!userCredentials.isSuper() && !userCredentials.isAuthorized("F_TRACKED_ENTITY_INSTANCE_SEARCH_IN_ALL_ORGUNITS")) {
                throw new IllegalQueryException("User has no access to organisation unit: " + ou.getUid());
            }
        }
        event.setOrgUnit(ou.getUid());
        event.setOrgUnitName(ou.getName());
    }
    Program program = programStageInstance.getProgramInstance().getProgram();
    if (!userCredentials.isSuper() && !userCredentials.getAllPrograms().contains(program)) {
        throw new IllegalQueryException("User has no access to program: " + program.getUid());
    }
    event.setProgram(program.getUid());
    event.setEnrollment(programStageInstance.getProgramInstance().getUid());
    event.setProgramStage(programStageInstance.getProgramStage().getUid());
    event.setAttributeOptionCombo(programStageInstance.getAttributeOptionCombo().getUid());
    event.setAttributeCategoryOptions(String.join(";", programStageInstance.getAttributeOptionCombo().getCategoryOptions().stream().map(DataElementCategoryOption::getUid).collect(Collectors.toList())));
    if (programStageInstance.getProgramInstance().getEntityInstance() != null) {
        event.setTrackedEntityInstance(programStageInstance.getProgramInstance().getEntityInstance().getUid());
    }
    if (programStageInstance.getProgramStage().getCaptureCoordinates()) {
        Coordinate coordinate = null;
        if (programStageInstance.getLongitude() != null && programStageInstance.getLatitude() != null) {
            coordinate = new Coordinate(programStageInstance.getLongitude(), programStageInstance.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()) {
            event.setCoordinate(coordinate);
        }
    }
    Collection<TrackedEntityDataValue> dataValues = dataValueService.getTrackedEntityDataValues(programStageInstance);
    for (TrackedEntityDataValue dataValue : dataValues) {
        DataValue value = new DataValue();
        value.setCreated(DateUtils.getIso8601NoTz(dataValue.getCreated()));
        value.setLastUpdated(DateUtils.getIso8601NoTz(dataValue.getLastUpdated()));
        value.setDataElement(dataValue.getDataElement().getUid());
        value.setValue(dataValue.getValue());
        value.setProvidedElsewhere(dataValue.getProvidedElsewhere());
        value.setStoredBy(dataValue.getStoredBy());
        event.getDataValues().add(value);
    }
    List<TrackedEntityComment> comments = programStageInstance.getComments();
    for (TrackedEntityComment comment : comments) {
        Note note = new Note();
        note.setValue(comment.getCommentText());
        note.setStoredBy(comment.getCreator());
        if (comment.getCreatedDate() != null) {
            note.setStoredDate(DateUtils.getIso8601NoTz(comment.getCreatedDate()));
        }
        event.getNotes().add(note);
    }
    return event;
}
Also used : TrackedEntityComment(org.hisp.dhis.trackedentitycomment.TrackedEntityComment) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) IOException(java.io.IOException) UserCredentials(org.hisp.dhis.user.UserCredentials) List(java.util.List) ArrayList(java.util.ArrayList)

Example 7 with TrackedEntityDataValue

use of org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue 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 8 with TrackedEntityDataValue

use of org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue in project dhis2-core by dhis2.

the class AbstractEventService method saveDataValue.

private void saveDataValue(ProgramStageInstance programStageInstance, String storedBy, DataElement dataElement, String value, Boolean providedElsewhere, TrackedEntityDataValue dataValue, ImportSummary importSummary) {
    if (value != null && value.trim().length() == 0) {
        value = null;
    }
    if (value != null) {
        if (dataValue == null) {
            dataValue = new TrackedEntityDataValue(programStageInstance, dataElement, value);
            dataValue.setStoredBy(storedBy);
            dataValue.setProvidedElsewhere(providedElsewhere);
            dataValueService.saveTrackedEntityDataValue(dataValue);
            if (importSummary != null) {
                importSummary.getImportCount().incrementImported();
            }
        } else {
            dataValue.setValue(value);
            dataValue.setStoredBy(storedBy);
            dataValue.setProvidedElsewhere(providedElsewhere);
            dataValueService.updateTrackedEntityDataValue(dataValue);
            if (importSummary != null) {
                importSummary.getImportCount().incrementUpdated();
            }
        }
    } else if (dataValue != null) {
        dataValueService.deleteTrackedEntityDataValue(dataValue);
        if (importSummary != null) {
            importSummary.getImportCount().incrementDeleted();
        }
    }
}
Also used : TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue)

Example 9 with TrackedEntityDataValue

use of org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue 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 10 with TrackedEntityDataValue

use of org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue 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)

Aggregations

TrackedEntityDataValue (org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue)14 Date (java.util.Date)8 DataElement (org.hisp.dhis.dataelement.DataElement)7 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)6 ProgramStageDataElement (org.hisp.dhis.program.ProgramStageDataElement)4 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Program (org.hisp.dhis.program.Program)3 ProgramInstance (org.hisp.dhis.program.ProgramInstance)3 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)3 Matcher (java.util.regex.Matcher)2 IdScheme (org.hisp.dhis.common.IdScheme)2 IllegalQueryException (org.hisp.dhis.common.IllegalQueryException)2 ValueType (org.hisp.dhis.common.ValueType)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 ProgramStage (org.hisp.dhis.program.ProgramStage)2