cancel
Showing results for 
Search instead for 
Did you mean: 

Controlling Versioning with Java

alihammad
Champ in-the-making
Champ in-the-making
I create a node and then write something to it. If the versioning is enabled, that creates two version, once when node is created and another when something is written to that node.

Here is how i created the node

ChildAssociationRef association = nodeService.createNode(parentNode,
            ContentModel.ASSOC_CONTAINS, QName.createQName(
                  NamespaceService.CONTENT_MODEL_PREFIX, resourceName),
            ContentModel.TYPE_CONTENT, contentProps);


I have also enabled the versioning like this

nodeService.addAspect(nodeRef, ContentModel.ASPECT_TITLED, titledProps);

After creating the node and applying versioning I write something in the node  like this.

ContentWriter writer = contentService.getWriter(nodeRef,
            ContentModel.PROP_CONTENT, true);
      writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
      writer.setEncoding("UTF-8");

      BufferedWriter out = new BufferedWriter(new OutputStreamWriter(writer
            .getContentOutputStream()));
                out.write("I am writing some text in node")





But that ultimately creates two versions of the same node. The first node is created when "createNode" method is fired and it contains some ambiguous text like this

"The node's content is missing:
   node: versionStore://version2Store/70fea043-0d6c-431f-9c8a-919cabe35216
   reader: null 
Please contact your system administrator."


but the second version contains the text i wrote with buffered writer.

Is there a way that when I create the node, I could pass the content i want to write in the file, so that only one version is create.

thanks  a lot
4 REPLIES 4

alihammad
Champ in-the-making
Champ in-the-making
any feedback???

invictus9
Champ in-the-making
Champ in-the-making
Is there a reason that you don't create the node, write its contents, and then apply the versioning aspect?

By the way, in your example where you say "enable versioning like this", you add the cm:titled aspect instead of the cm:versionable aspect. Is there a reason for that?

dannyl999
Champ in-the-making
Champ in-the-making
I'm interested in this solution, as well.  The reason I can't create a node, write its contents, and then apply the versioning aspect is because in my content model (that I can't change), the versioning aspect is mandatory.

gamin
Champ in-the-making
Champ in-the-making
Create the content first, then call createNode with the cm:content property set to the content you've created.


ContentWriter writer = contentService.getWriter(null,
            ContentModel.PROP_CONTENT, true);
      writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
      writer.setEncoding("UTF-8");

      BufferedWriter out = new BufferedWriter(new OutputStreamWriter(writer
            .getContentOutputStream()));
                out.write("I am writing some text in node")

Add the following property to your contentProps map


contentProps.put( ContentModel.PROP_CONTENT, writer.getContentData());

Now call your createNode, and the initial version that is created by alfresco auto applying the versionable aspect will contain your content.


ChildAssociationRef association = nodeService.createNode(parentNode,
            ContentModel.ASSOC_CONTAINS, QName.createQName(
                  NamespaceService.CONTENT_MODEL_PREFIX, resourceName),
            ContentModel.TYPE_CONTENT, contentProps);