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