Class PropertyFilter

java.lang.Object
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: 2021-08-12 10:44:12 +0200 (Thu, 12 Aug 2021) $
  • Field Details

    • 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
    • DATA_FILES

      private final Pattern DATA_FILES
    • property

      private final String property
    • rowIndex

      private int rowIndex
    • valueType

      private Type valueType
    • operator

      private Operator operator
    • value

      private String value
    • unit

      private Unit unit
    • temporary

      private boolean temporary
  • Constructor Details

    • PropertyFilter

      public PropertyFilter​(String property)
      Create a new filter for a property on filter row 0.
      Parameters:
      property - The name of the property
    • PropertyFilter

      public PropertyFilter​(String property, int rowIndex)
      Create a new filter for a property.
      Parameters:
      property - The name of the property
      rowIndex - The filter row to create the filter on
    • 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, int rowIndex, PropertyFilterData filter)
      Create a new filter from a loaded database object.
  • Method Details

    • create

      static PropertyFilter create​(String propertyFromDb, PropertyFilterData data)
      Create a property filter instance from what has been saved in the database. Basically, we need to strip the [filterrow] if it is set.
      Since:
      3.5
    • 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.
    • isExtensionFilter

      public boolean isExtensionFilter​(String prefix)
      Checks if this filter is an extension filter. An extension filter has a getProperty() that starts with "!x.prefix".
      Parameters:
      prefix - The extension prefix to check, or null to check any extension
      Since:
      3.18
    • getRowIndex

      public int getRowIndex()
      Get the filter row index this filter is part of.
      Since:
      3.5
    • setRowIndex

      void setRowIndex​(int rowIndex)
      Only used internally when removing filter rows
      Since:
      3.5
      See Also:
      ItemContext.removeFilterRow(int)
    • 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 <T> T 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 <T> T 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
    • getValueAsObject

      private <T> T getValueAsObject​(String value, Unit unit, boolean asDouble)
    • getValuesAsObjects

      public <T> List<T> 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 <T> List<T> 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
    • getValuesAsObjects

      private <T> List<T> getValuesAsObjects​(String value, Unit unit, boolean asDouble)
    • convertValueToUnit

      private Double convertValueToUnit​(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​(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:

      • 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).
      • Since BASE 3.18: !x., which is intended to be handled by extensions. It is ignored by the BASE core code. The rest of the property should be a unique identifier for the extension that is handling the filter.

      The property may also start with $@.

      Returns:
      The restriction
      Since:
      2.8
    • getDynamicRestriction

      public Restriction getDynamicRestriction​(DbControl dc, SqlQuery query)
      Since:
      3.9.2
    • getValueSeparator

      public String getValueSeparator()