Wednesday, December 4, 2013

Alfresco share custom evaluator to check node associations

In Share, we have evaluator based on that templates, action, indicator can be displayed.
We also can have our own custom evaluator . 

I see - many has requirement to show/hide action based on content/folder associations and which is NOT available OOB.
So, In this example I will show you how you can implement it.

We will implement custom evaluator which checks if content has "cm:references" association then show OOB action - "Delete Document". You can have your custom action or any other action.

First we will start with Share side changes.

Step1:  Define custom evaluator

Create file - custom-documentlibrary-context.xml and put it to \tomcat\shared\classes\alfresco\web-extension

Add below code

Step 2: Create custom evaluator class.

This class calls alfresco repository webscript which accepts content noderef and association name and
checks if content has given association or not and accordingly return true or false.

Step 3:  Add new custom evaluator for delete document action in share-config-custom.xml
So now we are done with Share side changes.
We need to create alfresco repository webscript which checks if node has given association or not. This would be simple javascript webscript

Step1: Create desc file . node-association.get.desc.xml.
Put in to extension folder: \tomcat\shared\classes\alfresco\extension\templates\webscripts\custom\example\node\association
Step 2: Create node-association.get.js

Step 3: Create node-association.get.json.ftl

Deploy your custom code and restart server. Verify Delete action for content with and without cm:reference association.
I have not considered multiple association, you can change it same way.

Hope its helpful!
 

1 comment:

  1. hello,

    great article - I want exactly what you described. Unfortunately, I get stucked:

    I tried to deploy your code to an amp file (I followd the Tutorial here: http://ecmarchitect.com/alfresco-developer-series-tutorials/content/tutorial/tutorial.html#creating-a-custom-content-model), but if I make the change for the Alfresco Repo path and try to execute mvn install (CMD or Eclipse), I got the following error:

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project related-documents: Compilation failure: Compilation failure:
    [ERROR] /C:/Users/User/WBS Training/Kurse/z_Alfresco/projects/related-documents/src/main/java/org/alfresco/association/HasAssociationEvaluator.java:[6,34] package org.alfresco.web.evaluator does not exist
    [ERROR] /C:/Users/User/WBS Training/Kurse/z_Alfresco/projects/related-documents/src/main/java/org/alfresco/association/HasAssociationEvaluator.java:[9,43] package org.springframework.extensions.surf does not exist
    [ERROR] /C:/Users/User/WBS Training/Kurse/z_Alfresco/projects/related-documents/src/main/java/org/alfresco/association/HasAssociationEvaluator.java:[10,43] package org.springframework.extensions.surf does not exist
    [ERROR] /C:/Users/User/WBS Training/Kurse/z_Alfresco/projects/related-documents/src/main/java/org/alfresco/association/HasAssociationEvaluator.java:[12,51] package org.springframework.extensions.surf.support does not exist
    [ERROR] /C:/Users/User/WBS Training/Kurse/z_Alfresco/projects/related-documents/src/main/java/org/alfresco/association/HasAssociationEvaluator.java:[23,46] cannot find symbol
    [ERROR] symbol: class BaseEvaluator
    [ERROR] /C:/Users/User/WBS Training/Kurse/z_Alfresco/projects/related-documents/src/main/java/org/alfresco/association/HasAssociationEvaluator.java:[28,1] method does not override or implement a method from a supertype
    [ERROR] /C:/Users/User/WBS Training/Kurse/z_Alfresco/projects/related-documents/src/main/java/org/alfresco/association/HasAssociationEvaluator.java:[68,7] cannot find symbol
    [ERROR] symbol: class RequestContext
    [ERROR] location: class com.custom.web.evaluator.HasAssociationEvaluator
    [ERROR] /C:/Users/User/WBS Training/Kurse/z_Alfresco/projects/related-documents/src/main/java/org/alfresco/association/HasAssociationEvaluator.java:[68,27] cannot find symbol
    [ERROR] symbol: variable ThreadLocalRequestContext
    [ERROR] location: class com.custom.web.evaluator.HasAssociationEvaluator
    [ERROR] /C:/Users/User/WBS Training/Kurse/z_Alfresco/projects/related-documents/src/main/java/org/alfresco/association/HasAssociationEvaluator.java:[71,103] cannot find symbol
    [ERROR] symbol: variable ServletUtil
    [ERROR] location: class com.custom.web.evaluator.HasAssociationEvaluator

    I'm not experienced in java programming. So mayby you have a hint so that I can solve my problem?

    thanks in advance,
    georgie

    PS: Environment win7, maven 3.2, eclipse luna, alfresco 4.2e

    ReplyDelete