AnsweredAssumed Answered

Feature request: TemplateNode Factory & ScriptNodeFactory

Question asked by ajmas on Apr 29, 2011
Latest reply on Feb 15, 2012 by jpotts
We have developed an Alfresco module in-house that adds extra functionality. For certain reasons we have needed to add extra functionality to the ScriptNode and the TemplateNode, but in doing so we have had to over-ride the definition of these classes, since the current architecture does not take into account a module needing to extend the functionality of theses classes.

If these two classes were generated via a factory, then it would allow us to easily extend these classes through inheritance, rather than redefinition. Using the TemplateNode as an example, I see something like this:

<bean id="templateNodeFactory"
          class="org.alfresco.repo.template.TemplateNodeFactory"
          p:serviceRegistry-ref="serviceRegistry" />

public class TemplateNodeFactory
{

    private ServiceRegistry services;
    private TemplateImageResolver resolver;
   
    @Required
    public void setServiceRegistry( ServiceRegistry services )
    {
        this.services = services;
    }
       
    public void setTemplateImageResolver( TemplateImageResolver resolver )
    {
        this.resolver = resolver;
    }
   
    /**
     * Creates a new TemplateNode for the nodeRef
     *
     * @param nodeRef
     * @return
     */
    public TemplateNode createTemplateNode( NodeRef nodeRef )
    {
        return createTemplateNode(nodeRef,this.services,this.resolver);
    }
   
    /**
     * Creates a new TemplateNode for the nodeRef
     * 
     * @param nodeRef
     * @param services
     * @param resolver
     * @return
     */
    protected TemplateNode createTemplateNode(
                          NodeRef nodeRef,
                          ServiceRegistry services,
                         TemplateImageResolver resolver )
    {
        return new CoreTemplateNode(nodeRef, services, resolver);
    }
   
}
The factory class could then be extended to return the appropriate implementation. There are probably other ways of going about this, but this would certainly be helpful.

Edit: The same factory implementation could be reused, simply using reflection and specifying the class to be instantiated as a property:


    /* templateNodeClass node would be specified as a bean property, p:templateNodeClass="package.MyTemplateNode" */
    protected TemplateNode createTemplateNode( NodeRef nodeRef, ServiceRegistry services, TemplateImageResolver resolver )
    {
        try {
            Constructor<TemplateNode> constructor = templateNodeClass.getConstructor(NodeRef.class,ServiceRegistry.class,TemplateImageResolver.class);
            return constructor.newInstance(nodeRef,services,resolver);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

Outcomes