2.17.2: 2011-06-17

net.sf.basedb.clients.web
Class Base

java.lang.Object
  extended by net.sf.basedb.clients.web.Base

public final class Base
extends Object

This class is used to collect some useful methods for the web client into a single place. All methods in this class are static methods.

Version:
2.0
Author:
Nicklas
Last modified
$Date$

Field Summary
private static Integer defaultMaxRecent
           
private static Float defaultScale
           
 
Constructor Summary
Base()
           
 
Method Summary
static ItemContext createDefaultContext(String sortProperty, String defaultColumns)
          Create a default ItemContext with settings for default visible columns and sort property.
static ItemContext getAndSetCurrentContext(SessionControl sc, Item itemType, PageContext pageContext, ItemContext defaultContext)
          Same as: getAndSetCurrentContext(sc, itemType, "", pageContext, defaultContext, false).
static ItemContext getAndSetCurrentContext(SessionControl sc, Item itemType, PageContext pageContext, ItemContext defaultContext, boolean resetTemporary)
          Same as: getAndSetCurrentContext(sc, itemType, "", pageContext, defaultContext, resetTemporary).
static ItemContext getAndSetCurrentContext(SessionControl sc, Item itemType, String subContext, PageContext pageContext, ItemContext defaultContext)
          Same as: getAndSetCurrentContext(sc, itemType, subContext, pageContext, defaultContext, false).
static ItemContext getAndSetCurrentContext(SessionControl sc, Item itemType, String subContext, PageContext pageContext, ItemContext defaultContext, boolean resetTemporary)
           
static ItemContext getAndSetCurrentContext(SessionControl sc, Item itemType, String subContext, ServletRequest request, ItemContext defaultContext, boolean resetTemporary)
          Get the current context and update it from request information if available.
static ItemQuery<AnnotationType> getAnnotationTypesQuery(AnnotatableProxy proxy)
          Get a query that returns all annotation types defined for a AnnotatableProxy item.
static ItemQuery<AnnotationType> getAnnotationTypesQuery(Item itemType)
          Get a query that returns all annotation types defined for a particular item type.
static ItemQuery<AnnotationType> getAnnotationTypesQuery(Item itemType, Boolean isProtocolParameter)
          Get a query that returns all annotation types defined for a particular item type and, optionally, with a specific protocol parameter setting.
static Enumeration<String,String> getBioMaterialListsEnum(DbControl dc, Item memberType, Collection<Include> include)
          Get available biomaterial lists as an Enumeration that is suitable to use in a column definition for a table.
static ColorGenerator getColorGenerator(SessionControl sc, Coloring coloring)
           
static Color[] getConfiguredColors(SessionControl sc)
           
static
<T extends BasicItem>
ItemQuery<T>
getConfiguredQuery(DbControl dc, ItemContext cc, boolean autoLeftJoin, ItemQuery<T> query, ModeInfo mode)
          Configures a query.
static
<T extends BasicItem>
ItemQuery<T>
getConfiguredQuery(ItemContext cc, boolean autoLeftJoin, ItemQuery<T> query, ModeInfo mode)
          Deprecated. Use getConfiguredQuery(DbControl, ItemContext, boolean, ItemQuery, ModeInfo) instead
static Cookie getCookie(HttpServletRequest request, String name)
          Get the cookie with the specified name.
static String getCookieValue(HttpServletRequest request, String name)
          Get the value of the cookie with the specified name.
static ItemQuery<DataFileType> getDataFileTypes(Item itemType, FileStoreEnabled item, Platform platform, PlatformVariant variant)
          Get a query that returns all data file types for a specific platform/variant that can be used on a given item type.
static String getEncodedName(Nameable item, boolean denied)
           
static String getEncodedName(Nameable item, boolean denied, String link)
          Deprecated. Use getLinkedName(String, Nameable, boolean, boolean) instead
static SessionControl getExistingSessionControl(PageContext pageContext, boolean checkLoggedIn)
          Get an existing SessionControl object given a PageContext and optionally check that a user is logged in.
static SessionControl getExistingSessionControl(PageContext pageContext, Permission permission, Item itemType)
          Get an existing SessionControl object given a PageContext and check for generic (role-based) permission to an item type.
static SessionControl getExistingSessionControl(ServletRequest request, boolean checkLoggedIn)
          Get an existing SessionControl object given a ServletRequest object and optionally check that a user is logged in.
static SessionControl getExistingSessionControl(ServletRequest request, Permission permission, Item itemType)
          Get an existing SessionControl object given a ServletRequest and check for generic (role-based) permission to an item type.
static String getFileLinks(String ID, File file, String root)
          Get view/download links for a file.
static
<T extends BasicItem>
T
getFirstMatching(DbControl dc, ItemQuery<T> query, String property, PropertyFilter filter)
           
static String getLink(String ID, String name, Item itemType, int itemId, boolean enableEditLink)
           
static String getLink(String ID, String name, Item itemType, int itemId, boolean enableEditLink, String tooltip)
           
static String getLinkedFile(String ID, File file, boolean denied, boolean enableEditLink, boolean enableViewDownload, String root)
           
static String getLinkedName(String ID, Nameable item, boolean denied, boolean enableEditLink)
           
static int getMaxRecent(SessionControl sc)
          Get the setting for the maximum number of recently used items to save.
static
<T extends BasicItem>
T
getOldItem(SessionControl sc, T newItem, int oldVersion)
          Get the old item that is stored in the SessionControl:s session setting itemType.item.
static String getPropertyFilterString(PropertyFilter filter, Formatter<Date> dateFormatter)
          Convert a property filter to a displayable string with operator + value.
static ItemQuery<AnnotationType> getProtocolParametersQuery(Protocol protocol)
          Get a query that returns all annotation types assigned as parameters to a specific protocol.
static Enumeration<String,String> getReporterListsEnum(DbControl dc)
           
static float getScale(SessionControl sc)
          Get the display scale setting.
static SessionControl getSessionControl(PageContext pageContext, boolean create)
          Get a new or existing SessionControl object given a PageContext.
static SessionControl getSessionControl(ServletRequest request, boolean create)
          Get a new or existing SessionControl object given a ServletRequest instance.
static ItemContext loadContext(SessionControl sc, int contextId, ItemContext defaultContext)
           
private static boolean setInclude(Set<Include> includes, String option, Include i)
          Set include option if option parameter isn't null.
static void updateAnnotations(DbControl dc, Annotatable oldItem, Annotatable newItem, HttpServletRequest request)
          Update annotations on an item taking concurrent modifications into account.
static void updateFiles(DbControl dc, FileStoreEnabled item, HttpServletRequest request, boolean validate, boolean extractMetadata, ItemContext cc, int maxRecent)
          Update files that are associated with a FileStoreEnabled item.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

defaultScale

private static final Float defaultScale

defaultMaxRecent

private static final Integer defaultMaxRecent
Constructor Detail

Base

public Base()
Method Detail

getSessionControl

public static final SessionControl getSessionControl(PageContext pageContext,
                                                     boolean create)
                                              throws BaseException
Get a new or existing SessionControl object given a PageContext. This method will get the ID parameter from the page URL and try to retrieve the corresponding object from the BASE Application. If no SessionControl object exists, a new one will be created if the create parameter is TRUE.

Parameters:
pageContext - The JSP pageContext variable
create - If a new SessionControl should be created or not
Returns:
A SessionControl object
Throws:
BaseException - If there is an error

getSessionControl

public static final SessionControl getSessionControl(ServletRequest request,
                                                     boolean create)
Get a new or existing SessionControl object given a ServletRequest instance. This method will get the ID parameter from the page URL and try to retrieve the corresponding object from the BASE Application. If no SessionControl object exists, a new one will be created if the create parameter is TRUE.

Parameters:
request - The Servlet request variable
create - If a new SessionControl should be created or not
Returns:
A SessionControl object
Throws:
BaseException - If there is an error
Since:
2.7

getExistingSessionControl

public static final SessionControl getExistingSessionControl(PageContext pageContext,
                                                             boolean checkLoggedIn)
                                                      throws NotLoggedInException,
                                                             BaseException
Get an existing SessionControl object given a PageContext and optionally check that a user is logged in. This method will get the ID parameter from the page URL and try to retrieve the corresponding object from the Application and check that a user is logged in if the checkLoggedIn parameter is TRUE.

Parameters:
pageContext - The JSP pageContext variable
checkLoggedIn - If the method should check if a user is logged in or not
Returns:
A SessionControl object
Throws:
NotLoggedInException - If a SessionControl object isn't found or no user is logged in
BaseException - If there is another error

getExistingSessionControl

public static final SessionControl getExistingSessionControl(ServletRequest request,
                                                             boolean checkLoggedIn)
                                                      throws NotLoggedInException,
                                                             BaseException
Get an existing SessionControl object given a ServletRequest object and optionally check that a user is logged in. This method will get the ID parameter from the page URL and try to retrieve the corresponding object from the Application and check that a user is logged in if the checkLoggedIn parameter is TRUE.

Parameters:
request - The Servlet request variable
checkLoggedIn - If the method should check if a user is logged in or not
Returns:
A SessionControl object
Throws:
NotLoggedInException - If a SessionControl object isn't found or no user is logged in
BaseException - If there is another error
Since:
2.7

getExistingSessionControl

public static final SessionControl getExistingSessionControl(PageContext pageContext,
                                                             Permission permission,
                                                             Item itemType)
                                                      throws PermissionDeniedException,
                                                             NotLoggedInException,
                                                             BaseException
Get an existing SessionControl object given a PageContext and check for generic (role-based) permission to an item type. This method will get the ID parameter from the page URL and try to retrieve the corresponding object from the Application. Then it checks that a user is logged in and that the logged in user has the specified permission for item type. If Permission.DENIED is specified, this method checks that the logged in user haven't been denied acces to those items.

Parameters:
pageContext - The JSP pageContext variable
permission - The permission to check for, use one of the codes defined by the Permission class
itemType - The code for the item type to check for the permission, use one of the values defined by the Item class
Returns:
A SessionControl object
Throws:
PermissionDeniedException - If the logged in user doesn't have the requested permission
NotLoggedInException - If no user is logged in or a SessionControl object not is found
BaseException - If there is another error

getExistingSessionControl

public static final SessionControl getExistingSessionControl(ServletRequest request,
                                                             Permission permission,
                                                             Item itemType)
                                                      throws PermissionDeniedException,
                                                             NotLoggedInException,
                                                             BaseException
Get an existing SessionControl object given a ServletRequest and check for generic (role-based) permission to an item type. This method will get the ID parameter from the page URL and try to retrieve the corresponding object from the Application. Then it checks that a user is logged in and that the logged in user has the specified permission for item type. If Permission.DENIED is specified, this method checks that the logged in user haven't been denied acces to those items.

Parameters:
request - The Servlet request variable
permission - The permission to check for, use one of the codes defined by the Permission class
itemType - The code for the item type to check for the permission, use one of the values defined by the Item class
Returns:
A SessionControl object
Throws:
PermissionDeniedException - If the logged in user doesn't have the requested permission
NotLoggedInException - If no user is logged in or a SessionControl object not is found
BaseException - If there is another error
Since:
2.7

getAndSetCurrentContext

public static ItemContext getAndSetCurrentContext(SessionControl sc,
                                                  Item itemType,
                                                  PageContext pageContext,
                                                  ItemContext defaultContext)
Same as: getAndSetCurrentContext(sc, itemType, "", pageContext, defaultContext, false).

See Also:
getAndSetCurrentContext(SessionControl, Item, String, PageContext, ItemContext, boolean)

getAndSetCurrentContext

public static ItemContext getAndSetCurrentContext(SessionControl sc,
                                                  Item itemType,
                                                  PageContext pageContext,
                                                  ItemContext defaultContext,
                                                  boolean resetTemporary)
Same as: getAndSetCurrentContext(sc, itemType, "", pageContext, defaultContext, resetTemporary).

See Also:
getAndSetCurrentContext(SessionControl, Item, String, PageContext, ItemContext, boolean)

getAndSetCurrentContext

public static ItemContext getAndSetCurrentContext(SessionControl sc,
                                                  Item itemType,
                                                  String subContext,
                                                  PageContext pageContext,
                                                  ItemContext defaultContext)
Same as: getAndSetCurrentContext(sc, itemType, subContext, pageContext, defaultContext, false).

See Also:
getAndSetCurrentContext(SessionControl, Item, String, PageContext, ItemContext, boolean)

getAndSetCurrentContext

public static ItemContext getAndSetCurrentContext(SessionControl sc,
                                                  Item itemType,
                                                  String subContext,
                                                  PageContext pageContext,
                                                  ItemContext defaultContext,
                                                  boolean resetTemporary)
See Also:
getAndSetCurrentContext(SessionControl, Item, String, ServletRequest, ItemContext, boolean)

getAndSetCurrentContext

public static ItemContext getAndSetCurrentContext(SessionControl sc,
                                                  Item itemType,
                                                  String subContext,
                                                  ServletRequest request,
                                                  ItemContext defaultContext,
                                                  boolean resetTemporary)
Get the current context and update it from request information if available. This methods updates the following settings on the context:

Parameters:
sc - The current session control
itemType - The type of item to get the context for
subContext - The name of the subcontext
request - A ServletRequest object to get request information from, or null if the current context shouldn't be updated
defaultContext - A default context to copy information from if a current context doesn't exist
resetTemporary - If information of temporary nature should be reset or not, for example the 'exclude' filter which is only used in popup windows
Returns:
an updated ItemContext object.
Since:
2.7

loadContext

public static ItemContext loadContext(SessionControl sc,
                                      int contextId,
                                      ItemContext defaultContext)

getPropertyFilterString

public static String getPropertyFilterString(PropertyFilter filter,
                                             Formatter<Date> dateFormatter)
Convert a property filter to a displayable string with operator + value. Ie. >10000, =Nicklas

Parameters:
filter - The property filter to convert
dateFormatter - Describing how the date is format locally. The format will be yyyy-MM-dd if this is null.
Returns:
A displayable string (null is never returned)

setInclude

private static boolean setInclude(Set<Include> includes,
                                  String option,
                                  Include i)
Set include option if option parameter isn't null.

Returns:
TRUE if the option was changed, FALSE if not

createDefaultContext

public static ItemContext createDefaultContext(String sortProperty,
                                               String defaultColumns)
Create a default ItemContext with settings for default visible columns and sort property. 'MINE' and 'SHARED' objects are set to be included.

Parameters:
sortProperty - The property, as a String, to sort on. If it starts with a '-', the sort is made in descending order (and the minus is removed)
defaultColumns - The visible columns as a comma separated String.
Returns:
an ItemContext object

getConfiguredQuery

public static <T extends BasicItem> ItemQuery<T> getConfiguredQuery(ItemContext cc,
                                                                    boolean autoLeftJoin,
                                                                    ItemQuery<T> query,
                                                                    ModeInfo mode)
Deprecated. Use getConfiguredQuery(DbControl, ItemContext, boolean, ItemQuery, ModeInfo) instead

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


getConfiguredQuery

public static <T extends BasicItem> ItemQuery<T> getConfiguredQuery(DbControl dc,
                                                                    ItemContext cc,
                                                                    boolean autoLeftJoin,
                                                                    ItemQuery<T> query,
                                                                    ModeInfo mode)
Configures a query. This method calls ItemContext.configureQuery(DbControl, EntityQuery, boolean) and then adds the following filter:

Type Parameters:
T - The kind of BasicItem this method is used with.
Parameters:
dc - The DbControl that will be used to execute the query
cc - The current context
autoLeftJoin - If associated items should be left joined or not when they appear in a filter
query - The query to configure
mode - Info about the mode the page has.
Returns:
The same query, configured according to information in the current context
Since:
2.8

getAnnotationTypesQuery

public static ItemQuery<AnnotationType> getAnnotationTypesQuery(Item itemType)
Get a query that returns all annotation types defined for a particular item type. The query will include all items owned by or shared to the logged in user or the active project.

Parameters:
itemType - The item type
Returns:
An ItemQuery object
See Also:
getAnnotationTypesQuery(Item, Boolean)

getAnnotationTypesQuery

public static ItemQuery<AnnotationType> getAnnotationTypesQuery(Item itemType,
                                                                Boolean isProtocolParameter)
Get a query that returns all annotation types defined for a particular item type and, optionally, with a specific protocol parameter setting. The query will include all possible annotation types.

Parameters:
itemType - The item type
isProtocolParameter - Specify if only annotation types assigned as protocol parameters should be returned, a null value ignores the protocol parameter flag
Returns:
An ItemQuery object

getAnnotationTypesQuery

public static ItemQuery<AnnotationType> getAnnotationTypesQuery(AnnotatableProxy proxy)
Get a query that returns all annotation types defined for a AnnotatableProxy item.

Parameters:
proxy - The annotatable proxy
Returns:
An ItemQuery object

getProtocolParametersQuery

public static ItemQuery<AnnotationType> getProtocolParametersQuery(Protocol protocol)
Get a query that returns all annotation types assigned as parameters to a specific protocol. The query will include all possible annotation types.

Parameters:
protocol - The protocol, or null
Returns:
An ItemQuery object, or null if the protocol is null

getDataFileTypes

public static ItemQuery<DataFileType> getDataFileTypes(Item itemType,
                                                       FileStoreEnabled item,
                                                       Platform platform,
                                                       PlatformVariant variant)
Get a query that returns all data file types for a specific platform/variant that can be used on a given item type. If an item is specified the query will also return data file types that already are present in the item's file set even if those file types are not part of the platform / variant.

Parameters:
itemType - The item type
item - An optional item
platform - The platform to get file types for, or null
variant - The platform variant to get file types for, or null
Returns:
An ItemQuery object
Since:
2.5

getReporterListsEnum

public static Enumeration<String,String> getReporterListsEnum(DbControl dc)

getBioMaterialListsEnum

public static Enumeration<String,String> getBioMaterialListsEnum(DbControl dc,
                                                                 Item memberType,
                                                                 Collection<Include> include)
Get available biomaterial lists as an Enumeration that is suitable to use in a column definition for a table.

Parameters:
dc - The DbControl to use
memberType - Only load lists with this member type, or null to load any lists
include - Include options
Returns:
The found lists as an enumeration, empty if no lists are found
Since:
2.10

getScale

public static float getScale(SessionControl sc)
Get the display scale setting. For normal font the scale is 1.0, for larger fonts the scale is higher and for smaller fonts the scale is lower. The scale is used for recalculating the size of popup windows depending on the font size setting.

Parameters:
sc - SessionControl of the active session
Returns:
The current scale as a float.

getMaxRecent

public static int getMaxRecent(SessionControl sc)
Get the setting for the maximum number of recently used items to save.

Parameters:
sc - SessionControl of the active session.
Returns:
an int.

getCookie

public static final Cookie getCookie(HttpServletRequest request,
                                     String name)
Get the cookie with the specified name.

Parameters:
request - The JSP request variable
name - The name of the cookie
Returns:
A Cookie object, or null if it is not found

getCookieValue

public static final String getCookieValue(HttpServletRequest request,
                                          String name)
Get the value of the cookie with the specified name.

Parameters:
request - The JSP request variable
name - The name of the cookie
Returns:
The value of the cookie, or null if it is not found

getOldItem

public static final <T extends BasicItem> T getOldItem(SessionControl sc,
                                                       T newItem,
                                                       int oldVersion)
                                            throws ItemModifiedException
Get the old item that is stored in the SessionControl:s session setting itemType.item. If the setting is null compare the newItem:s version with the oldVersion, and if the match the new item is returned as the old item. If the versions doesn't match, an ItemModifiedException is thrown.

Type Parameters:
T - The kind of item that extends from BasicItem.
Parameters:
sc - The SessionControl which keeps the old item in the session settings
newItem - The new item
oldVersion - The version of the old item
Returns:
The old item if it is found in the session setting, or the new item if the old version is the same
Throws:
ItemModifiedException - If the old item wasn't found in the session setting and the old version is different from the new item's version

updateAnnotations

public static final void updateAnnotations(DbControl dc,
                                           Annotatable oldItem,
                                           Annotatable newItem,
                                           HttpServletRequest request)
                                    throws ItemModifiedException,
                                           BaseException
Update annotations on an item taking concurrent modifications into account. The old item is the object that was used to present the annotations for the user in the web form. The new item is the object that is loaded in the submit request. The new item is used to update the annotations. While updating the annotations the version of each annotation on the old an new item is compared. If they don't match an ItemModifiedException is thrown. We do not have to compare with the values in the request, since only modified annotations are submitted.

Parameters:
dc - A DbControl object used to read information from the database
oldItem - The old annotatable item, or null if no old item exists
newItem - The new item, or null if only an old item exists
request - The request object containing the modified annotations
Throws:
ItemModifiedException - If the new and old item don't match
BaseException - If the update fails in some way.

updateFiles

public static void updateFiles(DbControl dc,
                               FileStoreEnabled item,
                               HttpServletRequest request,
                               boolean validate,
                               boolean extractMetadata,
                               ItemContext cc,
                               int maxRecent)
Update files that are associated with a FileStoreEnabled item. The request parameter is checked for parameters with a name like datafile.id where id is the numeric id of a DataFileType. The parameter value is the path of the file to associated with the data file type, or null to remove an existing association.

Parameters:
dc - A DbControl to access the database
item - The item the files should be associated with
request - The request object containing the selected files
validate - If FileSet.validate(DbControl, boolean) should be called or not
extractMetadata - If meta data should be extracted or not (requires that validate is true)
cc - Save the current context information. Null is allowed.
maxRecent - The maximum of recent items to remember.
Since:
2.5

getEncodedName

public static String getEncodedName(Nameable item,
                                    boolean denied)
See Also:
getEncodedName(Nameable, boolean, String)

getEncodedName

public static String getEncodedName(Nameable item,
                                    boolean denied,
                                    String link)
Deprecated. Use getLinkedName(String, Nameable, boolean, boolean) instead

Encode an link the name of the nameable item using the HTML.encodeTags(String) method. If the item parameter is null, - none - or - denied - is returned, depending on if the denied parameter is true or false. The link parameter should contain the relative path to the JSP page and include the session control ID: ../samples/index.jsp?ID=XXXX. This method will automatically add the id of the nameable item to the link: ../samples/index.jsp?ID=XXXX&cmd=ViewItem&item_id=YYYY

Parameters:
item - The nameable item or null
denied - TRUE if the item is null because the current use doesn't have read permission
link - The URL of the JSP page to link the name to, or null if a link shouldn't be created
See Also:
getEncodedName(Nameable, boolean)

getLinkedName

public static String getLinkedName(String ID,
                                   Nameable item,
                                   boolean denied,
                                   boolean enableEditLink)

getLink

public static String getLink(String ID,
                             String name,
                             Item itemType,
                             int itemId,
                             boolean enableEditLink)

getLink

public static String getLink(String ID,
                             String name,
                             Item itemType,
                             int itemId,
                             boolean enableEditLink,
                             String tooltip)
Since:
2.9

getLinkedFile

public static String getLinkedFile(String ID,
                                   File file,
                                   boolean denied,
                                   boolean enableEditLink,
                                   boolean enableViewDownload,
                                   String root)

getFileLinks

public static String getFileLinks(String ID,
                                  File file,
                                  String root)
Get view/download links for a file.

Parameters:
ID - The session ID
file - The file item
root - Relative path to root directory of the web server
Returns:
HTML code for the links
Since:
2.5

getFirstMatching

public static <T extends BasicItem> T getFirstMatching(DbControl dc,
                                                       ItemQuery<T> query,
                                                       String property,
                                                       PropertyFilter filter)

getConfiguredColors

public static Color[] getConfiguredColors(SessionControl sc)

getColorGenerator

public static ColorGenerator getColorGenerator(SessionControl sc,
                                               Coloring coloring)

2.17.2: 2011-06-17