Search in sources :

Example 1 with TrackedEntityComment

use of org.hisp.dhis.trackedentitycomment.TrackedEntityComment 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 2 with TrackedEntityComment

use of org.hisp.dhis.trackedentitycomment.TrackedEntityComment 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 3 with TrackedEntityComment

use of org.hisp.dhis.trackedentitycomment.TrackedEntityComment in project dhis2-core by dhis2.

the class AbstractEnrollmentService method saveTrackedEntityComment.

private void saveTrackedEntityComment(ProgramInstance programInstance, Enrollment enrollment) {
    String storedBy = currentUserService.getCurrentUsername();
    for (Note note : enrollment.getNotes()) {
        TrackedEntityComment comment = new TrackedEntityComment();
        comment.setCreator(storedBy);
        comment.setCreatedDate(new Date());
        comment.setCommentText(note.getValue());
        commentService.addTrackedEntityComment(comment);
        programInstance.getComments().add(comment);
        programInstanceService.updateProgramInstance(programInstance);
    }
}
Also used : TrackedEntityComment(org.hisp.dhis.trackedentitycomment.TrackedEntityComment) Note(org.hisp.dhis.dxf2.events.event.Note) Date(java.util.Date)

Example 4 with TrackedEntityComment

use of org.hisp.dhis.trackedentitycomment.TrackedEntityComment in project dhis2-core by dhis2.

the class JdbcEventCommentStore method saveAllComments.

/**
 * Save all the comments ({@see TrackedEntityComment} for the list of
 * {@see ProgramStageInstance}
 *
 * @param batch a List of {@see ProgramStageInstance}
 */
public void saveAllComments(List<ProgramStageInstance> batch) {
    try {
        // List of PSI that has at least one non empty comment (i.e. PSI
        // having comments
        // that can actually be saved)
        // In resulting PSI list, all comments without text are removed.
        List<ProgramStageInstance> programStageInstances = batch.stream().map(this::withoutEmptyComments).filter(this::hasComments).collect(toList());
        for (ProgramStageInstance psi : programStageInstances) {
            Integer sortOrder = getInitialSortOrder(psi);
            for (TrackedEntityComment comment : psi.getComments()) {
                Long commentId = saveComment(comment);
                if (commentId != null && commentId != 0) {
                    saveCommentToEvent(psi.getId(), commentId, sortOrder);
                    sortOrder++;
                }
            }
        }
    } catch (DataAccessException dae) {
        log.error("An error occurred saving a Program Stage Instance comment", dae);
        throw dae;
    }
}
Also used : TrackedEntityComment(org.hisp.dhis.trackedentitycomment.TrackedEntityComment) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) DataAccessException(org.springframework.dao.DataAccessException)

Example 5 with TrackedEntityComment

use of org.hisp.dhis.trackedentitycomment.TrackedEntityComment in project dhis2-core by dhis2.

the class ProgramStageInstanceNoteMapper method map.

@Override
public TrackedEntityComment map(Note note) {
    final TrackedEntityComment comment = new TrackedEntityComment();
    comment.setUid(note.getNote());
    comment.setCommentText(note.getValue());
    comment.setCreator(getValidUsername(note.getStoredBy(), workContext.getImportOptions()));
    comment.setCreated(note.getStoredDate() == null ? new Date() : parseDate(note.getStoredDate()));
    comment.setLastUpdated(new Date());
    comment.setLastUpdatedBy(workContext.getServiceDelegator().getEventImporterUserService().getCurrentUser());
    return comment;
}
Also used : TrackedEntityComment(org.hisp.dhis.trackedentitycomment.TrackedEntityComment) Date(java.util.Date) DateUtils.parseDate(org.hisp.dhis.util.DateUtils.parseDate)

Aggregations

TrackedEntityComment (org.hisp.dhis.trackedentitycomment.TrackedEntityComment)20 Test (org.junit.jupiter.api.Test)11 Note (org.hisp.dhis.tracker.domain.Note)9 Date (java.util.Date)8 List (java.util.List)6 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)5 Collectors (java.util.stream.Collectors)4 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)4 Matchers.hasSize (org.hamcrest.Matchers.hasSize)4 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)4 BeanRandomizer (org.hisp.dhis.random.BeanRandomizer)4 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)4 TrackerPreheat (org.hisp.dhis.tracker.preheat.TrackerPreheat)4 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)4 TrackerImportValidationContext (org.hisp.dhis.tracker.validation.TrackerImportValidationContext)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 Note (org.hisp.dhis.dxf2.events.event.Note)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2