use of org.hisp.dhis.eventdatavalue.EventDataValue 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.eventdatavalue.EventDataValue in project dhis2-core by dhis2.
the class AbstractEventService method getEvent.
@Transactional(readOnly = true)
@Override
public Event getEvent(ProgramStageInstance programStageInstance, boolean isSynchronizationQuery, boolean skipOwnershipCheck) {
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.setCreatedByUserInfo(programStageInstance.getCreatedByUserInfo());
event.setLastUpdatedByUserInfo(programStageInstance.getLastUpdatedByUserInfo());
event.setCreatedAtClient(DateUtils.getIso8601NoTz(programStageInstance.getCreatedAtClient()));
event.setLastUpdated(DateUtils.getIso8601NoTz(programStageInstance.getLastUpdated()));
event.setLastUpdatedAtClient(DateUtils.getIso8601NoTz(programStageInstance.getLastUpdatedAtClient()));
event.setGeometry(programStageInstance.getGeometry());
event.setDeleted(programStageInstance.isDeleted());
if (programStageInstance.getAssignedUser() != null) {
event.setAssignedUser(programStageInstance.getAssignedUser().getUid());
event.setAssignedUserUsername(programStageInstance.getAssignedUser().getUsername());
event.setAssignedUserDisplayName(programStageInstance.getAssignedUser().getName());
}
User user = currentUserService.getCurrentUser();
OrganisationUnit ou = programStageInstance.getOrganisationUnit();
List<String> errors = trackerAccessManager.canRead(user, programStageInstance, skipOwnershipCheck);
if (!errors.isEmpty()) {
throw new IllegalQueryException(errors.toString());
}
if (ou != null) {
event.setOrgUnit(ou.getUid());
event.setOrgUnitName(ou.getName());
}
Program program = programStageInstance.getProgramInstance().getProgram();
event.setProgram(program.getUid());
event.setEnrollment(programStageInstance.getProgramInstance().getUid());
event.setProgramStage(programStageInstance.getProgramStage().getUid());
CategoryOptionCombo attributeOptionCombo = programStageInstance.getAttributeOptionCombo();
if (attributeOptionCombo != null) {
event.setAttributeOptionCombo(attributeOptionCombo.getUid());
event.setAttributeCategoryOptions(String.join(";", attributeOptionCombo.getCategoryOptions().stream().map(CategoryOption::getUid).collect(Collectors.toList())));
}
if (programStageInstance.getProgramInstance().getEntityInstance() != null) {
event.setTrackedEntityInstance(programStageInstance.getProgramInstance().getEntityInstance().getUid());
}
Collection<EventDataValue> dataValues;
if (!isSynchronizationQuery) {
dataValues = programStageInstance.getEventDataValues();
} else {
Set<String> dataElementsToSync = programStageInstance.getProgramStage().getProgramStageDataElements().stream().filter(psde -> !psde.getSkipSynchronization()).map(psde -> psde.getDataElement().getUid()).collect(Collectors.toSet());
dataValues = programStageInstance.getEventDataValues().stream().filter(dv -> dataElementsToSync.contains(dv.getDataElement())).collect(Collectors.toSet());
}
for (EventDataValue dataValue : dataValues) {
if (getDataElement(user.getUid(), dataValue.getDataElement())) {
DataValue value = new DataValue();
value.setCreated(DateUtils.getIso8601NoTz(dataValue.getCreated()));
value.setCreatedByUserInfo(dataValue.getCreatedByUserInfo());
value.setLastUpdated(DateUtils.getIso8601NoTz(dataValue.getLastUpdated()));
value.setLastUpdatedByUserInfo(dataValue.getLastUpdatedByUserInfo());
value.setDataElement(dataValue.getDataElement());
value.setValue(dataValue.getValue());
value.setProvidedElsewhere(dataValue.getProvidedElsewhere());
value.setStoredBy(dataValue.getStoredBy());
event.getDataValues().add(value);
} else {
log.info("Can not find a Data Element having UID [" + dataValue.getDataElement() + "]");
}
}
event.getNotes().addAll(NoteHelper.convertNotes(programStageInstance.getComments()));
event.setRelationships(programStageInstance.getRelationshipItems().stream().filter(Objects::nonNull).map((r) -> relationshipService.getRelationship(r.getRelationship(), RelationshipParams.FALSE, user)).collect(Collectors.toSet()));
return event;
}
use of org.hisp.dhis.eventdatavalue.EventDataValue in project dhis2-core by dhis2.
the class JdbcEventStore method getEventRows.
@Override
public List<EventRow> getEventRows(EventSearchParams params, List<OrganisationUnit> organisationUnits) {
User user = currentUserService.getCurrentUser();
setAccessiblePrograms(user, params);
List<EventRow> eventRows = new ArrayList<>();
String sql = buildSql(params, organisationUnits, user);
SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql);
log.debug("Event query SQL: " + sql);
EventRow eventRow = new EventRow();
eventRow.setEvent("not_valid");
Set<String> notes = new HashSet<>();
Map<String, List<DataValue>> processedDataValues = new HashMap<>();
while (rowSet.next()) {
if (rowSet.getString("psi_uid") == null || (params.getCategoryOptionCombo() == null && !isSuper(user) && !userHasAccess(rowSet))) {
continue;
}
if (eventRow.getUid() == null || !eventRow.getUid().equals(rowSet.getString("psi_uid"))) {
validateIdentifiersPresence(rowSet, params.getIdSchemes(), false);
eventRow = new EventRow();
eventRow.setUid(rowSet.getString("psi_uid"));
eventRow.setEvent(rowSet.getString("psi_uid"));
eventRow.setTrackedEntityInstance(rowSet.getString("tei_uid"));
eventRow.setTrackedEntityInstanceOrgUnit(rowSet.getString("tei_ou"));
eventRow.setTrackedEntityInstanceOrgUnitName(rowSet.getString("tei_ou_name"));
eventRow.setTrackedEntityInstanceCreated(rowSet.getString("tei_created"));
eventRow.setTrackedEntityInstanceInactive(rowSet.getBoolean("tei_inactive"));
eventRow.setDeleted(rowSet.getBoolean("psi_deleted"));
eventRow.setProgram(rowSet.getString("p_identifier"));
eventRow.setProgramStage(rowSet.getString("ps_identifier"));
eventRow.setOrgUnit(rowSet.getString("ou_identifier"));
ProgramType programType = ProgramType.fromValue(rowSet.getString("p_type"));
if (programType == ProgramType.WITHOUT_REGISTRATION) {
eventRow.setEnrollment(rowSet.getString("pi_uid"));
eventRow.setFollowup(rowSet.getBoolean("pi_followup"));
}
eventRow.setTrackedEntityInstance(rowSet.getString("tei_uid"));
eventRow.setOrgUnitName(rowSet.getString("ou_name"));
eventRow.setDueDate(DateUtils.getIso8601NoTz(rowSet.getDate("psi_duedate")));
eventRow.setEventDate(DateUtils.getIso8601NoTz(rowSet.getDate("psi_executiondate")));
eventRows.add(eventRow);
}
if (rowSet.getString("pav_value") != null && rowSet.getString("ta_uid") != null) {
String valueType = rowSet.getString("ta_valuetype");
Attribute attribute = new Attribute();
attribute.setCreated(DateUtils.getIso8601NoTz(rowSet.getDate("pav_created")));
attribute.setLastUpdated(DateUtils.getIso8601NoTz(rowSet.getDate("pav_lastupdated")));
attribute.setValue(rowSet.getString("pav_value"));
attribute.setDisplayName(rowSet.getString("ta_name"));
attribute.setValueType(valueType != null ? ValueType.valueOf(valueType.toUpperCase()) : null);
attribute.setAttribute(rowSet.getString("ta_uid"));
eventRow.getAttributes().add(attribute);
}
if (!StringUtils.isEmpty(rowSet.getString("psi_eventdatavalues")) && !processedDataValues.containsKey(rowSet.getString("psi_uid"))) {
List<DataValue> dataValues = new ArrayList<>();
Set<EventDataValue> eventDataValues = convertEventDataValueJsonIntoSet(rowSet.getString("psi_eventdatavalues"));
for (EventDataValue dv : eventDataValues) {
dataValues.add(convertEventDataValueIntoDtoDataValue(dv));
}
processedDataValues.put(rowSet.getString("psi_uid"), dataValues);
}
if (rowSet.getString("psinote_value") != null && !notes.contains(rowSet.getString("psinote_id"))) {
Note note = new Note();
note.setNote(rowSet.getString("psinote_uid"));
note.setValue(rowSet.getString("psinote_value"));
note.setStoredDate(DateUtils.getIso8601NoTz(rowSet.getDate("psinote_storeddate")));
note.setStoredBy(rowSet.getString("psinote_storedby"));
eventRow.getNotes().add(note);
notes.add(rowSet.getString("psinote_id"));
}
}
eventRows.forEach(e -> e.setDataValues(processedDataValues.get(e.getUid())));
IdSchemes idSchemes = ObjectUtils.firstNonNull(params.getIdSchemes(), new IdSchemes());
IdScheme dataElementIdScheme = idSchemes.getDataElementIdScheme();
if (dataElementIdScheme != IdScheme.ID && dataElementIdScheme != IdScheme.UID) {
CachingMap<String, String> dataElementUidToIdentifierCache = new CachingMap<>();
List<Collection<DataValue>> dataValuesList = eventRows.stream().map(EventRow::getDataValues).collect(Collectors.toList());
populateCache(dataElementIdScheme, dataValuesList, dataElementUidToIdentifierCache);
convertDataValuesIdentifiers(dataElementIdScheme, dataValuesList, dataElementUidToIdentifierCache);
}
return eventRows;
}
use of org.hisp.dhis.eventdatavalue.EventDataValue in project dhis2-core by dhis2.
the class EventUtils method jsonToEventDataValues.
/**
* Converts the Event Data Value json payload into a Set of EventDataValue
*
* Note that the EventDataValue payload is stored as a map: {dataelementid:{
* ...}, {dataelementid:{ ...} }
*
* Therefore, the conversion is a bit convoluted, since the payload has to
* be converted into a Map and then into a Set
*/
public static Set<EventDataValue> jsonToEventDataValues(ObjectMapper jsonMapper, Object eventsDataValues) throws JsonProcessingException {
final TypeFactory typeFactory = jsonMapper.getTypeFactory();
MapType mapType = typeFactory.constructMapType(HashMap.class, String.class, EventDataValue.class);
String content = null;
if (eventsDataValues instanceof String) {
content = (String) eventsDataValues;
} else if (eventsDataValues instanceof PGobject) {
content = ((PGobject) eventsDataValues).getValue();
}
Set<EventDataValue> dataValues = new HashSet<>();
if (!StringUtils.isEmpty(content)) {
Map<String, EventDataValue> parsed = jsonMapper.readValue(content, mapType);
for (String dataElementId : parsed.keySet()) {
EventDataValue edv = parsed.get(dataElementId);
edv.setDataElement(dataElementId);
dataValues.add(edv);
}
}
return dataValues;
}
use of org.hisp.dhis.eventdatavalue.EventDataValue in project dhis2-core by dhis2.
the class EventDataValueAggregator method toEventDataValue.
private EventDataValue toEventDataValue(DataValue dataValue, EventDataValue existing) {
EventDataValue eventDataValue = new EventDataValue();
eventDataValue.setDataElement(dataValue.getDataElement());
eventDataValue.setValue(dataValue.getValue());
// storedBy is always set by the EventStoredByPreProcessor
eventDataValue.setStoredBy(dataValue.getStoredBy());
eventDataValue.setProvidedElsewhere(dataValue.getProvidedElsewhere());
if (existing != null) {
eventDataValue.setCreated(existing.getCreated());
}
eventDataValue.setLastUpdated(new Date());
return eventDataValue;
}
Aggregations