/*
  * NOTE FOR DEVELOPERS:
  *
  * Never modify or reference this class directly. All methods that expect a folder model instance should use the {@link Folder} interface instead.
  */
 @Override
 public void persist() {
   if (this.isNew()) {
     FolderLocalServiceUtil.addFolder(this);
   } else {
     FolderLocalServiceUtil.updateFolder(this);
   }
 }
  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 Folder addFolder(String displayName) throws PortalException, SystemException {

    String[] names = _imapAccessor.addFolder(displayName);

    return FolderLocalServiceUtil.addFolder(
        user.getUserId(), account.getAccountId(), names[0], names[1], 0);
  }
  protected long getFolderId(String type) throws SystemException {
    Locale[] locales = LanguageUtil.getAvailableLocales();

    List<String> words = new ArrayList<String>();

    for (Locale locale : locales) {
      String translation = LanguageUtil.get(locale, type).toLowerCase();

      words.addAll(ListUtil.toList(translation.split(StringPool.SPACE)));
    }

    List<Folder> folders = FolderLocalServiceUtil.getFolders(account.getAccountId());

    for (String word : words) {
      for (Folder folder : folders) {
        String folderName = folder.getDisplayName().toLowerCase();

        if (folderName.contains(word)) {
          return folder.getFolderId();
        }
      }
    }

    return 0;
  }
  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 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 deleteFolder(long folderId) throws PortalException, SystemException {

    if ((account.getDraftFolderId() == folderId)
        || (account.getInboxFolderId() == folderId)
        || (account.getSentFolderId() == folderId)
        || (account.getTrashFolderId() == folderId)) {

      throw new MailException(MailException.FOLDER_REQUIRED);
    }

    _imapAccessor.deleteFolder(folderId);

    FolderLocalServiceUtil.deleteFolder(folderId);
  }
  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 void synchronize() throws PortalException, SystemException {
    if (_log.isDebugEnabled()) {
      _log.debug("Synchronizing all folders for accountId " + account.getAccountId());
    }

    updateFolders();

    List<Folder> folders = FolderLocalServiceUtil.getFolders(account.getAccountId());

    String key = AccountLock.getKey(user.getUserId(), account.getAccountId());

    if (AccountLock.acquireLock(key)) {
      try {
        for (Folder folder : folders) {
          _imapAccessor.storeEnvelopes(folder.getFolderId(), true);
        }
      } finally {
        AccountLock.releaseLock(key);
      }
    }
  }
  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);
  }