Search in sources :

Example 1 with Notification

use of org.hisp.dhis.api.mobile.model.LWUITmodel.Notification in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method handleLostToFollowUp.

@SuppressWarnings("finally")
@Override
public Notification handleLostToFollowUp(LostEvent lostEvent) throws NotAllowedException {
    Notification notification = new Notification();
    try {
        ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance(lostEvent.getId());
        programStageInstance.setDueDate(DateUtils.getMediumDate(lostEvent.getDueDate()));
        programStageInstance.setStatus(EventStatus.fromInt(lostEvent.getStatus()));
        if (lostEvent.getComment() != null) {
            List<MessageConversation> conversationList = new ArrayList<>();
            MessageConversation conversation = new MessageConversation(lostEvent.getName(), currentUserService.getCurrentUser(), MessageType.PRIVATE);
            conversation.addMessage(new Message(lostEvent.getComment(), null, currentUserService.getCurrentUser()));
            conversation.setRead(true);
            conversationList.add(conversation);
            programStageInstance.setMessageConversations(conversationList);
            messageService.saveMessageConversation(conversation);
        }
        programStageInstanceService.updateProgramStageInstance(programStageInstance);
        // send SMS
        if (programStageInstance.getProgramInstance().getEntityInstance().getTrackedEntityAttributeValues() != null && lostEvent.getSMS() != null) {
            List<User> recipientsList = new ArrayList<>();
            for (TrackedEntityAttributeValue attrValue : programStageInstance.getProgramInstance().getEntityInstance().getTrackedEntityAttributeValues()) {
                if (ValueType.PHONE_NUMBER == attrValue.getAttribute().getValueType()) {
                    User user = new User();
                    user.setPhoneNumber(attrValue.getValue());
                    recipientsList.add(user);
                }
            }
            Set<User> recipients = new HashSet<>();
            recipients.addAll(recipientsList);
            smsSender.sendMessage(lostEvent.getName(), lostEvent.getSMS(), null, currentUserService.getCurrentUser(), recipients, false);
        }
        notification.setMessage("Success");
    } catch (Exception e) {
        e.printStackTrace();
        notification.setMessage("Fail");
    } finally {
        return notification;
    }
}
Also used : User(org.hisp.dhis.user.User) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) ArrayList(java.util.ArrayList) Notification(org.hisp.dhis.api.mobile.model.LWUITmodel.Notification) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 NotAllowedException (org.hisp.dhis.api.mobile.NotAllowedException)1 Notification (org.hisp.dhis.api.mobile.model.LWUITmodel.Notification)1 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)1 TrackedEntityAttributeValue (org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)1 User (org.hisp.dhis.user.User)1