cancel
Showing results for 
Search instead for 
Did you mean: 

How to programatically populate Complex Multivalued Fields ?

Ansel_Andrew
Champ in-the-making
Champ in-the-making

I want to programatically populate Complex Multivalued Fields using Nuxeo Coresession object. How do we access the subfield in the below case?

Questions Question1 Answer1

  Question2
  Answer2
1 ACCEPTED ANSWER

Aykut_Açikel
Champ on-the-rise
Champ on-the-rise

I hope this will answer your question. Let's say you have this in your schema (.xsd) :

<xs:element name="questions" type="nxs:questions" />

<xs:complexType name="question">
  <xs:sequence>
      <xs:element name="label" type="xs:string" />
      <xs:element name="answer" type="xs:string" />
  </xs:sequence>
</xs:complexType>

<xs:complexType name="questions">
  <xs:sequence>
      <xs:element name="item" type="nxs:questions" minOccurs="0" maxOccurs="unbounded" />
  </xs:sequence>
</xs:complexType>

In your code, you can do this :

DocumentModel dm = ...;

 // get the list of complex values
List<Map<String, String>> questions = (List<Map<String, String>>)dm.getProperty("yourschema", "questions");
// you could get the subfields like that 
String question1 = questions.get(0).get("label");
String answer1 = questions.get(0).get("answer");

// add a new question
Map<String, String> newQuestion = new HashMap<String, String>();
newQuestion.put("label", "what is your question?");
newQuestion.put("answer", "my answer is that");
questions.add(newQuestion);

// update your document
dm.setProperty("yourschema", "questions", questions); // added

But as you can see, it's kinda boring to manipulate list of maps.. So you should also use the Nuxeo document adapters to encapsulate all this code in one simple method..

Let me know if you need more info on adapters.

View answer in original post

3 REPLIES 3

Aykut_Açikel
Champ on-the-rise
Champ on-the-rise

I hope this will answer your question. Let's say you have this in your schema (.xsd) :

<xs:element name="questions" type="nxs:questions" />

<xs:complexType name="question">
  <xs:sequence>
      <xs:element name="label" type="xs:string" />
      <xs:element name="answer" type="xs:string" />
  </xs:sequence>
</xs:complexType>

<xs:complexType name="questions">
  <xs:sequence>
      <xs:element name="item" type="nxs:questions" minOccurs="0" maxOccurs="unbounded" />
  </xs:sequence>
</xs:complexType>

In your code, you can do this :

DocumentModel dm = ...;

 // get the list of complex values
List<Map<String, String>> questions = (List<Map<String, String>>)dm.getProperty("yourschema", "questions");
// you could get the subfields like that 
String question1 = questions.get(0).get("label");
String answer1 = questions.get(0).get("answer");

// add a new question
Map<String, String> newQuestion = new HashMap<String, String>();
newQuestion.put("label", "what is your question?");
newQuestion.put("answer", "my answer is that");
questions.add(newQuestion);

// update your document
dm.setProperty("yourschema", "questions", questions); // added

But as you can see, it's kinda boring to manipulate list of maps.. So you should also use the Nuxeo document adapters to encapsulate all this code in one simple method..

Let me know if you need more info on adapters.

Thanks a lot, have been searching for a solution for quite a while now. I integrated your code without any major modifications and it worked perfectly

Great news, you are welcome!

Getting started

Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.