Search in sources :

Example 1 with OutboundSms

use of org.hisp.dhis.sms.outbound.OutboundSms in project dhis2-core by dhis2.

the class SendScheduledMessageTask method sendMessages.

// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
private void sendMessages() {
    List<OutboundSms> outboundSmsList = outboundSmsService.getOutboundSms(OutboundSmsStatus.OUTBOUND);
    if (outboundSmsList != null) {
        for (OutboundSms outboundSms : outboundSmsList) {
            outboundSms.setDate(new Date());
            outboundSms.setStatus(OutboundSmsStatus.SENT);
            smsSender.sendMessage(null, outboundSms.getMessage(), outboundSms.getRecipients());
        }
    }
}
Also used : OutboundSms(org.hisp.dhis.sms.outbound.OutboundSms) Date(java.util.Date)

Example 2 with OutboundSms

use of org.hisp.dhis.sms.outbound.OutboundSms in project dhis2-core by dhis2.

the class HibernateOutboundSmsStore method get.

@SuppressWarnings("unchecked")
@Override
public List<OutboundSms> get(OutboundSmsStatus status) {
    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(OutboundSms.class).addOrder(Order.desc("date"));
    if (status != null) {
        criteria.add(Restrictions.eq("status", status));
    }
    return criteria.list();
}
Also used : OutboundSms(org.hisp.dhis.sms.outbound.OutboundSms) Criteria(org.hibernate.Criteria) Session(org.hibernate.Session)

Example 3 with OutboundSms

use of org.hisp.dhis.sms.outbound.OutboundSms in project dhis2-core by dhis2.

the class HibernateOutboundSmsStore method get.

@SuppressWarnings("unchecked")
@Override
public List<OutboundSms> get(OutboundSmsStatus status, Integer min, Integer max) {
    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(OutboundSms.class).addOrder(Order.desc("date"));
    if (status != null) {
        criteria.add(Restrictions.eq("status", status));
    }
    if (min != null && max != null) {
        criteria.setFirstResult(min).setMaxResults(max);
    }
    return criteria.list();
}
Also used : OutboundSms(org.hisp.dhis.sms.outbound.OutboundSms) Criteria(org.hibernate.Criteria) Session(org.hibernate.Session)

Example 4 with OutboundSms

use of org.hisp.dhis.sms.outbound.OutboundSms in project dhis2-core by dhis2.

the class ShowSentSMSAction method execute.

// -------------------------------------------------------------------------
// Action Implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    listOutboundSMS = new ArrayList<>();
    if (filterStatusType != null && filterStatusType == 0) {
        total = outboundSmsService.getOutboundSms(OutboundSmsStatus.OUTBOUND).size();
        paging = createPaging(total);
        listOutboundSMS = outboundSmsService.getOutboundSms(OutboundSmsStatus.OUTBOUND, paging.getStartPos(), paging.getPageSize());
    }
    if (filterStatusType != null && filterStatusType == 1) {
        total = outboundSmsService.getOutboundSms(OutboundSmsStatus.SENT).size();
        paging = createPaging(total);
        listOutboundSMS = outboundSmsService.getOutboundSms(OutboundSmsStatus.SENT, paging.getStartPos(), paging.getPageSize());
    }
    if (filterStatusType != null && filterStatusType == 2) {
        total = outboundSmsService.getOutboundSms(OutboundSmsStatus.ERROR).size();
        paging = createPaging(total);
        listOutboundSMS = outboundSmsService.getOutboundSms(OutboundSmsStatus.ERROR, paging.getStartPos(), paging.getPageSize());
    }
    if (filterStatusType != null && filterStatusType == 3 || filterStatusType == null) {
        filterStatusType = 3;
        total = outboundSmsService.getAllOutboundSms().size();
        paging = createPaging(total);
        listOutboundSMS = outboundSmsService.getAllOutboundSms(paging.getStartPos(), paging.getPageSize());
    }
    // Get the name of recipients
    recipientNames = new ArrayList<>();
    recipientNames.add("");
    String tempString;
    for (OutboundSms outboundSms : listOutboundSMS) {
        tempString = "";
        for (String phoneNumber : outboundSms.getRecipients()) {
            Collection<User> users = userService.getUsersByPhoneNumber(phoneNumber);
            if (users == null || users.size() == 0) {
                tempString += "[unknown]";
            } else if (users.size() > 0) {
                Iterator<User> usersIterator = users.iterator();
                while (usersIterator.hasNext()) {
                    User user = usersIterator.next();
                    tempString += "[" + user.getUsername() + "]";
                }
            }
        }
        recipientNames.add(tempString);
    }
    return SUCCESS;
}
Also used : User(org.hisp.dhis.user.User) Iterator(java.util.Iterator) OutboundSms(org.hisp.dhis.sms.outbound.OutboundSms)

Example 5 with OutboundSms

use of org.hisp.dhis.sms.outbound.OutboundSms in project dhis2-core by dhis2.

the class SmsController method sendSMSMessage.

@PreAuthorize("hasRole('ALL') or hasRole('F_MOBILE_SENDSMS')")
@RequestMapping(value = "/outbound", method = RequestMethod.POST, consumes = "application/json")
public void sendSMSMessage(HttpServletResponse response, HttpServletRequest request) throws WebMessageException, IOException {
    OutboundSms sms = renderService.fromJson(request.getInputStream(), OutboundSms.class);
    OutboundMessageResponse status = smsSender.sendMessage(null, sms.getMessage(), sms.getRecipients());
    if (status.isOk()) {
        webMessageService.send(WebMessageUtils.ok("SMS sent"), response, request);
    } else {
        throw new WebMessageException(WebMessageUtils.error(status.getDescription()));
    }
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) OutboundSms(org.hisp.dhis.sms.outbound.OutboundSms) OutboundMessageResponse(org.hisp.dhis.outboundmessage.OutboundMessageResponse) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

OutboundSms (org.hisp.dhis.sms.outbound.OutboundSms)7 Criteria (org.hibernate.Criteria)3 Session (org.hibernate.Session)3 Date (java.util.Date)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)1 OutboundMessageResponse (org.hisp.dhis.outboundmessage.OutboundMessageResponse)1 User (org.hisp.dhis.user.User)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1