Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.19 KB | None | 0 0
  1. /// <summary>
  2. /// Unit contains logic for checking if currently loaded studies need updating.
  3. /// If a serie in specific study is modified in some way then it will be refreshed
  4. /// based on DB.
  5. /// </summary>
  6.  
  7. unit pm.View.DB.SeriesUpdateFetcher;
  8.  
  9. interface
  10.  
  11. uses
  12.   pmViewImageStorage,
  13.   pm.DB.ThreadInstances,
  14.   IBX.IBCustomDataSet,
  15.   System.Generics.Collections,
  16.   pmDICOMTypes;
  17.  
  18. type
  19.  
  20.   TPmSeriesUpdateEvent = procedure(
  21.     ASender                   : TObject;
  22.     const AStudyID, ASeriesIDs: TList<Int64>) of object;
  23.  
  24.   TPmSerieUpdateChecker = class
  25.   private
  26.     FOnSerieNeedsUpdating: TPmSeriesUpdateEvent;
  27.     function FetchStudyDBData: TIBDataSet;
  28.     function NeedsSerieUpdatingOrAdding(
  29.       const AStudy       : TPmStudyNode;
  30.       var ASeriesToUpdate: TList<Int64>): Boolean;
  31.   public
  32.     property OnSerieNeedsUpdate: TPmSeriesUpdateEvent read FOnSerieNeedsUpdating write FOnSerieNeedsUpdating;
  33.     /// <summary>
  34.     /// Downloads series updates fo studies loaded into pmview
  35.     /// </summary>
  36.     /// <returns>Number of series that need update.</returns>
  37.     function DownloadUpdates: Integer;
  38.   end;
  39.  
  40. implementation
  41.  
  42. uses
  43.   System.SysUtils,
  44.   daDB;
  45.  
  46. { TPmSerieUpdateChecker }
  47.  
  48. function TPmSerieUpdateChecker.DownloadUpdates: Integer;
  49. var
  50.   LNavi       : TPmStudyTreeNavigator;
  51.   LDBStudyData: TIBDataSet;
  52.   LImgType    : TPmImageType;
  53.   LStudyDBDiff: TPmDCMStudyDB;
  54.   LI          : Integer;
  55. begin
  56.   try
  57.     LStudyDBDiff := TPmDCMStudyDB.Create(nil);
  58.     LNavi        := TPmStudyTreeNavigator.Create();
  59.     // exit and clear, if no data is available
  60.     if (LNavi.ImageIndex < 0) then
  61.     begin
  62.       Exit(0);
  63.     end;
  64.  
  65.     LStudyDBDiff.DB := GetdaserverThreadDB();
  66.     // Check for updates
  67.     LDBStudyData := FetchStudyDBData;
  68.  
  69.     if not Assigned(LDBStudyData) then
  70.       Exit(0);
  71.  
  72.     repeat
  73.       // match DB record with current Study node in navtree
  74.       { TODO : DBID needs to be added to TpmStudyTreeNAvigator.Study class, StudyUID seems to be not unique enough }
  75.       if LDBStudyData.Locate('id', LNavi.Study.DBID) then
  76.       begin
  77.         { TODO : LastTransfer needs to be added to TpmStudyTreeNAvigator.Study class }
  78.         // process further only if lasttransferdate is not equal with currently loaded data
  79.         if LNavi.Study.LastTransferDate < LDBStudyData.FieldByName('LastTransfer').AsDateTime then
  80.         begin
  81.           // load study object form making diff for update process
  82.           LStudyDBDiff.Load(LDBStudyData.FieldByName('id').AsInteger, True);
  83.  
  84.           repeat
  85.             { TODO : DBID needs to be added }
  86.             if LStudyDBDiff.Series.SerieById[LNavi.Series.DBID] <> nil then
  87.             begin
  88.               if LNavi.Series.ImageCount <> LStudyDBDiff.Series.SerieById[LNavi.Series.DBID].Images.Count then
  89.               begin
  90.                 repeat
  91.                   if LStudyDBDiff.Series.SerieById[LNavi.Series.DBID].Images.ImageById[LNavi.Image.DBID] <> nil then
  92.                   begin
  93.                     LStudyDBDiff.Series.SerieById[LNavi.Series.DBID].Images.Delete
  94.                       (LStudyDBDiff.Series.SerieById[LNavi.Series.DBID].Images.ImageById[LNavi.Image.DBID].Index);
  95.                   end;
  96.  
  97.                 until LNavi.NextImage;
  98.               end
  99.               else
  100.               begin
  101.                 LStudyDBDiff.Series.Delete(LStudyDBDiff.Series.SerieById[LNavi.Series.DBID].Index);
  102.               end;
  103.             end;
  104.  
  105.           until LNavi.NextSeries;
  106.  
  107.         end;
  108.  
  109.         if LStudyDBDiff.StudyId <> 0 then
  110.           if Assigned(OnSerieNeedsUpdate) then
  111.           begin
  112.             // call event and pass study diff object to create download task
  113.             // every study frequency was choosen for better ux
  114.             OnSerieNeedsUpdate(Self, LStudyDBDiff);
  115.           end;
  116.       end;
  117.  
  118.       // next study
  119.     until (not LNavi.NextStudy);
  120.  
  121.   finally
  122.     FreeAndNil(LNavi);
  123.   end;
  124. end;
  125.  
  126. function TPmSerieUpdateChecker.FetchStudyDBData: TIBDataSet;
  127. var
  128.   LStudyIDSet: String;
  129.   LPatNode   : TPmPatientNode;
  130.   LStudyNode : TPmStudyNode;
  131. begin
  132.   // list all DaPatIDs e.g. = 15,34,12,204
  133.   for LPatNode in TPmImageStorage.Patients do
  134.     for LStudyNode in LPatNode.Studies do
  135.       if LStudyNode.DBID >= 0 then { TODO : DBID needs to be introduced }
  136.         if LStudyIDSet = '' then
  137.           LStudyIDSet := IntToStr(LStudyNode.DBID)
  138.         else
  139.           LStudyIDSet := LStudyIDSet + ',' + IntToStr(LStudyNode.DBID);
  140.  
  141.   // no patients to query?? bye bye
  142.   if LStudyIDSet = '' then
  143.     Exit(nil);
  144.  
  145.   // StudyID, StudyDate, StudyDescription, LastTransfer
  146.   // - example query
  147.   // 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
  148.   Result := daOpenDataSet(GetdaserverThreadDB(), 'STUDY std', ['std.id', 'std.dapatid', 'std.kontoid', 'std.modality',
  149.     'std.studydescription', 'std.studyinstanceuid', 'std.studydate', 'std.lasttransfer'],
  150.     'std.id in (' + LStudyIDSet + ') and std.deltag = ''f'' order by std.dapatid asc, std.studydate desc');
  151.   // fetch all results at once
  152.   if Result <> nil then
  153.     Result.FetchAll;
  154. end;
  155.  
  156. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement