Package rife.cmf.dam

Class ContentQueryManager<T>

All Implemented Interfaces:
Cloneable, GenericQueryManager<T>, ValidationContext

public class ContentQueryManager<T> extends GenericQueryManagerDelegate<T> implements Cloneable
The ContentQueryManager simplifies working with content a lot. It extends GenericQueryManager and is a drop-in replacement that can be used instead. The ContentQueryManager class works hand-in-hand with CMF-related constraints that are provided via the classes Validation and ConstrainedProperty. The additional constraints allow you to provide CMF-related metadata for bean properties while still having access to all regular constraints.

The most important additional constraint is 'mimeType'. Setting this constraint directs RIFE to delegate the handling of that property's data to the CMF instead of storing it as a regular column in a database table. The property content location (i.e. its full path) is generated automatically based on the bean class name, the instance's identifier value (i.e. the primary key used by GenericQueryManager), and the property name. So for example, if you have an instance of the NewsItem class whose identifier is 23, then the full path that is generated for a property named text is '/newsitem/23/text'. Note that this always specifies the most recent version of the property, but that older versions are also available from the content store.

Before being able to use the CMF and a ContentQueryManager, you must install both of them, as in this example:

 DatabaseContentFactory.instance(datasource).install();
 new ContentQueryManager(datasource, NewsItem.class).install();

Apart from the handling of content, this query manager also integrates the functionalities of the OrdinalManager class.

The new 'ordinal' constraint indicates which bean property will be used to order that table's rows. When saving and deleting beans, the ordinal values will be automatically updated in the entire table. The ContentQueryManager also provides the move, up and down methods to easily manipulate the order of existing rows.

Since:
1.0
  • Constructor Details

    • ContentQueryManager

      public ContentQueryManager(Datasource datasource, Class<T> klass, Class backendClass)
      Creates a new ContentQueryManager instance for a specific class.

      All content will be stored in a DatabaseContent.

      Parameters:
      datasource - the datasource that indicates where the data will be stored
      klass - the class of the bean that will be handled by this ContentQueryManager
      backendClass - the class will be used by this ContentQueryManager to reference data in the backend
      Since:
      1.0
    • ContentQueryManager

      public ContentQueryManager(Datasource datasource, Class<T> klass, String table)
      Creates a new ContentQueryManager instance for a specific class, but with a different table name for the database storage.

      All content will be stored in a DatabaseContent.

      Parameters:
      datasource - the datasource that indicates where the data will be stored
      klass - the class of the bean that will be handled by this ContentQueryManager
      table - the name of the database table in which the non CMF data will be stored
      Since:
      1.6
    • ContentQueryManager

      public ContentQueryManager(Datasource datasource, Class<T> klass)
      Creates a new ContentQueryManager instance for a specific class.

      All content will be stored in a DatabaseContent.

      Parameters:
      datasource - the datasource that indicates where the data will be stored
      klass - the class of the bean that will be handled by this ContentQueryManager
      Since:
      1.0
    • ContentQueryManager

      public ContentQueryManager(Datasource datasource, Class<T> klass, ContentManager contentManager)
      Creates a new ContentQueryManager instance for a specific class.

      All content will be stored in the provided ContentManager instance. This constructor is handy if you want to integrate a custom content manager implementation.

      Parameters:
      datasource - the datasource that indicates where the data will be stored
      klass - the class of the bean that will be handled by this ContentQueryManager
      contentManager - a ContentManager instance
      Since:
      1.0
  • Method Details

    • repository

      public ContentQueryManager<T> repository(String repository)
      Sets the default repository that will be used by this ContentQueryManager.
      Returns:
      this ContentQueryManager
      Since:
      1.4
      See Also:
    • getRepository

      public String getRepository()
      Retrieves the default repository that is used by this ContentQueryManager.
      Returns:
      this ContentQueryManager's repository
      Since:
      1.4
      See Also:
    • getContentManager

      public ContentManager getContentManager()
      Returns the ContentManager that is used to store and retrieve the content.
      Returns:
      the ContentManager
      Since:
      1.0
    • move

      public boolean move(Constrained bean, String propertyName, OrdinalManager.Direction direction)
      Moves the row that corresponds to the provided bean instance according to a property with an ordinal constraint.
      Parameters:
      bean - the bean instance that corresponds to the row that has to be moved
      propertyName - the name of the property with an ordinal constraint
      direction - OrdinalManager.UP or OrdinalManager.DOWN
      Returns:
      true if the row was moved successfully; or

      false otherwise

      Since:
      1.0
    • up

      public boolean up(Constrained bean, String propertyName)
      Moves the row that corresponds to the provided bean instance upwards according to a property with an ordinal constraint.
      Parameters:
      bean - the bean instance that corresponds to the row that has to be moved
      propertyName - the name of the property with an ordinal constraint
      Returns:
      true if the row was moved successfully; or

      false otherwise

      Since:
      1.0
    • down

      public boolean down(Constrained bean, String propertyName)
      Moves the row that corresponds to the provided bean instance downwards according to a property with an ordinal constraint.
      Parameters:
      bean - the bean instance that corresponds to the row that has to be moved
      propertyName - the name of the property with an ordinal constraint
      Returns:
      true if the row was moved successfully; or

      false otherwise

      Since:
      1.0
    • storeEmptyContent

      public boolean storeEmptyContent(T bean, String propertyName)
      Empties the content of a certain bean property.

      When a bean is saved, null content properties are simply ignored when the property hasn't got an autoRetrieved constraint. This is needed to make it possible to only update a bean's data without having to fetch the content from the back-end and store it together with the other data just to make a simple update. However, this makes it impossible to rely on null to indicate empty content. This method has thus been added explicitly for this purpose.

      Parameters:
      bean - the bean instance that contains the property
      propertyName - the name of the property whose content has to be emptied in the database
      Returns:
      true if the empty content was stored successfully; or

      false otherwise

      Since:
      1.0
    • save

      public int save(T bean) throws DatabaseException
      Saves a bean.

      This augments the regular GenericQueryManager's save method with behaviour that correctly handles content or ordinal properties. When a bean is saved, null content properties are simply ignored when the property hasn't got an autoRetrieved constraint. This is needed to make it possible to only update a bean's data without having to fetch the content from the back-end and store it together with the other data just to make a simple update.

      Specified by:
      save in interface GenericQueryManager<T>
      Overrides:
      save in class GenericQueryManagerDelegate<T>
      Parameters:
      bean - the bean instance that has to be saved
      Returns:
      true if the bean was stored successfully; or

      false otherwise

      Throws:
      DatabaseException
      Since:
      1.0
    • restore

      public T restore(int objectId) throws DatabaseException
      Restores a bean according to its ID.

      This augments the regular GenericQueryManager's restore method with behaviour that correctly handles content properties.

      Specified by:
      restore in interface GenericQueryManager<T>
      Overrides:
      restore in class GenericQueryManagerDelegate<T>
      Parameters:
      objectId - the ID of the bean that has to be restored
      Returns:
      the bean instance if it was restored successfully; or

      null if it couldn't be found

      Throws:
      DatabaseException
      Since:
      1.0
    • restoreFirst

      public T restoreFirst(RestoreQuery query) throws DatabaseException
      Restores the first bean from a RestoreQuery.

      This augments the regular GenericQueryManager's restore method with behaviour that correctly handles content properties.

      Specified by:
      restoreFirst in interface GenericQueryManager<T>
      Overrides:
      restoreFirst in class GenericQueryManagerDelegate<T>
      Parameters:
      query - the query that will be used to restore the beans
      Returns:
      the first bean instance that was found; or

      null if no beans could be found

      Throws:
      DatabaseException
      Since:
      1.0
      See Also:
    • restore

      public List<T> restore() throws DatabaseException
      Restores all beans.

      This augments the regular GenericQueryManager's restore method with behaviour that correctly handles content properties.

      Specified by:
      restore in interface GenericQueryManager<T>
      Overrides:
      restore in class GenericQueryManagerDelegate<T>
      Returns:
      the list of beans; or

      null if no beans could be found

      Throws:
      DatabaseException
      Since:
      1.0
    • restore

      public List<T> restore(RestoreQuery query) throws DatabaseException
      Restores all beans from a RestoreQuery.

      This augments the regular GenericQueryManager's restore method with behaviour that correctly handles content properties.

      Specified by:
      restore in interface GenericQueryManager<T>
      Overrides:
      restore in class GenericQueryManagerDelegate<T>
      Parameters:
      query - the query that will be used to restore the beans
      Returns:
      the list of beans; or

      null if no beans could be found

      Throws:
      DatabaseException
      Since:
      1.0
      See Also:
    • delete

      public boolean delete(int objectId) throws DatabaseException
      Deletes a bean according to its ID.

      This augments the regular GenericQueryManager's restore method with behaviour that correctly handles content and ordinal properties.

      Specified by:
      delete in interface GenericQueryManager<T>
      Overrides:
      delete in class GenericQueryManagerDelegate<T>
      Parameters:
      objectId - the ID of the bean that has to be restored
      Returns:
      true if the bean was deleted successfully; or

      false if it couldn't be found

      Throws:
      DatabaseException
      Since:
      1.0
    • hasContent

      public boolean hasContent(T bean, String propertyName) throws DatabaseException
      Checks if there's content available for a certain property of a bean.
      Parameters:
      bean - the bean instance that will be checked
      propertyName - the name of the property whose content availability will be checked
      Returns:
      true if content is available; or

      false otherwise

      Throws:
      DatabaseException
      Since:
      1.0
    • hasContent

      public boolean hasContent(int objectId, String propertyName) throws DatabaseException
      Checks if there's content available for a certain property of a bean.
      Parameters:
      objectId - the ID of the bean instance that will be checked
      propertyName - the name of the property whose content availability will be checked
      Returns:
      true if content is available; or

      false otherwise

      Throws:
      DatabaseException
      Since:
      1.0
    • buildCmfPath

      public String buildCmfPath(T bean, String propertyName)
      Builds the path that is used by the ContentQueryManager for a certain bean and property.
      Parameters:
      bean - the bean instance that will be used to construct the path
      propertyName - the name of the property that will be used to construct the path
      Returns:
      the requested path
      Since:
      1.0
    • buildCmfPath

      public String buildCmfPath(int objectId, String propertyName)
      Builds the path that is used by the ContentQueryManager for a certain bean ID and property.
      Parameters:
      objectId - the bean ID that will be used to construct the path
      propertyName - the name of the property that will be used to construct the path
      Returns:
      the requested path
      Since:
      1.0
    • buildServeContentPath

      public String buildServeContentPath(T bean, String propertyName)
      Builds the path that is used by the ServeContent element for a certain bean and property.

      Any declaration of the repository name will be ignored, since the ServeContent element doesn't allow you to provide this through the URL for safety reasons.

      Parameters:
      bean - the bean instance that will be used to construct the path
      propertyName - the name of the property that will be used to construct the path
      Returns:
      the requested path
      Since:
      1.4
    • buildServeContentPath

      public String buildServeContentPath(int objectId, String propertyName)
      Builds the path that is used by the ServeContent element for a certain bean ID and property.

      Any declaration of the repository name will be ignored, since the ServeContent element doesn't allow you to provide this through the URL for safety reasons.

      Parameters:
      objectId - the bean ID that will be used to construct the path
      propertyName - the name of the property that will be used to construct the path
      Returns:
      the requested path
      Since:
      1.4
    • getContentForHtml

      public String getContentForHtml(T bean, String propertyName, Context context, Route route) throws ContentManagerException
      Retrieves a content data representation for use in html.

      This is mainly used to integrate content data inside a html document. For instance, html content will be displayed as-is, while image content will cause an image tag to be generated with the correct source URL to serve the image.

      Parameters:
      bean - the bean instance that contains the data
      propertyName - the name of the property whose html representation will be provided
      context - an active web engine context
      route - a route that leads to a rife.cmf.elements.ServeContent element
      Returns:
      the html content representation
      Throws:
      ContentManagerException - if an unexpected error occurred
      Since:
      1.0
    • getContentForHtml

      public String getContentForHtml(int objectId, String propertyName, Context context, Route route) throws ContentManagerException
      Retrieves a content data representation for use in html.

      This is mainly used to integrate content data inside a html document. For instance, html content will be displayed as-is, while image content will cause an image tag to be generated with the correct source URL to serve the image.

      Parameters:
      objectId - the ID of the bean that contains the data
      propertyName - the name of the property whose html representation will be provided
      context - an active web engine context
      route - a route that leads to a rife.cmf.elements.ServeContent element
      Returns:
      the html content representation
      Throws:
      ContentManagerException - if an unexpected error occurred
      Since:
      1.0
    • clone

      public Object clone()
      Simply clones the instance with the default clone method. This creates a shallow copy of all fields and the clone will in fact just be another reference to the same underlying data. The independence of each cloned instance is consciously not respected since they rely on resources that can't be cloned.
      Overrides:
      clone in class Object
      Since:
      1.0
    • createNewManager

      public <OtherBeanType> GenericQueryManager<OtherBeanType> createNewManager(Class<OtherBeanType> type)
      Description copied from interface: GenericQueryManager
      Create a new generic query manager of the same kind but for another bean class.
      Specified by:
      createNewManager in interface GenericQueryManager<T>
      Overrides:
      createNewManager in class GenericQueryManagerDelegate<T>
      Parameters:
      type - the class of the bean for which the new generic query manager has to be created
      Returns:
      a new generic query manager instance