]> Creatis software - creaImageIO.git/blob - src/creaImageIODicomDatabase.h
no message
[creaImageIO.git] / src / creaImageIODicomDatabase.h
1 #ifndef __creaImageIODicomDatabase_h_INCLUDED__
2 #define __creaImageIODicomDatabase_h_INCLUDED__
3
4
5 #include <creaImageIODicomNode.h>
6 #include <creaImageIOImageReader.h>
7
8 #include <vector>
9 #include <wx/wx.h>
10 #include <wx/progdlg.h>
11
12 class CppSQLite3DB;
13
14 namespace creaImageIO
15 {
16
17
18   //=====================================================================
19   class DicomDatabase : public DicomNode
20   {
21   public:
22
23     /// Creates a database associated to the file 'filename'.
24     /// Does not create the db nor reads any data from the file
25     DicomDatabase(const std::string& filename, int flags = 0);
26     /// Dtor
27     ~DicomDatabase();
28     
29       /// Returns the DicomNodeTypeDescription for DicomNodes of type c
30     DicomNodeTypeDescription& GetDicomNodeTypeDescription(DicomNode::Type c) 
31     { return mDicomNodeTypeDescription[c]; }
32     /// Returns the DicomNodeTypeDescription for DicomNodes of type c (const)
33     const DicomNodeTypeDescription& GetDicomNodeTypeDescription(DicomNode::Type c) const
34     { return mDicomNodeTypeDescription[c]; }
35
36
37     /// Returns the sqlite db file name 
38     const std::string& GetFileName() const { return UnsafeGetFieldValue("File name"); }
39     void SetName(const std::string& n) { UnsafeSetFieldValue("Name",n); }
40     const std::string& GetName() const { return UnsafeGetFieldValue("Name"); }
41
42
43     void Print() const;
44
45
46     //=============================================================
47     void Clear();
48     bool LocationIsValid();
49     /// Creates a new db on disk 
50     bool New();
51     /// Opens an existing db on disk
52     bool Open();
53     /// Closes the db
54     bool Close();
55
56     /// Recursively loads the children of node 'parent' until maxlevel 
57     /// is reached.
58     /// If parent == NULL or parent == this then starts with the 'children' of 
59     /// the database itself, i.e. the patients.
60     /// Returns the total number of children loaded.
61     int DBLoadChildren(DicomNode* parent, DicomNode::Type maxlevel);
62    
63     /// 
64
65     struct UpdateSummary
66     {
67       int scanned_dirs;
68       int scanned_files;
69       int handled_images;
70       int added_images;
71
72       int added_patients;
73       int added_studies;
74       int added_series;
75
76       long parse_time;
77       long file_scan_time;
78       long update_structs_time;
79       long update_database_time;
80       long total_time;
81       bool cancelled_by_user;
82       
83       UpdateSummary() :
84         scanned_dirs(0),
85         scanned_files(0),
86         handled_images(0),
87         added_images(0),
88         added_patients(0),
89         added_studies(0),
90         added_series(0),
91         parse_time(0),
92         file_scan_time(0),
93         update_structs_time(0),
94         update_database_time(0),
95         total_time(0),
96         cancelled_by_user(false)
97       {}
98     };
99
100
101     ///
102     bool IsHandledFile( const std::string& filename);
103     bool AddFile( const std::string& filename,
104                   UpdateSummary& summary );
105     bool AddFiles( const std::vector<std::string>& filename,
106                    wxProgressDialog* progress, 
107                    UpdateSummary& summary);
108     bool AddDirectory( const std::string& directory, 
109                        bool recurse,
110                        wxProgressDialog* progress, 
111                        UpdateSummary& summary
112                        );
113
114     void ParseDirectory( const std::string& directory, 
115                          std::vector<std::string> &Filenames,
116                          bool recurse,
117                          wxProgressDialog* progress, 
118                          UpdateSummary& summary);
119
120
121     /// Removes the node and its descendants from the db
122     bool Remove(DicomNode*);
123     //=============================================================
124
125
126    int DBQueryNumberOfChildren(DicomNode* n);
127
128   protected:
129     // Open 
130     bool OpenDB();
131     void ImportDicomNodeTypeDescriptionsFromDB();
132
133     // Creation
134     bool CreateDB();
135     void BuildDefaultDicomNodeTypeDescriptions();
136     void AppendSQLFieldsDefinition(DicomNode::Type c, std::string& s);
137     void ExportDicomNodeTypeDescriptionsToDB();
138     //
139
140     // Test
141     bool DBStructureIsValid();
142
143     
144     // Insertion
145     bool DBInsert(DicomNode* alien_node, UpdateSummary& summary);
146     
147     //
148     std::string DBGetDicomNodeId(DicomNode* node, const std::string& parent_id);
149     DicomNode* GetDicomNodeFromTypeId(DicomNode::Type t, const std::string& id);
150
151     // 
152     DicomNode* DBGetOrCreateDicomNode(DicomNode* alien_node, 
153                                       DicomNode* internal_parent,
154                                       std::string parentid,
155                                       std::string& created_id, 
156                                       UpdateSummary& summary);
157     
158     DicomNode* DBGetOrCreateParent(DicomNode* alien_node,
159                                    std::string& parent_id, 
160                                    UpdateSummary& summary);
161     
162     void DBRecursiveGetOrCreateDicomNode(DicomNode* alien_node, 
163                                          DicomNode* parent, 
164                                          const std::string& parent_id, 
165                                          UpdateSummary& summary);
166     
167
168     // 
169     void DBRecursiveRemoveDicomNode(DicomNode* node);
170
171
172     void BuildSQLFieldsValues(DicomNode* n,
173                               std::string& str);
174
175
176     DicomNode* GetChildrenLike(DicomNode* internal_parent,
177                                DicomNode* alien_node);
178
179   private:
180     CppSQLite3DB* mDB;
181     ImageReader mReader;
182
183
184     struct TypeId
185     {
186       int type;
187       std::string id;
188       bool operator< (const TypeId& o) const 
189       {
190         return ((type<o.type) ||
191                 ((type==o.type)&&id<o.id));
192       }
193     };
194     
195     typedef std::map<TypeId,DicomNode*> TypeIdToDicomNodeMapType;
196     TypeIdToDicomNodeMapType mTypeIdToDicomNodeMap;
197
198     
199     /// The 5 descriptions of node types (Database,Patient,Study,Series,Image)
200     /// The array is accessed through the DicomNode::TypeCode
201     DicomNodeTypeDescription mDicomNodeTypeDescription[5];
202     
203
204     /// The physical location associated to the DicomDatabase (directory, db file...)
205     std::string mFileName;
206
207     std::string mName;
208
209     int mFlags;
210
211     wxStopWatch msw[10];
212
213   }; // class DicomDatabase
214   //=====================================================================
215
216
217
218 } // namespace creaImageIO
219
220
221
222 #endif // #ifndef __creaImageIODicomDatabase_h_INCLUDED__