This tutorial explores Evaluations and introduces Evaluators, demonstrating how they are defined and used.
This tutorial assumes you have created a new SDK All-In-One project.
This tutorial creates a module that conditionally renders the Site Members component on the site dashboard. If the site has a certain name the Site Members component will not be rendered.
-
Open the file
aio/aio-share-jar/src/main/resources/alfresco/web-extension/site-data/extensions/aio-share-jar-example-widgets.xml
and add the following module definition:
<extension> <modules> ... <module> <id>Module (Conditionally Hide Site Members)</id> <components> <component> <region-id>component-1-1</region-id> <source-id>site/{site}/dashboard</source-id> <scope>page</scope> <sub-components> <sub-component id="default"> <evaluations> <evaluation id="HideIfTestSite"> <evaluators> <evaluator type="beans.evaluator" ></evaluator> </evaluators> <render>false</render> </evaluation> </evaluations> </sub-component> </sub-components> </component> </components> </module> </modules> </extension>
Site dashboards are similar to user dashboards in that a new configuration is created from a preset for each new site. This means you need to target your Component using the site parameter.
Note: The Evaluator type attribute must map to a Spring bean ID defined in the application context. Therefore, you need to create this Evaluator and define it as a Spring bean. - In the aio/aio-share-jar/src/main/java/org/alfresco/tutorial package create a new class called SiteMembersEvaluator.
- In the Package Explorer double-click the file SiteMembersEvaluator.java to load it into the Eclipse editor (if not already showing).
-
Replace the code currently in SiteMembersEvaluator.java, with the
following content:
package org.alfresco.tutorial; import java.util.Map; import org.springframework.extensions.surf.RequestContext; import org.springframework.extensions.surf.extensibility.impl.DefaultSubComponentEvaluator; publicclassSiteMembersEvaluatorextendsDefaultSubComponentEvaluator { publicbooleanevaluate(RequestContext context, Map<String, String> params) { boolean result; String site = context.getUriTokens().get( "site" ); if (site == null ) { site = context.getParameter( "site" ); } result = (site != null && site.equals( "test-site" )); return result; } }
Note: At this point your code will be showing compile errors. You will need to link in the required Spring Framework libraries.The Spring bean compares the site name with the hard-coded string “test-site” (the site URL). The returned result will be true if the site URL matches the hard-coded string, causing the evaluator to be true, thus preventing rendering of the Site Members component.
-
Open the
aio/aio-share-jar/src/main/resources/alfresco/web-extension/aio-share-jar-slingshot-application-context.xml
file and add the Spring Bean configuration as follows:
... <beans> <bean id="beans.evaluator" class="org.alfresco.tutorial.SiteMembersEvaluator" /> </beans>
This configuration file maps the Java class to the Spring Bean ID.
-
Build the project with maven. For example:
/all-in-one$ ./run.sh build_start
- Log in to Share and create two new sites: Test Site and Sample Site. As the new module has not yet been deployed, you will see the Site Members on both sites.
- Now go to the Module Deployment WebScript http://localhost:8080/share/page/modules/deploy.
- Deploy the new module, Module (Conditionally Hide Site Members), as described in previous tutorials. When you refresh the dashboard pages for the Test Site and Sample Site sites, you will see that the Site Members component is displayed for Sample Site, but not for the Test Site.