1 | /** |
---|
2 | * SimpleQueryController Controler |
---|
3 | * |
---|
4 | * Description of my controller |
---|
5 | * |
---|
6 | * @author vincent@ludden.nl |
---|
7 | * @since 20100526 |
---|
8 | * @package dbnp.query |
---|
9 | * |
---|
10 | * Revision information: |
---|
11 | * $Rev: 1196 $ |
---|
12 | * $Author: j.saito@maastrichtuniversity.nl $ |
---|
13 | * $Date: 2010-11-24 10:47:41 +0000 (wo, 24 nov 2010) $ |
---|
14 | */ |
---|
15 | package dbnp.query |
---|
16 | |
---|
17 | import dbnp.data.* |
---|
18 | import dbnp.studycapturing.Study |
---|
19 | import dbnp.studycapturing.Assay |
---|
20 | import org.compass.core.engine.SearchEngineQueryParseException |
---|
21 | import dbnp.rest.common.CommunicationManager |
---|
22 | |
---|
23 | class SimpleQueryController { |
---|
24 | /** |
---|
25 | * index closure |
---|
26 | */ |
---|
27 | def index = { |
---|
28 | redirect( action:'pages') |
---|
29 | } |
---|
30 | |
---|
31 | def searchableService |
---|
32 | |
---|
33 | def pagesFlow = { |
---|
34 | |
---|
35 | // Starting simpleQuery flow, initialize variables |
---|
36 | onStart { |
---|
37 | flow.search_term = null |
---|
38 | flow.search_sa_compounds = [] |
---|
39 | flow.search_sa_operators = [] |
---|
40 | flow.search_sa_values = [] |
---|
41 | flow.page = 0 |
---|
42 | flow.pages = [ |
---|
43 | [title: 'Query'], |
---|
44 | [title: 'Results'] |
---|
45 | ] |
---|
46 | } |
---|
47 | |
---|
48 | // Render the query page and handle its actions |
---|
49 | query { |
---|
50 | render(view: "/simpleQuery/mainPage") |
---|
51 | |
---|
52 | |
---|
53 | onRender { |
---|
54 | flow.operators = ['>', '=', '<'] |
---|
55 | |
---|
56 | if (!flow.search_sa_compounds) { |
---|
57 | flow.showFirstRowCompounds = true |
---|
58 | } else { |
---|
59 | flow.showFirstRowCompounds = false |
---|
60 | } |
---|
61 | |
---|
62 | flow.species = Term.findAll() |
---|
63 | flow.page = 1 |
---|
64 | } |
---|
65 | |
---|
66 | on("search") { |
---|
67 | if (!params.search_term.trim()) { |
---|
68 | return [:] |
---|
69 | } |
---|
70 | }.to "searching" |
---|
71 | |
---|
72 | on("refresh").to "query" |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | // Searching for results |
---|
77 | searching { |
---|
78 | action { |
---|
79 | def searchResult |
---|
80 | def searchGscfResult |
---|
81 | def searchSamResult = [] |
---|
82 | |
---|
83 | // Map GSCF parameters |
---|
84 | flow.search_term = params.search_term // String |
---|
85 | |
---|
86 | // Map SAM parameters |
---|
87 | if (params.sa_compound instanceof String) { |
---|
88 | flow.search_sa_compounds = [] |
---|
89 | flow.search_sa_operators = [] |
---|
90 | flow.search_sa_values = [] |
---|
91 | |
---|
92 | if (params.sa_compound) { |
---|
93 | flow.search_sa_compounds.add(params.sa_compound) |
---|
94 | flow.search_sa_operators.add(params.sa_operator) |
---|
95 | flow.search_sa_values.add(params.sa_value) |
---|
96 | } |
---|
97 | } else { |
---|
98 | flow.search_sa_compounds = params.sa_compound as List |
---|
99 | flow.search_sa_operators = params.sa_operator as List |
---|
100 | flow.search_sa_values = params.sa_value as List |
---|
101 | } |
---|
102 | |
---|
103 | // Search the keyword with the Searchable plugin |
---|
104 | try { |
---|
105 | searchGscfResult = searchableService.search(flow.search_term) |
---|
106 | } catch (SearchEngineQueryParseException ex) { |
---|
107 | println ex |
---|
108 | return [parseException: true] |
---|
109 | } |
---|
110 | |
---|
111 | // Map non-study objects to Studies |
---|
112 | // ... todo when the plugin works and I can see the output |
---|
113 | |
---|
114 | // Search in the SAM module when a compound is entered |
---|
115 | def listSamStudies = [] |
---|
116 | def listGscfStudies = [] |
---|
117 | def listStudies = [] |
---|
118 | |
---|
119 | if ((flow.search_sa_compounds) && (flow.search_sa_compounds.size() > 0)) { |
---|
120 | def resultSAM = [:] |
---|
121 | resultSAM = this.searchSAM(flow.search_sa_compounds, flow.search_sa_operators, flow.search_sa_values) |
---|
122 | listSamStudies = resultSAM.get('studies') |
---|
123 | } |
---|
124 | |
---|
125 | for (i in searchGscfResult.results) { |
---|
126 | def objStudy = Study.get(i.id) |
---|
127 | listGscfStudies.add(objStudy.id) |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | |
---|
132 | // Merge the results of all searches |
---|
133 | if (listSamStudies.size() > 0) { |
---|
134 | listStudies = listGscfStudies.intersect(listSamStudies) |
---|
135 | } else { |
---|
136 | if (!flow.search_sa_compounds) { |
---|
137 | listStudies = listGscfStudies |
---|
138 | } else { |
---|
139 | listStudies = [] |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | def listObjStudies = [] |
---|
144 | for (i in listStudies) { |
---|
145 | def objStudy = Study.get(i) |
---|
146 | listObjStudies.add(objStudy) |
---|
147 | } |
---|
148 | |
---|
149 | // Save the results in the flow |
---|
150 | flow.listStudies = listObjStudies |
---|
151 | |
---|
152 | } |
---|
153 | |
---|
154 | on("error").to "query" |
---|
155 | on("success").to "results" |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | // Render result page including search options |
---|
160 | results { |
---|
161 | render(view: "/simpleQuery/mainPage") |
---|
162 | |
---|
163 | onRender { |
---|
164 | flow.page = 2 |
---|
165 | |
---|
166 | flow.showFirstRowCompounds = false |
---|
167 | } |
---|
168 | |
---|
169 | on("reset") { |
---|
170 | flow.search_term = null |
---|
171 | flow.studies = null |
---|
172 | flow.search_sa_compounds = [] |
---|
173 | flow.search_sa_operators = [] |
---|
174 | flow.search_sa_values = [] |
---|
175 | flow.search_tt_genepaths = null |
---|
176 | flow.search_tt_regulations = null |
---|
177 | }.to "query" |
---|
178 | |
---|
179 | on("search").to "searching" |
---|
180 | on("refresh").to "results" |
---|
181 | } |
---|
182 | |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | static Map searchSAM (List compounds, List operators, List values) { |
---|
187 | |
---|
188 | |
---|
189 | if (compounds.size() == 1) { |
---|
190 | def tmpResult = CommunicationManager.getQueryResult( compounds.get(0) ) |
---|
191 | def studies = tmpResult.studiesIds.collect{ Study.findByCode(it) } |
---|
192 | def assays = tmpResult.assays.collect { [it, Assay.findByExternalAssayID( it.externalAssayID ) ] } |
---|
193 | def mapSamResult = [studies:studies, assays:assays] |
---|
194 | |
---|
195 | |
---|
196 | def listStudies = [] |
---|
197 | |
---|
198 | for (i in mapSamResult.assays) { |
---|
199 | def objAssay = Assay.get(i) |
---|
200 | listStudies.add(objAssay.parent.id) |
---|
201 | } |
---|
202 | |
---|
203 | mapSamResult.put("studies", listStudies) |
---|
204 | |
---|
205 | return mapSamResult |
---|
206 | |
---|
207 | } else { |
---|
208 | def tmpResult = CommunicationManager.getQueryResult( compounds.get(0) ) |
---|
209 | def studies = tmpResult.studiesIds.collect{ Study.findByCode(it) } |
---|
210 | def mapSamResult = [studies:studies, assays:[]] |
---|
211 | |
---|
212 | def i = 0 |
---|
213 | compounds.each { compound -> |
---|
214 | tmpSamResult = CommunicationManager.getQueryResult(compound) |
---|
215 | |
---|
216 | if (i == 0) { |
---|
217 | mapSamResult.assays = tmpSamResult.assays |
---|
218 | } else { |
---|
219 | if (mapSamResult.assays) { |
---|
220 | mapSamResult.assays = mapSamResult.assays.intersect(tmpSamResult.assays) |
---|
221 | } |
---|
222 | } |
---|
223 | i++ |
---|
224 | } |
---|
225 | |
---|
226 | def listStudies = [] |
---|
227 | |
---|
228 | for (j in mapSamResult.assays) { |
---|
229 | def objAssay = Assay.get(j) |
---|
230 | listStudies.add(objAssay.parent.id) |
---|
231 | } |
---|
232 | |
---|
233 | mapSamResult.put("studies", listStudies) |
---|
234 | |
---|
235 | return mapSamResult |
---|
236 | } |
---|
237 | |
---|
238 | } |
---|
239 | |
---|
240 | |
---|
241 | |
---|
242 | static List merge (List list1, List list2) { |
---|
243 | |
---|
244 | def resultList = [] |
---|
245 | resultList = list1.intersect(list2) |
---|
246 | |
---|
247 | return resultList |
---|
248 | } |
---|
249 | |
---|
250 | } |
---|