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.

1 votes

2 answers

774 views

ANSWER

Isn't the nuxeo-java-client compatible only with Nuxeo LTS 2016 and upwards ? the automation operation only exists since 8.3
05/31/2017

nuxeo-java-client 1.x is compatible with LTS 2015.
05/31/2017



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.

1 votes



That looks great! I was trying to avoid server-side development, but this is a pretty generic solution. I suggest it to be incorporated as a native operation in Nuxeo, since it looks like a basic functionality to me. Thank you very much.
06/02/2017


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

1 votes



Thanks for the confirmation, Kevin. We've already copied the AddFacet operation, but I really expected the full document creation to be available as an atomic operation in the client API. If I have to do 3 or 4 call to fully create one document, and something fails in the last step (a dynamic facet's attribute validation, for instance), I will have to run undo/compensation operations manually, unless I do server-side programming. It doesn't sound OK for me, since my applications are just clients. I hope Nuxeo client API evolve. Thanks anyway.
06/01/2017