{*******************************************************} { } { Responsive Software http://www.responsive.co.nz } { } { Copyright (c) 2003-2006 Responsive Software Limited } { } {*******************************************************} unit AccountsCacheUnit; interface uses Classes, DatabaseObjects, GeneralUtilities; type TAccountsCache = class FList : TList; public constructor Create; destructor Destroy; override; procedure Clear; function GetAccount (AccountId : int64) : TAccount; procedure DeleteCombinedEntry (CombinedEntryId : int64); procedure AddEntries (Entries : TDatabaseObjectCollection); procedure SetPeriod (UseBeginPeriod : boolean; BeginPeriodDate : TDateTime; UseEndPeriod : boolean; EndPeriodDate : TDateTime); procedure UpdateAccounts; end; implementation uses DatabaseManager, Globals; constructor TAccountsCache.Create; begin FList := TList.Create; end; destructor TAccountsCache.Destroy; begin DestroyList(FList); end; procedure TAccountsCache.Clear; begin ClearList(FList); end; function TAccountsCache.GetAccount (AccountId : int64) : TAccount; var i : integer; begin // search list to see if it is already in list for i := 0 to FList.Count - 1 do if TAccount(FList[i]).Id = AccountId then begin Result := TAccount(FList[i]); Exit; end; // if not found in list then load and add it Result := TAccount(LoadDatabaseObject(TAccount,AccountId)); if Result <> nil then begin FList.Add(Result); // always load entries when adding to cache Result.LoadEntries; // set current period and create entries subset Result.SetPeriod( WorkstationConfiguration.UseBeginPeriod, WorkstationConfiguration.BeginPeriodDate, WorkstationConfiguration.UseEndPeriod, WorkstationConfiguration.EndPeriodDate); Result.LoadEntriesInPeriod; end; end; procedure TAccountsCache.DeleteCombinedEntry (CombinedEntryId : int64); var i,j : integer; Changed : boolean; ChangedAccounts : TList; begin // create a temporary list of accounts which have been changed ChangedAccounts := TList.Create; // go to each account and delete each entry which // has this combined entry id for i := 0 to FList.Count - 1 do begin Changed := false; // start at the last entry and work back because // we will be deleting as we go for j := TAccount(FList[i]).Entries.Count - 1 downto 0 do begin if TEntry(TAccount(FList[i]).Entries[j]).CombinedEntryId = CombinedEntryId then begin TAccount(FList[i]).Entries.Delete(j); Changed := true; end; end; // add the account to the list of changed accounts if Changed then ChangedAccounts.Add(FList[i]); end; // finally for the accounts that have changed // recreate the entries in the subset collection for i := 0 to ChangedAccounts.Count - 1 do TAccount(ChangedAccounts[i]).LoadEntriesInPeriod; // destroy the temporary list ChangedAccounts.Free; end; procedure TAccountsCache.AddEntries (Entries : TDatabaseObjectCollection); var i,j : integer; Entry : TEntry; Changed : boolean; ChangedAccounts : TList; begin // create a temporary list of accounts which have been changed ChangedAccounts := TList.Create; // add each entry if its account is in the cache for i := 0 to Entries.Count - 1 do begin // look for account in cache for j := 0 to FList.Count - 1 do begin Changed := false; if TEntry(Entries[i]).AccountId = TAccount(FList[j]).Id then begin // if account is found then create a copy of the entry and add it Entry := TEntry.Create; Entry.Assign(TEntry(Entries[i])); TAccount(FList[j]).Entries.Add(Entry); Changed := true; end; // add the account to the list of changed accounts if Changed then ChangedAccounts.Add(FList[j]); end; end; // finally sort the accounts that have changed // and also recreate the entries in the subset collection for i := 0 to ChangedAccounts.Count - 1 do begin TAccount(ChangedAccounts[i]).SortEntries; TAccount(ChangedAccounts[i]).LoadEntriesInPeriod; end; // destroy the temporary list ChangedAccounts.Free; end; procedure TAccountsCache.SetPeriod (UseBeginPeriod : boolean; BeginPeriodDate : TDateTime; UseEndPeriod : boolean; EndPeriodDate : TDateTime); var i : integer; begin for i := 0 to FList.Count - 1 do begin TAccount(FList[i]).SetPeriod( UseBeginPeriod, BeginPeriodDate, UseEndPeriod, EndPeriodDate); TAccount(FList[i]).LoadEntriesInPeriod; end; end; procedure TAccountsCache.UpdateAccounts; var i : integer; AccountId : int64; Account : TAccount; begin for i := 0 to FList.Count - 1 do begin AccountId := TAccount(FList[i]).Id; Account := TAccount(Globals.Accounts.ObjectsById[AccountId]); if Account <> nil then TAccount(FList[i]).Assign(Account); end; end; end.