[478] | 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: 1584 $ |
---|
| 12 | * $Author: work@osx.eu $ |
---|
| 13 | * $Date: 2011-03-03 19:02:44 +0000 (do, 03 mrt 2011) $ |
---|
| 14 | */ |
---|
| 15 | package dbnp.query |
---|
| 16 | |
---|
[547] | 17 | import dbnp.data.* |
---|
[1584] | 18 | import dbnp.studycapturing.* |
---|
| 19 | import org.dbnp.gdt.* |
---|
[547] | 20 | import org.compass.core.engine.SearchEngineQueryParseException |
---|
[642] | 21 | import dbnp.rest.common.CommunicationManager |
---|
[547] | 22 | |
---|
[478] | 23 | class SimpleQueryController { |
---|
| 24 | /** |
---|
| 25 | * index closure |
---|
| 26 | */ |
---|
| 27 | def index = { |
---|
| 28 | redirect( action:'pages') |
---|
| 29 | } |
---|
| 30 | |
---|
[547] | 31 | def searchableService |
---|
| 32 | |
---|
[478] | 33 | def pagesFlow = { |
---|
| 34 | |
---|
[642] | 35 | // Starting simpleQuery flow, initialize variables |
---|
[478] | 36 | onStart { |
---|
[594] | 37 | flow.search_term = null |
---|
[650] | 38 | flow.search_sa_compounds = [] |
---|
| 39 | flow.search_sa_operators = [] |
---|
| 40 | flow.search_sa_values = [] |
---|
[642] | 41 | flow.page = 0 |
---|
[478] | 42 | flow.pages = [ |
---|
| 43 | [title: 'Query'], |
---|
| 44 | [title: 'Results'] |
---|
| 45 | ] |
---|
| 46 | } |
---|
| 47 | |
---|
[555] | 48 | // Render the query page and handle its actions |
---|
[499] | 49 | query { |
---|
[505] | 50 | render(view: "/simpleQuery/mainPage") |
---|
[642] | 51 | |
---|
[1196] | 52 | |
---|
[547] | 53 | onRender { |
---|
[642] | 54 | flow.operators = ['>', '=', '<'] |
---|
[650] | 55 | |
---|
[755] | 56 | if (!flow.search_sa_compounds) { |
---|
[650] | 57 | flow.showFirstRowCompounds = true |
---|
| 58 | } else { |
---|
| 59 | flow.showFirstRowCompounds = false |
---|
| 60 | } |
---|
| 61 | |
---|
[555] | 62 | flow.species = Term.findAll() |
---|
| 63 | flow.page = 1 |
---|
[547] | 64 | } |
---|
| 65 | |
---|
| 66 | on("search") { |
---|
[594] | 67 | if (!params.search_term.trim()) { |
---|
[547] | 68 | return [:] |
---|
| 69 | } |
---|
[555] | 70 | }.to "searching" |
---|
[547] | 71 | |
---|
| 72 | on("refresh").to "query" |
---|
[499] | 73 | } |
---|
[547] | 74 | |
---|
[555] | 75 | |
---|
| 76 | // Searching for results |
---|
| 77 | searching { |
---|
| 78 | action { |
---|
[642] | 79 | def searchResult |
---|
| 80 | def searchGscfResult |
---|
[650] | 81 | def searchSamResult = [] |
---|
[594] | 82 | |
---|
[650] | 83 | // Map GSCF parameters |
---|
| 84 | flow.search_term = params.search_term // String |
---|
[594] | 85 | |
---|
[650] | 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 | |
---|
[755] | 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 | } |
---|
[642] | 97 | } else { |
---|
[650] | 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 |
---|
[642] | 101 | } |
---|
| 102 | |
---|
| 103 | // Search the keyword with the Searchable plugin |
---|
[555] | 104 | try { |
---|
[642] | 105 | searchGscfResult = searchableService.search(flow.search_term) |
---|
[555] | 106 | } catch (SearchEngineQueryParseException ex) { |
---|
| 107 | println ex |
---|
[642] | 108 | return [parseException: true] |
---|
[555] | 109 | } |
---|
| 110 | |
---|
[642] | 111 | // Map non-study objects to Studies |
---|
| 112 | // ... todo when the plugin works and I can see the output |
---|
[555] | 113 | |
---|
[650] | 114 | // Search in the SAM module when a compound is entered |
---|
[755] | 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') |
---|
[642] | 123 | } |
---|
[755] | 124 | |
---|
| 125 | for (i in searchGscfResult.results) { |
---|
| 126 | def objStudy = Study.get(i.id) |
---|
| 127 | listGscfStudies.add(objStudy.id) |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | |
---|
[650] | 131 | |
---|
[642] | 132 | // Merge the results of all searches |
---|
[755] | 133 | if (listSamStudies.size() > 0) { |
---|
| 134 | listStudies = listGscfStudies.intersect(listSamStudies) |
---|
| 135 | } else { |
---|
[772] | 136 | if (!flow.search_sa_compounds) { |
---|
| 137 | listStudies = listGscfStudies |
---|
| 138 | } else { |
---|
| 139 | listStudies = [] |
---|
| 140 | } |
---|
[755] | 141 | } |
---|
[555] | 142 | |
---|
[755] | 143 | def listObjStudies = [] |
---|
| 144 | for (i in listStudies) { |
---|
| 145 | def objStudy = Study.get(i) |
---|
| 146 | listObjStudies.add(objStudy) |
---|
| 147 | } |
---|
[555] | 148 | |
---|
[642] | 149 | // Save the results in the flow |
---|
[755] | 150 | flow.listStudies = listObjStudies |
---|
[642] | 151 | |
---|
[555] | 152 | } |
---|
| 153 | |
---|
| 154 | on("error").to "query" |
---|
| 155 | on("success").to "results" |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | // Render result page including search options |
---|
[547] | 160 | results { |
---|
[555] | 161 | render(view: "/simpleQuery/mainPage") |
---|
[547] | 162 | |
---|
| 163 | onRender { |
---|
[555] | 164 | flow.page = 2 |
---|
[594] | 165 | |
---|
[650] | 166 | flow.showFirstRowCompounds = false |
---|
[555] | 167 | } |
---|
[547] | 168 | |
---|
[555] | 169 | on("reset") { |
---|
[594] | 170 | flow.search_term = null |
---|
| 171 | flow.studies = null |
---|
[755] | 172 | flow.search_sa_compounds = [] |
---|
| 173 | flow.search_sa_operators = [] |
---|
| 174 | flow.search_sa_values = [] |
---|
[594] | 175 | flow.search_tt_genepaths = null |
---|
| 176 | flow.search_tt_regulations = null |
---|
[555] | 177 | }.to "query" |
---|
[547] | 178 | |
---|
| 179 | on("search").to "searching" |
---|
| 180 | on("refresh").to "results" |
---|
| 181 | } |
---|
| 182 | |
---|
[498] | 183 | } |
---|
[650] | 184 | |
---|
[658] | 185 | |
---|
[755] | 186 | static Map searchSAM (List compounds, List operators, List values) { |
---|
[650] | 187 | |
---|
[755] | 188 | |
---|
[1196] | 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] |
---|
[755] | 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 | |
---|
[658] | 205 | return mapSamResult |
---|
| 206 | |
---|
[650] | 207 | } else { |
---|
[1196] | 208 | def tmpResult = CommunicationManager.getQueryResult( compounds.get(0) ) |
---|
| 209 | def studies = tmpResult.studiesIds.collect{ Study.findByCode(it) } |
---|
| 210 | def mapSamResult = [studies:studies, assays:[]] |
---|
| 211 | |
---|
[650] | 212 | def i = 0 |
---|
[658] | 213 | compounds.each { compound -> |
---|
| 214 | tmpSamResult = CommunicationManager.getQueryResult(compound) |
---|
[650] | 215 | |
---|
[658] | 216 | if (i == 0) { |
---|
| 217 | mapSamResult.assays = tmpSamResult.assays |
---|
| 218 | } else { |
---|
[772] | 219 | if (mapSamResult.assays) { |
---|
| 220 | mapSamResult.assays = mapSamResult.assays.intersect(tmpSamResult.assays) |
---|
| 221 | } |
---|
[658] | 222 | } |
---|
[650] | 223 | i++ |
---|
[658] | 224 | } |
---|
| 225 | |
---|
[772] | 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 | |
---|
[658] | 235 | return mapSamResult |
---|
[650] | 236 | } |
---|
| 237 | |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | |
---|
| 241 | |
---|
| 242 | static List merge (List list1, List list2) { |
---|
| 243 | |
---|
[1473] | 244 | list1.intersect(list2) |
---|
[650] | 245 | |
---|
| 246 | } |
---|
| 247 | |
---|
[1196] | 248 | } |
---|