2.17.2: 2011-06-17

net.sf.basedb.core
Class ItemContext

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

public class ItemContext
extends Object

This item is a helper class for client applications when they need to store information about the current context for plugins that needs to find out the current context of the running client application. It is optional to use this class and it is recommended that plugins and client applications that expects to find context information also can handle situations without any context information. Some of the settings in this objects are saved in the database, some not.

Typical example for using this class are:

A client application storing list/table settings

SessionControl sc = ...
ItemContext current = sc.getCurrentContext(Item.SAMPLE);
current.setRowsPerPage(50);
current.setPage(0);
current.setSortProperty("name");
current.setSortDirection(SortDirection.DESC);
current.setPropertyFilter(
      new PropertyFilter("name", Operator.LIKE, "Nicklas%", Type.STRING)
   );
ItemQuery query = Sample.getQuery();
current.configureQuery(query, true);
// The query will now return the 50 first samples with a
// a name starting with 'Nicklas' sorted in descending order

A client application using filter information when creating new items

SessionControl sc = ...
DbControl dc = sc.newDbControl();
ItemContext current = sc.getCurrentContext(Item.SAMPLE);
Sample sample = Sample.getNew(dc);
if (current.getPropertyValue("name") != null)
{
   sample.setName(current.getPropertyValue("name"));
}
// The name is now 'Nicklas%'

Plugin finds out which items in a list has been selected

SessionControl sc = ...
ItemContext current = sc.getCurrentContext(Item.SAMPLE);
ItemQuery query = Sample.getQuery();
Set selected = current.getSelected();
if (selected != null && selected.size() > 0)
{
   query.restrict(
         Restrictions.in(Hql.property("id"), 
         Expressions.parameter("selected", selected, Type.INT))
      );
)
// The query will now return the selected items only

An import plugin asks client application for a file with a specific file type

public RequestInformation getRequestInformation(GuiContext context, String command)
   throws BaseException
{
   RequestInformation requestInformation = null;
   if (command.equals(Request.COMMAND_CONFIGURE_PLUGIN))
   {
      requestInformation = getConfigurePluginParameters(context);
   }
   else if (command.equals(Request.COMMAND_CONFIGURE_JOB))
   {
      requestInformation = getConfigureJobParameters(context);
      ItemContext fileContext = sc.getCurrentContext(Item.FILE);
      fileContext.setPropertyFilter(FileType.getPropertyFilter(FileType.REPORTER));
   }
   return requestInformation;
}

Version:
2.0
Author:
Nicklas
See Also:
SessionControl.getCurrentContext(Item), SessionControl.saveCurrentContextAs(Item, String, boolean, boolean)
Last modified
$Date: 2011-02-18 13:19:38 +0100 (Fri, 18 Feb 2011) $

Nested Class Summary
(package private) static class ItemContext.PropertyFilterPair
          Holds a pair of filter.
static class ItemContext.SortDirection
          Enumeration used to specify the sort direction.
 
Field Summary
private static JepFunction ch
           
private  int contextId
           
static String DEFAULT_NAME
          This is the name of the default context which is automatically saved when a user is logging out.
private  Set<Include> include
           
private  int itemId
           
private  Item itemType
           
private  String message
           
private  String name
           
private  Map<String,Object> objects
           
private  int page
           
private  Permission permission
           
private  Map<String,ItemContext.PropertyFilterPair> propertyFilters
           
private  Query query
           
private static JepFunction raw
           
private static JepFunction rawCh
           
private static JepFunction rep
           
private  int rowsPerPage
           
private  Set<Integer> selected
           
private  Map<String,String> settings
           
private  ItemContext.SortDirection sortDirection
           
private  String sortProperty
           
private  String subContext
           
 
Constructor Summary
ItemContext(ContextData context)
          Create a new context from a loaded database object.
ItemContext(Item itemType, String subContext, String name)
          Create a new empty context.
ItemContext(String sortProperty, ItemContext.SortDirection sortDirection, int rowsPerPage, int page)
          Create a new context.
 
Method Summary
 void clearAllRecent()
          Clear all recently used values lists.
 void clearRecent(Item itemType)
          Clear the recently used items list from items of the specified type.
 void clearRecent(Item itemType, String subList)
          Clear the recently used items sublist from items of the specified type.
 void clearRecent(String key)
          Clear the recently used values list.
 void configureQuery(DbControl dc, DynamicQuery query, List<String> selectionList)
          Use the settings in this context to configure a dynamic query.
 void configureQuery(DbControl dc, EntityQuery query, boolean autoLeftJoin)
          Use the settings in this context to configure a query.
 void configureQuery(EntityQuery query, boolean autoLeftJoin)
          Deprecated. Use configureQuery(DbControl, EntityQuery, boolean) instead
(package private)  void copyFrom(ItemContext other, boolean forceAll)
          Copy settings from another context to this context.
 void filterOnSelectedItems(EntityQuery query)
          Add a filter to the given query that restricts it to only return items that have been selected.
 int getContextId()
          Get the database ID of this context.
(package private)  ContextData getData(String name, UserData user, ClientData client, boolean isPublic)
          Create a new database context object from this context.
static Expression getDynamicExpression(DbControl dc, String propertyDef)
          Create an Expression from a string.
static Select getDynamicSelect(DbControl dc, String propertyDef)
          Same as getDynamicExpression(DbControl, String) but generates a select object instead.
 Restriction getFilterRestriction(String property, DbControl dc, EntityQuery query)
          Create a restriction from a property filter.
 int getId()
          Get the ID of the current item in this context.
 Set<Include> getInclude()
          Get a Set object to specify Include options for a query.
 Permission getItemPermission()
          The permission the logged in user must have for items returned by a query.
 Item getItemType()
          Get the type of item this object is storing context for.
 String getMessage()
          Get a message that should be displayed by a client application as soon as it is appropriate to do so.
 String getName()
          Get the name of this context.
 int getNumPropertyFilters()
          Get the number of property filters stored in this context.
 int getNumRecent(Item itemType)
          Get the number of recently used items in the list.
 int getNumRecent(Item itemType, String subList)
          Get the number of recently used items in a list with a sublist.
 int getNumRecent(String key)
          Get the number of recently used value in the list.
 Object getObject(String name)
          Get an object.
 int getPage()
          Get the current table page.
 PropertyFilter getPropertyFilter(String property)
          Get a filter for a property.
 Collection<PropertyFilter> getPropertyFilters()
          Get a collection of all property filters stored in this context.
 Object getPropertyObject(String property)
           
 String getPropertyValue(String property)
          Get the value of a property filter.
 Query getQuery()
           
 List<? extends BasicItem> getRecent(DbControl dc, Item itemType)
          Get all recently used items of the specified type.
 BasicItem getRecent(DbControl dc, Item itemType, int index)
           
 List<? extends BasicItem> getRecent(DbControl dc, Item itemType, String subList)
          Get all recently used items of the specified type in a given sublist.
 BasicItem getRecent(DbControl dc, Item itemType, String subList, int index)
           
 List<String> getRecent(String key)
          Get all recently used values for the specified key.
 String getRecent(String key, int index)
           
private  String getRecentSettingName(String key)
           
 int getRowsPerPage()
          Get the number of rows the current table page is displaying.
 Set<Integer> getSelected()
          Get a Set object to specify the ID:s of all items that are currently selected.
 String getSetting(String name)
          Get a value.
 ItemContext.SortDirection getSortDirection()
          If the items are sorted in ascending or descending order.
 Order getSortOrder()
          Get the current sort property as an Order instance.
 String getSortProperty()
          Get the name of the property the current listing is sorted by.
 String getSubContext()
          Get the sub-context name of this context.
private  List<? extends BasicItem> loadRecent(DbControl dc, Item itemType, String key)
           
private  BasicItem loadRecent(DbControl dc, Item itemType, String key, int index)
           
 void removeAllPropertyFilters()
          Remove all property filter from this context.
 void removeAllTemporaryFilters()
          Remove all temporary filters.
 Object removeObject(String name)
          Remove a value.
 void removePropertyFilter(String property)
          Remove a property filter.
 String removeSetting(String name)
          Remove a value.
 void removeTemporaryFilter(String property)
          Remote a temporary filter for the given property.
 void setId(int id)
          Set the ID of the current item in this context.
 void setItemPermission(Permission permission)
          Specify the permission the logged in user must have for items returned by a query.
 void setMessage(String message)
          Set a message to be displayed by a client application as soon as it is appropriate to do so.
 Object setObject(String name, Object value)
          Set a value.
 void setPage(int page)
          Set the current table page.
 void setPropertyFilter(PropertyFilter filter)
          Set a property filter.
 void setQuery(Query query)
           
 void setRecent(BasicItem item, int maxInList)
          Add an item to the "recently used items" list.
 void setRecent(BasicItem item, String subList, int maxInList)
          Add an item to the "recently used items" list.
private  void setRecent(String key, List<String> recent)
           
 void setRecent(String key, String value, int maxInList)
          Add a value to the list of recently used values for the specified key.
 void setRowsPerPage(int rowsPerPage)
          Set the number of rows the current table page should show.
 String setSetting(String name, String value)
          Set a value.
 void setSortDirection(ItemContext.SortDirection sortDirection)
          Set the sort direction (ascending or descending) of the current listing.
 void setSortProperty(String sortProperty)
          Set the name of the property that the current listing is sorted by.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_NAME

public static final String DEFAULT_NAME
This is the name of the default context which is automatically saved when a user is logging out. A client application shouldn't use this name for other named contexts that it wants to reload in the future.

See Also:
Constant Field Values

ch

private static final JepFunction ch

rawCh

private static final JepFunction rawCh

raw

private static final JepFunction raw

rep

private static final JepFunction rep

name

private final String name

itemType

private final Item itemType

subContext

private final String subContext

contextId

private int contextId

itemId

private int itemId

selected

private Set<Integer> selected

rowsPerPage

private int rowsPerPage

page

private int page

sortProperty

private String sortProperty

message

private String message

sortDirection

private ItemContext.SortDirection sortDirection

include

private Set<Include> include

permission

private Permission permission

propertyFilters

private Map<String,ItemContext.PropertyFilterPair> propertyFilters

settings

private Map<String,String> settings

objects

private Map<String,Object> objects

query

private Query query
Constructor Detail

ItemContext

public ItemContext(String sortProperty,
                   ItemContext.SortDirection sortDirection,
                   int rowsPerPage,
                   int page)
Create a new context.

Parameters:
sortProperty - The property used to sort a list
sortDirection - If the items should be sorted in ascending or descending order
rowsPerPage - The number of rows to display on a single page
page - The page number of the currently displayed page, starting at 0

ItemContext

ItemContext(Item itemType,
            String subContext,
            String name)
Create a new empty context.


ItemContext

ItemContext(ContextData context)
Create a new context from a loaded database object.

Method Detail

getData

ContextData getData(String name,
                    UserData user,
                    ClientData client,
                    boolean isPublic)
Create a new database context object from this context.

Parameters:
name - The name to use, or null if the current name should be used
user - The user that should own the context object
client - The client application currently in use
isPublic - If the context should be public or not

getItemType

public Item getItemType()
Get the type of item this object is storing context for.


getSubContext

public String getSubContext()
Get the sub-context name of this context.


getName

public String getName()
Get the name of this context.


getContextId

public int getContextId()
Get the database ID of this context. Can be used to load contexts

Returns:
The database ID of the context, or 0 if it hasn't been saved to the database
Since:
2.4

getId

public int getId()
Get the ID of the current item in this context.

Returns:
The ID of the item, or 0 if no item is current

setId

public void setId(int id)
Set the ID of the current item in this context. This property is stored in the database.

Parameters:
id - The ID of the item, or 0 if no item is current

getRowsPerPage

public int getRowsPerPage()
Get the number of rows the current table page is displaying.

Returns:
The number of rows

setRowsPerPage

public void setRowsPerPage(int rowsPerPage)
Set the number of rows the current table page should show. This property is stored in the database.

Parameters:
rowsPerPage - The number of rows to show on a page

getPage

public int getPage()
Get the current table page.

Returns:
The number of the page, starting at 0

setPage

public void setPage(int page)
Set the current table page. This property is stored in the database.

Parameters:
page - The number of the page, starting at 0

getSortProperty

public String getSortProperty()
Get the name of the property the current listing is sorted by. If multiple properties are used for sorting they are separated by a comma.


setSortProperty

public void setSortProperty(String sortProperty)
Set the name of the property that the current listing is sorted by. If you want to sort by multiple properties, separate them with a comma. This property is stored in the database.

Parameters:
sortProperty - The name of the property

getSortDirection

public ItemContext.SortDirection getSortDirection()
If the items are sorted in ascending or descending order.


setSortDirection

public void setSortDirection(ItemContext.SortDirection sortDirection)
Set the sort direction (ascending or descending) of the current listing. This property is stored in the database.

Parameters:
sortDirection - The sort direction

getSortOrder

public Order getSortOrder()
Get the current sort property as an Order instance. If multiple properties are used for sorting, this method only returns the first property.

Returns:
An Order instance, or null if no sort order has been specified

getMessage

public String getMessage()
Get a message that should be displayed by a client application as soon as it is appropriate to do so. Typically, the configureQuery(EntityQuery, boolean) sets an error message if it couldn't configure the query for some reason.


setMessage

public void setMessage(String message)
Set a message to be displayed by a client application as soon as it is appropriate to do so. Typically, the configureQuery(EntityQuery, boolean) sets an error message if it couldn't configure the query for some reason. This property is not stored in the database.

Parameters:
message - The message

getInclude

public Set<Include> getInclude()
Get a Set object to specify Include options for a query. This property is stored in the database.

See Also:
EntityQuery

setItemPermission

public void setItemPermission(Permission permission)
Specify the permission the logged in user must have for items returned by a query. This property is not stored in the database.

See Also:
EntityQuery.setItemPermission(Permission)

getItemPermission

public Permission getItemPermission()
The permission the logged in user must have for items returned by a query. This property is not stored in the database.

See Also:
EntityQuery.getItemPermission()

getSelected

public Set<Integer> getSelected()
Get a Set object to specify the ID:s of all items that are currently selected. This property is not stored in the database.


setPropertyFilter

public void setPropertyFilter(PropertyFilter filter)
Set a property filter. If a filter for the same property already exists it is replaced by the new filter. Note that each property may hold two filters, one primary and one temporary. Only primary filters are stored in the database.

Parameters:
filter - The new property filter
See Also:
PropertyFilter.isTemporary()

getPropertyFilter

public PropertyFilter getPropertyFilter(String property)
Get a filter for a property. If the given property has a temporary filter, it is returned, otherwise it's primary filter is returned.

Parameters:
property - The name of the property
Returns:
A PropertyFilter object or null if no filter exists

getPropertyFilters

public Collection<PropertyFilter> getPropertyFilters()
Get a collection of all property filters stored in this context. The returned collection may contain both primary and temporary filters, but only one filter for each property.

Returns:
A Collection containsing PropertyFilter objects or null

getNumPropertyFilters

public int getNumPropertyFilters()
Get the number of property filters stored in this context.

Since:
2.12

getPropertyValue

public String getPropertyValue(String property)
Get the value of a property filter. It is a shortcut for getPropertyFilter(property).getValue() but also handles the case of a non-existing filter for the specfied property.

Parameters:
property - The name of the property
Returns:
The value as a string

removePropertyFilter

public void removePropertyFilter(String property)
Remove a property filter. This method will remove both a primary and a temporary filter.

Parameters:
property - The name of the property

removeTemporaryFilter

public void removeTemporaryFilter(String property)
Remote a temporary filter for the given property. If a primary filter exists it will now be the effective filter.

Parameters:
property - The name of the property
Since:
2.16

getPropertyObject

public Object getPropertyObject(String property)

removeAllPropertyFilters

public void removeAllPropertyFilters()
Remove all property filter from this context.


removeAllTemporaryFilters

public void removeAllTemporaryFilters()
Remove all temporary filters.

Since:
2.16

getFilterRestriction

public Restriction getFilterRestriction(String property,
                                        DbControl dc,
                                        EntityQuery query)
Create a restriction from a property filter.

Parameters:
property - The property
dc - A DbControl that should be used if database access is needed
query - The query that the filter is going to be used in (can be null for most properties)
Returns:
A restriction, or null if no filter is active for the given property
Since:
2.15

setRecent

public void setRecent(BasicItem item,
                      int maxInList)
Add an item to the "recently used items" list. Each type of item has their own list which may contain at most maxInList items.

Note! The lists are stored as string of colon-separated ID:s in the setting given by the item type. For example the recently used protocols are found in the setting: getSetting("PROTOCOL.recent"). Client code is advised to not change these settings.

Parameters:
item - The item to add
maxInList - The maximum number of items in the list

setRecent

public void setRecent(BasicItem item,
                      String subList,
                      int maxInList)
Add an item to the "recently used items" list. Each type of item has their own list and sublists which may contain at most maxInList items.

Note! The lists are stored as string of colon-separated ID:s in the setting given by the item type and sublist name. For example the recently used protocols are found in the setting: getSetting("PROTOCOL.subList.recent"). Client code is advised to not change these settings.

Parameters:
item - The item to add
subList - The sublist
maxInList - The maximum number of items in the list
Since:
2.5

setRecent

public void setRecent(String key,
                      String value,
                      int maxInList)
Add a value to the list of recently used values for the specified key. Each key has it's own list of values which may contain at most maxInList values. If the value is already in the list it is moved to the front of the list.

Note! The lists are stored as string of colon-separated ID:s in the setting given by the key. For example, if key is "RAWDATATYPE", the recently used values are found in the setting: getSetting("RAWDATATYPE.recent"). Client code is advised to not change these settings.

Parameters:
key - The key to the list
value - The value to add
maxInList - The maximum number of values in the list

getNumRecent

public int getNumRecent(Item itemType)
Get the number of recently used items in the list.

Parameters:
itemType - The type of items

getNumRecent

public int getNumRecent(Item itemType,
                        String subList)
Get the number of recently used items in a list with a sublist.

Parameters:
itemType - The type of items
subList - The name of the sublist
Since:
2.5

getNumRecent

public int getNumRecent(String key)
Get the number of recently used value in the list.

Parameters:
key - The key to the list

getRecent

public List<? extends BasicItem> getRecent(DbControl dc,
                                           Item itemType)
Get all recently used items of the specified type. The list only contains items that the logged in user has read permission to and that hasn't been deleted. Thus, the getNumRecent(Item) may return a larger number than what is actually returned in this list.

Parameters:
dc - The DbControl used to access the database
itemType - The type of items
Returns:
A list with the items

getRecent

public List<? extends BasicItem> getRecent(DbControl dc,
                                           Item itemType,
                                           String subList)
Get all recently used items of the specified type in a given sublist. The list only contains items that the logged in user has read permission to and that hasn't been deleted. Thus, the getNumRecent(Item) may return a larger number than what is actually returned in this list.

Parameters:
dc - The DbControl used to access the database
itemType - The type of items
subList - The name of the sublist
Returns:
A list with the items
Since:
2.5

loadRecent

private List<? extends BasicItem> loadRecent(DbControl dc,
                                             Item itemType,
                                             String key)

getRecent

public BasicItem getRecent(DbControl dc,
                           Item itemType,
                           int index)

getRecent

public BasicItem getRecent(DbControl dc,
                           Item itemType,
                           String subList,
                           int index)

loadRecent

private BasicItem loadRecent(DbControl dc,
                             Item itemType,
                             String key,
                             int index)

getRecent

public List<String> getRecent(String key)
Get all recently used values for the specified key. Changes to the list are not recorded in the database. Use setRecent(String, String, int) instead.

Parameters:
key - The key to the list
Returns:
A list containing the values or an empty list if no recently used value has been stored

getRecent

public String getRecent(String key,
                        int index)

clearRecent

public void clearRecent(Item itemType)
Clear the recently used items list from items of the specified type.

Parameters:
itemType - The type of items

clearRecent

public void clearRecent(Item itemType,
                        String subList)
Clear the recently used items sublist from items of the specified type.

Parameters:
itemType - The type of items
subList - The name of the sublist

clearRecent

public void clearRecent(String key)
Clear the recently used values list.

Parameters:
key - The key to the list

clearAllRecent

public void clearAllRecent()
Clear all recently used values lists.


getRecentSettingName

private String getRecentSettingName(String key)

setRecent

private void setRecent(String key,
                       List<String> recent)

getSetting

public String getSetting(String name)
Get a value.

Parameters:
name - The name of the value
Returns:
The current value or null if no value exists
See Also:
setSetting(String, String)

setSetting

public String setSetting(String name,
                         String value)
Set a value. This property is stored in the database.

Parameters:
name - The name of the value
value - The value
Returns:
The old value or null if no old value existed.
See Also:
getSetting(String)

removeSetting

public String removeSetting(String name)
Remove a value.

Parameters:
name - The name of the value
Returns:
The old value or null if no old value existed.

getObject

public Object getObject(String name)
Get an object.

Parameters:
name - The name of the value
Returns:
The current value or null if no value exists
See Also:
setObject(String, Object)

setObject

public Object setObject(String name,
                        Object value)
Set a value. This property is not stored in the database.

Parameters:
name - The name of the value
value - The value
Returns:
The old value or null if no old value existed.
See Also:
getObject(String)

removeObject

public Object removeObject(String name)
Remove a value.

Parameters:
name - The name of the value
Returns:
The old value or null if no old value existed.

getQuery

public Query getQuery()

setQuery

public void setQuery(Query query)

configureQuery

public void configureQuery(EntityQuery query,
                           boolean autoLeftJoin)
                    throws BaseException
Deprecated. Use configureQuery(DbControl, EntityQuery, boolean) instead

This method will fail if used to filter on reporter lists or other items that require a DbControl.

Throws:
BaseException

configureQuery

public void configureQuery(DbControl dc,
                           EntityQuery query,
                           boolean autoLeftJoin)
                    throws BaseException
Use the settings in this context to configure a query.

Parameters:
dc - The DbControl that will be used to execute the query
query - The query to configure
autoLeftJoin - If true, and the sort property and filters are checked for associations to other items, which are automatically left joined to the query (instead of the hibernate default of inner join) (this feature is experimental)
Throws:
BaseException - If configuring the query fails.
Since:
2.8

copyFrom

void copyFrom(ItemContext other,
              boolean forceAll)
Copy settings from another context to this context. Only settings which doesn't have been set in this context is copied from the other context, unless the forceAll parameter is set.


filterOnSelectedItems

public void filterOnSelectedItems(EntityQuery query)
Add a filter to the given query that restricts it to only return items that have been selected. If no items have been selected the query is not modified.

Parameters:
query - The query to filter
Since:
2.17

configureQuery

public void configureQuery(DbControl dc,
                           DynamicQuery query,
                           List<String> selectionList)
                    throws BaseException
Use the settings in this context to configure a dynamic query.

Parameters:
dc - A DbControl to use if the database needs to be accessed
query - The query to configure
selectionList - A list of expression that we want to select from the query
Throws:
BaseException - If the configuring the query fails.

getDynamicExpression

public static Expression getDynamicExpression(DbControl dc,
                                              String propertyDef)
Create an Expression from a string. If the string starts with: It also recognises the following strings for columns to use by the Dynamic.column(VirtualColumn) method:

Parameters:
dc - A DbControl to use if the database needs to be accessed
propertyDef - The string to use for creating the expression.
Returns:
An expression object

getDynamicSelect

public static Select getDynamicSelect(DbControl dc,
                                      String propertyDef)
Same as getDynamicExpression(DbControl, String) but generates a select object instead.

Parameters:
dc - DbControl used to access the database
propertyDef - The string to use for creating the expression.
Returns:
An Expression object.

2.17.2: 2011-06-17