Changeset 1717


Ignore:
Timestamp:
Apr 6, 2011, 5:24:14 PM (12 years ago)
Author:
robert@…
Message:

Implemented searching for 'any field' in advanced query

Location:
trunk
Files:
7 edited

Legend:

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

    r1678 r1717  
    322322                                def templateFields = TemplateField.findAllByEntity( entity )
    323323
    324                                 def fieldNames = ( domainFields + templateFields ).collect { it.name }.unique() + 'Template'
    325 
    326                                 fields[ it ] = fieldNames.sort { a, b -> a[0].toUpperCase() + a[1..-1] <=> b[0].toUpperCase() + b[1..-1] };
     324                                def fieldNames = ( domainFields + templateFields ).collect { it.name }.unique() + 'Template' + '*'
     325
     326                                fields[ it ] = fieldNames.sort { a, b ->
     327                                        def aUC = a.size() > 1 ? a[0].toUpperCase() + a[1..-1] : a;
     328                                        def bUC = b.size() > 1 ? b[0].toUpperCase() + b[1..-1] : b;
     329                                        aUC <=> bUC
     330                                };
    327331                        }
    328332                }
     
    346350                                def moduleName = module.name.replace( 'module', '' ).trim()
    347351
    348                                 fields[ moduleName ] = moduleFields.unique();
     352                                fields[ moduleName ] = moduleFields.unique() + '*';
    349353                        } catch( Exception e ) {
    350354                                log.error( "Error while retrieving queryable fields from " + module.name + ": " + e.getMessage() )
  • trunk/grails-app/views/advancedQuery/_criteria.gsp

    r1581 r1717  
    55                <g:each in="${criteria}" var="criterion" status="j">
    66                        <li>
    7                                 <span class="entityfield">${criterion.entityField().toLowerCase()}</span>
     7                                <span class="entityfield">${criterion.humanReadableEntityField().toLowerCase()}</span>
    88                                <span class="operator">${criterion.operator}</span>
    99                                <span class="value">
  • trunk/grails-app/views/advancedQuery/index.gsp

    r1649 r1717  
    1515                                        <g:if test="${j > 0}">,</g:if>
    1616                                        {
    17                                                 label: "${entity.key.toString().encodeAsJavaScript()}.${field.toString().encodeAsJavaScript()} ${entity.key.toString().encodeAsJavaScript()} ${field.toString().encodeAsJavaScript()}",
    18                                                 show: "${(field[0].toUpperCase() + field[1..-1]).encodeAsJavaScript()}",
     17                                                label: "${(
     18                                                        entity.key.toString() + '.' + field.toString() + ' ' +
     19                                                        entity.key.toString() + ' ' + field.toString() + ' ' +
     20                                                        (field == '*' ? 'any field' : '')
     21                                                        ).encodeAsJavaScript()}",
     22                                                show: "${
     23                                                        (field == '*' ?
     24                                                                '[Any field in ' + entity.key.toString() + ']' :
     25                                                                (field?.size() > 1 ?
     26                                                                        field[0].toUpperCase() + field[1..-1] :
     27                                                                        field)
     28                                                        ).encodeAsJavaScript()}",
    1929                                                value: "${entity.key.toString().encodeAsJavaScript()}.${field.toString().encodeAsJavaScript()}",
    2030                                                entity: "${entity.key.toString().encodeAsJavaScript()}"
     
    7888                                                                <g:each in="${entity.value}" var="field">
    7989                                                                        <option value="${entity.key}.${field}">
    80                                                                                 ${field[0].toUpperCase() + field[1..-1]}
     90                                                                                <g:if test="${field?.size() > 1}">
     91                                                                                        ${field[0].toUpperCase() + field[1..-1]}
     92                                                                                </g:if>
     93                                                                                <g:else>
     94                                                                                        ${field}
     95                                                                                </g:else>
    8196                                                                        </option>
    8297                                                                </g:each>
  • trunk/src/groovy/dbnp/query/Criterion.groovy

    r1482 r1717  
    3838       
    3939        /**
     40         * Retrieves a human readable description of the combination of the entity and field
     41         * @return
     42         */
     43        public String humanReadableEntityField() {
     44                if( field == '*' ) {
     45                        return "any field in " + entity.toString();
     46                } else {
     47                        return entityField();
     48                }
     49        }
     50       
     51        /**
    4052         * Retrieves the correct value for this criterion in the given object (with template)
    4153         *
     
    5466                        } else if( field == "Template" ) {
    5567                                fieldValue = entity.template?.name
     68                        } else if( field == "*" ) {
     69                                fieldValue = entity.giveFields().collect{
     70                                        if( it && it.name ) {
     71                                                Search.prepare( entity.getFieldValue( it.name ), entity.giveFieldType( it.name ) )
     72                                        }
     73                                }
    5674                        } else {
    5775                                fieldValue = Search.prepare( entity.getFieldValue( field ), entity.giveFieldType( field ) )
    5876                        }
    59 
     77                       
    6078                        return fieldValue
    6179                } catch( Exception e ) {
     
    7391         * @return                      True iff there the entity satisfies the given criterion.
    7492         */
    75         public boolean matchEntity( TemplateEntity entity ) {
     93        public boolean matchOneEntity( TemplateEntity entity ) {
    7694                def fieldValue = this.getFieldValue( entity );
    7795
     
    163181                classname = classname[classname.lastIndexOf( '.' ) + 1..-1].toLowerCase();
    164182
     183                def matches = false;
    165184                try {
    166185                        switch( classname ) {
    167                                 case "integer":                                 return longCompare( new Long( fieldValue.longValue() ) );
    168                                 case "long":                                    return longCompare( fieldValue );
    169                                 case "float":                                   return doubleCompare( new Long( fieldValue.doubleValue() ) );
    170                                 case "double":                                  return doubleCompare( fieldValue );
    171                                 case "boolean":                                 return booleanCompare( fieldValue );
    172                                 case "date":                                    return dateCompare( fieldValue);
    173                                 case "reltime":                                 return relTimeCompare( fieldValue );
     186                                case "integer":                                 matches = longCompare( new Long( fieldValue.longValue() ) ); break;
     187                                case "long":                                    matches = longCompare( fieldValue ); break;
     188                                case "float":                                   matches = doubleCompare( new Long( fieldValue.doubleValue() ) ); break;
     189                                case "double":                                  matches = doubleCompare( fieldValue ); break;
     190                                case "boolean":                                 matches = booleanCompare( fieldValue ); break;
     191                                case "date":                                    matches = dateCompare( fieldValue); break;
     192                                case "reltime":                                 matches = relTimeCompare( fieldValue ); break;
    174193                                case "assaymodule":
    175194                                case "template":
     
    177196                                case "templatefieldlistitem":
    178197                                case "string":
    179                                 default:                                                return compareValues( fieldValue.toString().trim().toLowerCase(), this.operator, value.toString().toLowerCase().trim() );
     198                                default:                                                matches = compareValues( fieldValue.toString().trim().toLowerCase(), this.operator, value.toString().toLowerCase().trim() ); break;
    180199                        }
     200                       
     201                        return matches;
    181202                } catch( Exception e ) {
    182203                        log.error e.class.getName() + ": " + e.getMessage();
     
    252273                                longCriterion = new Long( doubleCriterion.longValue() );
    253274                        } catch( Exception e2 ) {
    254                                 log.error e2.class.getName() + ": " + e2.getMessage();
     275                                log.debug "Can't convert value to long for comparison: " + e2.class.getName() + ": " + e2.getMessage();
    255276                                return false;
    256277                        }
     
    270291                        return compareValues( fieldValue, this.operator, doubleCriterion );
    271292                } catch( Exception e ) {
    272                         log.error e.class.getName() + ": " + e.getMessage();
     293                        log.debug "Can't convert value to double for comparison: " + e.class.getName() + ": " + e.getMessage();
    273294                        return false;
    274295                }
     
    284305        protected boolean booleanCompare( Boolean fieldValue ) {
    285306                try {
     307                        // The comparison should only be performed iff the value
     308                        // contains 'true' or 'false' (case insensitive)
     309                        def lowerCaseValue = value.toString().toLowerCase();
     310                        if( lowerCaseValue != 'true' && lowerCaseValue != 'false' )
     311                                return false;
     312                               
    286313                        Boolean booleanCriterion = Boolean.parseBoolean( value );
    287314                        return compareValues( fieldValue, this.operator, booleanCriterion );
    288315                } catch( Exception e ) {
    289                         log.error e.class.getName() + ": " + e.getMessage();
     316                        log.debug "Can't convert value to boolean for comparison: " + e.class.getName() + ": " + e.getMessage();
    290317                        return false;
    291318                }
     
    311338                        return compareValues( fieldValue, this.operator, rt );
    312339                } catch( Exception e ) {
    313                         log.error e.class.getName() + ": " + e.getMessage();
     340                        log.debug "Can't convert value to reltime for comparison: " + e.class.getName() + ": " + e.getMessage();
    314341                        return false;
    315342                }
  • trunk/src/groovy/dbnp/query/SampleSearch.groovy

    r1526 r1717  
    232232                                return false
    233233
    234                         return criterion.matchEntity( assay );
     234                        return criterion.matchOneEntity( assay );
    235235                });
    236236
  • trunk/src/groovy/dbnp/query/Search.groovy

    r1596 r1717  
    303303                        case TemplateFieldType.DATE:
    304304                                try {
    305                                         return new SimpleDateFormat( "yyyy-MM-dd" ).parse( value )
     305                                        return new SimpleDateFormat( "yyyy-MM-dd" ).parse( value.toString() )
    306306                                } catch( Exception e ) {
    307307                                        return value.toString();
     
    580580                        // JSON object
    581581                        def token = entity.giveUUID()
    582                         if( !json[ token ] || json[ token ][ criterion.field ] == null )
    583                                 return false;
    584 
    585                         // Check whether a list or string is given
    586                         def value = json[ token ][ criterion.field ];
    587 
    588                         // Save the value of this entity for later use
    589                         saveResultField( entity.id, criterion.entity + " " + criterion.field, value )
    590 
    591                         if( !( value instanceof Collection ) ) {
    592                                 value = [ value ];
     582                        def value
     583                       
     584                        if( criterion.field == '*' ) {
     585                                // Collect the values from all fields
     586                                value = [];
     587                                json[ token ].each { field ->
     588                                        if( field.value instanceof Collection ) {
     589                                                field.value.each { value << it }
     590                                        } else {
     591                                                value << field.value;
     592                                        }
     593                                }
     594                        } else {
     595                                if( !json[ token ] || json[ token ][ criterion.field ] == null )
     596                                        return false;
     597       
     598                                // Check whether a list or string is given
     599                                value = json[ token ][ criterion.field ];
     600       
     601                                // Save the value of this entity for later use
     602                                saveResultField( entity.id, criterion.entity + " " + criterion.field, value )
     603       
     604                                if( !( value instanceof Collection ) ) {
     605                                        value = [ value ];
     606                                }
    593607                        }
    594608
     
    626640                def callUrl = 'entity=' + this.entity
    627641                tokens.sort().each { callUrl += "&tokens=" + it.encodeAsURL() }
    628                 fields.sort().each { callUrl += "&fields=" + it.encodeAsURL() }
     642               
     643                // If all fields are searched, all fields should be retrieved
     644                if( fields.contains( '*' ) ) {
     645                       
     646                } else {
     647                        fields.sort().each { callUrl += "&fields=" + it.encodeAsURL() }
     648                }
    629649
    630650                return callUrl;
     
    647667
    648668                criteria.each { criterion ->
    649                         if( criterion.field ) {
     669                        if( criterion.field && criterion.field != '*' ) {
    650670                                def valueCallback = valueCallback( criterion.entity );
    651671                               
     
    672692                for( criterion in criteria ) {
    673693                        for( entity in entities ) {
    674                                 if( criterion.field )
     694                                if( criterion.field && criterion.field != '*' )
    675695                                        saveResultField( entity.id, criterion.entity + ' ' + criterion.field, valueCallback( entity, criterion ) )
    676696                        }
     
    692712                if( value == null )
    693713                        value = "";
    694 
     714               
     715                if( fieldName == "*" )
     716                        return;
     717                       
    695718                if( value instanceof Collection ) {
    696719                        value = value.findAll { it != null }
    697720                }
    698 
     721               
    699722                resultFields[ id ][ fieldName ] = value;
    700723        }
  • trunk/web-app/css/default_style.css

    r1704 r1717  
    88
    99.container { margin: 0 auto; text-align: left; }
     10.ui-widget { text-align: left; }
    1011
    1112/*a:link, a:visited, a:hover {
Note: See TracChangeset for help on using the changeset viewer.