cancel
Showing results for 
Search instead for 
Did you mean: 

Cannot find NodeService instance trying to reproduce Alfresco Wiki examples

ardamose123
Champ on-the-rise
Champ on-the-rise
I was following the instructions in "Custom Actions" (http://wiki.alfresco.com/wiki/Custom_Actions). However, when I get to the
executeImpl(…)‍
method, Java cannot find the
this.nodeService‍
object.


public void executeImpl(Action action, NodeRef actionedUponNodeRef)
{
   if (this.nodeService.exists(actionedUponNodeRef) == true) // <– nodeService undefined here
   {
      QName aspectQName = (QName)action.getParameterValue(PARAM_ASPECT_NAME);
      this.nodeService.addAspect(actionedUponNodeRef, aspectQName, null); // <– nodeService undefined here
   }
}
‍‍‍‍‍‍‍‍‍‍


I reviewed the source code of
ActionExecuterAbstractBase‍
class, and found the
baseNodeService‍
field to be
private‍
, not
public‍
or even
protected‍
. It only has one setter and no getter. Since this class is part of Alfresco's core source code, I'd prefer not to mess up with it.

I'm currently using Alfresco 4.2.c SDK. Also, I'm working on a copy of the "SDK Basic AMP" project in the
samples‍
folder of said SDK. Project dependencies: JRE 6 and "SDK Alfresco Embedded" project.

Is there a way to make the code work without changing
ActionExecuterAbstractBase‍
code? Is there a workaround?

Thanks in advance!
2 REPLIES 2

scouil
Star Contributor
Star Contributor
Hi Ardamose,

Actually you'll need a bit of spring background to understand it.
Your class, to be picked up by Alfresco, will need to be declared in a config file via a been. It will be something like:
<bean id="add-aspect" class="org.alfresco.repo.action.executer.AddAspectActionExecuter" parent="action-executer"></bean>‍


With this bean, you can inject other beans that will be available in your class. In our case: the nodeService

<bean id="add-aspect" class="org.alfresco.repo.action.executer.AddAspectActionExecuter" parent="action-executer">
    <property name="nodeService">
        <ref bean="nodeService" />
    </property>
</bean>
‍‍‍‍‍‍‍


And for spring to be able to inject it, you'll need the appropriate setter in jour java code.
So you'll want to add this at the begining of your java class:

private NodeService nodeService;
public void setNodeService(NodeService nodeService)
    {
        this.nodeService = nodeService;
    }
‍‍‍‍‍‍‍


Then you should be able to access your
this.nodeService‍

Just as you pointed out, I was missing the Spring beans configuration. Many thanks!