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