cancel
Showing results for 
Search instead for 
Did you mean: 

Content update problem: url is unmodified

ale_carraro
Champ in-the-making
Champ in-the-making
Hi to everyone, I've a strange problem.

First I introduce briefly what I'm trying to do:
I'm trying to create a tool so that a workflow could write its history into its package documents (open offcice). To do this, i have a 'javascript' action (that is, a BaseProcessorExtension mapped into a javascript variable so that I could invoke that operation by a custom action or by a 'normal' org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript.

The action is called in the right manner, an I'm able to use a sort of template filler (greatly based on jooreports) with the following syntax (inside the wf):

if(bpm_package.children[i].name.endsWith(".odt")){
   template = {};
   template.name = person.properties.firstName;
   template.surname = person.properties.lastName;
   template.date = new Date().toString();
   ooTemplateWriter.transform(bpm_package.children[i], bpm_package.children[i], template);
}

The document produced is correct. However from the user interface the document is unmodified in its content (the mod date is updated).

Debugging I finally found the problem: the contentUrl part of property cm:content is updated (in the sense that DbNodeServiceImpl.setProperty(NodeRef nodeRef, QName qname, Serializable value) is normally called and returns without errors, but in the node browser it is not. The correct url points to a .bin file that is the correct openOffice file, but the node browser points instead to the old url, so when downloading I get the template document.


The code I'm using is similar to the one inside the OpenOffice translator:

       ContentReader reader = contentService.getReader(source.getNodeRef(), ContentModel.PROP_CONTENT);
       
        // create temporary files to convert from and to
        File tempFromFile = TempFileProvider.createTempFile("OpenOfficeContentFiller-source-", ".odt");
        File tempToFile = TempFileProvider.createTempFile("OpenOfficeContentFiller-target-", ".odt");
       
        // download the content from the source reader
        reader.getContent(tempFromFile);

        InputStream is = new FileInputStream(tempFromFile);
       
        ZippedDocumentTemplate zippedDocumentTemplate = new ZippedDocumentTemplate(is);

        OutputStream os = new FileOutputStream(tempToFile);
       
        zippedDocumentTemplate.createDocument(m, os);

        is.close();
       
        ContentWriter writer = contentService.getWriter(destinationNode.getNodeRef(), ContentModel.PROP_CONTENT, true);

        // upload the temp output to the writer given us
        writer.putContent(tempToFile);

Note that I'm sure that everything besides the last line is absolutely correct.

An interesting thing might be the following: Copying the first snippet in a js file and put that in the Scrips directory, replacing every "bpm_package.children" with "document" works perfectly.
What could it be?
2 REPLIES 2

ale_carraro
Champ in-the-making
Champ in-the-making
I found something interesting. If I save the document (using the custom action: script) I have the same behavior of the workflow.

Looking into the ScriptNode class, I also found something interesting: in method setContent(String) the operations are apparently the same as I followed, but at the end of the method we have:

writer.putContent(content.getInputStream());
// update cached variables after putContent()
this.contentData = (ContentData) services.getNodeService().getProperty(nodeRef, this.property);

Is it possible that I write the 'good' url into the Node but not on the ScriptNode using the writer.putContent (since I see it is explicitly set in ScriptNode), and then at the end of the Workflow task we have a sort of autosave on the ScriptNode that is overwriting the contentUrl with the old one?

ale_carraro
Champ in-the-making
Champ in-the-making
I found something interesting. If I save the document (using the custom action: script) I have the same behavior of the workflow.

Looking into the ScriptNode class, I also found something interesting: in method setContent(String) the operations are apparently the same as I followed, but at the end of the method we have:

writer.putContent(content.getInputStream());
// update cached variables after putContent()
this.contentData = (ContentData) services.getNodeService().getProperty(nodeRef, this.property);

Is it possible that I write the 'good' url into the Node but not on the ScriptNode using the writer.putContent (since I see it is explicitly set in ScriptNode), and then at the end of the Workflow task we have a sort of autosave on the ScriptNode that is overwriting the contentUrl with the old one?

Ok, the analysis was right: the ScripNode didn't update automatically the contentUrl property, so doing manually worked. Here is the complete code:
        File tempFromFile = TempFileProvider.createTempFile("OpenOfficeContentFiller-source-", ".odt");
        File tempToFile = TempFileProvider.createTempFile("OpenOfficeContentFiller-target-", ".odt");
       
        // download the content from the source reader
        reader.getContent(tempFromFile);

        InputStream is = new FileInputStream(tempFromFile);
       
        ZippedDocumentTemplate zippedDocumentTemplate = new ZippedDocumentTemplate(is);

        OutputStream os = new FileOutputStream(tempToFile);
       
        zippedDocumentTemplate.createDocument(m, os);

        os.close();
        InputStream is2 = new FileInputStream(tempToFile);
       
        ScriptContentData contentData = (ScriptContentData)destinationNode.getProperties().get(ContentModel.PROP_CONTENT);
        contentData.write(is2);
notice the last 2 lines