Changeset 4067


Ignore:
Timestamp:
Dec 14, 2007, 4:48:18 PM (15 years ago)
Author:
Martin Svensson
Message:

References #762 Updated PropertyFilter? class to use the new operators.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/PropertyFilter.java

    r4057 r4067  
    2626package net.sf.basedb.core;
    2727
     28import java.util.ArrayList;
    2829import java.util.Date;
     30import java.util.List;
    2931
    3032import net.sf.basedb.core.data.PropertyFilterData;
     
    180182  {
    181183    return valueType.parseString(value);
     184  }
     185 
     186  /**
     187    Splits the value string at each "|" and creates
     188    a list with objects from the substrings that were found.
     189    @return A list with objects.
     190    @throws InvalidDataException If any of the string values can't be parsed.
     191    @sine 2.5
     192   */
     193  public List<Object> getValuesAsObjects()
     194  {
     195    String[] values = value.split("|");
     196    List<Object> objects = new ArrayList<Object>(values.length);
     197    for (String v : values)
     198    {
     199      objects.add(valueType.parseString(v));
     200    }
     201    return objects;
    182202  }
    183203 
     
    232252      boolean searchInherited = property.startsWith("##");
    233253      int annotationTypeId = (Integer)Type.INT.parseString(property.substring(searchInherited ? 2 : 1));
    234       restriction = new AnnotationSimpleRestriction(alias, annotationTypeId, getValueType(),
    235         getOperator(), getValueAsObject(), searchInherited);
     254      if (operator.isListOperator())
     255      {
     256       
     257      }
     258      else
     259      {
     260        restriction = new AnnotationSimpleRestriction(alias, annotationTypeId, getValueType(),
     261            getOperator(), getValueAsObject(), searchInherited);
     262      }
    236263    }
    237264    else
     
    252279        if (getValueType() == Type.DATE)
    253280        {
    254           // To use ==, != and <= filter on dates we must add 24 hours to it
    255           // and use different restrictions
    256           Date filter = (Date)getValueAsObject();
    257           Date filterPlus24H = new Date(filter.getTime()+24*3600*1000);
    258           Expression parameterPlus24H =
    259             Expressions.parameter(parameterName+"24h", filterPlus24H, Type.DATE);
    260           if (operator == Operator.EQ)
     281          if (!operator.isListOperator())
    261282          {
    262             // date <= property < date + 24H
    263             restriction = Restrictions.between(propertyExpression, parameter, parameterPlus24H);
     283            // To use ==, != and <= filter on dates we must add 24 hours to it
     284            // and use different restrictions
     285            Date filter = (Date)getValueAsObject();
     286            Date filterPlus24H = new Date(filter.getTime()+24*3600*1000);
     287            Expression parameterPlus24H =
     288              Expressions.parameter(parameterName+"24h", filterPlus24H, Type.DATE);
     289            if (operator == Operator.EQ)
     290            {
     291              // date <= property < date + 24H
     292              restriction = Restrictions.between(propertyExpression, parameter, parameterPlus24H);
     293            }
     294            else if (operator == Operator.NEQ)
     295            {
     296              // NOT date <= property < date + 24H
     297              restriction = Restrictions.not(Restrictions.between(propertyExpression, parameter, parameterPlus24H));
     298            }
     299            else if (operator == Operator.LTEQ)
     300            {
     301              // property < date + 24H
     302              restriction = Restrictions.lt(propertyExpression, parameterPlus24H);
     303            }
     304            else if (operator == Operator.GT)
     305            {
     306              // property >= date + 24H
     307              restriction = Restrictions.gteq(propertyExpression, parameterPlus24H);
     308            }
    264309          }
    265           else if (operator == Operator.NEQ)
     310          else
    266311          {
    267             // NOT date <= property < date + 24H
    268             restriction = Restrictions.not(Restrictions.between(propertyExpression, parameter, parameterPlus24H));
     312            List<Object> objects = getValuesAsObjects();
     313            List<Restriction> restrictions = new ArrayList<Restriction>(objects.size());
     314            for (Object obj : objects)
     315            {
     316              Date filter = (Date)obj;
     317              Date filterPlus24H = new Date(filter.getTime()+24*3600*1000);
     318              parameterName = "p" + System.identityHashCode(obj);
     319              Expression parameterPlus24H =
     320                  Expressions.parameter(parameterName+"24h", filterPlus24H, Type.DATE);
     321              Expression dateParameter = Expressions.parameter(parameterName, obj, getValueType());
     322              if (operator == Operator.IN)
     323              {
     324                restrictions.add(Restrictions.between(propertyExpression, dateParameter, parameterPlus24H));
     325              }
     326              else if (operator == Operator.NOTIN)
     327              {
     328                restrictions.add(Restrictions.not(Restrictions.between(propertyExpression, dateParameter, parameterPlus24H)));
     329              }
     330            }
     331            restriction = Restrictions.or(restrictions.toArray(new Restriction[restrictions.size()]));
    269332          }
    270           else if (operator == Operator.LTEQ)
    271           {
    272             // property < date + 24H
    273             restriction = Restrictions.lt(propertyExpression, parameterPlus24H);
    274           }
    275           else if (operator == Operator.GT)
    276           {
    277             // property >= date + 24H
    278             restriction = Restrictions.gteq(propertyExpression, parameterPlus24H);
    279           }
     333         
    280334        }
    281335        if (restriction == null)
    282336        {
    283           restriction = operator.getRestriction(propertyExpression, parameter);
     337          if (operator.isListOperator())
     338          {
     339            List<Expression> parameters = new ArrayList<Expression>();
     340            List<Object> objects = getValuesAsObjects();
     341            for (Object obj : objects)
     342            {
     343              parameterName = "p" + System.identityHashCode(obj);
     344              parameters.add(Expressions.parameter(parameterName, obj, getValueType()));
     345            }
     346            restriction = operator.getRestriction(propertyExpression, parameters.toArray(new Expression[parameters.size()]));
     347          }
     348          else
     349          {
     350            restriction = operator.getRestriction(propertyExpression, parameter);
     351          }
    284352        }
    285353      }
Note: See TracChangeset for help on using the changeset viewer.