Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Unit contains logic for checking if currently loaded studies need updating.
- /// If a serie in specific study is modified in some way then it will be refreshed
- /// based on DB.
- /// </summary>
- unit pm.View.DB.SeriesUpdateFetcher;
- interface
- uses
- pmViewImageStorage,
- pm.DB.ThreadInstances,
- IBX.IBCustomDataSet,
- System.Generics.Collections,
- pmDICOMTypes;
- type
- TPmSeriesUpdateEvent = procedure(
- ASender : TObject;
- const AStudyID, ASeriesIDs: TList<Int64>) of object;
- TPmSerieUpdateChecker = class
- private
- FOnSerieNeedsUpdating: TPmSeriesUpdateEvent;
- function FetchStudyDBData: TIBDataSet;
- function NeedsSerieUpdatingOrAdding(
- const AStudy : TPmStudyNode;
- var ASeriesToUpdate: TList<Int64>): Boolean;
- public
- property OnSerieNeedsUpdate: TPmSeriesUpdateEvent read FOnSerieNeedsUpdating write FOnSerieNeedsUpdating;
- /// <summary>
- /// Downloads series updates fo studies loaded into pmview
- /// </summary>
- /// <returns>Number of series that need update.</returns>
- function DownloadUpdates: Integer;
- end;
- implementation
- uses
- System.SysUtils,
- daDB;
- { TPmSerieUpdateChecker }
- function TPmSerieUpdateChecker.DownloadUpdates: Integer;
- var
- LNavi : TPmStudyTreeNavigator;
- LDBStudyData: TIBDataSet;
- LImgType : TPmImageType;
- LStudyDBDiff: TPmDCMStudyDB;
- LI : Integer;
- begin
- try
- LStudyDBDiff := TPmDCMStudyDB.Create(nil);
- LNavi := TPmStudyTreeNavigator.Create();
- // exit and clear, if no data is available
- if (LNavi.ImageIndex < 0) then
- begin
- Exit(0);
- end;
- LStudyDBDiff.DB := GetdaserverThreadDB();
- // Check for updates
- LDBStudyData := FetchStudyDBData;
- if not Assigned(LDBStudyData) then
- Exit(0);
- repeat
- // match DB record with current Study node in navtree
- { TODO : DBID needs to be added to TpmStudyTreeNAvigator.Study class, StudyUID seems to be not unique enough }
- if LDBStudyData.Locate('id', LNavi.Study.DBID) then
- begin
- { TODO : LastTransfer needs to be added to TpmStudyTreeNAvigator.Study class }
- // process further only if lasttransferdate is not equal with currently loaded data
- if LNavi.Study.LastTransferDate < LDBStudyData.FieldByName('LastTransfer').AsDateTime then
- begin
- // load study object form making diff for update process
- LStudyDBDiff.Load(LDBStudyData.FieldByName('id').AsInteger, True);
- repeat
- { TODO : DBID needs to be added }
- if LStudyDBDiff.Series.SerieById[LNavi.Series.DBID] <> nil then
- begin
- if LNavi.Series.ImageCount <> LStudyDBDiff.Series.SerieById[LNavi.Series.DBID].Images.Count then
- begin
- repeat
- if LStudyDBDiff.Series.SerieById[LNavi.Series.DBID].Images.ImageById[LNavi.Image.DBID] <> nil then
- begin
- LStudyDBDiff.Series.SerieById[LNavi.Series.DBID].Images.Delete
- (LStudyDBDiff.Series.SerieById[LNavi.Series.DBID].Images.ImageById[LNavi.Image.DBID].Index);
- end;
- until LNavi.NextImage;
- end
- else
- begin
- LStudyDBDiff.Series.Delete(LStudyDBDiff.Series.SerieById[LNavi.Series.DBID].Index);
- end;
- end;
- until LNavi.NextSeries;
- end;
- if LStudyDBDiff.StudyId <> 0 then
- if Assigned(OnSerieNeedsUpdate) then
- begin
- // call event and pass study diff object to create download task
- // every study frequency was choosen for better ux
- OnSerieNeedsUpdate(Self, LStudyDBDiff);
- end;
- end;
- // next study
- until (not LNavi.NextStudy);
- finally
- FreeAndNil(LNavi);
- end;
- end;
- function TPmSerieUpdateChecker.FetchStudyDBData: TIBDataSet;
- var
- LStudyIDSet: String;
- LPatNode : TPmPatientNode;
- LStudyNode : TPmStudyNode;
- begin
- // list all DaPatIDs e.g. = 15,34,12,204
- for LPatNode in TPmImageStorage.Patients do
- for LStudyNode in LPatNode.Studies do
- if LStudyNode.DBID >= 0 then { TODO : DBID needs to be introduced }
- if LStudyIDSet = '' then
- LStudyIDSet := IntToStr(LStudyNode.DBID)
- else
- LStudyIDSet := LStudyIDSet + ',' + IntToStr(LStudyNode.DBID);
- // no patients to query?? bye bye
- if LStudyIDSet = '' then
- Exit(nil);
- // StudyID, StudyDate, StudyDescription, LastTransfer
- // - example query
- // Select std.id, std.dapatid, std.modality, std.studydescription, std.lasttransfer from STUDY std where std.dapatid in (1,3,5) and std.deltag = 'f' order by std.dapatid asc, std.studydate desc
- Result := daOpenDataSet(GetdaserverThreadDB(), 'STUDY std', ['std.id', 'std.dapatid', 'std.kontoid', 'std.modality',
- 'std.studydescription', 'std.studyinstanceuid', 'std.studydate', 'std.lasttransfer'],
- 'std.id in (' + LStudyIDSet + ') and std.deltag = ''f'' order by std.dapatid asc, std.studydate desc');
- // fetch all results at once
- if Result <> nil then
- Result.FetchAll;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement