четверг, 3 сентября 2015 г.

Custom root scope object (JS API service) in Alfresco

First of all you need to know that all JS objects that we can use in webscripts or rules are based on Java. It means that when we call, for example, siteService.getSite("shortSiteName") actually performed method getSite of org.alfresco.repo.site.script.ScriptSiteService java class.

Alfresco JS API is very comfortable to use, it allows to make custom webscripts or rules really quick and integrate it to the server without restarting, but there is a problem, it doesn't cover all methods that are allowed in the Java. For example you can't disable behaviors from JS API.

To make own service that will be allowed in JS API we need to create Java class that inherits BaseScopableProcessorExtension with some public methods. For exmple:

import org.alfresco.repo.jscript.BaseScopableProcessorExtension;
import org.alfresco.repo.policy.BehaviourFilter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;

/**
 * Created by s.palyukh
 */
public class RootScopeObject extends BaseScopableProcessorExtension {

    private NamespaceService namespaceService;
    private BehaviourFilter behaviourFilter;


    public void disableBehaviour(NodeRef nodeRef, String className) {

        behaviourFilter.disableBehaviour(nodeRef, stringToQName(className));
    }

    public void enableBehaviour(NodeRef nodeRef, String className) {

        behaviourFilter.enableBehaviour(nodeRef, stringToQName(className));
    }

    public QName stringToQName(String prop) {
        return QName.resolveToQName(namespaceService, prop);
    }

    public void setNamespaceService(NamespaceService namespaceService) {
        this.namespaceService = namespaceService;
    }

    public void setBehaviourFilter(BehaviourFilter behaviourFilter) {
        this.behaviourFilter = behaviourFilter;
    }
}


And then you need to add new bean in Spring context that will have parent="baseJavaScriptExtension".

    


        
        
        




After that RootScopeObject will be allowed to use in JS with name that you have set for "extensionName" property of spring bean. For example:

customService.disableBehaviour(userhome, "cm:content");

Комментариев нет :

Отправить комментарий