【问题标题】:How to create new content in Alfresco from ServiceTask using Java?如何使用 Java 从 ServiceTask 在 Alfresco 中创建新内容?
【发布时间】:2019-08-13 10:53:36
【问题描述】:

工作流程完成后,如何在 Alfresco 存储库中创建 xml 文件?

我创建了一个 ServiceTask,但从那里我只能访问 DelegateExecutionProcessEngine 和各种服务,但不能访问 NodeService.

问候,

迈克

编辑。 很难相信对于像从正在进行的工作流创建新文件这样的基本事情没有简单的解决方案。 (在这种情况下,REST API 似乎要好很多)

首先,需要在bpmn文件中创建ServiceTask

<serviceTask id="myServiceTask" activiti:class="com.example.myClass"></serviceTask>

其次,在myClass中:

Map<Object, Object> registeredBeans = Context.getProcessEngineConfiguration().getBeans();
ServiceRegistry registry = (ServiceRegistry)registeredBeans.get(ActivitiConstants.SERVICE_REGISTRY_BEAN_KEY);
NodeService nodeService = registry.getNodeService();


StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
ResultSet rs = registry.getSearchService().query(storeRef, SearchService.LANGUAGE_LUCENE, "PATH:\"/app:company_home/app:shared\"");
NodeRef companyHomeNodeRef = null;
try
{
  if (rs.length() == 0)
  {
      throw new AlfrescoRuntimeException("Bad Lucene Search!");
  }
  companyHomeNodeRef = rs.getNodeRef(0);

    // Create a map to contain the values of the properties of the node  
    Map<QName, Serializable> props = new HashMap<QName, Serializable>(1);
    props.put(ContentModel.PROP_NAME, "NewFile.txt"); 
    // use the node service to create a new node
    NodeRef node = nodeService.createNode(
                        companyHomeNodeRef, 
                        ContentModel.ASSOC_CONTAINS, 
                        QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "TEST"),
                        ContentModel.TYPE_CONTENT, 
                        props).getChildRef();

    // Use the content service to set the content onto the newly created node
    ContentWriter writer = registry.getContentService().getWriter(node, ContentModel.PROP_CONTENT, true);
    writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
    writer.setEncoding("UTF-8");
    writer.putContent("Message inside new content");
}
finally
{
  rs.close();
}

我必须获得 NodeSerivce,然后使用 Lucene 搜索 SharedFolder/Userhome(不应该在某处声明公共文件夹的“路径”吗?)最后使用 ContentWriter em>,我可以将内容放到他的 Alfresco Repository 中。

【问题讨论】:

    标签: alfresco activiti


    【解决方案1】:

    我相信您使用的是 Alfresco Activiti Engine 而不是 APS。 如果您使用的是 Alfresco Activiti,则在 module-context.xml 文件中注入 nodeService bean 并将其用作委托类。

    示例 module-context.xml 文件。

    <bean id="delegate" 
         parent="baseJavaDelegate" 
         class="com.example.mydelegate">
         <property name="nodeService" ref="NodeService"/>
    </bean>
    

    下面是示例 java 委托。

    public class mydelegate extends BaseJavaDelegate
    {
        private NodeService nodeService;
    
        public void setNodeService(NodeService nodeService)
        {
            this.nodeService = nodeService;
        }  
    //rest of the code below 
     }
    

    现在您在 delete 中获得了 nodeService,您应该可以使用它来创建 xml 文件。

    这样你也可以注入其他bean。

    希望对你有所帮助。

    【讨论】:

    • 我试过了,但是当我调试我的自定义类时,nodeService 为空(并且 Alfresco 崩溃)。我什至尝试注入 'ref="nodeService"'(小写),但效果不佳。我正在从 bpmn20 文件中调用我的课程:“"
    • 如何在 module-context.xml 文件中注入 com.example.Text 类?
    • 我复制粘贴了您的代码。只需将“类”属性更改为“com.example.Text”。但我认为这是 Activiti 的问题。也许我应该以某种方式使用委托表达式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多