Search in sources :

Example 1 with EventDataValue

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);
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) HashMap(java.util.HashMap) ProgramInstance(org.hisp.dhis.program.ProgramInstance) UserInfoSnapshot(org.hisp.dhis.program.UserInfoSnapshot) SMSCode(org.hisp.dhis.sms.command.code.SMSCode) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) Date(java.util.Date) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 2 with EventDataValue

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;
}
Also used : ListGrid(org.hisp.dhis.system.grid.ListGrid) Arrays(java.util.Arrays) CategoryService(org.hisp.dhis.category.CategoryService) FIRST_PAGE(org.hisp.dhis.common.SlimPager.FIRST_PAGE) CategoryOption(org.hisp.dhis.category.CategoryOption) ValidationUtils(org.hisp.dhis.system.util.ValidationUtils) EventRow(org.hisp.dhis.dxf2.events.report.EventRow) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) StringUtils(org.apache.commons.lang3.StringUtils) EVENT_CREATED_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_CREATED_ID) EVENT_EXECUTION_DATE_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_EXECUTION_DATE_ID) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) EnrollmentStatus(org.hisp.dhis.dxf2.events.enrollment.EnrollmentStatus) EventSyncService(org.hisp.dhis.program.EventSyncService) FileResourceService(org.hisp.dhis.fileresource.FileResourceService) Map(java.util.Map) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) JobConfiguration(org.hisp.dhis.scheduling.JobConfiguration) EVENT_STATUS_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_STATUS_ID) OrganisationUnitSelectionMode(org.hisp.dhis.common.OrganisationUnitSelectionMode) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) DateUtils.getMediumDateString(org.hisp.dhis.util.DateUtils.getMediumDateString) Set(java.util.Set) SchemaService(org.hisp.dhis.schema.SchemaService) TrackerOwnershipManager(org.hisp.dhis.trackedentity.TrackerOwnershipManager) Slf4j(lombok.extern.slf4j.Slf4j) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) TrackedEntityComment(org.hisp.dhis.trackedentitycomment.TrackedEntityComment) ProgramType(org.hisp.dhis.program.ProgramType) ProgramInstanceService(org.hisp.dhis.program.ProgramInstanceService) TrackedEntityCommentService(org.hisp.dhis.trackedentitycomment.TrackedEntityCommentService) EVENT_STORED_BY_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_STORED_BY_ID) QueryItem(org.hisp.dhis.common.QueryItem) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) EVENT_GEOMETRY(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_GEOMETRY) ArrayList(java.util.ArrayList) RelationshipParams(org.hisp.dhis.dxf2.events.RelationshipParams) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) SlimPager(org.hisp.dhis.common.SlimPager) ImportConflicts(org.hisp.dhis.dxf2.importsummary.ImportConflicts) EventManager(org.hisp.dhis.dxf2.events.importer.EventManager) EVENT_ENROLLMENT_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_ENROLLMENT_ID) IdSchemes(org.hisp.dhis.common.IdSchemes) RelationshipService(org.hisp.dhis.dxf2.events.relationship.RelationshipService) DEFAULT_PAGE_SIZE(org.hisp.dhis.common.Pager.DEFAULT_PAGE_SIZE) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) TrackedEntityInstanceService(org.hisp.dhis.trackedentity.TrackedEntityInstanceService) DebugUtils(org.hisp.dhis.commons.util.DebugUtils) ERROR(org.hisp.dhis.system.notification.NotificationLevel.ERROR) CodeGenerator(org.hisp.dhis.common.CodeGenerator) ProgramService(org.hisp.dhis.program.ProgramService) DateUtils(org.hisp.dhis.util.DateUtils) EVENT_PROGRAM_STAGE_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_PROGRAM_STAGE_ID) PAGER_META_KEY(org.hisp.dhis.dxf2.events.event.EventSearchParams.PAGER_META_KEY) Authorities(org.hisp.dhis.security.Authorities) Date(java.util.Date) WorkContextLoader(org.hisp.dhis.dxf2.events.importer.context.WorkContextLoader) OrganisationUnitService(org.hisp.dhis.organisationunit.OrganisationUnitService) EVENT_LAST_UPDATED_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_LAST_UPDATED_ID) ProgramStageInstanceService(org.hisp.dhis.program.ProgramStageInstanceService) NoteHelper(org.hisp.dhis.dxf2.events.NoteHelper) EVENT_PROGRAM_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_PROGRAM_ID) EVENT_DUE_DATE_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_DUE_DATE_ID) UserService(org.hisp.dhis.user.UserService) Collection(java.util.Collection) EventStatus(org.hisp.dhis.event.EventStatus) Collectors(java.util.stream.Collectors) QueryService(org.hisp.dhis.query.QueryService) EventRows(org.hisp.dhis.dxf2.events.report.EventRows) Objects(java.util.Objects) List(java.util.List) CollectionUtils.isNotEmpty(org.apache.commons.collections4.CollectionUtils.isNotEmpty) EventImporter(org.hisp.dhis.dxf2.events.importer.EventImporter) TrackerAccessManager(org.hisp.dhis.trackedentity.TrackerAccessManager) Cache(org.hisp.dhis.cache.Cache) EVENT_ORG_UNIT_NAME(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_ORG_UNIT_NAME) EVENT_CREATED_BY_USER_INFO_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_CREATED_BY_USER_INFO_ID) EVENT_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_ID) HashMap(java.util.HashMap) ObjectUtils.defaultIfNull(org.apache.commons.lang3.ObjectUtils.defaultIfNull) Program(org.hisp.dhis.program.Program) HashSet(java.util.HashSet) DataElement(org.hisp.dhis.dataelement.DataElement) WorkContext(org.hisp.dhis.dxf2.events.importer.context.WorkContext) Notifier(org.hisp.dhis.system.notification.Notifier) DbmsManager(org.hisp.dhis.dbms.DbmsManager) GridHeader(org.hisp.dhis.common.GridHeader) User(org.hisp.dhis.user.User) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) EVENT_ORG_UNIT_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_ORG_UNIT_ID) EVENT_LAST_UPDATED_BY_USER_INFO_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_LAST_UPDATED_BY_USER_INFO_ID) Collections.emptyMap(java.util.Collections.emptyMap) EVENT_DELETED(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_DELETED) Pager(org.hisp.dhis.common.Pager) Grid(org.hisp.dhis.common.Grid) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) NotificationLevel(org.hisp.dhis.system.notification.NotificationLevel) EVENT_ATTRIBUTE_OPTION_COMBO_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_ATTRIBUTE_OPTION_COMBO_ID) CurrentUserService(org.hisp.dhis.user.CurrentUserService) CachingMap(org.hisp.dhis.commons.collection.CachingMap) Collections(java.util.Collections) EVENT_COMPLETED_BY_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_COMPLETED_BY_ID) EVENT_COMPLETED_DATE_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_COMPLETED_DATE_ID) Transactional(org.springframework.transaction.annotation.Transactional) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) Program(org.hisp.dhis.program.Program) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) DateUtils.getMediumDateString(org.hisp.dhis.util.DateUtils.getMediumDateString) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with EventDataValue

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;
}
Also used : SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet) User(org.hisp.dhis.user.User) IdSchemes(org.hisp.dhis.common.IdSchemes) HashMap(java.util.HashMap) Attribute(org.hisp.dhis.dxf2.events.trackedentity.Attribute) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) ArrayList(java.util.ArrayList) DateUtils.getMediumDateString(org.hisp.dhis.util.DateUtils.getMediumDateString) TextUtils.getQuotedCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString) DateUtils.getLongGmtDateString(org.hisp.dhis.util.DateUtils.getLongGmtDateString) IdScheme(org.hisp.dhis.common.IdScheme) EventRow(org.hisp.dhis.dxf2.events.report.EventRow) CachingMap(org.hisp.dhis.commons.collection.CachingMap) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) ProgramType(org.hisp.dhis.program.ProgramType) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) HashSet(java.util.HashSet)

Example 4 with EventDataValue

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;
}
Also used : TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) MapType(com.fasterxml.jackson.databind.type.MapType) PGobject(org.postgresql.util.PGobject) HashSet(java.util.HashSet)

Example 5 with EventDataValue

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;
}
Also used : EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) Date(java.util.Date)

Aggregations

EventDataValue (org.hisp.dhis.eventdatavalue.EventDataValue)35 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)21 DataElement (org.hisp.dhis.dataelement.DataElement)14 Date (java.util.Date)10 HashSet (java.util.HashSet)10 Test (org.junit.jupiter.api.Test)9 DataValue (org.hisp.dhis.tracker.domain.DataValue)8 HashMap (java.util.HashMap)7 Event (org.hisp.dhis.tracker.domain.Event)7 User (org.hisp.dhis.user.User)7 ArrayList (java.util.ArrayList)6 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)6 Set (java.util.Set)5 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)5 ProgramInstance (org.hisp.dhis.program.ProgramInstance)5 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 WorkContext (org.hisp.dhis.dxf2.events.importer.context.WorkContext)4 EventStatus (org.hisp.dhis.event.EventStatus)4 Program (org.hisp.dhis.program.Program)4