public void synchronizePage(long folderId, int pageNumber, int messagesPerPage)
      throws PortalException, SystemException {

    long[] remoteMessageIds = _imapAccessor.getMessageUIDs(folderId, pageNumber, messagesPerPage);

    List<Long> missingRemoteMessageIdsList = new ArrayList<Long>();

    for (int i = 0; i < remoteMessageIds.length; i++) {
      long remoteMessageId = remoteMessageIds[i];

      try {
        MessageLocalServiceUtil.getMessage(folderId, remoteMessageId);
      } catch (NoSuchMessageException nsme) {
        missingRemoteMessageIdsList.add(remoteMessageId);
      }
    }

    if (!missingRemoteMessageIdsList.isEmpty()) {
      long[] missingRemoteMessageIds =
          ArrayUtil.toArray(
              missingRemoteMessageIdsList.toArray(new Long[missingRemoteMessageIdsList.size()]));

      _imapAccessor.storeEnvelopes(folderId, missingRemoteMessageIds);
    }
  }
	protected void initActionableDynamicQuery(
		ActionableDynamicQuery actionableDynamicQuery) {
		actionableDynamicQuery.setBaseLocalService(com.liferay.mail.service.MessageLocalServiceUtil.getService());
		actionableDynamicQuery.setClass(Message.class);
		actionableDynamicQuery.setClassLoader(getClassLoader());

		actionableDynamicQuery.setPrimaryKeyPropertyName("messageId");
	}
	@Override
	public ActionableDynamicQuery getActionableDynamicQuery() {
		ActionableDynamicQuery actionableDynamicQuery = new DefaultActionableDynamicQuery();

		actionableDynamicQuery.setBaseLocalService(com.liferay.mail.service.MessageLocalServiceUtil.getService());
		actionableDynamicQuery.setClass(Message.class);
		actionableDynamicQuery.setClassLoader(getClassLoader());

		actionableDynamicQuery.setPrimaryKeyPropertyName("messageId");

		return actionableDynamicQuery;
	}
  public void sendMessage(long accountId, long messageId) throws PortalException, SystemException {

    Account account = AccountLocalServiceUtil.getAccount(accountId);

    Message message = MessageLocalServiceUtil.getMessage(messageId);

    Address[] toAddresses = parseAddresses(message.getTo());
    Address[] ccAddresses = parseAddresses(message.getCc());
    Address[] bccAddresses = parseAddresses(message.getBcc());

    if ((toAddresses.length == 0) && (ccAddresses.length == 0) && (bccAddresses.length == 0)) {

      throw new MailException(MailException.MESSAGE_HAS_NO_RECIPIENTS);
    }

    List<Attachment> attachments = AttachmentLocalServiceUtil.getAttachments(messageId);

    List<MailFile> mailFiles = new ArrayList<MailFile>();

    for (Attachment attachment : attachments) {
      File file = AttachmentLocalServiceUtil.getFile(attachment.getAttachmentId());

      MailFile mailFile = new MailFile(file, attachment.getFileName(), attachment.getSize());

      mailFiles.add(mailFile);
    }

    _imapAccessor.sendMessage(
        account.getPersonalName(),
        account.getAddress(),
        toAddresses,
        ccAddresses,
        bccAddresses,
        message.getSubject(),
        message.getBody(),
        mailFiles);

    MessageLocalServiceUtil.deleteMessage(messageId);
  }
  public AttachmentHandler getAttachment(long attachmentId)
      throws IOException, PortalException, SystemException {

    Attachment attachment = AttachmentLocalServiceUtil.getAttachment(attachmentId);

    Message message = MessageLocalServiceUtil.getMessage(attachment.getMessageId());

    if (account.getDraftFolderId() == attachment.getFolderId()) {
      return new DefaultAttachmentHandler(
          AttachmentLocalServiceUtil.getInputStream(attachmentId), null);
    } else {
      return _imapAccessor.getAttachment(
          attachment.getFolderId(), message.getRemoteMessageId(), attachment.getContentPath());
    }
  }
  public void synchronizeMessage(long messageId) throws PortalException, SystemException {

    Message message = MessageLocalServiceUtil.getMessage(messageId);

    long remoteMessageId = message.getRemoteMessageId();

    if (remoteMessageId == 0) {
      return;
    }

    try {
      _imapAccessor.storeContents(message.getFolderId(), new long[] {message.getRemoteMessageId()});
    } catch (IOException ioe) {
      throw new MailException(ioe);
    }
  }
  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 moveMessages(long folderId, long[] messageIds)
      throws PortalException, SystemException {

    for (long messageId : messageIds) {
      Message message = MessageLocalServiceUtil.getMessage(messageId);

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

      long sourceFolderId = message.getFolderId();

      if ((account.getDraftFolderId() == sourceFolderId)
          || (account.getSentFolderId() == sourceFolderId)) {

        throw new MailException(MailException.FOLDER_INVALID_DESTINATION);
      }

      _imapAccessor.moveMessages(sourceFolderId, folderId, new long[] {messageId}, 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);
  }
  public Object invokeMethod(String name, String[] parameterTypes, Object[] arguments)
      throws Throwable {
    if (_methodName0.equals(name) && Arrays.deepEquals(_methodParameterTypes0, parameterTypes)) {
      return MessageLocalServiceUtil.addMessage((com.liferay.mail.model.Message) arguments[0]);
    }

    if (_methodName1.equals(name) && Arrays.deepEquals(_methodParameterTypes1, parameterTypes)) {
      return MessageLocalServiceUtil.createMessage(((Long) arguments[0]).longValue());
    }

    if (_methodName2.equals(name) && Arrays.deepEquals(_methodParameterTypes2, parameterTypes)) {
      return MessageLocalServiceUtil.deleteMessage(((Long) arguments[0]).longValue());
    }

    if (_methodName3.equals(name) && Arrays.deepEquals(_methodParameterTypes3, parameterTypes)) {
      return MessageLocalServiceUtil.deleteMessage((com.liferay.mail.model.Message) arguments[0]);
    }

    if (_methodName4.equals(name) && Arrays.deepEquals(_methodParameterTypes4, parameterTypes)) {
      return MessageLocalServiceUtil.dynamicQuery();
    }

    if (_methodName5.equals(name) && Arrays.deepEquals(_methodParameterTypes5, parameterTypes)) {
      return MessageLocalServiceUtil.dynamicQuery(
          (com.liferay.portal.kernel.dao.orm.DynamicQuery) arguments[0]);
    }

    if (_methodName6.equals(name) && Arrays.deepEquals(_methodParameterTypes6, parameterTypes)) {
      return MessageLocalServiceUtil.dynamicQuery(
          (com.liferay.portal.kernel.dao.orm.DynamicQuery) arguments[0],
          ((Integer) arguments[1]).intValue(),
          ((Integer) arguments[2]).intValue());
    }

    if (_methodName7.equals(name) && Arrays.deepEquals(_methodParameterTypes7, parameterTypes)) {
      return MessageLocalServiceUtil.dynamicQuery(
          (com.liferay.portal.kernel.dao.orm.DynamicQuery) arguments[0],
          ((Integer) arguments[1]).intValue(),
          ((Integer) arguments[2]).intValue(),
          (com.liferay.portal.kernel.util.OrderByComparator) arguments[3]);
    }

    if (_methodName8.equals(name) && Arrays.deepEquals(_methodParameterTypes8, parameterTypes)) {
      return MessageLocalServiceUtil.dynamicQueryCount(
          (com.liferay.portal.kernel.dao.orm.DynamicQuery) arguments[0]);
    }

    if (_methodName9.equals(name) && Arrays.deepEquals(_methodParameterTypes9, parameterTypes)) {
      return MessageLocalServiceUtil.fetchMessage(((Long) arguments[0]).longValue());
    }

    if (_methodName10.equals(name) && Arrays.deepEquals(_methodParameterTypes10, parameterTypes)) {
      return MessageLocalServiceUtil.getMessage(((Long) arguments[0]).longValue());
    }

    if (_methodName11.equals(name) && Arrays.deepEquals(_methodParameterTypes11, parameterTypes)) {
      return MessageLocalServiceUtil.getPersistedModel((java.io.Serializable) arguments[0]);
    }

    if (_methodName12.equals(name) && Arrays.deepEquals(_methodParameterTypes12, parameterTypes)) {
      return MessageLocalServiceUtil.getMessages(
          ((Integer) arguments[0]).intValue(), ((Integer) arguments[1]).intValue());
    }

    if (_methodName13.equals(name) && Arrays.deepEquals(_methodParameterTypes13, parameterTypes)) {
      return MessageLocalServiceUtil.getMessagesCount();
    }

    if (_methodName14.equals(name) && Arrays.deepEquals(_methodParameterTypes14, parameterTypes)) {
      return MessageLocalServiceUtil.updateMessage((com.liferay.mail.model.Message) arguments[0]);
    }

    if (_methodName43.equals(name) && Arrays.deepEquals(_methodParameterTypes43, parameterTypes)) {
      return MessageLocalServiceUtil.getBeanIdentifier();
    }

    if (_methodName44.equals(name) && Arrays.deepEquals(_methodParameterTypes44, parameterTypes)) {
      MessageLocalServiceUtil.setBeanIdentifier((java.lang.String) arguments[0]);

      return null;
    }

    if (_methodName49.equals(name) && Arrays.deepEquals(_methodParameterTypes49, parameterTypes)) {
      return MessageLocalServiceUtil.addMessage(
          ((Long) arguments[0]).longValue(),
          ((Long) arguments[1]).longValue(),
          (java.lang.String) arguments[2],
          (java.lang.String) arguments[3],
          (java.lang.String) arguments[4],
          (java.lang.String) arguments[5],
          (java.util.Date) arguments[6],
          (java.lang.String) arguments[7],
          (java.lang.String) arguments[8],
          (java.lang.String) arguments[9],
          ((Long) arguments[10]).longValue());
    }

    if (_methodName50.equals(name) && Arrays.deepEquals(_methodParameterTypes50, parameterTypes)) {
      return MessageLocalServiceUtil.deleteMessage(((Long) arguments[0]).longValue());
    }

    if (_methodName51.equals(name) && Arrays.deepEquals(_methodParameterTypes51, parameterTypes)) {
      return MessageLocalServiceUtil.deleteMessage((com.liferay.mail.model.Message) arguments[0]);
    }

    if (_methodName52.equals(name) && Arrays.deepEquals(_methodParameterTypes52, parameterTypes)) {
      MessageLocalServiceUtil.deleteMessages(((Long) arguments[0]).longValue());

      return null;
    }

    if (_methodName53.equals(name) && Arrays.deepEquals(_methodParameterTypes53, parameterTypes)) {
      return MessageLocalServiceUtil.getAccountUnreadMessagesCount(
          ((Long) arguments[0]).longValue());
    }

    if (_methodName54.equals(name) && Arrays.deepEquals(_methodParameterTypes54, parameterTypes)) {
      return MessageLocalServiceUtil.getCompanyMessages(
          ((Long) arguments[0]).longValue(),
          ((Integer) arguments[1]).intValue(),
          ((Integer) arguments[2]).intValue());
    }

    if (_methodName55.equals(name) && Arrays.deepEquals(_methodParameterTypes55, parameterTypes)) {
      return MessageLocalServiceUtil.getCompanyMessagesCount(((Long) arguments[0]).longValue());
    }

    if (_methodName56.equals(name) && Arrays.deepEquals(_methodParameterTypes56, parameterTypes)) {
      return MessageLocalServiceUtil.getFolderMessages(((Long) arguments[0]).longValue());
    }

    if (_methodName57.equals(name) && Arrays.deepEquals(_methodParameterTypes57, parameterTypes)) {
      return MessageLocalServiceUtil.getFolderMessagesCount(((Long) arguments[0]).longValue());
    }

    if (_methodName58.equals(name) && Arrays.deepEquals(_methodParameterTypes58, parameterTypes)) {
      return MessageLocalServiceUtil.getFolderUnreadMessagesCount(
          ((Long) arguments[0]).longValue());
    }

    if (_methodName59.equals(name) && Arrays.deepEquals(_methodParameterTypes59, parameterTypes)) {
      return MessageLocalServiceUtil.getMessage(
          ((Long) arguments[0]).longValue(), ((Long) arguments[1]).longValue());
    }

    if (_methodName60.equals(name) && Arrays.deepEquals(_methodParameterTypes60, parameterTypes)) {
      return MessageLocalServiceUtil.getRemoteMessage(
          ((Long) arguments[0]).longValue(), ((Boolean) arguments[1]).booleanValue());
    }

    if (_methodName61.equals(name) && Arrays.deepEquals(_methodParameterTypes61, parameterTypes)) {
      return MessageLocalServiceUtil.populateMessages(
          (java.util.List<com.liferay.mail.model.Message>) arguments[0],
          ((Long) arguments[1]).longValue(),
          (java.lang.String) arguments[2],
          ((Integer) arguments[3]).intValue(),
          ((Integer) arguments[4]).intValue(),
          (java.lang.String) arguments[5],
          (java.lang.String) arguments[6]);
    }

    if (_methodName62.equals(name) && Arrays.deepEquals(_methodParameterTypes62, parameterTypes)) {
      return MessageLocalServiceUtil.updateContent(
          ((Long) arguments[0]).longValue(),
          (java.lang.String) arguments[1],
          (java.lang.String) arguments[2]);
    }

    if (_methodName63.equals(name) && Arrays.deepEquals(_methodParameterTypes63, parameterTypes)) {
      return MessageLocalServiceUtil.updateFlag(
          ((Long) arguments[0]).longValue(),
          ((Integer) arguments[1]).intValue(),
          ((Boolean) arguments[2]).booleanValue());
    }

    if (_methodName64.equals(name) && Arrays.deepEquals(_methodParameterTypes64, parameterTypes)) {
      return MessageLocalServiceUtil.updateMessage(
          ((Long) arguments[0]).longValue(),
          ((Long) arguments[1]).longValue(),
          (java.lang.String) arguments[2],
          (java.lang.String) arguments[3],
          (java.lang.String) arguments[4],
          (java.lang.String) arguments[5],
          (java.util.Date) arguments[6],
          (java.lang.String) arguments[7],
          (java.lang.String) arguments[8],
          (java.lang.String) arguments[9],
          ((Long) arguments[10]).longValue());
    }

    throw new UnsupportedOperationException();
  }
  public Message saveDraft(
      long accountId,
      long messageId,
      String to,
      String cc,
      String bcc,
      String subject,
      String body,
      List<MailFile> mailFiles)
      throws PortalException, SystemException {

    Account account = AccountLocalServiceUtil.getAccount(accountId);

    StringBundler sb = new StringBundler();

    sb.append(user.getFullName());
    sb.append(" <");
    sb.append(account.getAddress());
    sb.append(StringPool.GREATER_THAN);

    String sender = sb.toString();

    Address[] toAddresses = parseAddresses(to);
    Address[] ccAddresses = parseAddresses(cc);
    Address[] bccAddresses = parseAddresses(bcc);

    if ((toAddresses.length == 0) && (ccAddresses.length == 0) && (bccAddresses.length == 0)) {

      throw new MailException(MailException.MESSAGE_HAS_NO_RECIPIENTS);
    }

    Message message = null;

    if (messageId != 0) {
      message =
          MessageLocalServiceUtil.updateMessage(
              messageId,
              account.getDraftFolderId(),
              sender,
              InternetAddressUtil.toString(toAddresses),
              InternetAddressUtil.toString(ccAddresses),
              InternetAddressUtil.toString(bccAddresses),
              null,
              subject,
              body,
              String.valueOf(MailConstants.FLAG_DRAFT),
              0);
    } else {
      message =
          MessageLocalServiceUtil.addMessage(
              user.getUserId(),
              account.getDraftFolderId(),
              sender,
              to,
              cc,
              bcc,
              null,
              subject,
              body,
              String.valueOf(MailConstants.FLAG_DRAFT),
              0);
    }

    if (mailFiles == null) {
      return message;
    }

    for (MailFile mailFile : mailFiles) {
      AttachmentLocalServiceUtil.addAttachment(
          user.getUserId(),
          message.getMessageId(),
          null,
          mailFile.getFileName(),
          mailFile.getSize(),
          mailFile.getFile());
    }

    return message;
  }
  @Override
  protected void doReindex(String className, long classPK) throws Exception {
    Message message = MessageLocalServiceUtil.getMessage(classPK);

    doReindex(message);
  }