如何在 Alfresco 4.2.2 中为 Share 编写自定义评估程序?

How to write a custom evaluator for Share in Alfresco 4.2.2?

例如,我需要评估器,我可以用它来显示分享中的操作。如果当前用户是文档的作者,那么我会显示操作。

项目结构如下所示 (alf221-action-share):

C:\EclipseWorkspaces\alfresco\alf221-action-share>tree /F
...
├───build
│   ├───classes
│   │   └───com
│   │       └───firm
│   │           └───alfresco
│   │               └───alf221
│   │                   └───evaluators
│   │                           UsersCompareEvaluator.class
│   │
│   ├───dist
│   └───lib
│           alf221-action-share.jar
│
└───src
    └───main
        ├───com
        │   └───firm
        │       └───alfresco
        │           └───alf221
        │               └───evaluators
        │                       UsersCompareEvaluator.java
        │
        └───resources
            ├───alfresco
            │   └───web-extension
            │       │   alf221-action-share-slingshot-application-context.x
            │       │
            │       └───messages
            │               alf221-action-share.properties
            │
            └───META-INF
                │   share-config-custom.xml
                │
                └───resources
                    └───components
                        └───documentlibrary
                            └───actions
                                    firm-exclude-participant-16.png

UsersCompareEvaluator class 中,我只是想确定当前用户:

package com.firm.alfresco.alf221.evaluators;

import org.alfresco.web.ui.common.component.evaluator.BaseEvaluator;
import org.springframework.extensions.surf.RequestContext;
import org.springframework.extensions.surf.support.ThreadLocalRequestContext;
import org.springframework.extensions.webscripts.connector.User;


public class UsersCompareEvaluator extends BaseEvaluator {

    @Override
    public boolean evaluate() {
        RequestContext requestContext = ThreadLocalRequestContext.getRequestContext();
        User user = requestContext.getUser();

        System.out.println("CURRENT USER IS ... " + user.getName());

        return true;
    }
}

我的alf221-action-share-slingshot-application-context.xml:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 
'http://www.springframework.org/dtd/spring-beans.dtd'>
<beans>

   <bean id="com.firm.alf221-action-share.resources" 
       class="org.springframework.extensions.surf.util.ResourceBundleBootstrapComponent">
      <property name="resourceBundles">
         <list>
            <value>alfresco.web-extension.messages.alf221-action-share</value>
         </list>
      </property>
   </bean>

    <bean id="evaluator.doclib.action.isDocumentOnApproval" 
          parent="evaluator.doclib.metadata.value">
        <property name="accessor" value="node.properties.idocs:documentStatus" />
        <property name="comparator">
            <bean class="org.alfresco.web.evaluator.StringEqualsComparator">
                <property name="value" value="onApproval" />
            </bean>
        </property>
    </bean>

    <bean id="evaluator.doclib.action.isCurrentUserAuthor" 
          class="com.firm.alfresco.alf221.evaluators.UsersCompareEvaluator" />

</beans>

我的share-config-custom.xml:

<alfresco-config>

    <config evaluator="string-compare" condition="DocLibActions">
        <actions>
            <action id="firm-exclude-participant" 
                    type="javascript" 
                    label="actions.firm.exclude-participant" 
                    icon="document-change-type">

                <param name="function">onActionTkbFormDialog</param>
                <permissions>
                    <permission allow="true">Write</permission>
                </permissions>
                <param name="itemKind">action</param>
                <param name="itemId">exclude-participant</param>
                <param name="mode">create</param>
                <param name="successMessage">message.exclude-participant.success</param>
                <param name="failureMessage">message.exclude-participant.failure</param>
                <evaluator>evaluator.doclib.action.isDocumentOnApproval</evaluator>
                <evaluator>evaluator.doclib.action.isCurrentUserAuthor</evaluator>
            </action>
        </actions>

        <actionGroups>
            <actionGroup id="document-details">
                <action index="500" id="firm-exclude-participant" />
            </actionGroup>
        </actionGroups>

    </config>

</alfresco-config>

我放置在以下路径中的 jar 文件:/opt/tomcat7/webapps/share/WEB-INF/lib

一切正常,除了我的自定义评估器。在此配置中,共享不起作用...在日志中我看到以下内容:

[aleksey@alfresco01 tomcat7]$ sudo vi share.log
...
Caused by: java.lang.NoClassDefFoundError: org/alfresco/web/ui/common/component/evaluator/BaseEvaluator
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2928)
        at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1174)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1669)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
        at org.springframework.util.ClassUtils.forName(ClassUtils.java:257)
        at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:408)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1271)
        at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1242)
        ... 22 more
Caused by: java.lang.ClassNotFoundException: org.alfresco.web.ui.common.component.evaluator.BaseEvaluator
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
        ... 33 more

如果在评估器中我实现了ActionEvaliator接口,并尝试如下识别当前用户...

public class UsersCompareEvaluator implements ActionEvaluator  {

    private Map<Object, Object> registeredBeans = 
        Context.getProcessEngineConfiguration().getBeans();
    private ServiceRegistry registry = 
        (ServiceRegistry)registeredBeans.get(ActivitiConstants.SERVICE_REGISTRY_BEAN_KEY);
    private AuthenticationService authenticationService = 
        registry.getAuthenticationService();  

    @Override
    public boolean evaluate(Node node) {
        System.out.println("CURRENT USER IS ... " + 
            authenticationService.getCurrentUserName());

        return true;
    }

    @Override
    public boolean evaluate(Object object) {
        if(object instanceof Node) {
            return evaluate((Node)object);
        } 

        return false;
    }
}

...并将我的 jar 文件放在同一个地方 (/opt/tomcat7/webapps/share/WEB-INF/lib) - 然后我得到以下异常:

...
Caused by: java.lang.ClassNotFoundException: org.alfresco.web.action.ActionEvaluator
...

我尝试将我的自定义评估程序打包到单独的 jar 中并将其放在此处:/opt/tomcat7/webapps/alfresco/WEB-INF/lib。但是如何从分享中引用这个评价器呢?

比如我有一个单独的项目(alf221-custom-evaluator):

C:\EclipseWorkspaces\alfresco\alf221-custom-evaluator>tree /F
...
└───src
    └───main
        ├───com
        │   └───firm
        │       └───alfresco
        │           └───alf221
        │               └───evaluators
        │                       UsersCompareEvaluator.java
        │
        └───resources
            └───alfresco
                └───web-extension
                        alf221-custom-evaluator-slingshot-application-context.xml

alf221-custom-evaluator-slingshot-application-context.xml 中,我指定以下内容:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 
'http://www.springframework.org/dtd/spring-beans.dtd'>
<beans>
    <bean id="evaluator.doclib.action.runtime.user" 
          class="com.firm.alfresco.alf221.evaluators.UsersCompareEvaluator" />
</beans>                                    

然后,在 alf221-action-share 中的 share-config-custom.xml 中,我指定以下内容:

...
<evaluator>evaluator.doclib.action.runtime.user</evaluator>
...

在这种情况下,我收到以下警告消息:

...
2018-01-03 12:10:59,956  WARN  [web.scripts.ActionEvaluatorHelper] [http-bio-8888-exec-8] Evaluator 'evaluator.doclib.action.runtime.user' not found.
...

而且我的自定义评估器不起作用。

如何在 Alfresco 4.2.2 中为 Share 编写自定义计算器?

如果能提供信息,我将不胜感激。感谢大家。

看来我找到了解决方案...问题是我使用了 Alfresco 库中的 BaseEvaluator class。同名 class 出现在共享库中。

即有两个 jar 文件:

alfresco-share-4.2.2.5.jar#uzip/org/alfresco/web/evaluator 我找到了我需要的一切。

此解决方案现在有效:

package com.firm.alfresco.alf221.evaluators;

// was import org.alfresco.web.ui.common.component.evaluator.BaseEvaluator;
import org.alfresco.web.evaluator.BaseEvaluator;
import org.json.simple.JSONObject;
import org.springframework.extensions.surf.RequestContext;
import org.springframework.extensions.surf.support.ThreadLocalRequestContext;
import org.springframework.extensions.webscripts.connector.User;


public class UsersCompareEvaluator extends BaseEvaluator  {

    @Override
    public boolean evaluate(JSONObject jSONObject) {
        RequestContext requestContext = ThreadLocalRequestContext.getRequestContext();
        User user = requestContext.getUser();

        System.out.println("CURRENT USER IS ... " + user.getName());

        return true;
    }
}

现在我可以在 catalina.out 中看到:

CURRENT USER IS ... admin

所有其他必要的信息都可以从JSONObject中获取(当前用户也可以使用继承自BaseEvaluatorclass的getUserId()方法获取)。