Class 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 Detail

      • WEBCLIENT_ID

        public static final String WEBCLIENT_ID
        The external ID of the web client application.
        Since:
        3.9
        See Also:
        Constant Field Values
      • 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,
                                                             String externalClientId,
                                                             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
        externalClientId - ID of the client application
        create - If a new SessionControl should be created or not
        Returns:
        A SessionControl object
        Throws:
        BaseException - If there is an error
        Since:
        3.9
      • 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,
                                                                     String externalClientId,
                                                                     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
        externalClientId - ID of the client application
        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:
        3.9
      • 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,
                                                          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:
        • If the active project has changed since last time the context was retrieved, the Include.IN_PROJECT option is added to ItemContext.getInclude()
        • If a default context is specified, the defaultColumns object is copied to the current context.
        • The following settings are only updated if they are available in the request parameters:
          Context settings
          Setting Request parameter
          setSetting("columns") columns
          setRowsPerPage rowsperpage
          setPage page
          setSortProperty sortby
          setSortDirection direction
          Include.REMOVED removed
          Include.MINE owned
          Include.SHARED shared
          Include.IN_PROJECT inproject
          Include.OTHERS others

          If a the name of a request parameter starts with "filter:" it is interpreted as a property filter. The name is formatted as follows: filter:TYPE:property where TYPE is one of Enum.name() strings and property is the name of the property to filter on. This information is used to create a new PropertyFilter object. The operator and value parameter is taken from the request parameters value.

          Operator mappings
          Value starts with Operator
          = Operator.EQ
          != or <> Operator.NEQ
          >= Operator.GTEQ
          > Operator.GT
          <= Operator.LTEQ
          < Operator.LT
          any other charcter Operator.EQ or Operator.LIKE (if value contains % sign which is wildcard match)

          If the name of a request parameter is a number it is interpreted as the ID of a selected itema and is added to the ItemContext.getSelected() set.

        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
      • 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 BasicItemItemQuery<T> getConfiguredQuery​(DbControl dc,
                                                                            ItemContext cc,
                                                                            ClientContext context,
                                                                            boolean autoLeftJoin,
                                                                            ItemQuery<T> query,
                                                                            ModeInfo mode)
        Configures a query. This method calls ItemContext.configureQuery(DbControl, EntityQuery, boolean) and then adds the following filter:
        • If ItemContext.getObject("exclude") contains a list of comma-separated ID:s those are added to the query as a filter that excludes item with those ID:s from the query result
        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:
        3.18
      • 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
      • getInheritedAnnotationColumns

        public static ItemQuery<AnnotationType> getInheritedAnnotationColumns​(String columns)
        Get a query returning all annotation types that are needed to display inherited annotations for the given columns. The columns is a string with comma-separated values that define the visible columns in a table. Columns for inherited annotations are defined by 'ia'+<id>.
        Since:
        3.5
      • getDataFileColumns

        public static ItemQuery<DataFileType> getDataFileColumns​(Item itemType,
                                                                 String columns)
        Get a query returning all data file types that are needed to display file set members for the given columns. The columns is a string with comma-separated values that define the visible columns in a table. Columns for data file types annotations are defined by 'ft'+<id>.
        Since:
        3.5
      • getSubtypesQuery

        public static ItemQuery<ItemSubtype> getSubtypesQuery​(Item itemType)
        Get a query that returns all subtypes for the given main item type.
        Parameters:
        itemType - The main item type
        Returns:
        An ItemQuery object
        Since:
        3.0
      • 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,
                                                               ItemSubtype subtype)
        Get a query that returns all data file types for a specific platform/variant/subtype 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
        subtype - The item subtype to get the file types for, or null
        Returns:
        An ItemQuery object
        Since:
        3.0
      • getItemListsEnum

        public static Enumeration<String,​String> getItemListsEnum​(DbControl dc,
                                                                        Item memberType,
                                                                        Collection<Include> include)
        Get available item 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 all lists
        include - Include options
        Returns:
        The found lists as an enumeration, empty if no lists are found
        Since:
        3.5
      • getItemListsEnum

        public static Enumeration<String,​String> getItemListsEnum​(DbControl dc,
                                                                        Item memberType,
                                                                        Collection<Include> include,
                                                                        Filter<? super ItemList> filter)
        Get available item 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 all lists
        include - Include options
        filter - Additional filter to select which item lists to include (optional)
        Returns:
        The found lists as an enumeration, empty if no lists are found
        Since:
        3.18
      • 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,
                                       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) should be called or not
        cc - Save the current context information. Null is allowed.
        maxRecent - The maximum of recent items to remember.
        Since:
        3.0
      • getEncodedName

        public static String getEncodedName​(Nameable item,
                                            boolean denied)
        Encode 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.
        Parameters:
        item - The nameable item or null
        denied - TRUE if the item is null because the current use doesn't have read permission
      • 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