Changeset 1002 for trunk/grails-app/services
- Timestamp:
- Oct 27, 2010, 3:50:56 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/services/dbnp/importer/ImporterService.groovy
r999 r1002 239 239 * 240 240 * @param study entity Study 241 * @param datamatrix two dimensional array containing entities with values read from Excel file *241 * @param datamatrix two dimensional array containing entities with values read from Excel file 242 242 */ 243 243 def saveDatamatrix(Study study, datamatrix) { 244 244 def validatedSuccesfully = 0 245 def entitystored = null 245 246 study.refresh() 246 247 … … 248 249 datamatrix.each { record -> 249 250 record.each { entity -> 250 251 252 251 switch (entity.getClass()) { 252 case Study : print "Persisting Study `" + entity + "`: " 253 if (persistEntity(entity)) validatedSuccesfully++ 253 254 break 254 255 case Subject : print "Persisting Subject `" + entity + "`: " 255 256 entity.parent = study 256 study.addToSubjects(entity) 257 258 // is the current entity not already in the database? 259 entitystored = isEntityStored(entity) 260 261 // this entity is new, so add it to the study 262 if (entitystored==null) study.addToSubjects(entity) 263 else // existing entity, so update it 264 updateEntity(entitystored, entity) 265 257 266 if (persistEntity(study)) validatedSuccesfully++ 258 267 break … … 271 280 study.addToSamplingEvents(entity) 272 281 if (persistEntity(entity)) validatedSuccesfully++ 273 break ;282 break 274 283 default : println "Skipping persisting of `" + entity.getclass() +"`" 275 284 break … … 278 287 } // end datamatrix 279 288 return validatedSuccesfully 289 } 290 291 /** 292 * Check whether an entity already exists within a study. A combination of 293 * the study where the entity will be stored and a unique field in the entity is 294 * used to check whether the instantiated entity (read from Excel) is new. 295 * If the entity is found in the database it will be returned as so and 296 * it should update the entity-values (read from Ecel) into the record in the database. 297 * 298 * @param entity entity object like a Study, Subject, Sample et cetera 299 * @return entity if found, otherwise null 300 */ 301 def isEntityStored(entity) { 302 switch (entity.getClass()) { 303 case Study : return Study.findByCode(entity.code) 304 break 305 case Subject : return Subject.findByParentAndName(entity.parent, entity.name) 306 break 307 case Event : break 308 case Sample : break 309 case SamplingEvent : break 310 default : // unknown entity 311 return null 312 } 313 } 314 315 /** 316 * Find the entity and update the fields. The entity is an instance 317 * read from Excel. This method looks in the database for the entity 318 * having the same identifier. If it has found the same entity 319 * already in the database, it will update the record. 320 * 321 * @param entitystored existing record in the database to update 322 * @param entity entity read from Excel 323 */ 324 def updateEntity(entitystored, entity) { 325 switch (entity.getClass()) { 326 case Study : break 327 case Subject : entitystored.properties = entity.properties 328 entitystored.save() 329 break 330 case Event : break 331 case Sample : break 332 case SamplingEvent : break 333 default : // unknown entity 334 return null 335 } 280 336 } 281 337
Note: See TracChangeset
for help on using the changeset viewer.