Changeset 1800


Ignore:
Timestamp:
May 2, 2011, 5:03:36 PM (12 years ago)
Author:
robert@…
Message:

Implemented searching in 'all fields'

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/controllers/dbnp/query/AdvancedQueryController.groovy

    r1799 r1800  
    265265         */
    266266        protected def getSearchableFields() {
    267                 def fields = [:];
     267                def fields = [ '*' : [ '*' ] ]; // Searches for all fields in all objects
    268268
    269269                // Retrieve all local search fields
     
    308308                        }
    309309                }
    310 
     310               
    311311                return fields;
    312312        }
     
    332332                ArrayList list = [];
    333333                flash.error = "";
     334               
    334335                // Loop through all keys of c and remove the non-numeric ones
    335336                for( c in formCriteria ) {
  • trunk/grails-app/views/advancedQuery/index.gsp

    r1780 r1800  
    1818                                                        entity.key.toString() + '.' + field.toString() + ' ' +
    1919                                                        entity.key.toString() + ' ' + field.toString() + ' ' +
    20                                                         (field == '*' ? 'any field' : '')
     20                                                        (field == '*' ? 'any field' : '') + ' ' +
     21                                                        (field == '*' && entity.key == '*' ? 'any field in any object' : '')
    2122                                                        ).encodeAsJavaScript()}",
    2223                                                show: "${
    2324                                                        (field == '*' ?
    24                                                                 '[Any field in ' + entity.key.toString() + ']' :
     25                                                                ( entity.key == '*' ? '[Any field in any object]' : '[Any field in ' + entity.key.toString() + ']' ) :
    2526                                                                (field?.size() > 1 ?
    2627                                                                        field[0].toUpperCase() + field[1..-1] :
  • trunk/src/groovy/dbnp/query/Criterion.groovy

    r1717 r1800  
    4343        public String humanReadableEntityField() {
    4444                if( field == '*' ) {
    45                         return "any field in " + entity.toString();
     45                        if( entity == '*' ) {
     46                                return "any field in any object"
     47                        } else {
     48                                return "any field in " + entity.toString();
     49                        }
    4650                } else {
    4751                        return entityField();
  • trunk/src/groovy/dbnp/query/SampleSearch.groovy

    r1717 r1800  
    112112                samples = filterOnSamplingEventCriteria( samples );
    113113                samples = filterOnAssayCriteria( samples );
    114 
     114               
     115                // Filter on criteria for which the entity is unknown
     116                samples = filterOnAllFieldsCriteria( samples );
     117               
     118                // Filter on module criteria
    115119                samples = filterOnModuleCriteria( samples );
    116120
     
    186190                        case "Assay":
    187191                                return { sample, criterion ->
    188                                         println "Find value for " + sample + " and " + criterion
    189192                                        def sampleAssays = Assay.findByParent( sample.parent ).findAll { it.samples?.contains( sample ) };
    190193                                        if( sampleAssays && sampleAssays.size() > 0 )
  • trunk/src/groovy/dbnp/query/Search.groovy

    r1798 r1800  
    166166                objects = filterOnAssayCriteria( objects );
    167167
     168                // Filter on criteria for which the entity is unknown
     169                objects = filterOnAllFieldsCriteria( objects );
     170               
     171                // Filter on module criteria
    168172                objects = filterOnModuleCriteria( objects );
    169173
     
    197201                objects = ( objects + filterOnSamplingEventCriteria( allObjects - objects ) ).unique();
    198202                objects = ( objects + filterOnAssayCriteria( allObjects - objects ) ).unique();
     203               
     204                // Filter on criteria for which the entity is unknown
     205                objects = ( objects + filterOnAllFieldsCriteria( allObjects - objects ) ).unique();
    199206               
    200207                // All objects (including the ones already found by another criterion) are sent to
     
    484491                def entity = "Assay"
    485492                return filterOnTemplateEntityCriteria(studies, entity, valueCallback( entity ) )
     493        }
     494       
     495       
     496        /**
     497         * Filters the given list of entities on criteria that mention all fields (e.g. search for studies with 'bacteria' in any field)
     498         * @param objects       Original list of entities.
     499         * @return                      List of all entities that match the given criteria
     500         */
     501        protected List filterOnAllFieldsCriteria( List objects ) {
     502                def criteria = getEntityCriteria( "*" );
     503               
     504                // Find methods to determine a value for a criterion, based on all entities
     505                def valueCallbacks = [:];
     506                def entities = [ "Study", "Subject", "Sample", "Event", "SamplingEvent", "Assay" ];
     507                entities.each {
     508                        valueCallbacks[ it ] = valueCallback( it );
     509                }
     510               
     511                // Create a closure that checks all entities
     512                def checkCallback = { object, criterion ->
     513                        def value = "";
     514                        for( def entity in entities ) {
     515                                value = valueCallbacks[ entity ]( object, criterion );
     516                               
     517                                if( value == null ) {
     518                                        continue;
     519                                }
     520       
     521                                if( value instanceof Collection ) {
     522                                        if( criterion.matchAny( value ) )
     523                                                return true;
     524                                } else {
     525                                        if( criterion.match( value ) )
     526                                                return true;
     527                                }
     528                        }
     529                       
     530                        // If no match is found, return
     531                        return false;
     532                }
     533
     534                return filterEntityList( objects, criteria, checkCallback);
    486535        }
    487536       
  • trunk/src/groovy/dbnp/query/StudySearch.groovy

    r1526 r1800  
    122122        * @return
    123123        */
    124    protected Closure valueCallback( String entity ) {
    125            switch( entity ) {
    126                    case "Study":
    127                            return { study, criterion -> return criterion.getFieldValue( study ) }
    128                    case "Subject":
    129                            return { study, criterion -> return study.subjects?.collect { criterion.getFieldValue( it ); } }
    130                    case "Sample":
    131                            return { study, criterion -> return study.samples?.collect { criterion.getFieldValue( it ); } }
    132                    case "Event":
    133                            return { study, criterion -> return study.events?.collect { criterion.getFieldValue( it ); } }
    134                    case "SamplingEvent":
    135                            return { study, criterion -> return study.samplingEvents?.collect { criterion.getFieldValue( it ); } }
    136                    case "Assay":
    137                            return { study, criterion -> return study.assays?.collect { criterion.getFieldValue( it ); } }
    138                    default:
    139                            return super.valueCallback( entity );
    140            }
    141    }
     124        protected Closure valueCallback( String entity ) {
     125                switch( entity ) {
     126                        case "Study":
     127                                return { study, criterion -> return criterion.getFieldValue( study ) }
     128                        case "Subject":
     129                                return { study, criterion -> return study.subjects?.collect { criterion.getFieldValue( it ); } }
     130                        case "Sample":
     131                                return { study, criterion -> return study.samples?.collect { criterion.getFieldValue( it ); } }
     132                        case "Event":
     133                                return { study, criterion -> return study.events?.collect { criterion.getFieldValue( it ); } }
     134                        case "SamplingEvent":
     135                                return { study, criterion -> return study.samplingEvents?.collect { criterion.getFieldValue( it ); } }
     136                        case "Assay":
     137                                return { study, criterion -> return study.assays?.collect { criterion.getFieldValue( it ); } }
     138                        default:
     139                                return super.valueCallback( entity );
     140                }
     141        }
    142142
    143143        /**
Note: See TracChangeset for help on using the changeset viewer.