Example usage for org.apache.commons.collections.list SetUniqueList addAll

List of usage examples for org.apache.commons.collections.list SetUniqueList addAll

Introduction

In this page you can find the example usage for org.apache.commons.collections.list SetUniqueList addAll.

Prototype

public boolean addAll(Collection coll) 

Source Link

Document

Adds an element to the end of the list if it is not already present.

Usage

From source file:ke.co.tawi.babblesms.server.servlet.sms.send.SendSMS.java

/**
 * Method to handle form processing/*from   ww w  .  j av  a2 s. c  o m*/
 * 
 * @param request
 * @param response
 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 * 
 * @throws IOException
 */
@SuppressWarnings("unchecked")
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

    // Respond as soon as possible to the client request
    HttpSession session = request.getSession(true);
    session.setAttribute(SessionConstants.SENT_SUCCESS, "success");
    response.sendRedirect("sendsms.jsp");

    // Get the relevant account      
    Account account = new Account();

    String username = request.getParameter("username");

    Element element;
    if ((element = accountsCache.get(username)) != null) {
        account = (Account) element.getObjectValue();
    }

    TawiGateway smsGateway = gatewayDAO.get(account);

    // Retrieve the web parameters
    String[] groupselected = request.getParameterValues("groupselected");
    String[] phones = request.getParameterValues("phones");
    String source = request.getParameter("source");
    String message = request.getParameter("message");

    // Deal with the case where one or more Groups has been selected
    // Get phones of all the Contacts in the Group(s)
    SetUniqueList phoneList = SetUniqueList.decorate(new LinkedList<Phone>()); // Does not allow duplicates 

    Group group;
    List<Contact> contactList;
    List<OutgoingGrouplog> outgoingGroupList = new LinkedList<>();

    if (groupselected != null) {
        OutgoingGrouplog groupLog;

        for (String groupUuid : groupselected) {
            group = new Group();
            group.setUuid(groupUuid);
            contactList = ctgrpDAO.getContacts(group);

            for (Contact contact : contactList) {
                phoneList.addAll(phoneDAO.getPhones(contact));
            }

            // Save an outgoing log for the group 
            groupLog = new OutgoingGrouplog();
            groupLog.setMessagestatusUuid(MsgStatus.SENT);
            groupLog.setSender(account.getUuid());
            groupLog.setDestination(group.getUuid());
            groupLog.setMessage(message);
            outgoingGroupList.add(groupLog);

        } // end 'for(String groupUuid : groupselected)'
    } // end 'if(groupselected != null)'

    // This is the case where individual Contacts may have been selected      
    if (phones == null) {
        phones = new String[0];
    }
    phones = StringUtil.removeDuplicates(phones);

    for (String phone : phones) {
        phoneList.add(phoneDAO.getPhone(phone));
    }

    // Determine whether a shortcode or mask is the source
    SMSSource smsSource;
    Shortcode shortcode = shortcodeDAO.get(source);
    Mask mask = null;
    if (shortcode == null) {
        mask = maskDAO.get(source);
        smsSource = mask;

    } else {
        smsSource = shortcode;
    }

    // Set the network in the groups (if any) and save the log
    for (OutgoingGrouplog log : outgoingGroupList) {
        log.setNetworkUuid(smsSource.getNetworkuuid());
        log.setOrigin(smsSource.getUuid());
        groupLogDAO.put(log);
    }

    // Filter the phones to the Network of the source (mask or short code)
    List<Phone> validPhoneList = new LinkedList<>();
    validPhoneList.addAll(
            CollectionUtils.select(phoneList, new PhonesByNetworkPredicate(smsSource.getNetworkuuid())));

    // Break down the phone list to go out to manageable sizes, each sublist
    // being sent to the SMS Gateway in one URL POST
    List<List<Phone>> phonePartition = ListPartitioner.partition(validPhoneList, 10);

    // Send the lists one by one
    PostSMS postThread;

    for (List<Phone> list : phonePartition) {
        postThread = new PostSMS(smsGateway, list, smsSource, message, account, true);

        postThread.start();
    }

    // Deduct credit
    smsBalanceDAO.deductBalance(account, smsSource, validPhoneList.size());
}