Changeset 1526 for trunk/src/groovy
- Timestamp:
- Feb 16, 2011, 11:12:14 AM (12 years ago)
- Location:
- trunk/src/groovy/dbnp/query
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/groovy/dbnp/query/SampleSearch.groovy
r1524 r1526 71 71 } 72 72 73 // We expect the s amplecriteria to be the most discriminative, and discard74 // the most samples. (e.g. by searching on s ample title of sampletype). For73 // We expect the study criteria to be the most discriminative, and discard 74 // the most samples. (e.g. by searching on study title or study type). For 75 75 // that reason we first look through the list of studies. However, when the 76 // user didn't enter any s amplecriteria, this will be an extra step, but doesn't76 // user didn't enter any study criteria, this will be an extra step, but doesn't 77 77 // cost much time to process. 78 78 def samples = [] … … 152 152 @Override 153 153 void executeOr() { 154 // We expect the sample criteria to be the most discriminative, and discard155 // the most samples. (e.g. by searching on sample title of sample type). For156 // that reason we first look through the list of studies. However, when the157 // user didn't enter any sample criteria, this will be an extra step, but doesn't158 // cost much time to process.159 def samples = []160 154 def allSamples = Sample.list().findAll { it.parent?.canRead( this.user ) }.toList(); 161 162 // If no criteria are found, return all samples 163 if( !criteria || criteria.size() == 0 ) { 164 results = allSamples 165 return; 166 } 167 168 samples = ( samples + filterOnStudyCriteria( allSamples - samples ) ).unique(); 169 samples = ( samples + filterOnSubjectCriteria( allSamples - samples ) ).unique(); 170 samples = ( samples + filterOnSampleCriteria( allSamples - samples ) ).unique(); 171 samples = ( samples + filterOnEventCriteria( allSamples - samples ) ).unique(); 172 samples = ( samples + filterOnSamplingEventCriteria( allSamples - samples ) ).unique(); 173 samples = ( samples + filterOnAssayCriteria( allSamples - samples ) ).unique(); 174 175 samples = ( samples + filterOnModuleCriteria( allSamples - samples ) ).unique(); 176 177 // Save matches 178 results = samples; 155 executeOr( allSamples ); 179 156 } 180 157 … … 203 180 return null 204 181 205 return criterion.getFieldValue( sample.parentEventGroup.events.toList() );182 return sample.parentEventGroup.events?.collect { criterion.getFieldValue( it ) }; 206 183 } 207 184 case "SamplingEvent": … … 258 235 }); 259 236 237 println "Matching assays: " + assays 238 260 239 // If no assays match these criteria, then no samples will match either 261 240 if( assays.size() == 0 ) … … 268 247 269 248 def studyAssays = assays.findAll { it.parent.equals( sample.parent ); } 270 249 250 println "Assays for " + sample + " (based on study): " + studyAssays 251 271 252 // See if this sample is present in any of the matching assays. If so, 272 253 // this sample matches the criteria … … 274 255 if( assay.samples?.contains( sample ) ) 275 256 return true; 257 258 println "Assay " + assay + " with samples " + assay.samples + " doesn't contain " + sample; 276 259 } 277 260 -
trunk/src/groovy/dbnp/query/Search.groovy
r1524 r1526 71 71 72 72 /** 73 * Returns a list of Criteria74 */75 public List getCriteria() { return criteria; }76 77 /**78 * Sets a new list of criteria79 * @param c List with criteria objects80 */81 public void setCriteria( List c ) { criteria = c; }82 83 /**84 * Adds a criterion to this query85 * @param c Criterion86 */87 public void addCriterion( Criterion c ) {88 if( criteria )89 criteria << c;90 else91 criteria = [c];92 }93 94 /**95 * Retrieves the results found using this query. The result is empty is96 * the query has not been executed yet.97 */98 public List getResults() { return results; }99 100 /**101 * Returns the results found using this query, filtered by a list of ids.102 * @param selectedIds List with ids of the entities you want to return.103 * @return A list with only those results for which the id is in the selectedIds104 */105 public List filterResults( List selectedIds ) {106 if( !selectedIds || !results )107 return results108 109 return results.findAll {110 selectedIds.contains( it.id )111 }112 }113 114 /**115 * Returns a list of fields for the results of this query. The fields returned are those116 * fields that the query searched for.117 */118 public Map getResultFields() { return resultFields; }119 120 /**121 73 * Constructor of this search object. Sets the user field to the 122 74 * currently logged in user … … 179 131 * subclasses searching for a specific entity 180 132 */ 181 p ublicvoid executeAnd() {133 protected void executeAnd() { 182 134 183 135 } … … 187 139 * subclasses searching for a specific entity 188 140 */ 189 public void executeOr() { 190 191 } 192 141 protected void executeOr() { 142 143 } 144 145 /** 146 * Default implementation of an inclusive (AND) search. Can be called by subclasses in order 147 * to simplify searches. 148 * 149 * Filters the list of objects on study, subject, sample, event, samplingevent and assaycriteria, 150 * based on the closures defined in valueCallback. Afterwards, the objects are filtered on module 151 * criteria 152 * 153 * @param objects List of objects to search in 154 */ 155 protected void executeAnd( List objects ) { 156 // If no criteria are found, return all studies 157 if( !criteria || criteria.size() == 0 ) { 158 results = objects; 159 return; 160 } 161 162 // Perform filters 163 objects = filterOnStudyCriteria( objects ); 164 objects = filterOnSubjectCriteria( objects ); 165 objects = filterOnSampleCriteria( objects ); 166 objects = filterOnEventCriteria( objects ); 167 objects = filterOnSamplingEventCriteria( objects ); 168 objects = filterOnAssayCriteria( objects ); 169 170 objects = filterOnModuleCriteria( objects ); 171 172 // Save matches 173 results = objects; 174 } 175 176 /** 177 * Default implementation of an exclusive (OR) search. Can be called by subclasses in order 178 * to simplify searches. 179 * 180 * Filters the list of objects on study, subject, sample, event, samplingevent and assaycriteria, 181 * based on the closures defined in valueCallback. Afterwards, the objects are filtered on module 182 * criteria 183 * 184 * @param allObjects List of objects to search in 185 */ 186 protected void executeOr( List allObjects ) { 187 // If no criteria are found, return all studies 188 if( !criteria || criteria.size() == 0 ) { 189 results = allObjects; 190 return; 191 } 192 193 // Perform filters on those objects not yet found by other criteria 194 def objects = [] 195 objects = ( objects + filterOnStudyCriteria( allObjects - objects ) ).unique(); 196 objects = ( objects + filterOnSubjectCriteria( allObjects - objects ) ).unique(); 197 objects = ( objects + filterOnSampleCriteria( allObjects - objects ) ).unique(); 198 objects = ( objects + filterOnEventCriteria( allObjects - objects ) ).unique(); 199 objects = ( objects + filterOnSamplingEventCriteria( allObjects - objects ) ).unique(); 200 objects = ( objects + filterOnAssayCriteria( allObjects - objects ) ).unique(); 201 202 // All objects (including the ones already found by another criterion) are sent to 203 // be filtered on module criteria, in order to have the module give data about all 204 // objects (for showing purposes later on) 205 objects = ( objects + filterOnModuleCriteria( allObjects ) ).unique(); 206 207 // Save matches 208 results = objects; 209 } 210 211 193 212 /************************************************************************ 194 213 * 195 * These methods are used in querying and shouldbe overridden by subclasses214 * These methods are used in querying and can be overridden by subclasses 196 215 * in order to provide custom searching 197 216 * 198 * /217 ************************************************************************/ 199 218 200 219 /** … … 270 289 return criteria?.findAll { it.entity == entity } 271 290 } 272 273 /** 274 * Filters a list with entities, based on the given criteria and a closure to check whether a criterion is matched 275 * 276 * @param entities Original list with entities to check for these criteria 277 * @param criteria List with criteria to match on 278 * @param check Closure to see whether a specific entity matches a criterion. Gets two arguments: 279 * element The element to check 280 * criterion The criterion to check on. 281 * Returns true if the criterion holds, false otherwise 282 * @return The filtered list of entities 283 */ 284 protected List filterEntityList( List entities, List<Criterion> criteria, Closure check ) { 285 if( !entities || !criteria || criteria.size() == 0 ) { 286 if( searchMode == SearchMode.and ) 287 return entities; 288 else if( searchMode == SearchMode.or ) 289 return [] 290 } 291 292 return entities.findAll { entity -> 293 if( searchMode == SearchMode.and ) { 294 for( criterion in criteria ) { 295 if( !check( entity, criterion ) ) { 296 return false; 297 } 298 } 299 return true; 300 } else if( searchMode == SearchMode.or ) { 301 for( criterion in criteria ) { 302 if( check( entity, criterion ) ) { 303 return true; 304 } 305 } 306 return false; 307 } 308 } 309 } 310 291 292 311 293 /** 312 294 * Prepares a value from a template entity for comparison, by giving it a correct type 313 295 * 314 * @param value Value of the field 296 * @param value Value of the field 315 297 * @param type TemplateFieldType Type of the specific field 316 298 * @return The value of the field in the correct entity … … 374 356 } 375 357 358 /***************************************************** 359 * 360 * Methods for filtering lists based on specific (GSCF) criteria 361 * 362 *****************************************************/ 363 364 365 /** 366 * Filters a list with entities, based on the given criteria and a closure to check whether a criterion is matched 367 * 368 * @param entities Original list with entities to check for these criteria 369 * @param criteria List with criteria to match on 370 * @param check Closure to see whether a specific entity matches a criterion. Gets two arguments: 371 * element The element to check 372 * criterion The criterion to check on. 373 * Returns true if the criterion holds, false otherwise 374 * @return The filtered list of entities 375 */ 376 protected List filterEntityList( List entities, List<Criterion> criteria, Closure check ) { 377 if( !entities || !criteria || criteria.size() == 0 ) { 378 if( searchMode == SearchMode.and ) 379 return entities; 380 else if( searchMode == SearchMode.or ) 381 return [] 382 } 383 384 return entities.findAll { entity -> 385 if( searchMode == SearchMode.and ) { 386 for( criterion in criteria ) { 387 if( !check( entity, criterion ) ) { 388 return false; 389 } 390 } 391 return true; 392 } else if( searchMode == SearchMode.or ) { 393 for( criterion in criteria ) { 394 if( check( entity, criterion ) ) { 395 return true; 396 } 397 } 398 return false; 399 } 400 } 401 } 402 376 403 /** 377 404 * Filters the given list of studies on the study criteria … … 460 487 return filterOnTemplateEntityCriteria(studies, entity, valueCallback( entity ) ) 461 488 } 489 490 /******************************************************************** 491 * 492 * Methods for filtering object lists on module criteria 493 * 494 ********************************************************************/ 462 495 463 496 /** … … 690 723 } 691 724 725 726 /************************************************************************ 727 * 728 * Getters and setters 729 * 730 ************************************************************************/ 731 732 /** 733 * Returns a list of Criteria 734 */ 735 public List getCriteria() { return criteria; } 736 737 /** 738 * Sets a new list of criteria 739 * @param c List with criteria objects 740 */ 741 public void setCriteria( List c ) { criteria = c; } 742 743 /** 744 * Adds a criterion to this query 745 * @param c Criterion 746 */ 747 public void addCriterion( Criterion c ) { 748 if( criteria ) 749 criteria << c; 750 else 751 criteria = [c]; 752 } 753 754 /** 755 * Retrieves the results found using this query. The result is empty is 756 * the query has not been executed yet. 757 */ 758 public List getResults() { return results; } 759 760 /** 761 * Returns the results found using this query, filtered by a list of ids. 762 * @param selectedIds List with ids of the entities you want to return. 763 * @return A list with only those results for which the id is in the selectedIds 764 */ 765 public List filterResults( List selectedIds ) { 766 if( !selectedIds || !results ) 767 return results 768 769 return results.findAll { 770 selectedIds.contains( it.id ) 771 } 772 } 773 774 /** 775 * Returns a list of fields for the results of this query. The fields returned are those 776 * fields that the query searched for. 777 */ 778 public Map getResultFields() { return resultFields; } 779 692 780 public String toString() { 693 781 return ( this.entity ? this.entity + " search" : "Search" ) + " " + this.id 694 782 } 783 784 public boolean equals( Object o ) { 785 if( o == null ) 786 return false 787 788 if( !( o instanceof Search ) ) 789 return false 790 791 Search s = (Search) o; 792 793 return ( searchMode == s.searchMode && 794 entity == s.entity && 795 criteria.size() == s.criteria.size() && 796 s.criteria.containsAll( criteria ) && 797 criteria.containsAll( s.criteria ) ); 798 } 695 799 } -
trunk/src/groovy/dbnp/query/StudySearch.groovy
r1524 r1526 65 65 */ 66 66 @Override 67 void executeAnd() {67 protected void executeAnd() { 68 68 def studies = Study.list().findAll { it.canRead( this.user ) }; 69 69 70 // If no criteria are found, return all studies 71 if( !criteria || criteria.size() == 0 ) { 72 results = studies; 73 return; 74 } 75 76 // Perform filters 77 studies = filterOnStudyCriteria( studies ); 78 studies = filterOnSubjectCriteria( studies ); 79 studies = filterOnSampleCriteria( studies ); 80 studies = filterOnEventCriteria( studies ); 81 studies = filterOnSamplingEventCriteria( studies ); 82 studies = filterOnAssayCriteria( studies ); 83 84 studies = filterOnModuleCriteria( studies ); 85 86 // Save matches 87 results = studies; 70 executeAnd( studies ); 88 71 } 89 72 … … 121 104 */ 122 105 @Override 123 void executeOr() {106 protected void executeOr() { 124 107 def allStudies = Study.list().findAll { it.canRead( this.user ) }; 125 126 // If no criteria are found, return all studies 127 if( !criteria || criteria.size() == 0 ) { 128 results = allStudies; 129 return; 130 } 131 132 // Perform filters 133 def studies = [] 134 studies = ( studies + filterOnStudyCriteria( allStudies - studies ) ).unique(); 135 studies = ( studies + filterOnSubjectCriteria( allStudies - studies ) ).unique(); 136 studies = ( studies + filterOnSampleCriteria( allStudies - studies ) ).unique(); 137 studies = ( studies + filterOnEventCriteria( allStudies - studies ) ).unique(); 138 studies = ( studies + filterOnSamplingEventCriteria( allStudies - studies ) ).unique(); 139 studies = ( studies + filterOnAssayCriteria( allStudies - studies ) ).unique(); 140 141 studies = ( studies + filterOnModuleCriteria( allStudies - studies ) ).unique(); 142 143 // Save matches 144 results = studies; 108 executeOr( allStudies ); 145 109 } 146 110
Note: See TracChangeset
for help on using the changeset viewer.