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