Thursday, June 21, 2012

Type subcomponent evaluator ...

A new Alfresco 4.0.x feature is the customisation of Share by component replacement ... see

If you want to show/hide/change components based on types? Look no further. Here is an example that lets choose a type or all others not this type (inverted/negated).

The extension configuration holdes the reference to the Java-bean id and the parameters you define:

<evaluator type="share.evaluation.type" >

Bean Id should be some thing like (replace <java-package>):

<bean id="share.evaluation.type"  class="<java-package>.TypeSubComponentEvaluator"/> 

 The Share java code needs to resolve the parameters, find the current nodeRef and perform a CMIS call to the repository to retrieve the type info.

public class TypeSubComponentEvaluator extends DefaultSubComponentEvaluator {

    public boolean evaluate(RequestContext context, Map<String, String> arg1) {
        String requestedType = arg1.get("nodetype");
        boolean resultSuccess = arg1.get("negate") != null ? false : true;

        if (null == requestedType)
            return !resultSuccess;

        Map<String, String> uriTokens = context.getUriTokens();
        String nodeRef = uriTokens.get("nodeRef");
        if (nodeRef == null) {
            nodeRef = context.getParameter("nodeRef");

        try {

            final Connector conn = context
                    .getConnector("alfresco", context.getUserId(),

            final Response response ="/api/node/"
                    + nodeRef.replace(":/", ""));
            if (response.getStatus().getCode() == Status.STATUS_OK) {

                String type = parseReponse(response);

                if (requestedType.equals(type))
                    return resultSuccess;
            } else {
                return !resultSuccess;
        } catch (ConnectorServiceException cse) {
            return !resultSuccess;

        return !resultSuccess;

    private String parseReponse(Response response) {
        try {
            Document dom = DocumentBuilderFactory.newInstance()
            NodeList list = dom.getElementsByTagName("cmis:propertyId");
            int len = list.getLength();

            for (int i = 0; i < len; i++) {
                Element element = (Element) list.item(i);
                String propertyName = element
                String objectTypeId = null;
                if (propertyName.equals("cmis:objectTypeId")) {
                    objectTypeId = element.getElementsByTagName("cmis:value")
                    objectTypeId = objectTypeId.replaceAll("F:", "");
                if (objectTypeId == null) {
                return objectTypeId;
        } catch (Exception exc) {
        return null;

I use this functionality to hide all comments for types not of a certain type :)