Changeset 1540
- Timestamp:
- Feb 21, 2011, 5:10:50 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/dbnp/studycapturing/StudyWizardController.groovy
r1539 r1540 1229 1229 def handlePublications(flow, flash, params) { 1230 1230 flash.wizardErrors = [:] 1231 1232 if (!flow.study.publications) flow.study.publications = [] 1233 1234 // Check the ids of the pubblications that should be attached 1235 // to this study. If they are already attached, keep 'm. If 1236 // studies are attached that are not in the selected (i.e. the 1237 // user deleted them), remove them 1238 def publicationIDs = params.get('publication_ids') 1239 if (publicationIDs) { 1240 // Find the individual IDs and make integers 1241 publicationIDs = publicationIDs.split(',').collect { Integer.parseInt(it, 10) } 1242 1243 // First remove the publication that are not present in the array 1244 flow.study.publications.removeAll { publication -> !publicationIDs.find { id -> id == publication.id } } 1245 1246 // Add those publications not yet present in the database 1247 publicationIDs.each { id -> 1248 if (!flow.study.publications.find { publication -> id == publication.id }) { 1249 def publication = Publication.get(id) 1250 if (publication) { 1251 flow.study.addToPublications(publication) 1252 } else { 1253 log.info('.publication with ID ' + id + ' not found in database.') 1254 } 1255 } 1256 } 1257 1258 } else { 1259 log.info('.no publications selected.') 1260 flow.study.publications.clear() 1261 } 1262 1231 handleStudyPublications( flow.study, params ); 1263 1232 } 1264 1233 … … 1273 1242 flash.wizardErrors = [:] 1274 1243 1275 if (!flow.study.persons) flow.study.persons = [] 1276 1277 // Check the ids of the contacts that should be attached 1278 // to this study. If they are already attached, keep 'm. If 1279 // studies are attached that are not in the selected (i.e. the 1280 // user deleted them), remove them 1281 1282 // Contacts are saved as [person_id]-[role_id] 1283 def contactIDs = params.get('contacts_ids') 1284 if (contactIDs) { 1285 // Find the individual IDs and make integers 1286 contactIDs = contactIDs.split(',').collect { 1287 def parts = it.split('-') 1288 return [person: Integer.parseInt(parts[0]), role: Integer.parseInt(parts[1])] 1289 } 1290 1291 // First remove the contacts that are not present in the array 1292 flow.study.persons.removeAll { 1293 studyperson -> !contactIDs.find { ids -> (ids.person == studyperson.person.id) && (ids.role == studyperson.role.id) } 1294 } 1295 1296 // Add those contacts not yet present in the database 1297 contactIDs.each { ids -> 1298 if (!flow.study.persons.find { studyperson -> (ids.person == studyperson.person.id) && (ids.role == studyperson.role.id) }) { 1299 def person = Person.get(ids.person) 1300 def role = PersonRole.get(ids.role) 1301 if (person && role) { 1302 // Find a studyperson object with these parameters 1303 def studyPerson = StudyPerson.findAll().find { studyperson -> studyperson.person.id == person.id && studyperson.role.id == role.id } 1304 1305 // If if does not yet exist, save the example 1306 if (!studyPerson) { 1307 studyPerson = new StudyPerson( 1308 person: person, 1309 role: role 1310 ) 1311 studyPerson.save(flush: true) 1312 } 1313 1314 flow.study.addToPersons(studyPerson) 1315 } else { 1316 log.info('.person ' + ids.person + ' or Role ' + ids.role + ' not found in database.') 1317 } 1318 } 1319 } 1320 } else { 1321 log.info('.no persons selected.') 1322 flow.study.persons.clear() 1323 } 1324 1244 handleStudyContacts( flow.study, params ); 1325 1245 } 1326 1246 … … 1330 1250 * @param Map localAttributeMap (the flash scope) 1331 1251 * @param Map GrailsParameterMap (the flow parameters = form data) 1332 1252 * @param String 'readers' or 'writers' 1333 1253 * @return boolean 1334 1254 */ 1335 1255 def handleUsers(flow, flash, params, type) { 1336 1256 flash.wizardErrors = [:] 1337 1338 def users = [] 1339 1340 if (type == "readers" && flow.study.readers ) { 1341 users += flow.study.readers 1342 } else if (type == "writers" && flow.study.writers ) { 1343 users += flow.study.writers 1344 } 1345 1346 // Check the ids of the contacts that should be attached 1347 // to this study. If they are already attached, keep 'm. If 1348 // studies are attached that are not in the selected (i.e. the 1349 // user deleted them), remove them 1350 1351 // Users are saved as user_id 1352 def userIDs = params.get(type + '_ids') 1353 if (userIDs) { 1354 // Find the individual IDs and make integers 1355 userIDs = userIDs.split(',').collect { Integer.parseInt(it, 10) } 1356 1357 // First remove the publication that are not present in the array 1358 users.removeAll { user -> !userIDs.find { id -> id == user.id } } 1359 1360 // Add those publications not yet present in the database 1361 userIDs.each { id -> 1362 if (!users.find { user -> id == user.id }) { 1363 def user = SecUser.get(id) 1364 if (user) { 1365 users.add(user) 1366 } else { 1367 log.info('.user with ID ' + id + ' not found in database.') 1368 } 1369 } 1370 } 1371 1372 } else { 1373 log.info('.no users selected.') 1374 users.clear() 1375 } 1376 1377 if (type == "readers") { 1378 if (flow.study.readers) 1379 flow.study.readers.clear() 1380 users.each { flow.study.addToReaders(it) } 1381 } else if (type == "writers") { 1382 if (flow.study.writers) 1383 flow.study.writers.clear() 1384 1385 users.each { flow.study.addToWriters(it) } 1386 } 1387 } 1257 handleStudyUsers( flow.study, params, type ); 1258 } 1259 1260 /** 1261 * re-usable code for handling publications form data 1262 * @param study Study object to update 1263 * @param params GrailsParameterMap (the flow parameters = form data) 1264 * @returns boolean 1265 */ 1266 def handleStudyPublications(Study study, params) { 1267 if (study.publications) study.publications = [] 1268 1269 // Check the ids of the pubblications that should be attached 1270 // to this study. If they are already attached, keep 'm. If 1271 // studies are attached that are not in the selected (i.e. the 1272 // user deleted them), remove them 1273 def publicationIDs = params.get('publication_ids') 1274 if (publicationIDs) { 1275 // Find the individual IDs and make integers 1276 publicationIDs = publicationIDs.split(',').collect { Integer.parseInt(it, 10) } 1277 1278 // First remove the publication that are not present in the array 1279 if( study.publications ) 1280 study.publications.removeAll { publication -> !publicationIDs.find { id -> id == publication.id } } 1281 1282 // Add those publications not yet present in the database 1283 publicationIDs.each { id -> 1284 if (!study.publications.find { publication -> id == publication.id }) { 1285 def publication = Publication.get(id) 1286 if (publication) { 1287 study.addToPublications(publication) 1288 } else { 1289 log.info('.publication with ID ' + id + ' not found in database.') 1290 } 1291 } 1292 } 1293 1294 } else { 1295 log.info('.no publications selected.') 1296 if( study.publications ) 1297 study.publications.clear() 1298 } 1299 } 1300 1301 /** 1302 * re-usable code for handling contacts form data 1303 * @param study Study object to update 1304 * @param Map GrailsParameterMap (the flow parameters = form data) 1305 * @return boolean 1306 */ 1307 def handleStudyContacts(Study study, params) { 1308 if (!study.persons) study.persons = [] 1309 1310 // Check the ids of the contacts that should be attached 1311 // to this study. If they are already attached, keep 'm. If 1312 // studies are attached that are not in the selected (i.e. the 1313 // user deleted them), remove them 1314 1315 // Contacts are saved as [person_id]-[role_id] 1316 def contactIDs = params.get('contacts_ids') 1317 if (contactIDs) { 1318 // Find the individual IDs and make integers 1319 contactIDs = contactIDs.split(',').collect { 1320 def parts = it.split('-') 1321 return [person: Integer.parseInt(parts[0]), role: Integer.parseInt(parts[1])] 1322 } 1323 1324 // First remove the contacts that are not present in the array 1325 if( study.persons ) { 1326 study.persons.removeAll { 1327 studyperson -> !contactIDs.find { ids -> (ids.person == studyperson.person.id) && (ids.role == studyperson.role.id) } 1328 } 1329 } 1330 1331 // Add those contacts not yet present in the database 1332 contactIDs.each { ids -> 1333 if (!study.persons.find { studyperson -> (ids.person == studyperson.person.id) && (ids.role == studyperson.role.id) }) { 1334 def person = Person.get(ids.person) 1335 def role = PersonRole.get(ids.role) 1336 if (person && role) { 1337 // Find a studyperson object with these parameters 1338 def studyPerson = StudyPerson.findAll().find { studyperson -> studyperson.person.id == person.id && studyperson.role.id == role.id } 1339 1340 // If if does not yet exist, save the example 1341 if (!studyPerson) { 1342 studyPerson = new StudyPerson( 1343 person: person, 1344 role: role 1345 ) 1346 studyPerson.save(flush: true) 1347 } 1348 1349 study.addToPersons(studyPerson) 1350 } else { 1351 log.info('.person ' + ids.person + ' or Role ' + ids.role + ' not found in database.') 1352 } 1353 } 1354 } 1355 } else { 1356 log.info('.no persons selected.') 1357 if( study.persons ) 1358 study.persons.clear() 1359 } 1360 } 1361 1362 /** 1363 * re-usable code for handling contacts form data 1364 * @param study Study object to update 1365 * @param Map GrailsParameterMap (the flow parameters = form data) 1366 * @param String 'readers' or 'writers' 1367 * @return boolean 1368 */ 1369 def handleStudyUsers(Study study, params, type) { 1370 def users = [] 1371 1372 if (type == "readers" && study.readers ) { 1373 users += study.readers 1374 } else if (type == "writers" && study.writers ) { 1375 users += study.writers 1376 } 1377 1378 // Check the ids of the contacts that should be attached 1379 // to this study. If they are already attached, keep 'm. If 1380 // studies are attached that are not in the selected (i.e. the 1381 // user deleted them), remove them 1382 1383 // Users are saved as user_id 1384 def userIDs = params.get(type + '_ids') 1385 1386 if (userIDs) { 1387 // Find the individual IDs and make integers 1388 userIDs = userIDs.split(',').collect { Long.valueOf(it, 10) } 1389 1390 // First remove the publication that are not present in the array 1391 users.removeAll { user -> !userIDs.find { id -> id == user.id } } 1392 1393 // Add those publications not yet present in the database 1394 userIDs.each { id -> 1395 if (!users.find { user -> id == user.id }) { 1396 def user = SecUser.get(id) 1397 if (user) { 1398 users.add(user) 1399 } else { 1400 log.info('.user with ID ' + id + ' not found in database.') 1401 } 1402 } 1403 } 1404 1405 } else { 1406 log.info('.no users selected.') 1407 users.clear() 1408 } 1409 1410 if (type == "readers") { 1411 if (study.readers) 1412 study.readers.clear() 1413 1414 users.each { study.addToReaders(it) } 1415 } else if (type == "writers") { 1416 if (study.writers) 1417 study.writers.clear() 1418 1419 users.each { study.addToWriters(it) } 1420 1421 } 1422 } 1423 1424 1425 1388 1426 1389 1427 /**
Note: See TracChangeset
for help on using the changeset viewer.