1

One of the ContentModelData returns content in the below format.

Code 1

content =  {question=Question 1?, answer={paraContent=RichTextData(fragments=[No. Answer 1 <strong>Strong text here</strong> Answer continues <br /> Answer para 2.])}}

question has value of type String, and answer is a list of paragraphs containing heading, list of paraContent. List of paracontent is actually a list of RichText. Now sometimes paraContent is simply a list of RichText fields but when we create RichText Data, RichTextData fragments are created. I have tried many methods.

Code 2

1. paraMultiLevel= (List<ParagraphMultiLevel>)
    content.getAndCast("answer", ParagraphMultiLevel.class);

2. paraMultiLevel2 = (List<ParagraphMultiLevel>)content.getElement("answer");

3. (List<ParagraphMultiLevel>)content.get("answer")

None of the above populates my entity viewModel. I am getting an exception

com.sdl.dxa.api.datamodel.model.ContentModelData cannot be cast to java.util.List

How can I fetch the Answer value and set it on a List <List<ParagraphMultiLevel> so I can access it in my view?

Update on 04/04/2018

When I originally tried to convertEntites(List<ComponentMetadata>) (see code 3), it returned null values so then I had to manually iterate over the list of ids and got the EntityModelData using DefaultModelService.loadEntity(). From the EntityModelData I get the ContentModelData (see Code 1). I want to set my customTeaser.content with ContentModelData.get("answer").

Code 3

try { dynamicList.setQueryResults(customContentProvider._convertEntities(components, dynamicList.getEntityType(), localization), query.isHasMore());
} catch (DxaException e) {
    throw new ContentProviderException("Cannot populate a dynamic list " + dynamicList.getId() + " localization " + localization.getId(), e);
}

returned [ContentTeaser(headline=null, introduction=null, content=null,...

Code 4

 @SemanticEntities({
     @SemanticEntity(entityName = "FAQs", vocabulary = SDL_CORE, prefix = "f")
 })

 public class ContentTeaser extends AbstractEntityModel {
     @JsonProperty("Headline")
     private String headline;

     @SemanticProperty("f:question")
     @JsonProperty("Introduction")
     private String introduction;

     @SemanticProperty("f:answer")
     @JsonProperty("Content")
     private List<ParagraphMultiLevel> content;             
 }

Code 5

@SemanticEntity(entityName = "ParagraphMultiLevel", vocabulary = SDL_CORE, prefix = "pml")
@Data
@EqualsAndHashCode(callSuper = true)
public class ParagraphMultiLevel extends AbstractEntityModel {      
    @SemanticProperty("pml:heading")
    @JsonProperty("Heading")
    private String heading;

    @SemanticProperty("pml:paraContent")
    @JsonProperty("ParaContent")
    private List<RichText> paraContent;

    @SemanticProperty("pml:subParagraph")
    @JsonProperty("SubParagraph")
    private List<SubParagraphLevel2> subParagraph;

    @SemanticProperty("pml:paraFootnote")
    @JsonProperty("ParaFootnote")
    private List<RichText> paraFootnote;
}
3
  • How did you define your View Model classes? And where are you trying to use this mapping logic? Direct access to the R2 Data Model (e.g. ContentModelData) is only expected in Model Builders, which is considered an advanced extension point which is normally not needed. Apr 4, 2018 at 5:57
  • Thanks @RickPannekoek for responding. I have updated my question to explain why I needed to access the R2 Data Model.
    – Rache
    Apr 4, 2018 at 6:53
  • @RickPannekoek please also take a look at another post which explains why we had to opt for this approach (directly accessing R2 data model). We were unable to populate entities/models using the standard DynamicList functionality. tridion.stackexchange.com/questions/18664/… Apr 4, 2018 at 7:05

1 Answer 1

1

It seems you are trying to mix the R2 Data Model (e.g. ContentModelData) with your Strongly Typed View Models (e.g. ParagraphMultiLevel). That doesn’t work.

The DXA Semantic Mapping logic is used to map the data in the R2 Data Model to Strongly Typed View Models, but these are distinct models; you won’t be able to simply cast one to the other.

See, for example: https://github.com/sdl/dxa-web-application-java/blob/master/dxa-framework/dxa-tridion-provider/src/main/java/com/sdl/dxa/tridion/mapping/EntityModelBuilder.java

Your Answer

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

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