1 | package nl.tno.metagenomics.integration |
---|
2 | |
---|
3 | import nl.tno.metagenomics.* |
---|
4 | import nl.tno.metagenomics.auth.* |
---|
5 | import org.codehaus.groovy.grails.commons.ConfigurationHolder |
---|
6 | |
---|
7 | class SynchronizationService { |
---|
8 | def gscfService |
---|
9 | def trashService |
---|
10 | |
---|
11 | String sessionToken = "" // Session token to use for communication |
---|
12 | User user = null // Currently logged in user. Must be set when synchronizing authorization |
---|
13 | boolean eager = false // When set to true, this method fetches data about all studies from GSCF. Otherwise, it will only look at the |
---|
14 | // studies marked as dirty in the database. Defaults to false. |
---|
15 | |
---|
16 | // Keeps track of the last time this module performed a full synchronization. |
---|
17 | static Date lastFullSynchronization = null; |
---|
18 | |
---|
19 | static transactional = true |
---|
20 | |
---|
21 | /** |
---|
22 | * Determines whether the synchronization should be performed or not. This can be entered |
---|
23 | * in configuration, to avoid synchronization when developing. |
---|
24 | * @return |
---|
25 | */ |
---|
26 | protected performSynchronization() { |
---|
27 | def conf = ConfigurationHolder.config.metagenomics.synchronization; |
---|
28 | |
---|
29 | // If nothing is entered in configuration, return true (default value) |
---|
30 | if( conf == null ) |
---|
31 | return true |
---|
32 | |
---|
33 | return conf |
---|
34 | } |
---|
35 | |
---|
36 | /** |
---|
37 | * Returns true iff a full synchronization should be performed |
---|
38 | * @return |
---|
39 | */ |
---|
40 | public boolean timeForFullSynchronization() { |
---|
41 | if( SynchronizationService.lastFullSynchronization == null ) |
---|
42 | return true |
---|
43 | |
---|
44 | // Compute the time since the last full synchronization in milliseconds |
---|
45 | Date today = new Date(); |
---|
46 | long difference = SynchronizationService.lastFullSynchronization.getTime() - today.getTime() |
---|
47 | |
---|
48 | if( difference / 1000 > ConfigurationHolder.config.metagenomics.fullSynchronization ) |
---|
49 | return true |
---|
50 | else |
---|
51 | return false |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * Redirects to a temporary page to give the user a 'waiting' page while synchronizing |
---|
56 | * @return |
---|
57 | */ |
---|
58 | public String urlForFullSynchronization( def params ) { |
---|
59 | def returnUrl = ConfigurationHolder.config.grails.serverURL |
---|
60 | if (params.controller != null){ |
---|
61 | returnUrl += "/${params.controller}" |
---|
62 | if (params.action != null){ |
---|
63 | returnUrl += "/${params.action}" |
---|
64 | if (params.id != null){ |
---|
65 | returnUrl += "/${params.id}" |
---|
66 | } |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | // Append other parameters |
---|
71 | returnUrl += "?" + params.collect { |
---|
72 | if( it.key != "controller" && it.key != "action" && it.key != "id" ) |
---|
73 | return it.key.toString().encodeAsURL() + "=" + it.value.toString().encodeAsURL(); |
---|
74 | else |
---|
75 | return "" |
---|
76 | }.findAll { it }.join( "&" ); |
---|
77 | |
---|
78 | if( timeForFullSynchronization() ) { |
---|
79 | return ConfigurationHolder.config.grails.serverURL + "/synchronize/full?redirect=" + returnUrl.encodeAsURL() |
---|
80 | } else { |
---|
81 | return returnUrl |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * Performs a full synchronization in order to retrieve all studies |
---|
87 | * @return |
---|
88 | */ |
---|
89 | public void fullSynchronization() { |
---|
90 | def previousEager = this.eager |
---|
91 | this.eager = true |
---|
92 | this.synchronizeStudies(); |
---|
93 | this.eager = previousEager |
---|
94 | |
---|
95 | SynchronizationService.lastFullSynchronization = new Date(); |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * Synchronizes all studies with the data from GSCF. |
---|
100 | * @return ArrayList List of studies or null if the synchronization has failed |
---|
101 | */ |
---|
102 | public ArrayList<Study> synchronizeStudies() throws BadRequestException, NotAuthenticatedException, NotAuthorizedException, ResourceNotFoundException, Exception { |
---|
103 | if( !performSynchronization() ) |
---|
104 | return Study.findAllWhereTrashcan(false) |
---|
105 | |
---|
106 | // When eager fetching is enabled, ask for all studies, otherwise only ask for studies marked dirty |
---|
107 | // Synchronization is performed on all studies, not only the studies the user has access to. Otherwise |
---|
108 | // we would never notice that a user was given read-access to a study. |
---|
109 | def studies |
---|
110 | if( eager ) { |
---|
111 | studies = [] |
---|
112 | log.trace "Eager synchronization"; |
---|
113 | } else { |
---|
114 | studies = Study.findAllWhere( [trashcan: false, isDirty: true] ); |
---|
115 | log.trace "Default synchronization: " + studies.size() |
---|
116 | |
---|
117 | // Perform no synchronization if no studies have to be synchronized |
---|
118 | if( studies.size() == 0 ) |
---|
119 | return [] |
---|
120 | } |
---|
121 | |
---|
122 | // Perform synchronization on only one study directly, because otherwise |
---|
123 | // the getStudies method could throw a ResourceNotFoundException or NotAuthorizedException |
---|
124 | // that can better be handled by synchronizeStudy |
---|
125 | if( studies.size() == 1 ) { |
---|
126 | def newStudy = synchronizeStudy( studies[0] ); |
---|
127 | if( newStudy ) |
---|
128 | return [ newStudy ]; |
---|
129 | else |
---|
130 | return [] |
---|
131 | } |
---|
132 | |
---|
133 | // Fetch all studies from GSCF |
---|
134 | def newStudies |
---|
135 | try { |
---|
136 | if( !eager ) { |
---|
137 | def studyTokens = studies.studyToken; |
---|
138 | |
---|
139 | if( studyTokens instanceof String ) { |
---|
140 | studyTokens = [studyTokens]; |
---|
141 | } |
---|
142 | println "Only updating studies with tokens " + studyTokens.join( ',' ); |
---|
143 | newStudies = gscfService.getStudies(sessionToken, studyTokens) |
---|
144 | } else { |
---|
145 | newStudies = gscfService.getStudies(sessionToken) |
---|
146 | } |
---|
147 | } catch( Exception e ) { // All exceptions are thrown. |
---|
148 | // Can't retrieve data. Maybe sessionToken has expired or invalid. Anyway, stop |
---|
149 | // synchronizing and return null |
---|
150 | log.error( "Exception occurred when fetching studies: " + e.getMessage() ) |
---|
151 | throw e |
---|
152 | } |
---|
153 | |
---|
154 | synchronizeStudies( newStudies ); |
---|
155 | studies = handleDeletedStudies( studies, newStudies ); |
---|
156 | |
---|
157 | log.trace( "Returning " + studies.size() + " studies after synchronization" ) |
---|
158 | |
---|
159 | return studies |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * Synchronizes all studies given by 'newStudies' with existing studies in the database, and adds them |
---|
164 | * if they don't exist |
---|
165 | * |
---|
166 | * @param newStudies JSON object with studies as returned by GSCF |
---|
167 | * @return |
---|
168 | */ |
---|
169 | protected synchronizeStudies( def newStudies ) { |
---|
170 | // Synchronize all studies that are returned. Studies that are not returned by GSCF might be removed |
---|
171 | // but could also be invisible for the current user. |
---|
172 | newStudies.each { gscfStudy -> |
---|
173 | if( gscfStudy.studyToken ) { |
---|
174 | log.trace( "Processing GSCF study " + gscfStudy.studyToken + ": " + gscfStudy ) |
---|
175 | |
---|
176 | Study studyFound = Study.findByStudyToken( gscfStudy.studyToken as String ) |
---|
177 | |
---|
178 | if(studyFound) { |
---|
179 | log.trace( "Study found with name " + studyFound.name ) |
---|
180 | |
---|
181 | // Synchronize the study itself with the data retrieved |
---|
182 | synchronizeStudy( studyFound, gscfStudy ); |
---|
183 | } else { |
---|
184 | log.trace( "Study not found. Creating a new one" ) |
---|
185 | |
---|
186 | // If it doesn't exist, create a new object |
---|
187 | studyFound = new Study( studyToken: gscfStudy.studyToken, name: gscfStudy.title, isDirty: true ); |
---|
188 | studyFound.save(); |
---|
189 | |
---|
190 | // Synchronize authorization and study assays (since the study itself is already synchronized) |
---|
191 | synchronizeAuthorization(studyFound); |
---|
192 | if( studyFound.canRead( user ) ) |
---|
193 | synchronizeStudyAssays(studyFound); |
---|
194 | |
---|
195 | // Mark the study as clean |
---|
196 | studyFound.isDirty = false |
---|
197 | studyFound.save(); |
---|
198 | } |
---|
199 | } |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * Removes studies from the database that are expected but not found in the list from GSCF |
---|
205 | * @param studies List with existing studies in the database that were expected in the output of GSCF |
---|
206 | * @param newStudies JSON object with studies as returned by GSCF |
---|
207 | * @return List of remaining studies |
---|
208 | */ |
---|
209 | protected ArrayList<Study> handleDeletedStudies( def studies, def newStudies ) { |
---|
210 | // If might also be that studies have been removed from the system. In that case, the studies |
---|
211 | // should be deleted from this module as well. Looping backwards in order to avoid conflicts |
---|
212 | // when removing elements from the list |
---|
213 | |
---|
214 | println "Handle deleted studies: " + studies.size() + " -> " + newStudies.size(); |
---|
215 | def numStudies = studies.size(); |
---|
216 | for( int i = numStudies - 1; i >= 0; i-- ) { |
---|
217 | def existingStudy = studies[i]; |
---|
218 | |
---|
219 | def studyFound = newStudies.find { it.studyToken == existingStudy.studyToken } |
---|
220 | |
---|
221 | if( !studyFound ) { |
---|
222 | log.trace( "Study " + existingStudy.studyToken + " not found. Check whether it is removed or the user just can't see it." ) |
---|
223 | |
---|
224 | // Study was not given to us by GSCF. This might be because the study is removed, or because the study is not visible (anymore) |
---|
225 | // to the current user. |
---|
226 | // Synchronize authorization and see what is the case (it returns null if the study has been deleted) |
---|
227 | if( synchronizeAuthorization( existingStudy ) == null ) { |
---|
228 | // Update studies variable to keep track of all existing studies |
---|
229 | studies.remove( existingStudy ) |
---|
230 | } |
---|
231 | } |
---|
232 | } |
---|
233 | |
---|
234 | return studies |
---|
235 | } |
---|
236 | |
---|
237 | /** |
---|
238 | * Synchronizes the given study with the data from GSCF |
---|
239 | * @param study Study to synchronize |
---|
240 | * @return Study Synchronized study or null if the synchronization has failed |
---|
241 | */ |
---|
242 | public Study synchronizeStudy(Study study ) { |
---|
243 | if( !performSynchronization() ) |
---|
244 | return study |
---|
245 | |
---|
246 | if( study == null ) |
---|
247 | return null |
---|
248 | |
---|
249 | // Trashcan should never be synchronized |
---|
250 | if( study.trashcan ) |
---|
251 | return study |
---|
252 | |
---|
253 | // If the study hasn't changed, don't update anything |
---|
254 | if( !eager && !study.isDirty ) |
---|
255 | return study; |
---|
256 | |
---|
257 | // Retrieve the study from GSCF |
---|
258 | def newStudy |
---|
259 | try { |
---|
260 | newStudy = gscfService.getStudy(sessionToken, study.studyToken) |
---|
261 | } catch( NotAuthorizedException e ) { |
---|
262 | // User is not authorized to access this study. Update the authorization within the module and return |
---|
263 | synchronizeAuthorization( study ); |
---|
264 | return null |
---|
265 | } catch( ResourceNotFoundException e ) { |
---|
266 | // Study can't be found within GSCF. |
---|
267 | trashService.moveToTrash( study ); |
---|
268 | return null |
---|
269 | } catch( Exception e ) { // All other exceptions |
---|
270 | // Can't retrieve data. Maybe sessionToken has expired or invalid. Anyway, stop |
---|
271 | // synchronizing and return null |
---|
272 | e.printStackTrace() |
---|
273 | log.error( "Exception occurred when fetching study " + study.studyToken + ": " + e.getMessage() ) |
---|
274 | throw new Exception( "Error while fetching study " + study.studyToken, e) |
---|
275 | } |
---|
276 | |
---|
277 | // If no study is returned, something went wrong. |
---|
278 | if( newStudy.size() == 0 ) { |
---|
279 | throw new Exception( "No data returned for study " + study.studyToken + " but no error has occurred either. Please contact your system administrator" ); |
---|
280 | return null; |
---|
281 | } |
---|
282 | |
---|
283 | return synchronizeStudy(study, newStudy); |
---|
284 | } |
---|
285 | |
---|
286 | /** |
---|
287 | * Synchronizes the given study with the data from GSCF |
---|
288 | * @param study Study to synchronize |
---|
289 | * @param newStudy Data to synchronize the study with |
---|
290 | * @return Study Synchronized study or null if the synchronization has failed |
---|
291 | */ |
---|
292 | protected Study synchronizeStudy(Study study, def newStudy) { |
---|
293 | if( !performSynchronization() ) |
---|
294 | return study |
---|
295 | |
---|
296 | if( study == null || newStudy == null) |
---|
297 | return null |
---|
298 | |
---|
299 | // If the study hasn't changed, don't update anything |
---|
300 | if( !eager && !study.isDirty ) { |
---|
301 | return study; |
---|
302 | } |
---|
303 | |
---|
304 | // If no study is returned, something went wrong. |
---|
305 | if( newStudy.size() == 0 ) { |
---|
306 | return null; |
---|
307 | } |
---|
308 | |
---|
309 | // Mark study dirty to enable synchronization |
---|
310 | study.isDirty = true; |
---|
311 | synchronizeAuthorization( study ); |
---|
312 | if( study.canRead( user ) ) |
---|
313 | synchronizeStudyAssays( study ); |
---|
314 | |
---|
315 | // Update properties and mark as clean |
---|
316 | study.name = newStudy.title |
---|
317 | study.isDirty = false; |
---|
318 | study.save(flush:true) |
---|
319 | |
---|
320 | return study |
---|
321 | } |
---|
322 | |
---|
323 | /** |
---|
324 | * Synchronizes the assays of the given study with the data from GSCF |
---|
325 | * @param study Study of which the assays should be synchronized |
---|
326 | * @return ArrayList List of assays or null if the synchronization has failed |
---|
327 | */ |
---|
328 | protected ArrayList<Assay> synchronizeStudyAssays( Study study ) { |
---|
329 | if( !performSynchronization() ) |
---|
330 | return study.assays.toList() |
---|
331 | |
---|
332 | if( !eager && !study.isDirty ) |
---|
333 | return study.assays as List |
---|
334 | |
---|
335 | // Also update all assays, belonging to this study |
---|
336 | // Retrieve the assays from GSCF |
---|
337 | def newAssays |
---|
338 | try { |
---|
339 | newAssays = gscfService.getAssays(sessionToken, study.studyToken) |
---|
340 | } catch( Exception e ) { // All exceptions are thrown. If we get a NotAuthorized or NotFound Exception, something has changed in between the two requests. This will result in an error |
---|
341 | // Can't retrieve data. Maybe sessionToken has expired or invalid. Anyway, stop |
---|
342 | // synchronizing and return null |
---|
343 | log.error( "Exception occurred when fetching assays for study " + study.studyToken + ": " + e.getMessage() ) |
---|
344 | throw new Exception( "Error while fetching samples for assay " + study.studyToken, e) |
---|
345 | } |
---|
346 | |
---|
347 | // If no assay is returned, we remove all assays |
---|
348 | // from this study and return an empty list |
---|
349 | if( newAssays.size() == 0 && study.assays != null ) { |
---|
350 | def studyAssays = study.assays.toArray(); |
---|
351 | def numStudyAssays = study.assays.size(); |
---|
352 | for( int i = numStudyAssays - 1; i >= 0; i-- ) { |
---|
353 | def existingAssay = studyAssays[i]; |
---|
354 | |
---|
355 | // Move data to trash |
---|
356 | trashService.moveToTrash( existingAssay ); |
---|
357 | } |
---|
358 | |
---|
359 | return [] |
---|
360 | } |
---|
361 | |
---|
362 | synchronizeStudyAssays( study, newAssays ); |
---|
363 | return handleDeletedAssays( study, newAssays ); |
---|
364 | } |
---|
365 | |
---|
366 | /** |
---|
367 | * Synchronizes the assays of a study with the given data from GSCF |
---|
368 | * @param study Study to synchronize |
---|
369 | * @param newAssays JSON object given by GSCF to synchronize the assays with |
---|
370 | */ |
---|
371 | protected void synchronizeStudyAssays( Study study, def newAssays ) { |
---|
372 | // Otherwise, we search for all assays in the new list, if they |
---|
373 | // already exist in the list of assays |
---|
374 | newAssays.each { gscfAssay -> |
---|
375 | if( gscfAssay.assayToken ) { |
---|
376 | log.trace( "Processing GSCF assay " + gscfAssay.assayToken + ": " + gscfAssay ) |
---|
377 | |
---|
378 | Assay assayFound = study.assays.find { it.assayToken == gscfAssay.assayToken } |
---|
379 | |
---|
380 | if(assayFound) { |
---|
381 | log.trace( "Assay found with name " + assayFound.name ) |
---|
382 | |
---|
383 | // Synchronize the assay itself with the data retrieved |
---|
384 | synchronizeAssay( assayFound, gscfAssay ); |
---|
385 | } else { |
---|
386 | log.trace( "Assay not found in study. Creating a new one" ) |
---|
387 | |
---|
388 | // If it doesn't exist, create a new object |
---|
389 | assayFound = new Assay( assayToken: gscfAssay.assayToken, name: gscfAssay.name, study: study ); |
---|
390 | |
---|
391 | log.trace( "Connecting assay to study" ) |
---|
392 | study.addToAssays( assayFound ); |
---|
393 | assayFound.save() |
---|
394 | |
---|
395 | // Synchronize assay samples (since the assay itself is already synchronized) |
---|
396 | synchronizeAssaySamples(assayFound) |
---|
397 | } |
---|
398 | } |
---|
399 | } |
---|
400 | } |
---|
401 | |
---|
402 | /** |
---|
403 | * Removes assays from the system that have been deleted from GSCF |
---|
404 | * @param study Study to synchronize |
---|
405 | * @param newAssays JSON object given by GSCF to synchronize the assays with |
---|
406 | * @return List with all assays from the study |
---|
407 | */ |
---|
408 | protected ArrayList<Assay> handleDeletedAssays( Study study, def newAssays ) { |
---|
409 | if( study.assays == null ) { |
---|
410 | return [] |
---|
411 | } |
---|
412 | |
---|
413 | // If might also be that assays have been removed from this study. In that case, the removed assays |
---|
414 | // should be deleted from this study in the module as well. Looping backwards in order to avoid conflicts |
---|
415 | // when removing elements from the list |
---|
416 | def assays = study.assays.toArray(); |
---|
417 | def numAssays = assays.size(); |
---|
418 | for( int i = numAssays - 1; i >= 0; i-- ) { |
---|
419 | def existingAssay = assays[i]; |
---|
420 | |
---|
421 | Assay assayFound = newAssays.find { it.assayToken == existingAssay.assayToken } |
---|
422 | |
---|
423 | if( !assayFound ) { |
---|
424 | log.trace( "Assay " + existingAssay.assayToken + " not found. Removing it." ) |
---|
425 | |
---|
426 | // The assay has been removed |
---|
427 | trashService.moveToTrash( existingAssay ); |
---|
428 | } |
---|
429 | } |
---|
430 | |
---|
431 | return study.assays.toList() |
---|
432 | } |
---|
433 | |
---|
434 | /** |
---|
435 | * Retrieves the authorization for the currently logged in user |
---|
436 | * Since GSCF only provides authorization information about the currently |
---|
437 | * logged in user, we can not guarantee that the authorization information |
---|
438 | * is synchronized for all users. |
---|
439 | * |
---|
440 | * Make sure synchronizationService.user is set beforehand |
---|
441 | * |
---|
442 | * @param study Study to synchronize authorization for |
---|
443 | * @return Auth object for the given study and user |
---|
444 | */ |
---|
445 | public Auth synchronizeAuthorization(Study study) { |
---|
446 | // If the user is not set, we can't save anything to the database. |
---|
447 | if( user == null ) { |
---|
448 | throw new Exception( "Property user of SynchronizationService must be set to the currently logged in user" ); |
---|
449 | } |
---|
450 | |
---|
451 | // Only perform synchronization if needed |
---|
452 | if( !eager && !study.isDirty ) |
---|
453 | return Auth.findByUserAndStudy( user, study ) |
---|
454 | |
---|
455 | if( !performSynchronization() ) |
---|
456 | return Auth.findByUserAndStudy( user, study ) |
---|
457 | |
---|
458 | def gscfAuthorization |
---|
459 | try { |
---|
460 | gscfAuthorization = gscfService.getAuthorizationLevel( sessionToken, study.studyToken ) |
---|
461 | } catch( ResourceNotFoundException e ) { |
---|
462 | // Study has been deleted, remove all authorization on that study |
---|
463 | log.trace( "Study " + study.studyToken + " has been deleted. Remove all authorization on that study") |
---|
464 | trashService.moveToTrash( study ); |
---|
465 | |
---|
466 | return null |
---|
467 | } |
---|
468 | |
---|
469 | // Update the authorization object, or create a new one |
---|
470 | Auth a = Auth.authorization( study, user ) |
---|
471 | |
---|
472 | if( !a ) { |
---|
473 | log.trace( "Authorization not found for " + study.studyToken + " and " + user.username + ". Creating a new object" ); |
---|
474 | |
---|
475 | a = Auth.createAuth( study, user ); |
---|
476 | } |
---|
477 | |
---|
478 | // Copy properties from gscf object |
---|
479 | if( gscfAuthorization.canRead instanceof Boolean ) |
---|
480 | a.canRead = gscfAuthorization.canRead.booleanValue() |
---|
481 | |
---|
482 | if( gscfAuthorization.canWrite instanceof Boolean ) |
---|
483 | a.canWrite = gscfAuthorization.canWrite.booleanValue() |
---|
484 | |
---|
485 | if( gscfAuthorization.isOwner instanceof Boolean ) |
---|
486 | a.isOwner = gscfAuthorization.isOwner.booleanValue() |
---|
487 | |
---|
488 | a.save() |
---|
489 | |
---|
490 | return a |
---|
491 | } |
---|
492 | |
---|
493 | /** |
---|
494 | * Synchronizes the given assay with the data from GSCF |
---|
495 | * @param assay Assay to synchronize |
---|
496 | * @return Assay Synchronized assay or null if the synchronization has failed |
---|
497 | */ |
---|
498 | public Assay synchronizeAssay(Assay assay) { |
---|
499 | if( !performSynchronization() ) |
---|
500 | return assay |
---|
501 | |
---|
502 | if( assay == null ) |
---|
503 | return null |
---|
504 | |
---|
505 | // Only perform synchronization if needed |
---|
506 | if( !eager && !assay.study.isDirty ) |
---|
507 | return assay |
---|
508 | |
---|
509 | // Retrieve the assay from GSCF |
---|
510 | def newAssay |
---|
511 | try { |
---|
512 | newAssay = gscfService.getAssay(sessionToken, assay.study.studyToken, assay.assayToken) |
---|
513 | } catch( NotAuthorizedException e ) { |
---|
514 | // User is not authorized to access this study. Update the authorization within the module and return |
---|
515 | synchronizeAuthorization( assay.study ); |
---|
516 | return null |
---|
517 | } catch( ResourceNotFoundException e ) { |
---|
518 | // Assay can't be found within GSCF. |
---|
519 | trashService.moveToTrash( assay ); |
---|
520 | return null |
---|
521 | } catch( Exception e ) { // All other exceptions are thrown |
---|
522 | // Can't retrieve data. Maybe sessionToken has expired or invalid. Anyway, stop |
---|
523 | // synchronizing and return null |
---|
524 | log.error( "Exception occurred when fetching assay " + assay.assayToken + ": " + e.getMessage() ) |
---|
525 | throw new Exception( "Error while fetching assay " + assay.assayToken, e) |
---|
526 | } |
---|
527 | |
---|
528 | // If new assay is empty, this means that the assay does exist, but now belongs to another module. Remove it from our system |
---|
529 | if( newAssay.size() == 0 ) { |
---|
530 | log.info( "No data is returned by GSCF for assay " + assay.assayToken + "; probably the assay is connected to another module." ) |
---|
531 | trashService.moveToTrash( assay ); |
---|
532 | return null; |
---|
533 | } |
---|
534 | |
---|
535 | return synchronizeAssay( assay, newAssay ); |
---|
536 | } |
---|
537 | |
---|
538 | /** |
---|
539 | * Synchronizes the given assay with the data given |
---|
540 | * @param assay Assay to synchronize |
---|
541 | * @param newAssay New data for the assay, retrieved from GSCF |
---|
542 | * @return Assay Synchronized assay or null if the synchronization has failed |
---|
543 | */ |
---|
544 | protected Assay synchronizeAssay(Assay assay, def newAssay) { |
---|
545 | if( !performSynchronization() ) |
---|
546 | return assay |
---|
547 | |
---|
548 | if( assay == null || newAssay == null ) |
---|
549 | return null |
---|
550 | |
---|
551 | // Only perform synchronization if needed |
---|
552 | if( !eager && !assay.study.isDirty ) |
---|
553 | return assay |
---|
554 | |
---|
555 | // If new assay is empty, something went wrong |
---|
556 | if( newAssay.size() == 0 ) { |
---|
557 | return null; |
---|
558 | } |
---|
559 | |
---|
560 | log.trace( "Assay is found in GSCF: " + assay.name + " / " + newAssay ) |
---|
561 | if( newAssay?.name ) { |
---|
562 | assay.name = newAssay.name |
---|
563 | assay.save() |
---|
564 | } |
---|
565 | |
---|
566 | // Synchronize samples |
---|
567 | synchronizeAssaySamples(assay); |
---|
568 | |
---|
569 | return assay |
---|
570 | } |
---|
571 | |
---|
572 | /** |
---|
573 | * Synchronizes the samples of a given assay with the data from GSCF |
---|
574 | * @param assay Assay to synchronize |
---|
575 | * @return Sample List of samples or null if the synchronization failed |
---|
576 | */ |
---|
577 | protected ArrayList<AssaySample> synchronizeAssaySamples(Assay assay) { |
---|
578 | if( !performSynchronization() ) |
---|
579 | return assay.assaySamples.toList() |
---|
580 | |
---|
581 | // If no assay is given, return null |
---|
582 | if( assay == null ) |
---|
583 | return null |
---|
584 | |
---|
585 | // Retrieve the assay from GSCF |
---|
586 | def newSamples |
---|
587 | try { |
---|
588 | newSamples = gscfService.getSamples(sessionToken, assay.assayToken) |
---|
589 | } catch( NotAuthorizedException e ) { |
---|
590 | // User is not authorized to access this study. Update the authorization within the module and return |
---|
591 | synchronizeAuthorization( assay.study ); |
---|
592 | return null |
---|
593 | } catch( ResourceNotFoundException e ) { |
---|
594 | // Assay can't be found within GSCF. Samples will be removed |
---|
595 | trashService.moveToTrash( assay ); |
---|
596 | |
---|
597 | return null |
---|
598 | } catch( Exception e ) { |
---|
599 | // Can't retrieve data. Maybe sessionToken has expired or invalid. Anyway, stop |
---|
600 | // synchronizing and return null |
---|
601 | log.error( "Exception occurred when fetching samples for assay " + assay.assayToken + ": " + e.getMessage() ) |
---|
602 | throw new Exception( "Error while fetching samples for assay " + assay.assayToken, e) |
---|
603 | } |
---|
604 | |
---|
605 | // If no sample is returned, we remove all samples from the list |
---|
606 | if( newSamples.size() == 0 ) { |
---|
607 | assay.removeAssaySamples(); |
---|
608 | return [] |
---|
609 | } |
---|
610 | |
---|
611 | synchronizeAssaySamples( assay, newSamples ); |
---|
612 | return handleDeletedSamples( assay, newSamples ); |
---|
613 | } |
---|
614 | |
---|
615 | /** |
---|
616 | * Synchronize all samples for a given assay with the data from GSCF |
---|
617 | * @param assay Assay to synchronize samples for |
---|
618 | * @param newSamples New samples in JSON object, as given by GSCF |
---|
619 | */ |
---|
620 | protected void synchronizeAssaySamples( Assay assay, def newSamples ) { |
---|
621 | // Otherwise, we search for all samples in the new list, if they |
---|
622 | // already exist in the list of samples |
---|
623 | newSamples.each { gscfSample -> |
---|
624 | log.trace( "Processing GSCF sample " + gscfSample.sampleToken + ": " + gscfSample ) |
---|
625 | if( gscfSample.name ) { |
---|
626 | |
---|
627 | AssaySample assaySampleFound = assay.assaySamples.find { it.sample.sampleToken == gscfSample.sampleToken } |
---|
628 | Sample sampleFound |
---|
629 | |
---|
630 | if(assaySampleFound) { |
---|
631 | sampleFound = assaySampleFound.sample |
---|
632 | log.trace( "AssaySample found with sample name " + sampleFound.name ) |
---|
633 | |
---|
634 | // Update the sample object if necessary |
---|
635 | sampleFound.name = gscfSample.name |
---|
636 | sampleFound.subject = gscfSample.subject.toString() |
---|
637 | sampleFound.event = gscfSample.event.toString() + ( gscfSample.startTime ? " (" + gscfSample.startTime + ")" : "" ) |
---|
638 | sampleFound.save(); |
---|
639 | } else { |
---|
640 | log.trace( "AssaySample not found in assay" ) |
---|
641 | |
---|
642 | // Check if the sample already exists in the database. |
---|
643 | sampleFound = Sample.findBySampleTokenAndStudy( gscfSample.sampleToken as String, assay.study ) |
---|
644 | |
---|
645 | if( sampleFound ){ |
---|
646 | log.trace( "Sample " + gscfSample.sampleToken + " is found in database. Updating if necessary" ) |
---|
647 | |
---|
648 | // Update the sample object if necessary |
---|
649 | sampleFound.name = gscfSample.name |
---|
650 | sampleFound.subject = gscfSample.subject.toString() |
---|
651 | sampleFound.event = gscfSample.event.toString() + ( gscfSample.startTime ? " (" + gscfSample.startTime + ")" : "" ) |
---|
652 | sampleFound.save(); |
---|
653 | } else { |
---|
654 | log.trace( "Sample " + gscfSample.sampleToken + " not found in database. Creating a new object." ) |
---|
655 | |
---|
656 | // If it doesn't exist, create a new object |
---|
657 | sampleFound = new Sample( sampleToken: gscfSample.sampleToken, name: gscfSample.name, study: assay.study ); |
---|
658 | sampleFound.subject = gscfSample.subject.toString() |
---|
659 | sampleFound.event = gscfSample.event.toString() + ( gscfSample.startTime ? " (" + gscfSample.startTime + ")" : "" ) |
---|
660 | assay.study.addToSamples( sampleFound ); |
---|
661 | sampleFound.save(); |
---|
662 | } |
---|
663 | |
---|
664 | // Create a new assay-sample combination |
---|
665 | log.trace( "Connecting sample to assay" ) |
---|
666 | assaySampleFound = new AssaySample(); |
---|
667 | |
---|
668 | assay.addToAssaySamples( assaySampleFound ); |
---|
669 | sampleFound.addToAssaySamples( assaySampleFound ); |
---|
670 | |
---|
671 | assaySampleFound.save() |
---|
672 | } |
---|
673 | } |
---|
674 | } |
---|
675 | } |
---|
676 | |
---|
677 | /** |
---|
678 | * Removes samples from the system that have been removed from an assay in GSCF |
---|
679 | * @param assay Assay to remove samples for |
---|
680 | * @param newSamples JSON object with all samples for this assay as given by GSCF |
---|
681 | */ |
---|
682 | protected ArrayList<AssaySample> handleDeletedSamples( Assay assay, def newSamples ) { |
---|
683 | // If might also be that samples have been removed from this assay. In that case, the removed samples |
---|
684 | // should be deleted from this assay. Looping backwards in order to avoid conflicts when removing elements |
---|
685 | // from the list |
---|
686 | if( assay.assaySamples != null ) { |
---|
687 | def assaySamples = assay.assaySamples.toArray(); |
---|
688 | def numAssaySamples = assay.assaySamples.size(); |
---|
689 | for( int i = numAssaySamples - 1; i >= 0; i-- ) { |
---|
690 | def existingSample = assaySamples[i]; |
---|
691 | |
---|
692 | AssaySample sampleFound = newSamples.find { it.sampleToken == existingSample.sample.sampleToken } |
---|
693 | |
---|
694 | if( !sampleFound ) { |
---|
695 | log.trace( "Sample " + existingSample.sample.sampleToken + " not found. Removing it." ) |
---|
696 | |
---|
697 | // The sample has been removed |
---|
698 | trashService.moveToTrash( existingSample ); |
---|
699 | } |
---|
700 | } |
---|
701 | } |
---|
702 | |
---|
703 | // Create a list of samples to return |
---|
704 | if( assay.assaySamples ) |
---|
705 | return assay.assaySamples.toList() |
---|
706 | else |
---|
707 | return [] |
---|
708 | } |
---|
709 | } |
---|