/**
  * Deletes the email account with the given id.
  *
  * @param accountID acoount id
  * @throws com.funambol.email.exception.EntityException
  * @return number of deleted rows
  */
 public int deleteAccount(long accountID) throws EntityException {
   int numDeleted = 0;
   try {
     long[] principalIds = cdao.getPrincipals(accountID);
     numDeleted = cdao.deleteUser(accountID, principalIds);
   } catch (DBAccessException e) {
     throw new EntityException("Error deleting account with id " + accountID, e);
   }
   return numDeleted;
 }
  /**
   * 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;
  }