use of org.hisp.dhis.program.UserInfoSnapshot in project dhis2-core by dhis2.
the class CommandSMSListener method register.
protected void register(List<ProgramInstance> programInstances, Map<String, String> commandValuePairs, SMSCommand smsCommand, IncomingSms sms, Set<OrganisationUnit> ous) {
if (programInstances.isEmpty()) {
ProgramInstance pi = new ProgramInstance();
pi.setEnrollmentDate(new Date());
pi.setIncidentDate(new Date());
pi.setProgram(smsCommand.getProgram());
pi.setStatus(ProgramStatus.ACTIVE);
programInstanceService.addProgramInstance(pi);
programInstances.add(pi);
} else if (programInstances.size() > 1) {
update(sms, SmsMessageStatus.FAILED, false);
sendFeedback("Multiple active program instances exists for program: " + smsCommand.getProgram().getUid(), sms.getOriginator(), ERROR);
return;
}
ProgramInstance programInstance = programInstances.get(0);
UserInfoSnapshot currentUserInfo = UserInfoSnapshot.from(currentUserService.getCurrentUser());
ProgramStageInstance programStageInstance = new ProgramStageInstance();
programStageInstance.setOrganisationUnit(ous.iterator().next());
programStageInstance.setProgramStage(smsCommand.getProgramStage());
programStageInstance.setProgramInstance(programInstance);
programStageInstance.setExecutionDate(sms.getSentDate());
programStageInstance.setDueDate(sms.getSentDate());
programStageInstance.setAttributeOptionCombo(dataElementCategoryService.getDefaultCategoryOptionCombo());
programStageInstance.setCompletedBy("DHIS 2");
programStageInstance.setStoredBy(currentUserInfo.getUsername());
programStageInstance.setCreatedByUserInfo(currentUserInfo);
programStageInstance.setLastUpdatedByUserInfo(currentUserInfo);
Map<DataElement, EventDataValue> dataElementsAndEventDataValues = new HashMap<>();
for (SMSCode smsCode : smsCommand.getCodes()) {
EventDataValue eventDataValue = new EventDataValue(smsCode.getDataElement().getUid(), commandValuePairs.get(smsCode.getCode()), currentUserInfo);
eventDataValue.setAutoFields();
// therefore, empty values are ignored
if (!StringUtils.isEmpty(eventDataValue.getValue())) {
dataElementsAndEventDataValues.put(smsCode.getDataElement(), eventDataValue);
}
}
programStageInstanceService.saveEventDataValuesAndSaveProgramStageInstance(programStageInstance, dataElementsAndEventDataValues);
update(sms, SmsMessageStatus.PROCESSED, true);
sendFeedback(StringUtils.defaultIfEmpty(smsCommand.getSuccessMessage(), SMSCommand.SUCCESS_MESSAGE), sms.getOriginator(), INFO);
}
use of org.hisp.dhis.program.UserInfoSnapshot in project dhis2-core by dhis2.
the class UserInfoTestHelper method testUserInfo.
public static UserInfoSnapshot testUserInfo(String username) {
UserInfoSnapshot userInfo = new UserInfoSnapshot();
userInfo.setUsername(username);
return userInfo;
}
use of org.hisp.dhis.program.UserInfoSnapshot in project dhis2-core by dhis2.
the class CompressionSMSListener method saveNewEvent.
protected List<Object> saveNewEvent(String eventUid, OrganisationUnit orgUnit, ProgramStage programStage, ProgramInstance programInstance, IncomingSms sms, CategoryOptionCombo aoc, User user, List<SmsDataValue> values, SmsEventStatus eventStatus, Date eventDate, Date dueDate, GeoPoint coordinates) {
ArrayList<Object> errorUids = new ArrayList<>();
ProgramStageInstance programStageInstance;
if (programStageInstanceService.programStageInstanceExists(eventUid)) {
programStageInstance = programStageInstanceService.getProgramStageInstance(eventUid);
} else {
programStageInstance = new ProgramStageInstance();
programStageInstance.setUid(eventUid);
}
programStageInstance.setOrganisationUnit(orgUnit);
programStageInstance.setProgramStage(programStage);
programStageInstance.setProgramInstance(programInstance);
programStageInstance.setExecutionDate(eventDate);
programStageInstance.setDueDate(dueDate);
programStageInstance.setAttributeOptionCombo(aoc);
programStageInstance.setStoredBy(user.getUsername());
UserInfoSnapshot currentUserInfo = UserInfoSnapshot.from(user);
programStageInstance.setCreatedByUserInfo(currentUserInfo);
programStageInstance.setLastUpdatedByUserInfo(currentUserInfo);
programStageInstance.setStatus(getCoreEventStatus(eventStatus));
programStageInstance.setGeometry(convertGeoPointToGeometry(coordinates));
if (eventStatus.equals(SmsEventStatus.COMPLETED)) {
programStageInstance.setCompletedBy(user.getUsername());
programStageInstance.setCompletedDate(new Date());
}
Map<DataElement, EventDataValue> dataElementsAndEventDataValues = new HashMap<>();
if (values != null) {
for (SmsDataValue dv : values) {
Uid deid = dv.getDataElement();
String val = dv.getValue();
DataElement de = dataElementService.getDataElement(deid.getUid());
// TODO: Is this the correct way of handling errors here?
if (de == null) {
log.warn(String.format("Given data element [%s] could not be found. Continuing with submission...", deid));
errorUids.add(deid);
continue;
} else if (val == null || StringUtils.isEmpty(val)) {
log.warn(String.format("Value for atttribute [%s] is null or empty. Continuing with submission...", deid));
continue;
}
EventDataValue eventDataValue = new EventDataValue(deid.getUid(), dv.getValue(), currentUserInfo);
eventDataValue.setAutoFields();
dataElementsAndEventDataValues.put(de, eventDataValue);
}
}
programStageInstanceService.saveEventDataValuesAndSaveProgramStageInstance(programStageInstance, dataElementsAndEventDataValues);
return errorUids;
}
use of org.hisp.dhis.program.UserInfoSnapshot in project dhis2-core by dhis2.
the class AbstractUserInfoPreProcessor method process.
@Override
public void process(Event event, WorkContext workContext) {
User user = findUserFromImportOptions(workContext.getImportOptions()).orElseGet(() -> getUser(workContext));
if (user != null) {
UserInfoSnapshot userInfo = UserInfoSnapshot.from(user);
updateEventUserInfo(event, userInfo);
Set<String> updatableDataValues = Optional.ofNullable(event).map(Event::getDataValues).orElse(Collections.emptySet()).stream().map(DataValue::getDataElement).collect(Collectors.toSet());
Set<EventDataValue> eventDataValuesToUpdate = getWorkContextDataValueMapEntry(workContext, event.getUid()).stream().filter(eventDataValue -> updatableDataValues.contains(eventDataValue.getDataElement())).collect(Collectors.toSet());
updateDataValuesUserInfo(getExistingPsi(workContext, event.getUid()), eventDataValuesToUpdate, userInfo);
}
}
use of org.hisp.dhis.program.UserInfoSnapshot in project dhis2-core by dhis2.
the class EventTrackerConverterService method to.
@Override
public List<Event> to(List<ProgramStageInstance> programStageInstances) {
List<Event> events = new ArrayList<>();
programStageInstances.forEach(psi -> {
Event event = new Event();
event.setEvent(psi.getUid());
event.setFollowup(BooleanUtils.toBoolean(psi.getProgramInstance().getFollowup()));
event.setStatus(psi.getStatus());
event.setOccurredAt(DateUtils.instantFromDate(psi.getExecutionDate()));
event.setScheduledAt(DateUtils.instantFromDate(psi.getDueDate()));
event.setStoredBy(psi.getStoredBy());
event.setCompletedBy(psi.getCompletedBy());
event.setCompletedAt(DateUtils.instantFromDate(psi.getCompletedDate()));
event.setCreatedAt(DateUtils.instantFromDate(psi.getCreated()));
event.setCreatedAtClient(DateUtils.instantFromDate(psi.getCreatedAtClient()));
event.setUpdatedAt(DateUtils.instantFromDate(psi.getLastUpdated()));
event.setUpdatedAtClient(DateUtils.instantFromDate(psi.getLastUpdatedAtClient()));
event.setGeometry(psi.getGeometry());
event.setDeleted(psi.isDeleted());
OrganisationUnit ou = psi.getOrganisationUnit();
if (ou != null) {
event.setOrgUnit(ou.getUid());
event.setOrgUnitName(ou.getName());
}
event.setEnrollment(psi.getProgramInstance().getUid());
event.setProgramStage(psi.getProgramStage().getUid());
event.setAttributeOptionCombo(psi.getAttributeOptionCombo().getUid());
event.setAttributeCategoryOptions(psi.getAttributeOptionCombo().getCategoryOptions().stream().map(CategoryOption::getUid).collect(Collectors.joining(";")));
Set<EventDataValue> dataValues = psi.getEventDataValues();
for (EventDataValue dataValue : dataValues) {
DataValue value = new DataValue();
value.setCreatedAt(DateUtils.instantFromDate(dataValue.getCreated()));
value.setUpdatedAt(DateUtils.instantFromDate(dataValue.getLastUpdated()));
value.setDataElement(dataValue.getDataElement());
value.setValue(dataValue.getValue());
value.setProvidedElsewhere(dataValue.getProvidedElsewhere());
value.setStoredBy(dataValue.getStoredBy());
value.setLastUpdatedBy(Optional.ofNullable(dataValue.getLastUpdatedByUserInfo()).map(UserInfoSnapshot::getUsername).orElse(""));
value.setCreatedBy(Optional.ofNullable(dataValue.getCreatedByUserInfo()).map(UserInfoSnapshot::getUsername).orElse(""));
event.getDataValues().add(value);
}
events.add(event);
});
return events;
}
Aggregations