[2181] | 1 | /** |
---|
| 2 | * ApiController Controler |
---|
| 3 | * |
---|
| 4 | * Description of my controller |
---|
| 5 | * |
---|
| 6 | * @author your email (+name?) |
---|
| 7 | * @since 2010mmdd |
---|
| 8 | * @package ??? |
---|
| 9 | * |
---|
| 10 | * Revision information: |
---|
| 11 | * $Rev$ |
---|
| 12 | * $Author$ |
---|
| 13 | * $Date$ |
---|
| 14 | */ |
---|
| 15 | package api |
---|
| 16 | |
---|
| 17 | import grails.plugins.springsecurity.Secured |
---|
| 18 | import grails.converters.JSON |
---|
| 19 | import dbnp.studycapturing.Study |
---|
| 20 | import dbnp.authentication.SecUser |
---|
| 21 | |
---|
| 22 | class ApiController { |
---|
| 23 | def authenticationService |
---|
| 24 | def ApiService |
---|
| 25 | |
---|
| 26 | /** |
---|
| 27 | * index closure |
---|
| 28 | */ |
---|
| 29 | def index = { |
---|
| 30 | render(view:'index') |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | @Secured(['ROLE_CLIENT', 'ROLE_ADMIN']) |
---|
| 34 | def authenticate = { |
---|
| 35 | println "api::authenticate: ${params}" |
---|
| 36 | |
---|
| 37 | // see if we already have a token on file for this device id |
---|
| 38 | def token = Token.findByDeviceID(params.deviceID) |
---|
| 39 | |
---|
| 40 | // generate a new token if we don't have a token on file |
---|
| 41 | def result = [:] |
---|
| 42 | try { |
---|
| 43 | if (!token) { |
---|
| 44 | // generate a token for this device |
---|
| 45 | token = new Token( |
---|
| 46 | deviceID: params.deviceID, |
---|
| 47 | deviceToken: UUID.randomUUID().toString(), |
---|
| 48 | sequence: 0 |
---|
| 49 | ).save(failOnError: true) |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | result = ['token':token.deviceToken,'sequence':token.sequence] |
---|
| 53 | |
---|
| 54 | // set output headers |
---|
| 55 | response.status = 200 |
---|
| 56 | } catch (Exception e) { |
---|
| 57 | // caught an error |
---|
| 58 | response.status = 500 |
---|
| 59 | result = ['error':e.getMessage()] |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | response.contentType = 'application/json;charset=UTF-8' |
---|
| 63 | |
---|
| 64 | if (params.containsKey('callback')) { |
---|
| 65 | render "${params.callback}(${result as JSON})" |
---|
| 66 | } else { |
---|
| 67 | render result as JSON |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | @Secured(['ROLE_CLIENT', 'ROLE_ADMIN']) |
---|
| 72 | def getStudies = { |
---|
| 73 | String deviceID = (params.containsKey('deviceID')) ? params.deviceID : '' |
---|
| 74 | String validation = (params.containsKey('validation')) ? params.validation : '' |
---|
| 75 | |
---|
| 76 | // check |
---|
| 77 | if (!apiService.validateRequest(deviceID,validation)) { |
---|
| 78 | response.sendError(401, 'Unauthorized') |
---|
| 79 | } else { |
---|
| 80 | def user = authenticationService.getLoggedInUser() |
---|
| 81 | def readableStudies = Study.giveReadableStudies(user) |
---|
| 82 | def studies = [] |
---|
| 83 | |
---|
| 84 | // iterate through studies and define resultset |
---|
| 85 | readableStudies.each { study -> |
---|
| 86 | // get result data |
---|
| 87 | studies[ studies.size() ] = [ |
---|
| 88 | 'title' : study.title, |
---|
| 89 | 'description' : study.description, |
---|
| 90 | 'subjects' : study.subjects.size(), |
---|
| 91 | 'species' : study.subjects.species.collect { it.name }.unique(), |
---|
| 92 | 'assays' : study.assays.collect { it.module.name }.unique(), |
---|
| 93 | 'events' : study.events.size(), |
---|
| 94 | 'uniqueEvents' : study.events.collect { it.toString() }.unique(), |
---|
| 95 | 'samplingEvents' : study.samplingEvents.size(), |
---|
| 96 | 'uniqueSamplingEvents' : study.samplingEvents.collect { it.toString() }.unique(), |
---|
| 97 | 'eventGroups' : study.eventGroups.size(), |
---|
| 98 | 'uniqueEventGroups' : study.eventGroups.collect { it.name }.unique(), |
---|
| 99 | 'samples' : study.samples.size() |
---|
| 100 | ] |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | def result = [ |
---|
| 105 | 'count' : studies.size(), |
---|
| 106 | 'studies' : studies |
---|
| 107 | ] |
---|
| 108 | |
---|
| 109 | // set output headers |
---|
| 110 | response.status = 200 |
---|
| 111 | response.contentType = 'application/json;charset=UTF-8' |
---|
| 112 | |
---|
| 113 | if (params.containsKey('callback')) { |
---|
| 114 | render "${params.callback}(${result as JSON})" |
---|
| 115 | } else { |
---|
| 116 | render result as JSON |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | } |
---|