public MessageStatus sendBulkCaresMessage(
      String messageId,
      String workerNumber,
      Care[] cares,
      MediaType messageType,
      Date startDate,
      Date endDate) {
    if (workerNumber == null || workerNumber.isEmpty()) {
      return MessageStatus.REJECTED;
    }

    logger.info("Constructing MessageDetails object...");

    MessageFormatter formatter = omiManager.createMessageFormatter();
    MessageRequest messageRequest = coreManager.createMessageRequest();

    String content = formatter.formatBulkCaresMessage(cares);

    messageRequest.setTryNumber(1);
    messageRequest.setRequestId(messageId);
    messageRequest.setDateFrom(startDate);
    messageRequest.setDateTo(endDate);
    messageRequest.setRecipientNumber(workerNumber);
    messageRequest.setMessageType(MessageType.valueOf(messageType.toString()));
    messageRequest.setStatus(MStatus.QUEUED);
    messageRequest.setDateCreated(new Date());

    logger.info("MessageRequest object successfully constructed");
    logger.debug(messageRequest);

    MessageStatus status = scheduleMessage(messageRequest, content);
    return status;
  }
  public MessageStatus scheduleMessage(MessageRequest message, String content) {
    if ((message.getRecipientNumber() == null || message.getRecipientNumber().isEmpty())
        && !ContactNumberType.PUBLIC.toString().equals(message.getPhoneNumberType())) {
      return MessageStatus.REJECTED;
    }

    MessageRequestDAO msgReqDao = coreManager.createMessageRequestDAO();

    msgReqDao.save(message);

    // TODO Check length of message and break if necessary
    logger.info("Constructing GatewayRequest...");
    GatewayRequest gwReq = storeManager.constructMessage(message, null);
    gwReq.setMessage(content);
    gwReq.getGatewayRequestDetails().setMessage(content);

    logger.info("Initializing OMP MessagingService...");
    MessagingService msgSvc = ompManager.createMessagingService();

    logger.info("Scheduling GatewayRequest...");

    msgSvc.scheduleMessage(gwReq);

    logger.info("Updating MessageRequest...");
    message.setDateProcessed(new Date());
    message.setStatus(MStatus.PENDING);
    logger.debug(message);

    msgReqDao.save(message);
    logger.info("Messages sent successfully");
    return MessageStatus.valueOf(message.getStatus().toString());
  }
  public MessageStatus sendMessage(MessageRequest message, String content) {
    if ((message.getRecipientNumber() == null || message.getRecipientNumber().isEmpty())
        && !ContactNumberType.PUBLIC.toString().equals(message.getPhoneNumberType())) {
      return MessageStatus.REJECTED;
    }

    MessageRequestDAO msgReqDao = coreManager.createMessageRequestDAO();

    message.setStatus(MStatus.QUEUED);
    msgReqDao.save(message);

    // TODO Check length of message and break if necessary
    logger.info("Constructing GatewayRequest...");
    GatewayRequest gwReq = storeManager.constructMessage(message, null);
    gwReq.setMessage(content);
    gwReq.getGatewayRequestDetails().setMessage(content);

    logger.info("Initializing OMP MessagingService...");
    MessagingService msgSvc = ompManager.createMessagingService();

    logger.info("Sending GatewayRequest...");
    Map<Boolean, Set<GatewayResponse>> responses = msgSvc.sendMessage(gwReq);

    Boolean falseBool = false;
    if (responses.containsKey(falseBool)) {
      Set<GatewayResponse> resps = responses.get(falseBool);
      for (GatewayResponse gp : resps) {
        statHandler.handleStatus(gp);
      }
    }

    logger.info("Updating MessageRequest...");
    message.setDateProcessed(new Date());
    message.setStatus(MStatus.PENDING);
    logger.debug(message);

    msgReqDao.save(message);
    logger.info("Messages sent successfully");
    return MessageStatus.valueOf(message.getStatus().toString());
  }
  public MessageStatus savePatientMessageRequest(
      String messageId,
      NameValuePair[] personalInfo,
      String patientNumber,
      ContactNumberType patientNumberType,
      String langCode,
      MediaType messageType,
      Long notificationType,
      Date startDate,
      Date endDate,
      String recipientId) {
    logger.debug("Constructing MessageRequest object...");

    if (patientNumberType == ContactNumberType.PUBLIC && messageType == MediaType.TEXT) {
      return MessageStatus.REJECTED;
    }

    if ((patientNumber == null || patientNumber.isEmpty())
        && patientNumberType != ContactNumberType.PUBLIC) {
      return MessageStatus.REJECTED;
    }

    MessageRequest messageRequest = coreManager.createMessageRequest();

    NotificationTypeDAO noteTypeDao = coreManager.createNotificationTypeDAO();
    NotificationType noteType = (NotificationType) noteTypeDao.getById(notificationType);

    Language langObject = coreManager.createLanguageDAO().getByCode(langCode);

    if (personalInfo != null) {
      HashSet<NameValuePair> details = new HashSet<NameValuePair>();
      details.addAll(Arrays.asList(personalInfo));
      messageRequest.setPersInfos(details);
    }

    messageRequest.setTryNumber(1);
    messageRequest.setRequestId(messageId);
    // VOICE messages need to have a start date to accommodate replaying DELIVERED messages
    messageRequest.setDateFrom(
        startDate == null && messageType == MediaType.VOICE ? new Date() : startDate);
    messageRequest.setDateTo(endDate);
    messageRequest.setRecipientNumber(patientNumber);
    messageRequest.setPhoneNumberType(patientNumberType.toString());
    messageRequest.setRecipientId(recipientId);
    messageRequest.setNotificationType(noteType);
    messageRequest.setMessageType(MessageType.valueOf(messageType.toString()));
    messageRequest.setLanguage(langObject);
    messageRequest.setStatus(MStatus.QUEUED);
    messageRequest.setDateCreated(new Date());

    logger.debug("MessageRequest object successfully constructed");
    logger.debug(messageRequest);

    if (messageRequest.getDateFrom() == null && messageRequest.getDateTo() == null) {
      return sendMessage(messageRequest);
    }

    logger.info("Saving MessageRequest...");
    MessageRequestDAO msgReqDao = coreManager.createMessageRequestDAO();

    msgReqDao.save(messageRequest);
    return MessageStatus.valueOf(messageRequest.getStatus().toString());
  }
  public MessageStatus scheduleMessage(String content, String recipient) {
    if (recipient == null || recipient.isEmpty()) {
      return MessageStatus.REJECTED;
    }

    logger.info("Constructing MessageDetails object...");

    MessageRequest messageRequest = coreManager.createMessageRequest();
    messageRequest.setTryNumber(1);
    messageRequest.setRequestId("");
    messageRequest.setDateFrom(null);
    messageRequest.setDateTo(null);
    messageRequest.setRecipientNumber(recipient);
    messageRequest.setNotificationType(null);
    messageRequest.setMessageType(MessageType.TEXT);
    messageRequest.setLanguage(null);
    messageRequest.setStatus(MStatus.QUEUED);
    messageRequest.setDateCreated(new Date());

    logger.info("MessageRequest object successfully constructed");
    logger.debug(messageRequest);

    MessageStatus status = scheduleMessage(messageRequest, content);
    return status;
  }
  public MessageStatus saveCHPSMessageRequest(
      String messageId,
      NameValuePair[] personalInfo,
      String workerNumber,
      Patient[] patientList,
      String langCode,
      MediaType messageType,
      Long notificationType,
      Date startDate,
      Date endDate) {
    logger.info("Constructing MessageDetails object...");

    if (workerNumber == null || workerNumber.isEmpty()) {
      return MessageStatus.REJECTED;
    }

    MessageRequest messageRequest = coreManager.createMessageRequest();

    NotificationTypeDAO noteTypeDao = coreManager.createNotificationTypeDAO();
    NotificationType noteType = (NotificationType) noteTypeDao.getById(notificationType);

    Language langObject = coreManager.createLanguageDAO().getByCode(langCode);

    HashSet<NameValuePair> details = new HashSet<NameValuePair>();
    if (personalInfo != null) {
      details.addAll(Arrays.asList(personalInfo));
    }
    if (patientList != null) {
      for (Patient p : patientList) {
        if (p.getPreferredName() != null) {
          details.add(new NameValuePair("PreferredName", p.getPreferredName()));
        }
        if (p.getLastName() != null) {
          details.add(new NameValuePair("LastName", p.getLastName()));
        }
        if (p.getCommunity() != null) {
          details.add(new NameValuePair("Community", p.getCommunity()));
        }
        if (p.getFirstName() != null) {
          details.add(new NameValuePair("FirstName", p.getFirstName()));
        }
        if (p.getMotechId() != null) {
          details.add(new NameValuePair("MotechId", p.getMotechId()));
        }
        if (p.getPhoneNumber() != null) {
          details.add(new NameValuePair("PhoneNumber", p.getPhoneNumber()));
        }
      }
    }
    messageRequest.setPersInfos(details);

    messageRequest.setTryNumber(1);
    messageRequest.setRequestId(messageId);
    messageRequest.setDateFrom(startDate);
    messageRequest.setDateTo(endDate);
    messageRequest.setRecipientNumber(workerNumber);
    messageRequest.setNotificationType(noteType);
    messageRequest.setMessageType(MessageType.valueOf(messageType.toString()));
    messageRequest.setLanguage(langObject);
    messageRequest.setStatus(MStatus.QUEUED);
    messageRequest.setDateCreated(new Date());

    logger.info("MessageRequest object successfully constructed");
    logger.debug(messageRequest);

    if (messageRequest.getDateFrom() == null && messageRequest.getDateTo() == null) {
      return sendMessage(messageRequest);
    }

    logger.info("Saving MessageRequest...");
    MessageRequestDAO msgReqDao = coreManager.createMessageRequestDAO();

    msgReqDao.save(messageRequest);

    return MessageStatus.valueOf(messageRequest.getStatus().toString());
  }