- Timestamp:
- Jan 26, 2011, 5:08:25 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/services/nl/tno/metagenomics/integration/TrashService.groovy
r4 r7 2 2 3 3 import nl.tno.metagenomics.* 4 5 4 6 5 class TrashService { … … 16 15 if( study.trashcan ) 17 16 return 18 17 19 18 saveDataInTrash( study ); 19 20 def l = [] 21 l += study.auth 22 23 l.each { auth -> 24 auth.user.removeFromAuth( auth ); 25 study.removeFromAuth( auth ); 26 } 27 20 28 study.delete(flush:true); 21 29 } 22 30 23 31 /** 24 32 * Moves the valuable data from an assay to trash and deletes the assay … … 27 35 def moveToTrash( Assay assay ) { 28 36 saveDataInTrash( assay ); 29 37 30 38 // Remove associations 39 def l = [] 40 if( assay.runs ) { 41 l += assay.runs 42 43 l.each { 44 if( it ) { 45 assay.removeFromRuns( it ); 46 it.removeFromAssays( assay ); 47 } 48 } 49 } 50 51 l = [] 52 l += assay.assaySamples 53 54 l.each { 55 it.sample.removeFromAssaySamples( it ); 56 assay.removeFromAssaySamples( it ); 57 } 58 59 def study = assay.study 60 if( study ) { 61 study.removeFromAssays( assay ); 62 } 63 64 /* 65 def assaySamples = assay.assaySamples.toList(); 66 assaySamples.each { 67 it.assay = null 68 69 assay.removeFromAssaySamples( it ); 70 it.sample.removeFromAssaySamples( it ); 71 it.sample = null; 72 it.delete( flush: true ); 73 } 74 31 75 assay.study.removeFromAssays( assay ); 32 assay.delete(flush:true); 33 } 34 76 assay.study.save(); 77 //assay.study = null 78 //assay.delete(flush:true); 79 */ 80 } 81 35 82 /** 36 83 * Moves the valuable data from a sample to trash and deletes the sample … … 39 86 def moveToTrash( Sample sample ) { 40 87 saveDataInTrash( sample ); 41 88 42 89 // Remove associations 90 def l = [] 91 l += sample.assaySamples 92 93 l.each { 94 it.assay.removeFromAssaySamples( it ); 95 sample.removeFromAssaySamples( it ); 96 } 97 98 def study = sample.study 43 99 sample.study.removeFromSamples( sample ); 44 sample.delete(flush:true); 45 } 46 100 study.save(); 101 } 102 103 /** 104 * Moves the valuable data from an assaySample to trash and deletes the assay 105 * @param Assay to move to trash 106 */ 107 def moveToTrash( AssaySample assaySample ) { 108 saveDataInTrash( assaySample ); 109 110 // Remove associations 111 if( assaySample.run ) { 112 assaySample.run.removeFromAssaySamples( assaySample ); 113 } 114 115 if( assaySample.assay ) { 116 assaySample.assay.removeFromAssaySamples( assaySample ); 117 } 118 119 if( assaySample.sample ) { 120 assaySample.sample.removeFromAssaySamples( assaySample ); 121 } 122 123 def l = [] 124 l += assaySample.sequenceData 125 126 l.each { 127 if( it ) { 128 assaySample.removeFromSequenceData( it ); 129 } 130 } 131 132 } 133 47 134 /** 48 135 * Saves data from the study in the trash can (if any data exists) … … 51 138 */ 52 139 def saveDataInTrash( Study study ) { 53 Study trashcan = Study.findByTrashcan(true);140 Study trashcan = this.giveTrashcan() 54 141 55 142 if( !trashcan ) { … … 71 158 */ 72 159 def saveDataInTrash( Assay assay ) { 73 Study trashcan = Study.findByTrashcan(true);160 Study trashcan = this.giveTrashcan() 74 161 75 162 if( !trashcan ) { … … 91 178 assaySamples.each { assaySample -> 92 179 Sample sample = assaySample.sample 93 180 94 181 // Create dummy sample 95 182 String newSampleToken = 'TrashSample ' + new Date().format( 'yyyyMMddHHmmssSSS') + ( Math.random() * 10000 ); … … 118 205 */ 119 206 def saveDataInTrash( Sample sample ) { 120 Study trashcan = Study.findByTrashcan(true);207 Study trashcan = this.giveTrashcan() 121 208 122 209 if( !trashcan ) { … … 159 246 } 160 247 248 249 /** 250 * Saves data from the assay-sample in the trash can (if any data exists) 251 * @param study Sample to save data from 252 * @return 253 */ 254 def saveDataInTrash( AssaySample assaySample ) { 255 Study trashcan = this.giveTrashcan() 256 257 if( !trashcan ) { 258 log.warn "No trashcan (study with trashcan property set to true) found in the database when deleting sample " + sample.name + ". Possibly valuable data is deleted forever." 259 return; 260 } 261 262 // For every assay sample that contains data, save that data in the trashcan 263 if( assaySample.containsData() ) { 264 // Create dummy sample 265 String newSampleToken = 'TrashSample ' + new Date().format( 'yyyyMMddHHmmssSSS') + ( Math.random() * 10000 ); 266 Sample dummySample = new Sample( sampleToken: newSampleToken, name: assaySample.sample.name, study: trashcan ); 267 trashcan.addToSamples( dummySample ); 268 dummySample.save() 269 270 Assay assay = assaySample.assay; 271 272 // Create a dummy assay copy of the existing assay 273 String newAssayToken = 'TrashAssay ' + new Date().format( 'yyyyMMddHHmmssSSS') + ( Math.random() * 10000 ); 274 275 Assay dummyAssay = new Assay( assayToken: newAssayToken, name: assay.name, study: trashcan ); 276 trashcan.addToAssays( dummyAssay ); 277 dummyAssay.save() 278 279 // Create dummy assay sample 280 AssaySample dummyAssaySample = new AssaySample( assay: dummyAssay, sample: dummySample ); 281 282 dummyAssay.addToAssaySamples( dummyAssaySample ); 283 dummySample.addToAssaySamples( dummyAssaySample ); 284 dummyAssaySample.save(); 285 286 // Move data from this assay sample to the trash version of it 287 assaySample.moveValuableDataTo( dummyAssaySample ); 288 dummyAssaySample.save(); 289 } 290 } 291 292 /** 293 * Retrieves the trashcan study from the database 294 */ 295 def giveTrashcan = { 296 def study = Study.findByTrashcan( true ); 297 298 if( !study ) 299 return null; 300 else 301 return study 302 } 303 304 /** 305 * Creates a new trashcan study. Should only be used by the bootstrap to create a trashcan 306 */ 307 def createTrashcan = { 308 def study = new Study( name: "Trashcan", studyToken: "trash", trashcan: true ) 309 study.save(); 310 } 311 161 312 /** 162 313 * Cleans up the trash by removing empty assays or samples. Empty means: … … 168 319 def cleanTrash = { 169 320 def studies = Study.findAllByTrashcan( true ); 170 321 171 322 studies.each { study -> 172 323 def numAssays = study.assays?.size() 173 324 def assayList = study.assays?.toList(); 174 325 175 326 def numSamples 176 327 def sampleList 177 328 178 329 // Loop backwards through the assays in order to facilitate removing assays 179 330 for( def i = numAssays -1; i >= 0; i-- ) { … … 182 333 numSamples = assayList[ i ].assaySamples.size() 183 334 sampleList = assayList[ i ].assaySamples.toList(); 184 335 185 336 for( def j = numSamples - 1; j >= 0; j-- ) { 186 337 def s = sampleList[ j ]; … … 197 348 study.removeFromAssays( assayList[ i ] ); 198 349 assayList[i].delete(); 199 } 200 } 201 350 } 351 } 352 202 353 // Loop through samples and delete the ones not referenced by an assaysample 203 354 /* 204 numSamples = study.samples?.size() 205 sampleList = study.samples?.toList(); 206 207 for( def j = numSamples - 1; j >= 0; j-- ) { 208 def s = sampleList[ j ]; 209 if( s.assaySamples == null || s.assaySamples.size() == 0 ) { 210 study.removeFromSamples(s); 211 s.delete(flush:true); 212 } 213 } 214 */ 215 } 216 } 217 355 numSamples = study.samples?.size() 356 sampleList = study.samples?.toList(); 357 for( def j = numSamples - 1; j >= 0; j-- ) { 358 def s = sampleList[ j ]; 359 if( s.assaySamples == null || s.assaySamples.size() == 0 ) { 360 study.removeFromSamples(s); 361 s.delete(flush:true); 362 } 363 } 364 */ 365 } 366 } 367 218 368 /** 219 369 * Restore an assay from trash and put the contents of the assay in another assay … … 227 377 // Find a sample with the same name 228 378 def restoreSample = restoreTo.assaySamples.find { it.sample?.name == assaySample.sample?.name } 229 379 230 380 if( restoreSample ) { 231 381 this.restoreSample( assaySample, restoreSample ); … … 234 384 } 235 385 } 236 386 237 387 /** 238 388 * Restore a sample from trash and put the contents of the sample into another sample
Note: See TracChangeset
for help on using the changeset viewer.