11-29-2012 05:18 PM
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
11-30-2012 12:28 PM
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.
11-30-2012 12:28 PM
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.
11-30-2012 03:59 PM
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
12-02-2012 09:09 AM
Great news, you are welcome!
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.