/**
  * the UI cannot delete the entry. it will be marked as deleted. the push listener framework will
  * remove the entry.
  *
  * @param accountID String
  * @return int
  * @throws EntityException
  */
 public int markUserAsDelete(long accountID) throws EntityException {
   int result = 0;
   try {
     MailServerAccount msa = cdao.getUserFromID(accountID);
     if (msa == null) {
       return 0;
     }
     msa.setStatus(RegistryEntryStatus.DELETED);
     msa.setLastUpdate(System.currentTimeMillis());
     result = cdao.markUserAsDelete(msa);
   } catch (DBAccessException ee) {
     throw new EntityException("Error marking account as delete.", ee);
   }
   return result;
 }
  /**
   * check the account on the mail server ok = 0 invalid protocol = 1 invalid username or password =
   * 2 cannection failed = 3
   *
   * @param msa MailServerAccount
   * @param timeoutStore timeout for the store
   * @param timeoutConnection timeout for connection
   * @return true if the user is a mail server account boolean
   * @throws com.funambol.email.exception.EntityException
   */
  public MailServerError guessAccount(
      MailServerAccount msa, int timeoutStore, int timeoutConnection) throws EntityException {

    MailServerError errorCode = MailServerError.ERR_OK;

    try {

      if (msa != null) {

        String login = msa.getMsLogin();
        String password = msa.getMsPassword();
        String protocol = msa.getMailServer().getProtocol();
        String outgoingServer = msa.getMailServer().getOutServer();
        int outgoingPort = msa.getMailServer().getOutPort();
        String incomingServer = msa.getMailServer().getInServer();
        int incomingPort = msa.getMailServer().getInPort();
        boolean isSSLIn = msa.getMailServer().getIsSSLIn();

        IMailServerWrapper mswf = MailServerWrapperFactory.getMailServerWrapper(protocol);

        if (mswf != null) {
          mswf.checkUser(
              outgoingServer,
              Integer.toString(outgoingPort),
              incomingServer,
              Integer.toString(incomingPort),
              login,
              password,
              isSSLIn,
              String.valueOf(timeoutStore),
              String.valueOf(timeoutConnection),
              sslSocketFactory);
        } else {
          errorCode = MailServerError.ERR_INVALID_PROTOCOL;
        }

      } else {
        errorCode = MailServerError.ERR_INVALID_INPUT;
      }

    } catch (EmailAccessException dae) {
      if (dae != null) {
        errorCode = MailServerErrorHandler.exceptionHandler(dae);
      } else {
        errorCode = MailServerError.ERR_GENERIC_MAILSERVER;
      }
    }

    return errorCode;
  }
 /**
  * check if the input mail server account has the correct mail server information (type and
  * description)
  *
  * @param msa Mail server account
  */
 private void checkMailServerInfo(MailServerAccount msa) throws Exception {
   // check serverType
   if (msa.getMailServer().getMailServerType() == null
       || msa.getMailServer().getMailServerType().equals("")
       || msa.getMailServer().getMailServerType().equals("null")) {
     msa.getMailServer().setMailServerType(Def.SERVER_TYPE_OTHER);
   }
   // check description
   if (msa.getMailServer().getDescription() == null
       || msa.getMailServer().getDescription().equals("")
       || msa.getMailServer().getDescription().equals("null")) {
     msa.getMailServer().setDescription(Def.SERVER_TYPE_OTHER);
   }
 }
 /**
  * check if the input mail server account has the correct folder name values
  *
  * @param msa Mail server account
  */
 private void checkFoldersName(MailServerAccount msa) throws Exception {
   // check inbox
   if (msa.getMailServer().getInboxPath() == null
       || msa.getMailServer().getInboxPath().equals("")
       || msa.getMailServer().getInboxPath().equals("null")) {
     msa.getMailServer().setInboxPath(Def.FOLDER_INBOX_ENG);
     msa.getMailServer().setInboxActivation(true);
   }
   // outbox
   if (msa.getMailServer().getOutboxPath() == null
       || msa.getMailServer().getOutboxPath().equals("")
       || msa.getMailServer().getOutboxPath().equals("null")) {
     msa.getMailServer().setOutboxPath(Def.FOLDER_OUTBOX_ENG);
     msa.getMailServer().setOutboxActivation(true);
   }
   // sent
   if (msa.getMailServer().getSentPath() == null
       || msa.getMailServer().getSentPath().equals("")
       || msa.getMailServer().getSentPath().equals("null")) {
     if (msa.getMailServer().getMailServerType().equals(Def.SERVER_TYPE_EXCHANGE)) {
       msa.getMailServer().setSentPath(Def.FOLDER_SENT_ENG_EXC);
     } else {
       msa.getMailServer().setSentPath(Def.FOLDER_SENT_ENG);
     }
     // not enabled by default
   }
   // drafts
   if (msa.getMailServer().getDraftsPath() == null
       || msa.getMailServer().getDraftsPath().equals("")
       || msa.getMailServer().getDraftsPath().equals("null")) {
     msa.getMailServer().setDraftsPath(Def.FOLDER_DRAFTS_ENG);
     // not enabled by default
   }
   // trash
   if (msa.getMailServer().getTrashPath() == null
       || msa.getMailServer().getTrashPath().equals("")
       || msa.getMailServer().getTrashPath().equals("null")) {
     if (msa.getMailServer().getMailServerType().equals(Def.SERVER_TYPE_EXCHANGE)) {
       msa.getMailServer().setTrashPath(Def.FOLDER_TRASH_ENG_EXC);
     } else {
       msa.getMailServer().setTrashPath(Def.FOLDER_TRASH_ENG);
     }
     // not enabled by default
   }
 }
  /**
   * check if the fnbl_email_inbox must be refreshed for the given user
   *
   * @param currMsa
   * @throws com.funambol.email.exception.EntityException
   * @return true if the table must be refreshed
   */
  private boolean needCacheRefreshing(MailServerAccount currMsa) throws EntityException {

    boolean check = false;

    MailServerAccount prevMsa = null;
    String prevMailServerID = null;
    String prevProtocol = null;
    String currMailServerID = null;
    String currProtocol = null;
    try {

      // get all info from DB
      prevMsa = cdao.getUserFromID(currMsa.getId());

      prevMailServerID = prevMsa.getMailServer().getMailServerId();
      prevProtocol = prevMsa.getMailServer().getProtocol();

      currMailServerID = currMsa.getMailServer().getMailServerId();
      currProtocol = currMsa.getMailServer().getProtocol();

      if (currMailServerID != null && prevMailServerID != null) {
        if (!currMailServerID.equals(prevMailServerID)) {
          check = true;
        }
      }

      // same mail server but differt protocol
      if (currProtocol != null && prevProtocol != null) {
        if (!currProtocol.equals(prevProtocol)) {
          check = true;
        }
      }

      // different login or mail address
      String currMsLogin = currMsa.getMsLogin();
      String prevMsLogin = prevMsa.getMsLogin();

      if (currMsLogin != null && prevMsLogin != null) {
        if (!currMsLogin.equals(prevMsLogin)) {
          check = true;
        }
      }

      String currMsAddress = currMsa.getMsAddress();
      String prevMsAddress = prevMsa.getMsAddress();

      if (currMsAddress != null && prevMsAddress != null) {
        if (!currMsAddress.equals(prevMsAddress)) {
          check = true;
        }
      }

    } catch (DBAccessException ee) {
      throw new EntityException(
          "Error checking if the cache for the "
              + "account "
              + currMsa.getId()
              + " must be freshed",
          ee);
    }
    return check;
  }
  /**
   * update user regarding the mail server connection
   *
   * @param msa MailServerAccount
   * @return int
   * @throws EntityException
   */
  public int updateUser(MailServerAccount msa) throws EntityException {

    int result = 0;
    boolean needCacheRefresh = false;

    try {

      if (msa.getMailServer().getMailServerId() == null
          || msa.getMailServer().getMailServerId().equals("")) {

        // this case happens when there is a swith from public
        // to custom in the UI so the cache refresh is required
        needCacheRefresh = true;
        msa.getMailServer().setMailServerId("" + this.idMailServerSpace.next());

      } else {

        //  check if a refresh is required
        needCacheRefresh = needCacheRefreshing(msa);

        //
        // update custom or update public
        // - if custom:
        // leave the input values that the UI sends
        // - if public;
        // get info from DB, replace the input from UI
        // except the folder names settings
        //
        if (msa.getMailServer().getIsPublic()) {

          String msId = msa.getMailServer().getMailServerId();
          MailServer msFromDB = cdao.getPubMailServer(msId);
          msa.getMailServer().setMailServerId(msId);
          msa.getMailServer().setMailServerType(msFromDB.getMailServerType());
          msa.getMailServer().setDescription(msFromDB.getDescription());
          msa.getMailServer().setIsSoftDelete(msFromDB.getIsSoftDelete());
          msa.getMailServer().setIsPublic(msFromDB.getIsPublic());
          msa.getMailServer().setProtocol(msFromDB.getProtocol());
          msa.getMailServer().setOutServer(msFromDB.getOutServer());
          msa.getMailServer().setOutPort(msFromDB.getOutPort());
          msa.getMailServer().setOutAuth(msFromDB.getOutAuth());
          msa.getMailServer().setInServer(msFromDB.getInServer());
          msa.getMailServer().setInPort(msFromDB.getInPort());
          msa.getMailServer().setIsSSLIn(msFromDB.getIsSSLIn());
          msa.getMailServer().setIsSSLOut(msFromDB.getIsSSLOut());
        }
      }

      // set the period (push listener framework)
      msa.setPeriod(Utility.setMillis(msa.getPeriod()));

      // check serverType
      checkMailServerInfo(msa);

      // check folder names
      checkFoldersName(msa);

      msa.setTaskBeanFile(Def.DEFAULT_INBOX_LISTENER_BEAN_FILE);
      msa.setStatus(RegistryEntryStatus.UPDATED);
      msa.setLastUpdate(System.currentTimeMillis());

      // update user
      result = cdao.updateUser(msa);

      // refresh cache if it's required
      if (needCacheRefresh) {
        cdao.clearCache(msa.getUsername());
        // get the principal
        long[] principals = cdao.getPrincipals(msa.getUsername());
        for (long principalID : principals) {
          cdao.clearFolder(msa.getUsername(), principalID);
        }
      }

    } catch (DBIDGeneratorException ee) {
      throw new EntityException("Error handling ID ", ee);
    } catch (EntityException ee) {
      throw new EntityException("Error updating Account", ee);
    } catch (Exception ee) {
      throw new EntityException("Error checking the folders name of the Account", ee);
    }

    return result;
  }
  /**
   * insert user and the mail server configuration
   *
   * @param msa MailServerAccount
   * @return int
   * @throws EntityException
   */
  public int insertUser(MailServerAccount msa) throws EntityException {
    int result = 0;
    try {

      if (msa.getMailServer().getMailServerId() == null
          || msa.getMailServer().getMailServerId().equals("")) {

        msa.getMailServer().setMailServerId("" + this.idMailServerSpace.next());

      } else {

        String msId = msa.getMailServer().getMailServerId();

        if (msa.getMailServer().getIsPublic()) {

          MailServer msFromDB = cdao.getPubMailServer(msId);

          MailServer ms = msa.getMailServer();

          // replace the imput info with the db info
          msa.getMailServer().setMailServerId(msId);
          msa.getMailServer().setMailServerType(msFromDB.getMailServerType());
          msa.getMailServer().setDescription(msFromDB.getDescription());
          msa.getMailServer().setIsSoftDelete(msFromDB.getIsSoftDelete());
          msa.getMailServer().setIsPublic(msFromDB.getIsPublic());
          msa.getMailServer().setProtocol(msFromDB.getProtocol());
          msa.getMailServer().setOutServer(msFromDB.getOutServer());
          msa.getMailServer().setOutPort(msFromDB.getOutPort());
          msa.getMailServer().setOutAuth(msFromDB.getOutAuth());
          msa.getMailServer().setInServer(msFromDB.getInServer());
          msa.getMailServer().setInPort(msFromDB.getInPort());
          msa.getMailServer().setIsSSLIn(msFromDB.getIsSSLIn());
          msa.getMailServer().setIsSSLOut(msFromDB.getIsSSLOut());

          msa.getMailServer()
              .setInboxPath(getValueOrDefault(ms.getInboxPath(), msFromDB.getInboxPath()));

          msa.getMailServer().setInboxActivation(msFromDB.getInboxActivation());

          msa.getMailServer()
              .setOutboxPath(getValueOrDefault(ms.getOutboxPath(), msFromDB.getOutboxPath()));

          msa.getMailServer().setOutboxActivation(msFromDB.getOutboxActivation());

          msa.getMailServer()
              .setSentPath(getValueOrDefault(ms.getSentPath(), msFromDB.getSentPath()));

          msa.getMailServer().setSentActivation(msFromDB.getSentActivation());

          msa.getMailServer()
              .setDraftsPath(getValueOrDefault(ms.getDraftsPath(), msFromDB.getDraftsPath()));

          msa.getMailServer().setDraftsActivation(msFromDB.getDraftsActivation());

          msa.getMailServer()
              .setTrashPath(getValueOrDefault(ms.getTrashPath(), msFromDB.getTrashPath()));

          msa.getMailServer().setTrashActivation(msFromDB.getTrashActivation());
        }
      }

      // from UI we get the minutes but in the DB we save the seconds
      msa.setPeriod(Utility.setMillis(msa.getPeriod()));

      // check serverType
      checkMailServerInfo(msa);

      // check folder names
      checkFoldersName(msa);

      // set the push listener framework properties
      msa.setTaskBeanFile(Def.DEFAULT_INBOX_LISTENER_BEAN_FILE);
      msa.setStatus(RegistryEntryStatus.NEW);
      msa.setLastUpdate(System.currentTimeMillis());

      // insert the account
      result = cdao.insertUser(msa);

    } catch (DBIDGeneratorException ee) {
      throw new EntityException("Error handling ID ", ee);
    } catch (EntityException ee) {
      throw new EntityException("Error addind Account", ee);
    } catch (Exception ee) {
      throw new EntityException("Error checking input values of the Account", ee);
    }

    return result;
  }