How to add a dynamic facet via nuxeo-java-client?
Hi there.
I'm trying to create a document with an optional (dynamic) Facet, but it's not working. I'm using java-client 1.0 with Nuxeo 7.10 HF-24. My code is as follows:
Document doc = new Document(name, "MyType");
doc.set("dc:title", "Doc Title");
doc.setFacets(Arrays.asList("MyFacet"));
doc.set("myfacet:attr1", "xpto");
doc = nuxeo.repository().createDocumentByPath(parentPath, doc);
The document is created, but the facet and its attributes are not there. How to accomplish that using Nuxeo Java Client? I don't want that Facet to be mandatory in MyType, since it is not applicable for all document instances.
Thanks in advance. Daniel Viero.
You can also use automation chain to perform this operation atomically.
For example the following chain creates a document, adds a facet and updates properties.
`
<chain id="create-doc-attach-facet">
<param type="string" name="docType" />
<param type="string" name="name" />
<param type="properties" name="properties" />
<param type="string" name="facet" />
<!-- This operation needs parent as input, we could use DocRef in java client to provide its parent -->
<operation id="Document.Create">
<param type="string" name="type">expr:ChainParameters['docType']</param>
<param type="string" name="name">expr:ChainParameters['name']</param>
</operation>
<!-- Add facet -->
<operation id="Document.AddFacet">
<param type="string" name="facet">expr:ChainParameters['facet']</param>
</operation>
<operation id="Document.Update">
<param type="properties" name="properties">expr:ChainParameters['properties']</param>
</operation>
</chain>
`
And then you use it with nuxeo java client:
` public void itCanAnswer() {
Document doc = nuxeoClient.automation("create-doc-attach-facet")
.param("docType", "File")
.param("name", "file-test")
.param("properties",
Collections.singletonMap("dc:description", "description for test"))
.param("facet", "NotFulltextIndexable")
.input(new DocRef(("/")))
.execute();
assertNotNull(doc);
assertTrue(doc.getFacets().contains("NotFulltextIndexable"));
assertEquals("description for test", doc.getPropertyValue("dc:description"));
} `
If something went wrong during the chain execution, the transaction is rollback.
Hi Daniel,
There's no way to add dynamically a facet on a document through REST in 7.10.
The method Document#setFacets
is misleading and should only be used for internal client usage, like specific marshallers.
You can easily create your own operation to do that, you can also copy AddFacet operation and deploy it to your Nuxeo Server.
Regards