Mike
Mike

Reputation: 383

How to create new content in Alfresco from ServiceTask using Java?

How can I create an xml file in Alfresco repository after the workflow has finished?

I created a ServiceTask but from there I have access only to DelegateExecution, ProcessEngine, and various services but NOT a NodeService.

Regards,

Mike

EDIT. It is hard to believe that there is not a simple solution for such an elementary thing like creating a new file from ongoing workflow. (in such a case, REST API seems to be a lot better)

Firstly, in bpmn file ServiceTask needs to be created:

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

Secondly, in 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();
}

I have to obtain NodeSerivce, then search for SharedFolder/Userhome using Lucene (shouldn't the "path" for common folders be declared somewhere?) and finally using a ContentWriter, I can put content to he Alfresco Repository.

Upvotes: 0

Views: 337

Answers (1)

I believe you're using Alfresco Activiti Engine and not the APS. If you're using Alfresco Activiti then Inject the nodeService bean in the module-context.xml file and use it the delegate class.

Sample module-context.xml file.

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

Below is the sample java delegate.

public class mydelegate extends BaseJavaDelegate
{
    private NodeService nodeService;

    public void setNodeService(NodeService nodeService)
    {
        this.nodeService = nodeService;
    }  
//rest of the code below 
 }

Now you got the nodeService inside your delete and you should be able to use it to create the xml file.

Like this you can inject other beans also.

Hope this helps you.

Upvotes: 2

Related Questions