{*******************************************************} { } { Responsive Software http://www.responsive.co.nz } { } { Copyright (c) 2003-2006 Responsive Software Limited } { } {*******************************************************} unit CashbooksCacheUnit; interface uses Classes, DatabaseObjects, GeneralUtilities; type TCashbooksCache = class FList : TList; public constructor Create; destructor Destroy; override; function GetCashbook (CashbookId : int64) : TCashbook; procedure DeleteCashbookEntry (CashbookEntryId : int64); procedure AddCashbookEntry (CashbookEntry : TCashbookEntry); procedure SetPeriod (UseBeginPeriod : boolean; BeginPeriodDate : TDateTime; UseEndPeriod : boolean; EndPeriodDate : TDateTime); procedure UpdateCashbooks; end; implementation uses DatabaseManager, Globals; constructor TCashbooksCache.Create; begin FList := TList.Create; end; destructor TCashbooksCache.Destroy; begin DestroyList(FList); end; function TCashbooksCache.GetCashbook (CashbookId : int64) : TCashbook; var i : integer; begin // search list to see if it is already in list for i := 0 to FList.Count - 1 do if TCashbook(FList[i]).Id = CashbookId then begin Result := TCashbook(FList[i]); Exit; end; // if not found in list then load and add it Result := TCashbook(LoadDatabaseObject(TCashbook,CashbookId)); if Result <> nil then begin FList.Add(Result); // always load entries when adding to cache Result.LoadCashbookEntries; // set current period and create entries subset Result.SetPeriod( WorkstationConfiguration.UseBeginPeriod, WorkstationConfiguration.BeginPeriodDate, WorkstationConfiguration.UseEndPeriod, WorkstationConfiguration.EndPeriodDate); Result.LoadCashbookEntriesInPeriod; end; end; procedure TCashbooksCache.DeleteCashbookEntry (CashbookEntryId : int64); var i,j : integer; begin for i := 0 to FList.Count - 1 do begin for j := 0 to TCashbook(FList[i]).CashbookEntries.Count - 1 do begin if TCashbookEntry(TCashbook(FList[i]).CashbookEntries[j]).Id = CashbookEntryId then begin TCashbook(FList[i]).CashbookEntries.Delete(j); // recreate subsetted list TCashbook(FList[i]).LoadCashbookEntriesInPeriod; Exit; end; end; end; end; procedure TCashbooksCache.AddCashbookEntry (CashbookEntry : TCashbookEntry); var i : integer; begin for i := 0 to FList.Count - 1 do begin if TCashbook(FList[i]).Id = CashbookEntry.CashbookId then begin TCashbook(FList[i]).CashbookEntries.Add(CashbookEntry); // sort entries TCashbook(FList[i]).SortCashbookEntries; // recreate subsetted list TCashbook(FList[i]).LoadCashbookEntriesInPeriod; Exit; end; end; // if it wasn't added then free the memory CashbookEntry.Free; end; procedure TCashbooksCache.SetPeriod (UseBeginPeriod : boolean; BeginPeriodDate : TDateTime; UseEndPeriod : boolean; EndPeriodDate : TDateTime); var i : integer; begin for i := 0 to FList.Count - 1 do begin TCashbook(FList[i]).SetPeriod( UseBeginPeriod, BeginPeriodDate, UseEndPeriod, EndPeriodDate); TCashbook(FList[i]).LoadCashbookEntriesInPeriod; end; end; procedure TCashbooksCache.UpdateCashbooks; var i : integer; CashbookId : int64; Cashbook : TCashbook; begin for i := 0 to FList.Count - 1 do begin CashbookId := TCashbook(FList[i]).Id; Cashbook := TCashbook(Globals.Cashbooks.ObjectsById[CashbookId]); if Cashbook <> nil then TCashbook(FList[i]).Assign(Cashbook); end; end; end.