AmetysObjectFactory


AmetysObjectFactory

Repository plugin declares an extension point for registering AmetysObjectFactory instances.

An AmetysObjectFactory is responsible for managing AmetysObject of a given node type or retrieving them from a given id scheme.

By default, the Repository plugin comes only with the RootAmetysObjectFactory which exposes the ametys root node (ametys root node real path is /jcr:root/ametys:root), has the node type ametys:root and is a TraversableAmetysObject in order to create children.
But, for creating a child below this root TraversableAmetysObject you need to give the node type to use and therefore you need to register at least one AmetysObjectFactory.

By registering your own AmetysObjectFactory each level of the Ametys repository tree can exposed different type of entities.
The CMS and the Web products already registered several AmetysObjectFactory for business AmetysObject like Content, Page, Sitemap, Tag, etc, ...

Registration

AmetysObjectFactory registration is done like this in a plugin.xml:

  ...
  <!-- Factory for managing MyItem ametys object -->
  <extension point="org.ametys.plugins.repository.AmetysObjectFactoryExtensionPoint"
             id="org.ametys.example.MyItemFactory"
             class="org.ametys.example.MyItemFactory">
       <scheme>item</scheme>
       <nodetype>feed:item</nodetype>
       <nodetype-definition>nodetypes/feed_nodetypes.xml</nodetype-definition>
  </extension>
  ...

class must be Java class which implements org.ametys.plugins.repository.AmetysObjectFactory interface.
scheme element is mandatory and must be unique in the whole application.
nodetype element is the node type to use for creating MyItem instances and retrieve the right factory later.
nodetype-definition element is optional and contains the path to the node types definition file relative to the plugin directory or from the plugin package in embedded mode (inside a jar).

Default implementations of AmetysObject and AmetysObjectFactory are provided:

  • SimpleAmetysObject for a JCRAmetysObject (MetadataAwareAmetysObject with JCR node access).
  • DefaultAmetysObject for a SimpleAmetysObject which is a VersionableAmetysObject and a LockableAmetysObject.
  • DefaultTraversableAmetysObject for a DefaultAmetysObject which is a TraversableAmetysObject.
  • AmetysObjectCollection for storing children using a hash in order to prevent Jackrabbit 10k subnodes limitations.

If you need a custom type of AmetysObject you are advised to extend existing factories corresponding to the previous AmetysObject.
For specific needs you can always create your own from scratch.

The following code can be used to declare the implementations corresponding to the previous declaration:

/**
 * DefaultAmetysObjectFactory for creating MyItem.
 */
public class MyItemFactory extends DefaultAmetysObjectFactory
{
    @Override
    public MyItem getAmetysObject(Node node, String parentPath) throws AmetysRepositoryException
    {
        return new MyItem(node, parentPath, this);
    }
}

/**
 * MyItem is a DefaultAmetysObject and therefore a:
 * <ul>
 *  <li>MetadataAwareAmetysObject
 *  <li>VersionableAmetysObject
 *  <li>LockableAmetysObject
 * </ul>
 */
public class MyItem extends DefaultAmetysObject<MyItemFactory>
{
    /**
     * Creates a {@link MyItem}.
     * @param node the node backing this {@link AmetysObject}.
     * @param parentPath the parent path in the Ametys hierarchy.
     * @param factory the {@link MyItemFactory} which creates the AmetysObject.
     */
    public MyItem(Node node, String parentPath, MyItemFactory factory)
    {
        super(node, parentPath, factory);
    }
}
Back to top