/**
   * @param response
   * @param messageList
   * @throws SOAPRequestException
   */
  private void generateReply(WebServicesResponse response, List messageList, boolean wsi)
      throws SOAPRequestException {
    try {
      SOAPElement listElement = createElement("messageList", NAMESPACE);

      Iterator messagesIterator = messageList.iterator();
      for (int i = 0; messagesIterator.hasNext(); i++) {
        MessageDVO currentMessage = (MessageDVO) messagesIterator.next();

        // Create Message Element and append Value of MessageID and MessageBox
        SOAPElement msgElement = createElement("messageElement", NAMESPACE);
        SOAPElement childElement_MsgId =
            createElement("messageId", NAMESPACE, currentMessage.getMessageId());
        SOAPElement childElement_MsgBox =
            createElement("messageBox", NAMESPACE, currentMessage.getMessageBox());
        msgElement.addChildElement(childElement_MsgId);
        msgElement.addChildElement(childElement_MsgBox);

        listElement.addChildElement(msgElement);
      }

      if (wsi) {
        EbmsProcessor.core.log.debug("WS-I Response");

        SOAPElement responseElement = createElement("ResponseElement", NAMESPACE);
        responseElement.addChildElement(listElement);
        response.setBodies(new SOAPElement[] {responseElement});
      } else {
        EbmsProcessor.core.log.debug("Non WS-I Response");

        response.setBodies(new SOAPElement[] {listElement});
      }
    } catch (Exception e) {
      throw new SOAPRequestException("Unable to generate reply message", e);
    }
  }
  public void serviceRequested(WebServicesRequest request, WebServicesResponse response)
      throws SOAPRequestException, DAOException {

    String msgId = null;
    String msgBox = null;
    String convId = null;
    String cpaId = null;
    String service = null;
    String action = null;
    String status = null;
    String limit = null;

    boolean wsi = false;

    SOAPBodyElement[] bodies = (SOAPBodyElement[]) request.getBodies();
    // WS-I <RequestElement>
    if (bodies != null && bodies.length == 1 && isElement(bodies[0], "RequestElement", NAMESPACE)) {

      EbmsProcessor.core.log.debug("WS-I Request");

      wsi = true;

      SOAPElement[] childElement = getChildElementArray(bodies[0]);
      msgId = getText(childElement, "messageId");
      msgBox = getText(childElement, "messageBox");
      convId = getText(childElement, "conversationId");
      cpaId = getText(childElement, "cpaId");
      service = getText(childElement, "service");
      action = getText(childElement, "action");
      status = getText(childElement, "status");
      limit = getText(childElement, "limit");
    } else {
      EbmsProcessor.core.log.debug("Non WS-I Request");

      msgId = getText(bodies, "messageId");
      msgBox = getText(bodies, "messageBox");
      convId = getText(bodies, "conversationId");
      cpaId = getText(bodies, "cpaId");
      service = getText(bodies, "service");
      action = getText(bodies, "action");
      status = getText(bodies, "status");
      limit = getText(bodies, "limit");
    }

    EbmsProcessor.core.log.info(
        "Message History Query received request - "
            + "MessageID : "
            + (msgId == null ? "NULL" : msgId)
            + ", MessageBox: "
            + (msgBox == null ? "NULL" : msgBox)
            + ", CovID: "
            + (convId == null ? "NULL" : convId)
            + ", CpaID: "
            + (cpaId == null ? "NULL" : cpaId)
            + ", Service: "
            + (service == null ? "NULL" : service)
            + ", Action: "
            + (action == null ? "NULL" : action)
            + ", Status: "
            + (status == null ? "NULL" : status)
            + ", Number of Messages: "
            + (limit == null ? "NULL" : limit));

    int resultLimit = -1;
    try {
      resultLimit = Integer.parseInt(limit);
      if (resultLimit < 1) {
        resultLimit = MAX_NUMBER;
      }
    } catch (NumberFormatException e) {
      resultLimit = MAX_NUMBER;
    }

    try {
      MessageDAO msgDAO = (MessageDAO) EbmsProcessor.core.dao.createDAO(MessageDAO.class);
      MessageDVO criteriaDVO = (MessageDVO) msgDAO.createDVO();
      criteriaDVO.setMessageId(msgId);
      criteriaDVO.setMessageBox(checkMessageBox(msgBox));
      criteriaDVO.setConvId(convId);
      criteriaDVO.setCpaId(cpaId);
      criteriaDVO.setService(service);
      criteriaDVO.setAction(action);
      criteriaDVO.setStatus(checkMessageStatus(status));

      List results = msgDAO.findMessagesByHistory(criteriaDVO, resultLimit, 0);

      generateReply(response, results, wsi);

    } catch (DAOException daoExp) {
      throw new DAOException("Unable to query the repository", daoExp);
    } catch (SOAPRequestException e) {
      throw e;
    }
  }
  /**
   * Attempt to clean the EBMS messages.
   *
   * @param months
   * @return
   * @throws Exception
   * @throws Exception
   * @throws DAOException
   */
  protected Transaction cleanEBMS(int months) throws Exception {
    try {
      hk.hku.cecid.ebms.spa.dao.MessageDAO dao =
          (hk.hku.cecid.ebms.spa.dao.MessageDAO)
              EbmsProcessor.core.dao.createDAO(hk.hku.cecid.ebms.spa.dao.MessageDAO.class);
      InboxDAO inboxDao = (InboxDAO) EbmsProcessor.core.dao.createDAO(InboxDAO.class);
      OutboxDAO outboxDao = (OutboxDAO) EbmsProcessor.core.dao.createDAO(OutboxDAO.class);
      hk.hku.cecid.ebms.spa.dao.RepositoryDAO repDao =
          (hk.hku.cecid.ebms.spa.dao.RepositoryDAO)
              EbmsProcessor.core.dao.createDAO(hk.hku.cecid.ebms.spa.dao.RepositoryDAO.class);

      Transaction tr =
          ((hk.hku.cecid.ebms.spa.dao.MessageDataSourceDAO) dao).getFactory().createTransaction();

      dao.setTransaction(tr);
      inboxDao.setTransaction(tr);
      outboxDao.setTransaction(tr);
      repDao.setTransaction(tr);
      tr.begin();

      List list = dao.findMessagesBeforeTime(months);
      EBMSLogging(Integer.toString(list.size()) + " messages will be removed.");
      EBMSLogging("Initializing...");

      Iterator itr = list.iterator();

      hk.hku.cecid.ebms.spa.dao.MessageDVO dvo;
      InboxDVO inboxDvo;
      OutboxDVO outboxDvo;
      hk.hku.cecid.ebms.spa.dao.RepositoryDVO repDvo;

      while (itr.hasNext()) {
        dvo = (hk.hku.cecid.ebms.spa.dao.MessageDVO) itr.next();
        if (dvo.getMessageBox().equals("inbox")) {
          /** delete the inbox entry */
          inboxDvo = (InboxDVO) inboxDao.createDVO();
          inboxDvo.setMessageId(dvo.getMessageId());
          inboxDao.remove(inboxDvo);
        } else if (dvo.getMessageBox().equals("outbox")) {
          /** delete the outbox entry */
          outboxDvo = (OutboxDVO) outboxDao.createDVO();
          outboxDvo.setMessageId(dvo.getMessageId());
          outboxDao.remove(outboxDvo);
        } else {
          EBMSError("Unknown value in MessageBox relation.");
          throw new Exception("Error, unknown value in MessageBox relation.");
        }
        /** remove entry in repository */
        repDvo = (hk.hku.cecid.ebms.spa.dao.RepositoryDVO) repDao.createDVO();
        repDvo.setMessageId(dvo.getMessageId());
        repDvo.setMessageBox(dvo.getMessageBox());
        repDao.remove(repDvo);
        /** finally remove from message table */
        dao.remove(dvo);
      }
      return tr;
    } catch (DAOException e) {
      EBMSError("Error encountered while cleaning.");
      throw new Exception("Error encountered while cleaning EBmS.", e);
    }
  }