Search in sources :

Example 21 with TrackedEntityInstance

use of org.hisp.dhis.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.

the class DefaultProgramNotificationService method resolveProgramMessageRecipients.

private ProgramMessageRecipients resolveProgramMessageRecipients(ProgramNotificationTemplate template, OrganisationUnit organisationUnit, ProgramInstance programInstance) {
    ProgramMessageRecipients recipients = new ProgramMessageRecipients();
    TrackedEntityInstance trackedEntityInstance = programInstance.getEntityInstance();
    ProgramNotificationRecipient recipientType = template.getNotificationRecipient();
    if (recipientType == ProgramNotificationRecipient.ORGANISATION_UNIT_CONTACT) {
        recipients.setOrganisationUnit(organisationUnit);
    } else if (recipientType == ProgramNotificationRecipient.TRACKED_ENTITY_INSTANCE) {
        recipients.setTrackedEntityInstance(trackedEntityInstance);
    } else if (recipientType == ProgramNotificationRecipient.PROGRAM_ATTRIBUTE && template.getRecipientProgramAttribute() != null) {
        List<String> recipientList = programInstance.getEntityInstance().getTrackedEntityAttributeValues().stream().filter(av -> template.getRecipientProgramAttribute().getUid().equals(av.getAttribute().getUid())).map(av -> av.getPlainValue()).collect(Collectors.toList());
        if (template.getDeliveryChannels().contains(DeliveryChannel.SMS)) {
            recipients.getPhoneNumbers().addAll(recipientList);
        } else if (template.getDeliveryChannels().contains(DeliveryChannel.EMAIL)) {
            recipients.getEmailAddresses().addAll(recipientList);
        }
    }
    return recipients;
}
Also used : ProgramMessageService(org.hisp.dhis.program.message.ProgramMessageService) NotificationMessageRenderer(org.hisp.dhis.notification.NotificationMessageRenderer) Date(java.util.Date) ProgramMessage(org.hisp.dhis.program.message.ProgramMessage) BatchResponseStatus(org.hisp.dhis.outboundmessage.BatchResponseStatus) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) MessageService(org.hisp.dhis.message.MessageService) ProgramMessageRecipients(org.hisp.dhis.program.message.ProgramMessageRecipients) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) ProgramInstance(org.hisp.dhis.program.ProgramInstance) User(org.hisp.dhis.user.User) Nullable(javax.annotation.Nullable) MessageType(org.hisp.dhis.message.MessageType) ProgramInstanceStore(org.hisp.dhis.program.ProgramInstanceStore) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Set(java.util.Set) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Clock(org.hisp.dhis.system.util.Clock) List(java.util.List) DeliveryChannel(org.hisp.dhis.common.DeliveryChannel) ProgramStageInstanceStore(org.hisp.dhis.program.ProgramStageInstanceStore) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) NotificationMessage(org.hisp.dhis.notification.NotificationMessage) Transactional(org.springframework.transaction.annotation.Transactional) ProgramMessageRecipients(org.hisp.dhis.program.message.ProgramMessageRecipients) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance)

Example 22 with TrackedEntityInstance

use of org.hisp.dhis.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.

the class DeliveryChannelStrategy method getTrackedEntityInstance.

// -------------------------------------------------------------------------
// Public methods
// -------------------------------------------------------------------------
protected TrackedEntityInstance getTrackedEntityInstance(ProgramMessage message) {
    if (message.getRecipients().getTrackedEntityInstance() == null) {
        return null;
    }
    String uid = message.getRecipients().getTrackedEntityInstance().getUid();
    TrackedEntityInstance tei = trackedEntityInstanceService.getTrackedEntityInstance(uid);
    message.getRecipients().setTrackedEntityInstance(tei);
    return tei;
}
Also used : TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance)

Example 23 with TrackedEntityInstance

use of org.hisp.dhis.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.

the class EmailDeliveryChannelStrategy method setAttributes.

@Override
public ProgramMessage setAttributes(ProgramMessage message) {
    validate(message);
    OrganisationUnit orgUnit = getOrganisationUnit(message);
    TrackedEntityInstance tei = getTrackedEntityInstance(message);
    if (orgUnit != null) {
        message.getRecipients().getEmailAddresses().add(getOrganisationUnitRecipient(orgUnit));
    }
    if (tei != null) {
        message.getRecipients().getEmailAddresses().add(getTrackedEntityInstanceRecipient(tei, ValueType.EMAIL));
    }
    return message;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance)

Example 24 with TrackedEntityInstance

use of org.hisp.dhis.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method getAllActivityPlan.

@Override
public ActivityPlan getAllActivityPlan(OrganisationUnit unit, String localeString) {
    List<Activity> items = new ArrayList<>();
    TrackedEntityInstanceQueryParams param = new TrackedEntityInstanceQueryParams();
    param.addOrganisationUnit(unit);
    Grid trackedEntityDrid = entityInstanceService.getTrackedEntityInstancesGrid(param);
    List<List<Object>> entityInstanceList = trackedEntityDrid.getRows();
    for (List<Object> entityInstance : entityInstanceList) {
        TrackedEntityInstance trackedEntityInstance = entityInstanceService.getTrackedEntityInstance(entityInstance.get(0).toString());
        for (ProgramStageInstance programStageInstance : programStageInstanceService.getProgramStageInstances(trackedEntityInstance, EventStatus.ACTIVE)) {
            items.add(getActivity(programStageInstance, false));
        }
    }
    if (items.isEmpty()) {
        return null;
    }
    Collections.sort(items, activityComparator);
    return new ActivityPlan(items);
}
Also used : Grid(org.hisp.dhis.common.Grid) ActivityPlan(org.hisp.dhis.api.mobile.model.ActivityPlan) ArrayList(java.util.ArrayList) Activity(org.hisp.dhis.api.mobile.model.Activity) List(java.util.List) PatientList(org.hisp.dhis.api.mobile.model.LWUITmodel.PatientList) ArrayList(java.util.ArrayList) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 25 with TrackedEntityInstance

use of org.hisp.dhis.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method updatePatient.

@Override
public Patient updatePatient(org.hisp.dhis.api.mobile.model.LWUITmodel.Patient patient, int orgUnitId, String programIdText) throws NotAllowedException {
    TrackedEntityInstance entityInstance = entityInstanceService.getTrackedEntityInstance(patient.getId());
    TrackedEntityInstance tempTEI = entityInstance;
    TrackedEntity trackedEntity = null;
    Program program = programService.getProgram(Integer.parseInt(programIdText));
    trackedEntity = program.getTrackedEntity();
    entityInstance.setTrackedEntity(trackedEntity);
    // get attributes to be saved/updated/deleted
    Collection<TrackedEntityAttribute> attributes = attributeService.getAllTrackedEntityAttributes();
    List<TrackedEntityAttributeValue> valuesForSave = new ArrayList<>();
    List<TrackedEntityAttributeValue> valuesForUpdate = new ArrayList<>();
    Collection<TrackedEntityAttributeValue> valuesForDelete = null;
    TrackedEntityAttributeValue attributeValue = null;
    Collection<org.hisp.dhis.api.mobile.model.PatientAttribute> attributesMobile = patient.getAttributes();
    if (attributes != null && attributes.size() > 0) {
        valuesForDelete = attValueService.getTrackedEntityAttributeValues(entityInstance);
        tempTEI.getTrackedEntityAttributeValues().clear();
        for (TrackedEntityAttribute attribute : attributes) {
            String value = getAttributeValue(attributesMobile, attribute.getName());
            if (value != null) {
                attributeValue = attValueService.getTrackedEntityAttributeValue(entityInstance, attribute);
                if (attributeValue == null) {
                    attributeValue = new TrackedEntityAttributeValue();
                    attributeValue.setEntityInstance(entityInstance);
                    attributeValue.setAttribute(attribute);
                    attributeValue.setValue(value.trim());
                    valuesForSave.add(attributeValue);
                } else {
                    attributeValue.setValue(value.trim());
                    valuesForUpdate.add(attributeValue);
                    valuesForDelete.remove(attributeValue);
                }
                tempTEI.getTrackedEntityAttributeValues().add(attributeValue);
            }
        }
    }
    // validate
    String[] errorCode = entityInstanceService.validateTrackedEntityInstance(tempTEI, program).split("_");
    int code = Integer.parseInt(errorCode[0]);
    if (code >= 1) {
        if (code == TrackedEntityInstanceService.ERROR_DUPLICATE_IDENTIFIER) {
            throw new NotAllowedException("Duplicate value of " + attributeService.getTrackedEntityAttribute(Integer.parseInt(errorCode[1])).getDisplayName());
        } else {
            throw new NotAllowedException("Validation error");
        }
    }
    entityInstanceService.updateTrackedEntityInstance(entityInstance, null, null, valuesForSave, valuesForUpdate, valuesForDelete);
    enrollProgram(patient.getId() + "-" + programIdText, null, new Date());
    entityInstance = entityInstanceService.getTrackedEntityInstance(patient.getId());
    entityInstance.setTrackedEntity(trackedEntity);
    return getPatientModel(entityInstance);
}
Also used : Program(org.hisp.dhis.program.Program) NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntity(org.hisp.dhis.trackedentity.TrackedEntity) PatientAttribute(org.hisp.dhis.api.mobile.model.PatientAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) ArrayList(java.util.ArrayList) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date)

Aggregations

TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)33 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)11 Date (java.util.Date)10 HashSet (java.util.HashSet)9 TrackedEntityAttributeValue (org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)8 ArrayList (java.util.ArrayList)7 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)7 Program (org.hisp.dhis.program.Program)6 ProgramInstance (org.hisp.dhis.program.ProgramInstance)6 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)6 List (java.util.List)5 NotAllowedException (org.hisp.dhis.api.mobile.NotAllowedException)5 PatientList (org.hisp.dhis.api.mobile.model.LWUITmodel.PatientList)5 PatientAttribute (org.hisp.dhis.api.mobile.model.PatientAttribute)4 Grid (org.hisp.dhis.common.Grid)4 TrackedEntityInstanceQueryParams (org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams)4 Activity (org.hisp.dhis.api.mobile.model.Activity)3 Patient (org.hisp.dhis.api.mobile.model.LWUITmodel.Patient)3 IllegalQueryException (org.hisp.dhis.common.IllegalQueryException)3 QueryItem (org.hisp.dhis.common.QueryItem)3