1 | /** |
---|
2 | * AjaxController Controller |
---|
3 | * |
---|
4 | * A collection of application wide Ajax resources |
---|
5 | * |
---|
6 | * @author Jeroen Wesbeek work@osx.eu |
---|
7 | * @since 20120123 |
---|
8 | * |
---|
9 | * Revision information: |
---|
10 | * $Rev$ |
---|
11 | * $Author$ |
---|
12 | * $Date$ |
---|
13 | */ |
---|
14 | package generic |
---|
15 | |
---|
16 | import dbnp.studycapturing.* |
---|
17 | import grails.converters.JSON |
---|
18 | |
---|
19 | class AjaxController { |
---|
20 | def authenticationService |
---|
21 | |
---|
22 | /** |
---|
23 | * Get all unique species (for accessible studies) |
---|
24 | */ |
---|
25 | def uniqueSpecies = { |
---|
26 | def result = Subject.executeQuery("SELECT DISTINCT a.species FROM Subject a") |
---|
27 | |
---|
28 | // set output header to json |
---|
29 | response.contentType = 'application/json' |
---|
30 | |
---|
31 | // render result |
---|
32 | if (params.callback) { |
---|
33 | render "${params.callback}(${result as JSON})" |
---|
34 | } else { |
---|
35 | render result as JSON |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | def uniqueEventTemplateNames = { |
---|
40 | def user = authenticationService.getLoggedInUser() |
---|
41 | def studies = Study.giveReadableStudies(user) |
---|
42 | def uniqueEventTemplates = [] |
---|
43 | |
---|
44 | // iterate through studies |
---|
45 | studies.each { study -> |
---|
46 | study.events.each { event -> |
---|
47 | if (!uniqueEventTemplates.contains(event.template)) { |
---|
48 | uniqueEventTemplates.add(event.template) |
---|
49 | } |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | def result = uniqueEventTemplates |
---|
54 | |
---|
55 | // set output header to json |
---|
56 | response.contentType = 'application/json' |
---|
57 | |
---|
58 | // render result |
---|
59 | if (params.callback) { |
---|
60 | render "${params.callback}(${result as JSON})" |
---|
61 | } else { |
---|
62 | render result as JSON |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | def uniqueSamplingEventTemplateNames = { |
---|
67 | def user = authenticationService.getLoggedInUser() |
---|
68 | def studies = Study.giveReadableStudies(user) |
---|
69 | def uniqueSamplingEventTemplates = [] |
---|
70 | |
---|
71 | // iterate through studies |
---|
72 | studies.each { study -> |
---|
73 | study.samplingEvents.each { event -> |
---|
74 | if (!uniqueSamplingEventTemplates.contains(event.template)) { |
---|
75 | uniqueSamplingEventTemplates.add(event.template) |
---|
76 | } |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | def result = uniqueSamplingEventTemplates |
---|
81 | |
---|
82 | // set output header to json |
---|
83 | response.contentType = 'application/json' |
---|
84 | |
---|
85 | // render result |
---|
86 | if (params.callback) { |
---|
87 | render "${params.callback}(${result as JSON})" |
---|
88 | } else { |
---|
89 | render result as JSON |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | def studyCount = { |
---|
94 | println params |
---|
95 | println params.containsKey('data') |
---|
96 | // println params['data']['uniqueSpecies'] |
---|
97 | |
---|
98 | def matched = false |
---|
99 | def user = authenticationService.getLoggedInUser() |
---|
100 | def studies = Study.giveReadableStudies(user) |
---|
101 | def matchedStudies = [] |
---|
102 | |
---|
103 | // parameters |
---|
104 | /* |
---|
105 | def uniqueSpecies = (params['data']['uniqueSpecies']) ? params['data']['uniqueSpecies'] : [] |
---|
106 | println uniqueSpecies.class |
---|
107 | println uniqueSpecies.size() |
---|
108 | |
---|
109 | // iterate through studies |
---|
110 | studies.each { study -> |
---|
111 | matched = false |
---|
112 | // matched = (study.subjects.find{uniqueSpecies.contains(it.species.id)}) ? true : false; |
---|
113 | println "study: ${study} -> matched: ${matched}" |
---|
114 | |
---|
115 | if (matched) matchedStudies.add(study) |
---|
116 | } |
---|
117 | */ |
---|
118 | println "------" |
---|
119 | |
---|
120 | def result = ['count':matchedStudies.size()]; |
---|
121 | |
---|
122 | // set output header to json |
---|
123 | response.contentType = 'application/json' |
---|
124 | |
---|
125 | // render result |
---|
126 | if (params.callback) { |
---|
127 | render "${params.callback}(${result as JSON})" |
---|
128 | } else { |
---|
129 | render result as JSON |
---|
130 | } |
---|
131 | } |
---|
132 | } |
---|