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;
}
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;
}
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);
}
}
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;
}
}
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;
}
Aggregations