Changeset 1196
- Timestamp:
- Nov 24, 2010, 11:47:41 AM (10 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/conf/BootStrap.groovy
r1195 r1196 49 49 CommunicationManager.SAMServerURL = config.modules.sam.url 50 50 CommunicationManager.DSPServerURL = config.modules.metabolomics.url 51 CommunicationManager.registerRestWrapperMethodsSAMtoGSCF() 51 CommunicationManager.ServerURL = config.grails.serverURL 52 CommunicationManager.registerRestWrapperMethodsFromSAM() 52 53 } 53 54 -
trunk/grails-app/controllers/dbnp/query/SimpleQueryController.groovy
r959 r1196 35 35 // Starting simpleQuery flow, initialize variables 36 36 onStart { 37 println "Starting webflow simpleQuery"38 37 flow.search_term = null 39 38 flow.search_sa_compounds = [] … … 51 50 render(view: "/simpleQuery/mainPage") 52 51 52 53 53 onRender { 54 println "Rendering mainPage"55 54 flow.operators = ['>', '=', '<'] 56 55 57 56 if (!flow.search_sa_compounds) { 58 57 flow.showFirstRowCompounds = true 59 println "showRow true"60 58 } else { 61 59 flow.showFirstRowCompounds = false 62 println "showRow false"63 60 } 64 61 … … 68 65 69 66 on("search") { 70 println "Search!"71 67 if (!params.search_term.trim()) { 72 68 return [:] … … 81 77 searching { 82 78 action { 83 println "Starting simpleQuery search..."84 79 def searchResult 85 80 def searchGscfResult 86 81 def searchSamResult = [] 87 82 88 // TODO: walk parameters, remove empty entries89 90 83 // Map GSCF parameters 91 84 flow.search_term = params.search_term // String … … 93 86 // Map SAM parameters 94 87 if (params.sa_compound instanceof String) { 95 println "Compounds as String"96 //flow.search_sam = [:]97 88 flow.search_sa_compounds = [] 98 89 flow.search_sa_operators = [] … … 105 96 } 106 97 } else { 107 println "Compounds as List"108 98 flow.search_sa_compounds = params.sa_compound as List 109 99 flow.search_sa_operators = params.sa_operator as List … … 114 104 try { 115 105 searchGscfResult = searchableService.search(flow.search_term) 116 println "RESULT: " + searchGscfResult117 106 } catch (SearchEngineQueryParseException ex) { 118 107 println ex … … 124 113 125 114 // Search in the SAM module when a compound is entered 126 // Todo: check whether the module is active and to be used127 // ...128 115 def listSamStudies = [] 129 116 def listGscfStudies = [] … … 133 120 def resultSAM = [:] 134 121 resultSAM = this.searchSAM(flow.search_sa_compounds, flow.search_sa_operators, flow.search_sa_values) 135 println "Sam result: " + resultSAM136 122 listSamStudies = resultSAM.get('studies') 137 123 } 138 124 139 125 for (i in searchGscfResult.results) { 140 //def x = i.id141 126 def objStudy = Study.get(i.id) 142 println objStudy143 127 listGscfStudies.add(objStudy.id) 144 128 } 145 129 146 130 147 println "GSCF studies: " + listGscfStudies148 println "Sam studies " + listSamStudies149 131 150 132 // Merge the results of all searches 151 133 if (listSamStudies.size() > 0) { 152 134 listStudies = listGscfStudies.intersect(listSamStudies) 153 println "Combined: " + listStudies154 135 } else { 155 136 if (!flow.search_sa_compounds) { … … 168 149 // Save the results in the flow 169 150 flow.listStudies = listObjStudies 170 println flow.listStudies171 151 172 152 } … … 182 162 183 163 onRender { 184 println "Rendering resultPage"185 164 flow.page = 2 186 165 … … 196 175 flow.search_tt_genepaths = null 197 176 flow.search_tt_regulations = null 198 println "Resetting query flow"199 177 }.to "query" 200 178 … … 207 185 208 186 static Map searchSAM (List compounds, List operators, List values) { 187 188 209 189 if (compounds.size() == 1) { 210 println "Single SAM call" 211 def mapSamResult = [:] 212 213 //def listAssays = [3, 1] 214 //mapSamResult.put("assays", listAssays) 215 //println "CommMngr result: " + mapSamResult 216 217 CommunicationManager.addRestWrapper( 'http://localhost:8182/sam/rest', 'getQueryResult', ['query'] ) 218 mapSamResult = CommunicationManager.getQueryResult( compounds.get(0) ) 219 println "SAM REST query: " + compounds.get(0) 220 println "SAM REST result: " + mapSamResult 221 222 // mapSamResult = CommunicationManager.getQueryResult(compounds.get(0), operators.get(0), values.get(0)) 223 224 //objAssay = objAssay.get(i) 225 //println "Assay: " + objAssay 226 227 /* 228 for (i in mapSamResult.assays) { 229 //def listStudies = Study.findAll("from Study as s where s.assays.id = " + i) 230 def listStudies = Study.findAll("from Study as s where exists (from Assay as a where a.id = s.assays and a.id = ${i})") 231 println "Studies found: " + listStudies 232 } 233 */ 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 234 195 235 196 def listStudies = [] … … 245 206 246 207 } else { 247 println "Multiple SAM calls" 248 def tmpSamResult = [:] 249 def mapSamResult = [assays:[]] 208 def tmpResult = CommunicationManager.getQueryResult( compounds.get(0) ) 209 def studies = tmpResult.studiesIds.collect{ Study.findByCode(it) } 210 def mapSamResult = [studies:studies, assays:[]] 211 250 212 def i = 0 251 252 213 compounds.each { compound -> 253 println "SAM Search with " + compound254 CommunicationManager.addRestWrapper( 'http://localhost:8182/sam/rest', 'getQueryResult', ['query'] )255 214 tmpSamResult = CommunicationManager.getQueryResult(compound) 256 println "tmpsamres: " + tmpSamResult257 215 258 216 if (i == 0) { … … 263 221 } 264 222 } 265 266 223 i++ 267 224 } -
trunk/src/groovy/dbnp/rest/common/CommunicationManager.groovy
r1010 r1196 26 26 class CommunicationManager { 27 27 28 29 /* this should moved somewhere else */ 30 /* The static URLs are set in grails-app/conf/Config.groovy */ 28 31 def static Encoding = "UTF-8" 32 def public static ServerURL = "http://localhost:8182/sam" 29 33 def public static SAMServerURL = "http://localhost:8182/sam" 30 34 def public static GSCFServerURL = "http://localhost:8080/gscf" 31 def public static DSPServerURL = "http://localhost:8080/ gscf"35 def public static DSPServerURL = "http://localhost:8080/ncdsp" 32 36 33 37 … … 58 62 def url = RestServerURL + '/' + resource 59 63 def first = true 64 params['consumer']=ServerURL 60 65 params.each { name, value -> 61 66 if(first) { … … 107 112 108 113 public static addRestWrapper( serverURL, restName, params = [], closure = { return it } ) { 109 CommunicationManager.metaClass.registerStaticMethod( restName ) { Object [] strangeGroovyArgs -> 110 def map = [:] 111 def args = strangeGroovyArgs[0] // groovy nests the parameters of the methods in some other array 114 if(!serverURL) { throw new Exception("addRestWrapper: REST serverURL is null") } 115 def result 116 try { 117 CommunicationManager.metaClass.registerStaticMethod( restName ) { Object [] strangeGroovyArgs -> 118 def map = [:] 119 def args = strangeGroovyArgs[0] // groovy nests the parameters of the methods in some other array 120 if(params.size > 0 ) 121 { 122 for( i in 0..(params.size-1) ) { 123 def param = params[i] 124 map[param] = args[i] 125 } 126 } 127 result = closure( getRestResource( serverURL, restName, map ) ) 128 } 129 } catch ( Exception e ) { 130 throw new Exception("addRestWrapper: error. Could not retrieve data from RESTFful service. ") 131 } 112 132 113 if(params.size > 0 ) 114 { 115 for( i in 0..(params.size-1) ) { 116 def param = params[i] 117 map[param] = args[i] 118 } 119 } 120 121 return closure( getRestResource( serverURL, restName, map ) ) 122 } 123 } 124 125 126 127 128 129 /** 130 * This method dynamically registers a static method to the CommunicationManager. The new method 131 * gives url for a Grails view on some server and takes as arguments the arguments required 132 * as params by the view. 133 * 134 * @params String methodname The name for method to be registered. 135 * @params String serverURL The server's URL. 136 * @params String viewName The view's name, e.g., '/Assay/show' 137 * @params Map params The parameter list required by this view. 138 * @return String URL 139 * 140 */ 141 public static addViewWrapper( methodName, serverURL, viewName, params = [] ) { 142 143 CommunicationManager.metaClass.registerStaticMethod( methodName ) { Object [] strangeGroovyArgs -> 144 def map = [:] 145 def args = strangeGroovyArgs[0] // groovy nests the parameters of the methods in some other array 146 for( i in 0..(params.size-1) ) { 147 def param = params[i] 148 map[param] = args[i] 149 } 150 return getRestURL( serverURL, viewName, map ) 151 } 152 } 153 154 155 /** 156 * This creates on run time new methods for accessing Rest resources that GSCF provides for SAM. 157 * This method should be called in grails-app/conf/BootStrap.groovy in the SAM module. 158 */ 159 public static registerRestWrapperMethodsGSCFtoSAM() { 160 def url = GSCFServerURL + '/rest' 161 addRestWrapper( url , 'getStudies' ) 162 addRestWrapper( url , 'getSubjects', ['studyToken'] ) 163 addRestWrapper( url , 'getAssays', ['studyToken','moduleURL'] ) 164 addRestWrapper( url , 'getSamples', ['assayToken'] ) 133 return result 165 134 } 166 135 … … 169 138 * This method creates on run time new methods for accessing Grails views that SAM provides for GSCF. 170 139 * This method should be called in grails-app/conf/BootStrap.groovy in the GSCF module. 171 */ 172 public static registerRestWrapperMethods SAMtoGSCF() {140 */ 141 public static registerRestWrapperMethodsFromSAM() { 173 142 def url = SAMServerURL 174 175 // register method that links to the SAM view for importing a SimpleAssay. 176 // parameters: externalAssayID, an externalAssayID 177 addViewWrapper( 'getAssayImportURL', url, 'importer/pages', ['externalAssayID', 'externalStudyID'] ) 178 179 // register method that links to the SAM view for showing a SimpleAssay 180 // parameters: externalAssayID 181 addViewWrapper( 'getAssayShowURL', url, 'simpleAssay/show', ['externalAssayID'] ) 182 183 // register method that links to the SAM view for editing a SimpleAssay 184 // parameters: externalAssayID 185 addViewWrapper( 'getAssayEditURL', url, 'simpleAssay/show', ['externalAssayID'] ) 186 187 // register method that links to the SAM view for editing a SimpleAssay 188 // parameters: externalAssayID 189 addViewWrapper( 'getMeasurementTypesURL', url, 'simpleAssayMeasurementType/list', ['externalStudyID'] ) 190 191 // register rest resource that returns the results of a full text query on SAM 192 // parameters: query. A string for fulltext search on SAM 193 // return value: results map. It contains two keys 'studyIds', and 'samples'. 'studyIds' 194 // key maps to a list of Study domain objects of GSCF. 'samples' maps to a 195 // list of pairs. Each pair consists of a Sample domain object of GSCF and 196 // additional sample information from SAM provided as a map. 197 // Example of a returned map: 198 // ["studies":[NuGO PPS human study], 199 // "samples":[[ [...], dbnp.studycapturing.Sample: 1]]] 200 def closure = { map -> 201 def studies = [] 202 def assays = [] 203 def studiesHQ = "from dbnp.studycapturing.Study as s where s.code=?" 204 map['studyIds'].each { studies.add( dbnp.studycapturing.Study.find(studiesHQ,[it]) ) } 205 map['assays'].each { samAssay -> 206 def assayID = samAssay['externalAssayID'] 207 def assayHQ = "from dbnp.studycapturing.Assay as a where a.externalAssayID='${assayID}'" 208 def assay = dbnp.studycapturing.Assay.find(assayHQ) 209 assays.add( [samAssay,assay] ) 210 } 211 return [studies:studies, assays:assays] 212 } 213 214 addRestWrapper( url+'/rest', 'getQueryResult', ['query'], closure ) 215 216 217 // Rest resource: getQueryResultWithOperator 218 // 219 // register rest resource that returns the results of a simple query with measurement value on SAM 220 // parameters: query. A keyword to match a measurement type on SAM. 221 // operator. One of '=','<', or '>' that serves for selecting measurements. 222 // value. A double value for the measurement. 223 // 224 // return value: results list of maps. each map contains an assay, a sample, the value found, 225 // a unit (as String), and a type (as String). 226 // 227 // Example of a returned list of maps: 228 // [["type":"Glucose", "unit":"g", "value":"201.0", "assay":Lipid profiling, 229 // "sample":A10_B], ["type":"Glucose", "unit":"g", "value":"101.0", 230 // "assay":Lipid profiling, "sample":A1_B], ["type":"Insulin", "unit":"g", "value":"202.0", 231 // "assay":Lipid profiling, "sample":A10_B], ["type":"Insulin", "unit":"g", "value":"102.0", 232 // "assay":Lipid profiling, "sample":A1_B]] 233 // 234 def closure2 = { listOfMaps -> 235 def results = [] 236 listOfMaps.each{ map -> 237 def result = [ type:map['type'], unit:map['unit'], value:map['value'] ] 238 def assayId = map['externalAssayId'].toLong() 239 def assay = dbnp.studycapturing.Assay.find( "from dbnp.studycapturing.Assay as a where a.externalAssayID= ?", [assayId] ) 240 def sample = dbnp.studycapturing.Sample.find( "from dbnp.studycapturing.Sample as s where s.name = ?", [map['externalSampleId']] ) 241 result['assay']=assay 242 result['sample']=sample 243 results.add( result ) 244 } 245 return results 246 } 247 248 addRestWrapper( url+'/rest', 'getQueryResultWithOperator', ['query','operator','value'], closure2 ) 143 addRestWrapper( url+'/rest', 'getQueryResult', ['query'] ) 249 144 } 250 145 251 252 /**253 * This method creates on run time new methods for accessing Grails views that SAM provides for GSCF.254 * This method should be called in grails-app/conf/BootStrap.groovy in the GSCF module.255 */256 public static registerRestWrapperMethodsGSCFtoDSP() {257 def url = DSPServerURL258 addRestWrapper( url, 'isUser', ['username','password'] )259 addRestWrapper( url, 'listStudies', ['username','password'] )260 addRestWrapper( url, 'listStudySamples', ['username','password','study_token'] )261 addRestWrapper( url, 'getStudy', ['username','password','study_token'] )262 addRestWrapper( url, 'getStudySample', ['username','password','study_token','sample_token'] )263 addRestWrapper( url, 'isUser', ['username','password'] )264 }265 146 266 147 … … 273 154 */ 274 155 static String hasValidParams( params, Object [] requiredParams ) { 275 def list = [] 276 requiredParams.each { p -> 277 if( !params[p] ) list.push p 278 } 279 if(list.size()>0) { return true } 280 return false 156 requiredParams.every { params[it] } 281 157 } 282 158 283 159 284 285 160 }
Note: See TracChangeset
for help on using the changeset viewer.