public void renameFolder(long folderId, String displayName)
      throws PortalException, SystemException {

    Folder folder = FolderLocalServiceUtil.getFolder(folderId);

    String[] names = _imapAccessor.renameFolder(folderId, displayName);

    FolderLocalServiceUtil.updateFolder(
        folderId, names[0], names[1], folder.getRemoteMessageCount());
  }
  public void deleteMessages(long folderId, long[] messageIds)
      throws PortalException, SystemException {

    if ((account.getDraftFolderId() != folderId) && (account.getTrashFolderId() != folderId)) {

      Folder trashFolder = FolderLocalServiceUtil.getFolder(account.getTrashFolderId());

      _imapAccessor.moveMessages(folderId, trashFolder.getFolderId(), messageIds, true);
    } else {
      _imapAccessor.deleteMessages(folderId, messageIds);
    }
  }
  public void updateFolders() throws PortalException, SystemException {
    if (_log.isDebugEnabled()) {
      _log.debug("Updating folders");
    }

    List<javax.mail.Folder> jxFolders = _imapAccessor.getFolders();

    for (javax.mail.Folder jxFolder : jxFolders) {
      try {
        FolderLocalServiceUtil.getFolder(account.getAccountId(), jxFolder.getFullName());
      } catch (NoSuchFolderException nsfe) {
        FolderLocalServiceUtil.addFolder(
            user.getUserId(),
            account.getAccountId(),
            jxFolder.getFullName(),
            jxFolder.getName(),
            0);
      }
    }

    long draftFolderId = account.getDraftFolderId();
    long inboxFolderId = account.getInboxFolderId();
    long sentFolderId = account.getSentFolderId();
    long trashFolderId = account.getTrashFolderId();

    if (draftFolderId <= 0) {
      draftFolderId = getFolderId("draft");
    }

    if (inboxFolderId <= 0) {
      inboxFolderId = getFolderId("inbox");
    }

    if (sentFolderId <= 0) {
      sentFolderId = getFolderId("sent");
    }

    if (sentFolderId <= 0) {
      sentFolderId = getFolderId("sent-mail");
    }

    if (trashFolderId <= 0) {
      trashFolderId = getFolderId("trash");
    }

    updateFolders(inboxFolderId, draftFolderId, sentFolderId, trashFolderId);
  }
  public void updateFlags(long folderId, long[] messageIds, int flag, boolean value)
      throws PortalException, SystemException {

    Folder folder = FolderLocalServiceUtil.getFolder(folderId);

    Account account = AccountLocalServiceUtil.getAccount(folder.getAccountId());

    for (long messageId : messageIds) {
      MessageLocalServiceUtil.updateFlag(messageId, flag, value);
    }

    if (account.getDraftFolderId() == folderId) {
      _imapAccessor.updateFlags(folderId, messageIds, flag, value, false);
    } else {
      _imapAccessor.updateFlags(folderId, messageIds, flag, value, true);
    }
  }
  public MessagesDisplay getMessagesDisplay(
      long folderId,
      String keywords,
      int pageNumber,
      int messagesPerPage,
      String orderByField,
      String orderByType)
      throws PortalException, SystemException {

    if (orderByField.equals(MailConstants.ORDER_BY_ADDRESS)) {
      orderByField = "sender";

      if (account.getSentFolderId() == folderId) {
        orderByField = "to";
      }
    } else if (!orderByField.equals(MailConstants.ORDER_BY_SENT_DATE)
        && !orderByField.equals(MailConstants.ORDER_BY_SIZE)
        && !orderByField.equals(MailConstants.ORDER_BY_SUBJECT)) {

      orderByField = MailConstants.ORDER_BY_SENT_DATE;
    }

    List<Message> messages = new ArrayList<Message>(messagesPerPage);

    int messageCount =
        MessageLocalServiceUtil.populateMessages(
            messages, folderId, keywords, pageNumber, messagesPerPage, orderByField, orderByType);

    if (Validator.isNull(keywords) && (account.getDraftFolderId() != folderId)) {

      Folder folder = FolderLocalServiceUtil.getFolder(folderId);

      messageCount = folder.getRemoteMessageCount();
    }

    return new MessagesDisplay(messages, pageNumber, messagesPerPage, messageCount);
  }