Passing string parameter to action in folder rules

cancel
Showing results for 
Search instead for 
Did you mean: 
mmrvelj
Active Member

Passing string parameter to action in folder rules

Jump to solution

Hi all. 

I have created a Java class that extends ActionExecuterAbstractBase and exposes some properties (String values) through setters (in this example setApprovedText) that can be set. 

Then I registered it in a service-context.xml like this example:

<bean id="approve-document-action"
class="com.alfresco.ApproveDocument"
parent="action-executer">
<property name="serviceRegistry" ref="ServiceRegistry"/>
<property name="nodeService" ref="NodeService" />
<property name="publicAction">
<value>true</value>
</property>
<property name="approvedText" value="${digitalsign.approvedtext}" />
</bean>

Then I can register this value in alfresco-global.properties

digitalsign.approvedtext=APPROVED 

This all works fine, but in some cases is would be much better if I could provide this value as an action rule parameter.

Basically when I create rule on a folder, and when I choose action to perform, I would like to have field where I could provide this parameter value. Is this possible, and if yes - how can it be done?

 

1 Solution

Accepted Solutions
abhinavmishra14
Advanced

Re: Passing string parameter to action in folder rules

Jump to solution

Not exatcly sure how you have setup. Mostly likely your 'messages' (e.g. action-messages.properties) which has the labels defined, are not getting loaded by the time action class bean is initialized and 'addParameterDefinitions' method is inovked. By default bean scope is singleton, hence spring container initializes them at application startup time, so it depends how you have defined beans in the context file.

Try these steps, i am updating the method provided earlier:

@Override
protected void addParameterDefinitions(final List<ParameterDefinition> paramDefinitions) {
	paramDefinitions.add(new ParameterDefinitionImpl(PARAM_COMMENT, DataTypeDefinition.TEXT,
			true, getParamDisplayLabel(PARAM_COMMENT))); 
}

When getParamDisplayLabel method is called, it will try to load the label using I18NUtil, see the method impl below:

protected String getParamDisplayLabel(String paramName) {
//<actionName>.<paramName>.display-label
//e.g. update-comments.comment.display-label return I18NUtil.getMessage(this.name + "." + paramName + "." + DISPLAY_LABEL); }

Add the labels as necessary, example:

#This is used to show param label
update-comments.comment.display-label=A Comment
#This is used to show Action tile/name in the actions list in rule configuration update-comments.title=Update Comments

Now most importantly, make sure that message resource bundle bean is initialized in first place then all actions etc., See the details here

e.g.

<bean id="com.demo.resourceBundle" class="org.alfresco.i18n.ResourceBundleBootstrapComponent">
  <property name="resourceBundles">
     <list>
        <value>alfresco/module/${project.artifactId}/messages/action-messages</value>
     </list>
  </property>
</bean>

<bean id="update-comments" class="com.demo.action.UpdateCommentsActionDemo" parent="action-executer">
   <property name="publicAction">
     <value>true</value>
   </property>
   <constructor-arg ref="ServiceRegistry" />
</bean>

 

 

~Abhinav
(ACSCE, AWS SAA, Azure Admin)

View solution in original post

6 Replies
abhinavmishra14
Advanced

Re: Passing string parameter to action in folder rules

Jump to solution

You can do that by implementing 'org.alfresco.repo.action.ParameterizedItemAbstractBase.addParameterDefinitions(List<ParameterDefinition>)' method 

ActionExecuterAbstractBase extends another abstract class : org.alfresco.repo.action.ParameterizedItemAbstractBase

ParameterizedItemAbstractBase provides an abstract method for providing option to add params to the action.

Here is an example of action class that can get a parameter.

public class UpdateCommentsActionDemo extends ActionExecuterAbstractBase {

	private static final String PARAM_COMMENT = "comment";
	
	private transient final NodeService nodeService;

	public UpdateCommentsAction(final ServiceRegistry serviceRegistry) {
		super();
		nodeService = serviceRegistry.getNodeService();
	}

	@Override
	protected void executeImpl(final Action action, final NodeRef actionedUponNodeRef) {
		if (nodeService.exists(actionedUponNodeRef)) {
			final String commentValue = (String) action.getParameterValue(PARAM_COMMENT);
			final Map<QName, Serializable> properties = nodeService.getProperties(actionedUponNodeRef);
			if (StringUtils.isNotBlank(commentValue)) {
				properties.put(DemoContentModel.CommentableAspect.Prop.COMMENTS, commentValue);
			}

			if (nodeService.hasAspect(actionedUponNodeRef,
					DemoContentModel.CommentableAspect.ASPECT)) {
				nodeService.setProperties(actionedUponNodeRef, properties);
			} else {
				nodeService.addAspect(actionedUponNodeRef,
						DemoContentModel.CommentableAspect.ASPECT, properties);
			}
		}
	}
	
	@Override
	protected void addParameterDefinitions(final List<ParameterDefinition> paramDefinitions) {
		paramDefinitions.add(new ParameterDefinitionImpl(PARAM_COMMENT, DataTypeDefinition.TEXT,
				true, PARAM_COMMENT)); 
	}
}

After implementing addParameterDefinition method, you can remove the property value which you are getting from global properties, and setup the rule to pass the value from there. 

I would recommed to go through this doc as well : https://docs.alfresco.com/6.0/references/dev-extension-points-actions.html

~Abhinav
(ACSCE, AWS SAA, Azure Admin)
mmrvelj
Active Member

Re: Passing string parameter to action in folder rules

Jump to solution

Thank you , this works great!

However, I am still struglling with the last part - label of the field. Basically I cannot get the proper text to be displayed. 

I have tried to put in .properties file:

comment=My comment
approve-document-action.comment=My comment
approve-document-action.comment.display-label=My comment
approve-document-action.comment.title=My comment

However no matter what I tried - I never get "My comment", but instead, I only get  "comment" for the label.

I also noticed in many examples that registration of parameter looks like this:

paramDefinitions.add(new ParameterDefinitionImpl(PARAM_COMMENT, DataTypeDefinition.TEXT,
				true, getParamDisplayLabel(PARAM_COMMENT))); 

when I use getParamDisplayLabel(PARAM_COMMENT), I don't get anything. also I notice that this function is supposed to return something like bean-name.param-name.display-label, but when I print it out, it returns null.

What am I missing?

abhinavmishra14
Advanced

Re: Passing string parameter to action in folder rules

Jump to solution

Not exatcly sure how you have setup. Mostly likely your 'messages' (e.g. action-messages.properties) which has the labels defined, are not getting loaded by the time action class bean is initialized and 'addParameterDefinitions' method is inovked. By default bean scope is singleton, hence spring container initializes them at application startup time, so it depends how you have defined beans in the context file.

Try these steps, i am updating the method provided earlier:

@Override
protected void addParameterDefinitions(final List<ParameterDefinition> paramDefinitions) {
	paramDefinitions.add(new ParameterDefinitionImpl(PARAM_COMMENT, DataTypeDefinition.TEXT,
			true, getParamDisplayLabel(PARAM_COMMENT))); 
}

When getParamDisplayLabel method is called, it will try to load the label using I18NUtil, see the method impl below:

protected String getParamDisplayLabel(String paramName) {
//<actionName>.<paramName>.display-label
//e.g. update-comments.comment.display-label return I18NUtil.getMessage(this.name + "." + paramName + "." + DISPLAY_LABEL); }

Add the labels as necessary, example:

#This is used to show param label
update-comments.comment.display-label=A Comment
#This is used to show Action tile/name in the actions list in rule configuration update-comments.title=Update Comments

Now most importantly, make sure that message resource bundle bean is initialized in first place then all actions etc., See the details here

e.g.

<bean id="com.demo.resourceBundle" class="org.alfresco.i18n.ResourceBundleBootstrapComponent">
  <property name="resourceBundles">
     <list>
        <value>alfresco/module/${project.artifactId}/messages/action-messages</value>
     </list>
  </property>
</bean>

<bean id="update-comments" class="com.demo.action.UpdateCommentsActionDemo" parent="action-executer">
   <property name="publicAction">
     <value>true</value>
   </property>
   <constructor-arg ref="ServiceRegistry" />
</bean>

 

 

~Abhinav
(ACSCE, AWS SAA, Azure Admin)
mmrvelj
Active Member

Re: Passing string parameter to action in folder rules

Jump to solution

This works perfectly. I was registering the beans before resources were loaded. It was sufficient to reorder xml file to have the .properties file load before, just as you explained.

Thank you!

EddieMay
Alfresco Employee

Re: Passing string parameter to action in folder rules

Jump to solution

Hi @mmrvelj 

Great that it's now working - thanks for the update, helpful to other users.

Cheers,

Digital Community Manager, Alfresco Software.
Problem solved? Click Accept as Solution!
abhinavmishra14
Advanced

Re: Passing string parameter to action in folder rules

Jump to solution

Good to hear that it worked for you. Happy coding.

~Abhinav
(ACSCE, AWS SAA, Azure Admin)