How to programatically populate Complex Multivalued Fields ?

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
0 votes

1 answers

6257 views

ANSWER



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.

1 votes



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 :D …..Thanksss
11/30/2012

Great news, you are welcome! Could you accept my answer as the selected answer?
12/02/2012