bool
   PreSaveLimitationsCheck::CheckLimitations(boost::shared_ptr<Account> account, String &resultDescription)
   {
      if (account->GetVacationMessage().GetLength() > 1000)
      {
         resultDescription = "The auto reply message length exceeds the 1000 character limit.";
         return false;
      }

      if (account->GetAddress().FindOneOf(_T("?*|\\/<>")) >= 0)
      {
         resultDescription = "The characters ?*|\\/<> are not permitted in hMailServer account addresses.";
         return false;
      }


      boost::shared_ptr<Domain> domain = GetDomain(account->GetDomainID());

      if (GetDuplicateExist(domain, TypeAccount, account->GetID(), account->GetAddress()))      
         return DuplicateError(resultDescription);

      if (account->GetID() == 0)
      {
         if (domain->GetMaxNoOfAccountsEnabled() && 
             domain->GetAccounts()->GetCount() >= domain->GetMaxNoOfAccounts())
         {
            resultDescription = "The maximum number of accounts have been created.";
            return false;
         }
      }

      if (domain->GetMaxAccountSize() != 0)
      {
         if (account->GetAccountMaxSize() > domain->GetMaxAccountSize())
         {
            resultDescription = "The account is larger than the maximum account size specified in the domain settings.";
            return false;
         }

         if (account->GetAccountMaxSize() == 0)
         {
            resultDescription = "The domain has a maximum account size set. When this is the case, all accounts in the domains must have an account size set.";
            return false;
         }
      }

      if (domain->GetMaxSizeMB() > 0)
      {

         if (account->GetAccountMaxSize() == 0)
         {
            resultDescription = "The domain has a maximum account size set. When this is the case, all accounts in the domains must have an account size set.";
            return false;
         }

         String sError = "Account could not be saved. The total size of all accounts in the domain would exceed the maximum size for the domain.";
         
         __int64 currentSize = PersistentDomain::GetAllocatedSize(domain);

         if (account->GetID() == 0)
         {
            if (currentSize + account->GetAccountMaxSize() > domain->GetMaxSizeMB())
            {
               resultDescription = sError;
               return false;
            }
         }
         else
         {
            boost::shared_ptr<Account> currentAccountSettings = boost::shared_ptr<Account>(new Account);
            PersistentAccount::ReadObject(currentAccountSettings, account->GetID());

            if (currentSize - currentAccountSettings->GetAccountMaxSize() + account->GetAccountMaxSize() > domain->GetMaxSizeMB())
            {
               resultDescription = sError;
               return false;
            }
         }
      }

      String address = account->GetAddress().ToLower();
      if (address.Find(_T("\\")) >= 0 || address.Find(_T("/")) >= 0)
      {
         resultDescription = "An account name may not contain the characters \\ or /.";
         return false;
      }


      return true;
   }
bool
PersistentAccount::SaveObject(boost::shared_ptr<Account> pAccount, String &sErrorMessage, bool createInbox)
{
    if (!PreSaveLimitationsCheck::CheckLimitations(pAccount, sErrorMessage))
        return false;

    __int64 iID = pAccount->GetID();
    if (iID > 0)
    {
        // First read the domain to see if we've changed its name.
        boost::shared_ptr<Account> tempAccount = boost::shared_ptr<Account>(new Account());
        if (!PersistentAccount::ReadObject(tempAccount, iID))
            return false;

        if (tempAccount->GetAddress().CompareNoCase(pAccount->GetAddress()) != 0)
        {
            // Check if this account contains messages with full path. If so, the user cannot
            // rename it at this point.
            // Disallow change, if not all files are partial in the database.
            if (!PersistentMessage::GetAllMessageFilesArePartialNames())
            {
                sErrorMessage = "As of hMailServer 5.4, partial file names are stored in the database, rather than the full path\r\n"
                                "To be able to rename the account, you must first migrate your database to the new partial paths scheme. To do\r\n"
                                "this, run Data Directory Synchronizer.";
                return false;
            }

            // Name has been changed. Rename all sub objects first.
            NameChanger nameChanger;
            if (!nameChanger.RenameAccount(tempAccount->GetAddress(), pAccount, sErrorMessage))
                return false;

            // Remove the old account from the cache.
            Cache<Account, PersistentAccount>::Instance()->RemoveObject(tempAccount->GetAddress());
        }
    }

    SQLStatement oStatement;
    oStatement.AddColumnInt64("accountdomainid", pAccount->GetDomainID());
    oStatement.AddColumn("accountaddress", pAccount->GetAddress());
    oStatement.AddColumn("accountpassword", pAccount->GetPassword());
    oStatement.AddColumn("accountactive", pAccount->GetActive());
    oStatement.AddColumn("accountisad", pAccount->GetIsAD());
    oStatement.AddColumn("accountaddomain", pAccount->GetADDomain());
    oStatement.AddColumn("accountadusername", pAccount->GetADUsername());
    oStatement.AddColumn("accountmaxsize", pAccount->GetAccountMaxSize());

    oStatement.AddColumn("accountvacationmessageon", pAccount->GetVacationMessageIsOn());
    oStatement.AddColumn("accountvacationmessage", pAccount->GetVacationMessage());
    oStatement.AddColumn("accountvacationsubject", pAccount->GetVacationSubject());
    oStatement.AddColumn("accountvacationexpires", pAccount->GetVacationExpires());
    oStatement.AddColumn("accountvacationexpiredate", pAccount->GetVacationExpiresDate());

    oStatement.AddColumn("accountpwencryption", pAccount->GetPasswordEncryption());
    oStatement.AddColumn("accountadminlevel", pAccount->GetAdminLevel());

    oStatement.AddColumn("accountforwardenabled", pAccount->GetForwardEnabled());
    oStatement.AddColumn("accountforwardaddress", pAccount->GetForwardAddress());
    oStatement.AddColumn("accountforwardkeeporiginal", pAccount->GetForwardKeepOriginal());

    oStatement.AddColumn("accountenablesignature", pAccount->GetEnableSignature());
    oStatement.AddColumn("accountsignatureplaintext", pAccount->GetSignaturePlainText());
    oStatement.AddColumn("accountsignaturehtml", pAccount->GetSignatureHTML());

    oStatement.AddColumn("accountlastlogontime", pAccount->GetLastLogonTime());

    oStatement.AddColumn("accountpersonfirstname", pAccount->GetPersonFirstName());
    oStatement.AddColumn("accountpersonlastname", pAccount->GetPersonLastName());

    oStatement.SetTable("hm_accounts");


    if (pAccount->GetID() == 0)
    {
        oStatement.SetStatementType(SQLStatement::STInsert);
        oStatement.SetIdentityColumn("accountid");
    }
    else
    {
        oStatement.SetStatementType(SQLStatement::STUpdate);

        String sWhere;
        sWhere.Format(_T("accountid = %I64d"), pAccount->GetID());

        oStatement.SetWhereClause(sWhere);

    }

    bool bNewObject = pAccount->GetID() == 0;

    // Save and fetch ID
    __int64 iDBID = 0;
    bool bRetVal = Application::Instance()->GetDBManager()->Execute(oStatement, bNewObject ? &iDBID : 0);
    if (bRetVal && bNewObject)
    {
        pAccount->SetID((long) iDBID);

        if (createInbox)
        {
            if (!CreateInbox(*pAccount))
            {
                PersistentAccount::DeleteObject(pAccount);
            }
        }
    }

    if (!bNewObject)
        Cache<Account, PersistentAccount>::Instance()->RemoveObject(pAccount);

    return bRetVal;
}