cancel
Showing results for 
Search instead for 
Did you mean: 

nodeService.create( ) question

aweber1nj
Champ in-the-making
Champ in-the-making
Using an example found in the Alfresco docs, I am trying to create a new document-node using the nodeService.create method.  The problem is that despite reading the javadocs on the method, I don't truly understand what the middle parameters are denoting or being used for.

I am actually trying to "copy" the properties of an existing node, and put the copy of the node (with different content) into the same folder as the "original".  Thus, I get the original's type, and properties, then try to create the new document-node with these values (changing only the name property)…
<code lang="java">
docProps.put(ContentModel.PROP_NAME, name);
NodeRef childAtt = nodeService.createNode(parentFolderNodeRef,
           ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, name), type, docProps).getChildRef();
</code>

However, I get this exception:

org.alfresco.repo.domain.node.NodeExistsException: 00300011 Node already exists: (7397, workspace://SpacesStore/d1743f82-08b1-4f24-85ab-c949437239d8)
        at org.alfresco.repo.domain.node.AbstractNodeDAOImpl.newNodeImpl(AbstractNodeDAOImpl.java:1180)
        at org.alfresco.repo.domain.node.AbstractNodeDAOImpl.newNode(AbstractNodeDAOImpl.java:1066)
        at org.alfresco.repo.node.db.DbNodeServiceImpl.createNode(DbNodeServiceImpl.java:361)


I don't know if this is me creating that assocQName, or if there is some other property I should not be copying (or I should be replacing) from the original node.  Is there a reference to the QName ID's (i.e. 7397) that might tell me what Alfresco is complaining about being a duplicate?

Thanks in advance,
AJ
2 REPLIES 2

mrogers
Star Contributor
Star Contributor
Its probably easier if you use the FileFolderService. 

But if you want to use the node service there are two "names" that matter.   There's a name property on a node and the name of the association to the parent.   FileFolderService looks after this detail by making both values the same.

Its the name of the association that's causing you problems because there's a constraint to stop duplicate names.

aweber1nj
Champ in-the-making
Champ in-the-making
I looked at FileFolderService as an alternative, but since I really only wanted to copy the Properties, and set new content, and that FileFolderService returns a FileInfo object makes it add some additional steps…it seemed like it (FileFolderService.create or .copy) was trying to make things simpler, but in my case it would actually  make things more complex.  I appreciate the tip, though.

With further testing, I found that the actual/current issue was if you copy (all) the properties from one node to another, you necessarily copy all the sys-namespace properties, and many of those are, of course, unique and system-defined.  One would THINK that even if I supply these properties as part of the nodeService.create( ), it would ignore them and generate its own – for example, passing a DBID to any node-create method should be ignored IMHO, but simply filtering-out all sys-namespace properties in my code seems to be working now (more testing needed, for sure).

But you absolutely touch-upon another issue I have not thoroughly resolved, and would think would be provided by some Alfresco API – but I haven't found it:  Ensuring unique node-names within a folder (or within that part of the tree in general).  Is there any util/method provided to pass a folder nodeRef (parent) and a "proposed name", and have Alfresco either validate that the name is unique, or return a "possible, unique alternative" (hopefully based upon the "proposed name"?

Thanks very much for the quick reply!
-AJ