/**
   * @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);
    }
  }
  /**
   * 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);
    }
  }