2

In ViewModel class I am not able to fetch the field values; the field has multiple keywords as its value (say keyword1 and keyword2). Below is the snapshot of the

enter image description here

In ViewModel class I tried to use variable type as KeywordField as suggested in http://blog.trivident.com/viewmodel-functionality-in-dd4t-2-0-for-java-part-ii-creating-viewmodels/ but it gives below Exception:

java.lang.IllegalArgumentException: Can not set org.dd4t.contentmodel.impl.KeywordField field com.web.cms.model.PackageBundle.bonusItem to org.dd4t.contentmodel.impl.KeywordImpl
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
    at java.lang.reflect.Field.set(Field.java:764)
    at org.dd4t.databind.builder.AbstractModelConverter.setFieldValueOnField(AbstractModelConverter.java:94)
    at org.dd4t.databind.builder.AbstractModelConverter.setFieldValue(AbstractModelConverter.java:72)
    at org.dd4t.databind.builder.json.JsonModelConverter.deserializeGeneric(JsonModelConverter.java:296)
    at org.dd4t.databind.builder.json.JsonModelConverter.buildField(JsonModelConverter.java:208)
    at org.dd4t.databind.builder.json.JsonModelConverter.buildModelProperties(JsonModelConverter.java:116)
    at org.dd4t.databind.builder.json.JsonModelConverter.convertSource(JsonModelConverter.java:95)
    at org.dd4t.databind.builder.json.JsonDataBinder.buildModel(JsonDataBinder.java:175)
3
  • Which version of the CM Template Building Blocks are you using? Are you publishing out XML ?
    – Raimond
    Commented Mar 4, 2016 at 10:58
  • Hi Raimond; CM TBB version is DD4T 2.0.1 beta and we are publishing as JSON. Its just converted to XML format for better readability/debug here. Commented Mar 4, 2016 at 19:33
  • OK. Thanks. I will investigate shortly. Can you also tell me how the field is defined in the ViewModel ?
    – Raimond
    Commented Mar 5, 2016 at 10:39

1 Answer 1

2

First of all, your Keyword Json structure should look something like this:

"Color" : {
    "Name" : "Color",
    "Value" : "Blue",
    "Values" : [ "Blue", "Red", "White" ],
    "NumericValues" : [ ],
    "DateTimeValues" : [ ],
    "LinkedComponentValues" : [ ],
    "FieldType" : 3,
    "CategoryName" : "Colors",
    "CategoryId" : "tcm:7-1020-512",
    "XPath" : "tcm:Metadata/custom:Metadata/custom:Color",
    "KeywordValues" : [ {
      "Description" : "",
      "Key" : "",
      "TaxonomyId" : "tcm:7-1020-512",
      "Path" : "\\Colors\\Blue",
      "ParentKeywords" : [ ],
      "MetadataFields" : { },
      "Id" : "tcm:7-2116-1024",
      "Title" : "Blue"
    }, {
      "Description" : "",
      "Key" : "",
      "TaxonomyId" : "tcm:7-1020-512",
      "Path" : "\\Colors\\Red",
      "ParentKeywords" : [ ],
      "MetadataFields" : { },
      "Id" : "tcm:7-2114-1024",
      "Title" : "Red"
    }, {
      "Description" : "",
      "Key" : "",
      "TaxonomyId" : "tcm:7-1020-512",
      "Path" : "\\Colors\\White",
      "ParentKeywords" : [ ],
      "MetadataFields" : { },
      "Id" : "tcm:7-2115-1024",
      "Title" : "White"
    } ]

If this is not OK, then you need to check your field type in the Schema and check with which version of the DD4T TBBs you are rendering content.

If that is OK, then the next step is to define the property correctly in your view model. Since you are adding multiple Keywords, the field is multi-value, which means you must enclose the individual keywords in a List:

@ViewModelProperty(entityFieldName = "Color")
private List<Keyword> color;

public List<Keyword> getColor () {
    return color;
}

public void setColor (final List<Keyword> color) {
    this.color = color;
}

If you debug this, then the actual values come out as as List of KeywordImpls:

Keyword List

Subsequently, you can then use the view model in your view as follows:

<c:if test="${!empty event.color}">
    <c:forEach items="${event.color}" var="colorKeyword">
        <c:out value="${colorKeyword.title}" />
    </c:forEach>
</c:if>
2
  • Hi Raimond; The JSON structure is same as you mentioned but each keyword entry is displayed twice under "KeywordValues" section of JSON and thus in the List<Keywords> of ModelView Class it will give a list with duplicate entry for each keyword value. To overcome this issue I was looking a way to fetch keywords from "Values" section of JSON because that does not have duplicate entries. This seems to be a bug in DD4T 2.0.1 what is the simplest way to fix this bug. Commented Mar 5, 2016 at 12:06
  • Can you update your POM to use version 2.0.2 and see what happens ?
    – Raimond
    Commented Mar 5, 2016 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.