2.17.2: 2011-06-17

net.sf.basedb.core
Class PropertyFilter

java.lang.Object
  extended by net.sf.basedb.core.PropertyFilter

public class PropertyFilter
extends Object

Used to store filter settings in the current context. A filter is a simple expression of the form: property operator value, ie. name = 'Nicklas' or spots > 10000.

Version:
2.0
Author:
Nicklas
See Also:
SessionControl.getCurrentContext(Item), ItemContext
Last modified
$Date: 2010-10-26 14:55:47 +0200 (Tue, 26 Oct 2010) $

Field Summary
private  Pattern ENTITY_COLLECTION
           
static int MAX_VALUE_LENGTH
          The maximum allowed length of a filter value.
private  Operator operator
           
private  String property
           
private  boolean temporary
           
static boolean TEMPORARY
          Flag to use for temporary filters.
private  Unit unit
           
private  String value
           
private static String value_separator
          The string/characters used as a separator in a string with more then one filter values.
private  Type valueType
           
 
Constructor Summary
PropertyFilter(String property)
          Create a new filter for a property.
PropertyFilter(String property, Operator operator, String value, Type valueType)
          Create a new filter for a property.
PropertyFilter(String property, Operator operator, String value, Type valueType, boolean temporary)
          Create a new filter for a property.
PropertyFilter(String property, PropertyFilterData filter)
          Create a new filter from a loaded database object.
 
Method Summary
private  Double convertValueToUnit(org.hibernate.Session session, String value, String unitSymbol, Unit convertTo)
          Converts a string to a numeric value with the correct unit.
(package private)  PropertyFilterData getData()
          Create a new database filter object from this filter.
 Restriction getDynamicRestriction(DbControl dc)
           
 Operator getOperator()
          Get the operator to use in the filter.
 String getProperty()
          Get the name of the property this filter is used on.
 Restriction getRestriction()
          Deprecated. Use getRestriction(DbControl, EntityQuery) instead
 Restriction getRestriction(DbControl dc)
          Deprecated. Use getRestriction(DbControl, EntityQuery) instead
 Restriction getRestriction(DbControl dc, EntityQuery query)
          Create a restriction from the filter.
 String getValue()
          Get the value to use in the filter.
 Object getValueAsObject()
          Get the value as an object of the correct type.
 Object getValueAsObject(Unit unit, boolean asDouble)
          Get the value as an object of the proper type converted to the specified unit.
 List<Object> getValuesAsObjects()
          Splits the value string at each "|" and creates a list with objects from the substrings that were found.
 List<Object> getValuesAsObjects(Unit unit, boolean asDouble)
          Same as getValueAsObject(Unit, boolean) but the filter string is first split at each "|".
 String getValueSeparator()
           
 Type getValueType()
          Get the type of the property.
 boolean isTemporary()
          Is this filter a temporary filter or not.
 void setOperator(Operator operator)
          Set the operator to use in the filter.
 void setTemporary(boolean temporary)
          Set or clear a flag indicating that this filter is temporary filter.
 void setUnit(Unit unit)
          Sets the unit the values for the property are stored in the database.
 void setValue(String value)
          Set the value to use in the filter.
 void setValueType(Type valueType)
          Set the type of the property.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

MAX_VALUE_LENGTH

public static final int MAX_VALUE_LENGTH
The maximum allowed length of a filter value.

Since:
2.6.2
See Also:
Constant Field Values

TEMPORARY

public static final boolean TEMPORARY
Flag to use for temporary filters.

Since:
2.16
See Also:
Constant Field Values

value_separator

private static final String value_separator
The string/characters used as a separator in a string with more then one filter values.

See Also:
Constant Field Values

ENTITY_COLLECTION

private final Pattern ENTITY_COLLECTION

property

private final String property

valueType

private Type valueType

operator

private Operator operator

value

private String value

unit

private Unit unit

temporary

private boolean temporary
Constructor Detail

PropertyFilter

public PropertyFilter(String property)
Create a new filter for a property.

Parameters:
property - The name of the property

PropertyFilter

public PropertyFilter(String property,
                      Operator operator,
                      String value,
                      Type valueType)
Create a new filter for a property.

Parameters:
property - The name of the property
operator - The operator to use
value - The value to check against
valueType - The type of the value

PropertyFilter

public PropertyFilter(String property,
                      Operator operator,
                      String value,
                      Type valueType,
                      boolean temporary)
Create a new filter for a property.

Parameters:
property - The name of the property
operator - The operator to use
value - The value to check against
valueType - The type of the value
temporary - A flag indicating if the filter should be temporary or not
Since:
2.16

PropertyFilter

PropertyFilter(String property,
               PropertyFilterData filter)
Create a new filter from a loaded database object.

Method Detail

getData

PropertyFilterData getData()
Create a new database filter object from this filter.


toString

public String toString()
Overrides:
toString in class Object

setTemporary

public void setTemporary(boolean temporary)
Set or clear a flag indicating that this filter is temporary filter. Temporary filters are not saved between sessions.

Since:
2.16

isTemporary

public boolean isTemporary()
Is this filter a temporary filter or not.

Since:
2.16

getProperty

public String getProperty()
Get the name of the property this filter is used on.


getValueType

public Type getValueType()
Get the type of the property.


setValueType

public void setValueType(Type valueType)
Set the type of the property.

Parameters:
valueType - The type

getOperator

public Operator getOperator()
Get the operator to use in the filter.


setOperator

public void setOperator(Operator operator)
Set the operator to use in the filter.


setUnit

public void setUnit(Unit unit)
Sets the unit the values for the property are stored in the database. When a unit is set, it is possible to have filter values with units. The filter values are automatically converted to this unit before the filter is applied. For example, if we set the unit to 'g', someone may filter on '0.5kg', which is converted to 500 in the query.

Parameters:
unit - The unit, or null to remove the unit from the filter
Since:
2.9

getValue

public String getValue()
Get the value to use in the filter.

Returns:
The value as a string

setValue

public void setValue(String value)
Set the value to use in the filter.

Parameters:
value - The value as a string

getValueAsObject

public Object getValueAsObject()
Get the value as an object of the correct type. If the filter property is numeric and has a unit, the value is converted to the correct unit automatically. For example, if the value is '5kg' and the unit is 'g' this method returns 5000.

Note! The returned value is cast to the class supported by the getValueType(). Beware of this when the filter has an integer type and a unit with low precision. For example, if the value is '125min' and the unit is 'hour' this method returns the integer 2. To avoid this use getValueAsObject(Unit, boolean) with asDouble=true instead.

Returns:
The value as an object
Throws:
InvalidDataException - If the string value can't be parsed

getValueAsObject

public Object getValueAsObject(Unit unit,
                               boolean asDouble)
Get the value as an object of the proper type converted to the specified unit. For example, if the unit is 'g' and the value is '5kg' this method returns 5000.

If the asDouble parameter is true, the value is returned as a double, otherwise it is cast to the proper class as specified by the getValueType().

Note! The above only applies for numerical properties that has a unit. If not, the value is simply parsed with Type.parseString(String).

Parameters:
unit - A unit, or null to use the default unit
asDouble - TRUE to return the value as a double, FALSE to let the getValueType() decide
Returns:
The value as an object
Since:
2.9

getValuesAsObjects

public List<Object> getValuesAsObjects()
Splits the value string at each "|" and creates a list with objects from the substrings that were found.

Returns:
A list with objects, or null if splitting string of values returns null.
Throws:
InvalidDataException - If any of the string values can't be parsed.
Since:
2.5

getValuesAsObjects

public List<Object> getValuesAsObjects(Unit unit,
                                       boolean asDouble)
Same as getValueAsObject(Unit, boolean) but the filter string is first split at each "|". Each individual string may or may not specify a unit. If a unit is specified for some parts but not for all, the nearest following unit is used. For example, the string '300g|5|10kg' is the same as '300g|5kg|10kg'.'

Parameters:
unit - A unit, or null to use the default unit
asDouble - TRUE to return the value as a double, FALSE to let the getValueType() decide
Returns:
A list with the values
Since:
2.9

convertValueToUnit

private Double convertValueToUnit(org.hibernate.Session session,
                                  String value,
                                  String unitSymbol,
                                  Unit convertTo)
Converts a string to a numeric value with the correct unit.

Parameters:
session - Hibernate session used to lookup units
value - The string value which should be parsable with Double.parseDouble(String)
unitSymbol - The unit of the string value
convertTo - The unit to convert the value to
Returns:
The converted value

getRestriction

public Restriction getRestriction()
Deprecated. Use getRestriction(DbControl, EntityQuery) instead

This method will fail if used to filter on reporter lists or collections.


getRestriction

public Restriction getRestriction(DbControl dc)
Deprecated. Use getRestriction(DbControl, EntityQuery) instead

This method will fail if used to filter on collections.


getRestriction

public Restriction getRestriction(DbControl dc,
                                  EntityQuery query)
Create a restriction from the filter. There are a few special cases:

If the property starts with a $ it is interpreted as a alias.property value, ignoring the root alias of the query. The alias is removed from the property before proceeding.

If the property starts with a # or ## it is a filter on annotations. The rest of the property is the ID of the annotation type, and we use a AnnotationSimpleRestriction object for the restriction. If there is only one # in the prefix only direct annotations are searched, if there are two ##, inherited annotations are also searched.

If the property starts with a £, the rest of the property is the path that joins the ReporterData.getReporterListScores() collection. The filter value is the ID of the reporter list.

If the property starts with a @ the property is a collection of values and we always use an IN restriction, ignoring the specified operator.

If the property starts with & it is a collection of entities. The property must end with a property name from the entity in the collection within paranthesis. Eg. &experiments(name). The filter is applied as a subquery.

If the property starts with ! it is another special filter. There is currently one such filter (since BASE 2.15): !sharedTo.name, which is used to filter shareable items by the name of users/groups/projects it has been shared to. The 'name' part can be replaced with any other property that is common for users, groups and projects (eg. description or id).

The property may also start with $@.

Returns:
The restriction
Since:
2.8

getDynamicRestriction

public Restriction getDynamicRestriction(DbControl dc)

getValueSeparator

public String getValueSeparator()

2.17.2: 2011-06-17