Creating a custom evaluator - Alfresco Content Services - 23.4 - 23.4 - Ready - Alfresco - external

Alfresco Content Services

Platform
Alfresco
Product
Alfresco Content Services
Release
23.4
License

This tutorial demonstrates how to create a custom evaluator. You will create a custom evaluator class, wire it into Alfresco as a Spring bean, and learn how to use evaluator properties.

This tutorial assumes you have completed previous tutorials. This tutorial assumes you will be using the admin account. For testing convenience it will be useful if you create another user account before starting this tutorial.

In the previous tutorial you saw that the provided evaluators can be applied to a module. Different modules can have different evaluators applied to them. As well as using the provided evaluators, it is possible to create your own custom evaluator. This tutorial shows how to create a simple custom evaluator.

  1. In the aio/aio-share-jar/src/main/java/org/alfresco/tutorial package create a new class called SimpleModuleEvaluator.
  2. Add the following code to the class:
    package org.alfresco.tutorial;
        
    import java.util.Map;
        
    import org.springframework.extensions.surf.RequestContext;
    import org.springframework.extensions.surf.extensibility.ExtensionModuleEvaluator;
        
    publicclassSimpleModuleEvaluatorimplementsExtensionModuleEvaluator
    {
        publicstaticfinal String USER_PROP = "user";
        
        publicbooleanapplyModule(RequestContext context, Map<String, String> evaluationProperties)
        {
            String currUser = context.getUser().getId();
            String targetUser = evaluationProperties.get(USER_PROP);
            return (targetUser != null && targetUser.equals(currUser));
        }
        
        public String[] getRequiredProperties()
        {
            returnnew String[] { USER_PROP};
        }
    }
    
  3. Open the aio/aio-share-jar/src/main/resources/alfresco/web-extension/aio-share-jar-slingshot-application-context.xml file.
  4. Add a bean for your new evaluator:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beansPUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
    <beans>
    	<bean id="beans.evaluator" class="beans.SiteMembersEvaluator" />
    	**<bean id="beans.simple.module.evaluator" class="org.alfresco.tutorial.SimpleModuleEvaluator" />**
    </beans>
    
  5. Rebuild your project using the Ant build script as done in previous tutorials.
  6. Restart your application server.
  7. In a browser tab navigate to http://localhost:8080/share/page/modules/deploy.
  8. Click on a deployed module, to display the list of evaluators.
  9. From the list of evaluators select the evaluator, beans.simple.module.evaluator.

    You will see the evaluator property “user”.

    Note: If you do not see the properties then try the following: close the Module Deployment tab. Create a new tab. Reload the Module Deployment page.
  10. Set the user evaluator property to admin.
  11. Click Update.
  12. Click Apply Changes.
  13. In another tab navigate to the admin dashboard.

    You will see that the module has been applied.

  14. Log out from the admin account and log into another account.

    On the new user account dashboard you will see that the module is not applied. This is because the module was applied conditionally to the user; the module is only applied for the admin user, as was set by using the evaluator property.