Search in sources :

Example 1 with UserInfoSnapshot

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

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;
}
Also used : UserInfoSnapshot(org.hisp.dhis.program.UserInfoSnapshot)

Example 3 with UserInfoSnapshot

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;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Date(java.util.Date) DataElement(org.hisp.dhis.dataelement.DataElement) Uid(org.hisp.dhis.smscompression.models.Uid) SmsDataValue(org.hisp.dhis.smscompression.models.SmsDataValue) UserInfoSnapshot(org.hisp.dhis.program.UserInfoSnapshot) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 4 with UserInfoSnapshot

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);
    }
}
Also used : EventImporterUserService(org.hisp.dhis.dxf2.events.importer.EventImporterUserService) DataValue(org.hisp.dhis.dxf2.events.event.DataValue) ServiceDelegator(org.hisp.dhis.dxf2.events.importer.ServiceDelegator) UserInfoSnapshot(org.hisp.dhis.program.UserInfoSnapshot) Set(java.util.Set) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Collectors(java.util.stream.Collectors) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) Event(org.hisp.dhis.dxf2.events.event.Event) Processor(org.hisp.dhis.dxf2.events.importer.Processor) WorkContext(org.hisp.dhis.dxf2.events.importer.context.WorkContext) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) Optional(java.util.Optional) User(org.hisp.dhis.user.User) Collections(java.util.Collections) User(org.hisp.dhis.user.User) UserInfoSnapshot(org.hisp.dhis.program.UserInfoSnapshot) Event(org.hisp.dhis.dxf2.events.event.Event) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue)

Example 5 with UserInfoSnapshot

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;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataValue(org.hisp.dhis.tracker.domain.DataValue) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) UserInfoSnapshot(org.hisp.dhis.program.UserInfoSnapshot) ArrayList(java.util.ArrayList) Event(org.hisp.dhis.tracker.domain.Event) CategoryOption(org.hisp.dhis.category.CategoryOption) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue)

Aggregations

UserInfoSnapshot (org.hisp.dhis.program.UserInfoSnapshot)5 EventDataValue (org.hisp.dhis.eventdatavalue.EventDataValue)4 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 DataElement (org.hisp.dhis.dataelement.DataElement)2 Collections (java.util.Collections)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 CategoryOption (org.hisp.dhis.category.CategoryOption)1 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)1 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)1 DataValue (org.hisp.dhis.dxf2.events.event.DataValue)1 Event (org.hisp.dhis.dxf2.events.event.Event)1 EventImporterUserService (org.hisp.dhis.dxf2.events.importer.EventImporterUserService)1 Processor (org.hisp.dhis.dxf2.events.importer.Processor)1 ServiceDelegator (org.hisp.dhis.dxf2.events.importer.ServiceDelegator)1 WorkContext (org.hisp.dhis.dxf2.events.importer.context.WorkContext)1