@@ -118,59 +118,84 @@ def insertMetadataIntoDatabase(self, dbProgressCallback:Callable[[str], None]=No
118
118
if not self .contentCopied and self .metadata ["upload_type" ] == "files" :
119
119
return False , "Please ensure that the uploaded files are copied first before inserting in DB"
120
120
121
- try :
122
- with open ("filetype_db_mapping.json" , 'r' ) as filetypeMappingFile :
123
- filetypeMapping = json .load (filetypeMappingFile )
124
- except FileNotFoundError as err :
125
- return False , "The filetype mapping JSON cannot be loaded"
121
+ # try:
122
+ # with open("filetype_db_mapping.json", 'r') as filetypeMappingFile:
123
+ # filetypeMapping = json.load(filetypeMappingFile)
124
+ # except FileNotFoundError as err:
125
+ # return False, "The filetype mapping JSON cannot be loaded"
126
126
127
127
for UploadDetails in self .metadata ["uploaded_files" ]:
128
- if UploadDetails ["file_type" ] in filetypeMapping ["mapping" ].keys ():
129
- paths = []
130
- multiValues = filetypeMapping ["mapping" ][UploadDetails ["file_type" ]]["multivalues" ]
131
- granularity = filetypeMapping ["mapping" ][UploadDetails ["file_type" ]]["granularity" ]
132
- if multiValues :
133
- seperator = filetypeMapping ["mapping" ][UploadDetails ["file_type" ]]["delimiter" ]
134
- for filePath in UploadDetails ["Files" ]:
135
- parentPath , sep , fileName = filePath .rpartition ('/' )
136
- if granularity == "folder" :
137
- if parentPath not in paths :
138
- paths .append (parentPath )
139
- else :
140
- paths .append (filePath )
141
- if not multiValues :
142
- break
143
- fieldContent = ""
144
- if multiValues and len (paths ) > 1 :
145
- for path in paths :
146
- if fieldContent != "" :
147
- fieldContent += seperator
148
- fieldContent += path [len (self .currentContextId ):]
149
- else :
150
- fieldContent += paths [0 ][len (self .currentContextId ):]
151
- tableName = filetypeMapping ["mapping" ][UploadDetails ["file_type" ]]["table" ]
152
- fieldName = filetypeMapping ["mapping" ][UploadDetails ["file_type" ]]["field" ]
153
- insertStmt = f"UPDATE { tableName } SET { fieldName } = \' { fieldContent } \' "
154
- if tableName == "prescription" :
155
- insertStmt += "FROM patient " \
128
+ findTableNameSql = f"SELECT table_name from information_schema.columns where column_name=\' { UploadDetails ['file_type' ]} \' "
129
+ result = self .dbAdapter .executeFindOnImageDB (findTableNameSql )
130
+ if not result .success :
131
+ return result .success , result .message
132
+ tableName = result .result [0 ][0 ]
133
+ insertStmt = ""
134
+ if tableName == "prescription" :
135
+ insertStmt = f"UPDATE { tableName } SET { UploadDetails ['file_type' ]} = \' { UploadDetails ['folder_path' ][0 ]} \' "
136
+ insertStmt += "FROM patient " \
156
137
"WHERE patient.id=prescription.patient_id " \
157
138
+ f"AND patient.patient_trial_id=\' { self .metadata ['patient_trial_id' ]} \' " \
158
139
+ f"AND patient.clinical_trial=\' { self .metadata ['clinical_trial' ]} \' " \
159
140
+ f"AND patient.test_centre=\' { self .metadata ['test_centre' ]} \' "
160
- elif tableName == "images" :
161
- insertStmt += "FROM patient, prescription, fraction " \
162
- + "WHERE patient.id=prescription.patient_id " \
163
- + "AND prescription.prescription_id=fraction.prescription_id " \
164
- + "AND images.fraction_id=fraction.fraction_id " \
165
- + f"AND patient.patient_trial_id=\' { self .metadata ['patient_trial_id' ]} \' " \
166
- + f"AND patient.clinical_trial=\' { self .metadata ['clinical_trial' ]} \' " \
167
- + f"AND patient.test_centre=\' { self .metadata ['test_centre' ]} \' "
168
141
result = self .dbAdapter .executeUpdateOnImageDB (insertStmt )
169
142
if not result .success :
170
143
return result .success , result .message
171
- elif dbProgressCallback :
172
- dbProgressCallback (f"updated { tableName } .{ fieldName } = { fieldContent } " )
173
- self .markPacketAsImported ()
144
+ else :
145
+ for fraction in UploadDetails ["fraction" ]:
146
+ fractionDetail = self .dbAdapter .getFractionIdAndName (self .metadata ["patient_trial_id" ], fraction )
147
+ if fractionDetail :
148
+ fractionId = fractionDetail [0 ][0 ]
149
+ insertStmt = f"UPDATE { tableName } SET { UploadDetails ['file_type' ]} = \' { UploadDetails ['folder_path' ][fraction ]} \' WHERE fraction_id = \' { fractionId } \' "
150
+ result = self .dbAdapter .executeUpdateOnImageDB (insertStmt )
151
+ if not result .success :
152
+ return result .success , result .message
153
+ # if UploadDetails["file_type"] in filetypeMapping["mapping"].keys():
154
+ # paths = []
155
+ # multiValues = filetypeMapping["mapping"][UploadDetails["file_type"]]["multivalues"]
156
+ # granularity = filetypeMapping["mapping"][UploadDetails["file_type"]]["granularity"]
157
+ # if multiValues:
158
+ # seperator = filetypeMapping["mapping"][UploadDetails["file_type"]]["delimiter"]
159
+ # for filePath in UploadDetails["Files"]:
160
+ # parentPath, sep, fileName = filePath.rpartition('/')
161
+ # if granularity == "folder":
162
+ # if parentPath not in paths:
163
+ # paths.append(parentPath)
164
+ # else:
165
+ # paths.append(filePath)
166
+ # if not multiValues:
167
+ # break
168
+ # fieldContent = ""
169
+ # if multiValues and len(paths) > 1:
170
+ # for path in paths:
171
+ # if fieldContent != "":
172
+ # fieldContent += seperator
173
+ # fieldContent += path[len(self.currentContextId):]
174
+ # else:
175
+ # fieldContent += paths[0][len(self.currentContextId):]
176
+ # tableName = filetypeMapping["mapping"][UploadDetails["file_type"]]["table"]
177
+ # fieldName = filetypeMapping["mapping"][UploadDetails["file_type"]]["field"]
178
+ # insertStmt = f"UPDATE {tableName} SET {fieldName} = \'{fieldContent}\' "
179
+ # if tableName == "prescription":
180
+ # insertStmt += "FROM patient " \
181
+ # "WHERE patient.id=prescription.patient_id " \
182
+ # + f"AND patient.patient_trial_id=\'{self.metadata['patient_trial_id']}\' " \
183
+ # + f"AND patient.clinical_trial=\'{self.metadata['clinical_trial']}\' " \
184
+ # + f"AND patient.test_centre=\'{self.metadata['test_centre']}\'"
185
+ # elif tableName == "images":
186
+ # insertStmt += "FROM patient, prescription, fraction " \
187
+ # + "WHERE patient.id=prescription.patient_id " \
188
+ # + "AND prescription.prescription_id=fraction.prescription_id " \
189
+ # + "AND images.fraction_id=fraction.fraction_id " \
190
+ # + f"AND patient.patient_trial_id=\'{self.metadata['patient_trial_id']}\' " \
191
+ # + f"AND patient.clinical_trial=\'{self.metadata['clinical_trial']}\' " \
192
+ # + f"AND patient.test_centre=\'{self.metadata['test_centre']}\'"
193
+ # result = self.dbAdapter.executeUpdateOnImageDB(insertStmt)
194
+ # if not result.success:
195
+ # return result.success, result.message
196
+ # elif dbProgressCallback:
197
+ # dbProgressCallback(f"updated {tableName}.{fieldName} = {fieldContent}")
198
+ # self.markPacketAsImported()
174
199
return True , "Success"
175
200
176
201
def rejectUploadPacket (self ):
0 commit comments