cancel
Showing results for 
Search instead for 
Did you mean: 

How to add a dynamic facet via nuxeo-java-client?

Daniel_Viero
Champ on-the-rise
Champ on-the-rise

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 ACCEPTED ANSWER

Kevin_Leturc
Star Contributor
Star Contributor

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.

<component> <extension target="org.nuxeo.ecm.core.operation.OperationServiceComponent" point="chains"> <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> </extension> </component>

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.

View answer in original post

6 REPLIES 6

pibou_Bouvret
Elite Collaborator
Elite Collaborator

Isn't the nuxeo-java-client compatible only with Nuxeo LTS 2016 and upwards ?

Daniel_Viero
Champ on-the-rise
Champ on-the-rise

nuxeo-java-client 1.x is compatible with LTS 2015.

Kevin_Leturc
Star Contributor
Star Contributor

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

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.

Kevin_Leturc
Star Contributor
Star Contributor

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.

<component> <extension target="org.nuxeo.ecm.core.operation.OperationServiceComponent" point="chains"> <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> </extension> </component>

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.

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.