4

Inside Tridion 2013-SP1 with DD4T template 2.0.8

I have a Schema with one Field: Foobar of Type Component Link

  • Allow Multiple Values
  • Allowed Schemas {Foo, Bar}

Within Java DD4T 2.0.4

created a ViewModel

@ViewModel(viewModelNames = {"foobar"}, rootElementNames = {"foobarSchema"})    
public class Foobar extends TridionViewModelBase {
  @ViewModelProperty
  private List<Component> foobar;
  // Getters & Setters
}

@ViewModel(viewModelNames = {"foo"}, rootElementNames = {"fooSchema"})    
public class Foo extends TridionViewModelBase {
  @ViewModelProperty
  private String foo;
  // Getters & Setters
}

@ViewModel(viewModelNames = {"bar"}, rootElementNames = {"barSchema"})    
public class Bar extends TridionViewModelBase {
  @ViewModelProperty
  private double bar;
  // Getters & Setters
}

What i tried is to make a interface that both classes {Foo, Bar} uses and used that interface inside Foobar List<Interface>. This resulted into the same effect the above example. Containing only ComponentImp.

Creating a abstract class that extended the TridionViewModelBase and both Foo and Bar extended the abstract class resulted into serialize exception.

@ViewModel(viewModelNames = {"foobar"}, rootElementNames = {"foobarSchema"})    
public class Foobar extends TridionViewModelBase {
  @ViewModelProperty
  private List<FooBarItem> foobar;
  // Getters & Setters
}

public abstract class FooBarItem extends TridionViewModelBase {
  @ViewModelProperty
  private String sharedFooBar;
  // Getters & Setters
}

@ViewModel(viewModelNames = {"foo"}, rootElementNames = {"fooSchema"})    
public class Foo extends FooBarItem {
  @ViewModelProperty
  private String foo;
  // Getters & Setters
}

@ViewModel(viewModelNames = {"bar"}, rootElementNames = {"barSchema"})    
public class Bar extends FooBarItem {
  @ViewModelProperty
  private double bar;
  // Getters & Setters
}

My question is how to make the list strongly typte with only the two Schemas/Classes Foo and Bar?

To make rendering inside JSP more straight forward.

4
  • Would this question and answer help: tridion.stackexchange.com/questions/12859/… ?
    – Raimond
    Commented Jun 1, 2016 at 14:03
  • @Raimond I updated the question: with the example of the List containing both viewModels (Inside Tridion both schemas)
    – TheMiXeD
    Commented Jun 2, 2016 at 6:25
  • 1
    Right. Let me think about a solution - it's a tricky one!
    – Raimond
    Commented Jun 2, 2016 at 9:25
  • @Raimond where you able to find a solution. At the moment we avoid using this kind of situation. We still would like this option available.
    – TheMiXeD
    Commented Jul 29, 2016 at 7:16

2 Answers 2

1

Change:

@ViewModel(viewModelNames = {"foobar"}, rootElementNames = {"foobarSchema"})    
public class Foobar extends TridionViewModelBase {
  @ViewModelProperty
  private List<Component> foobar;
  // Getters & Setters
}

To:

@ViewModel(viewModelNames = {"foobar"}, rootElementNames = {"foobarSchema"})    
public class Foobar extends TridionViewModelBase {
      @ViewModelProperty(tridionFieldType = FieldType.COMPONENTLINK)
      private List<Component> foobar;
      // Getters & Setters
}

All available field types can be found in the FieldType enum in package org.dd4t.contentmodel

The available parameters on the ViewModelProperty can be found in the ViewModelProperty interface in package org.dd4t.databind.annotations

2
  • I followed this page: ViewModel. It tells I won't have to do that. There is content inside it's not strongly typte. Causing the problem I have to do all kind of tricks inside JSP to render it out again.
    – TheMiXeD
    Commented Jun 1, 2016 at 13:50
  • Updated the question to provide more insight on what I try to do within Java
    – TheMiXeD
    Commented Jun 2, 2016 at 6:25
0

What you want to do ff I understand correctly is having a List of type Interface (FooBarItem), for which the concrete type can either be Foo or Bar. This concrete type then should be mapped in the list if it is present in the Json.

Currently this is not possible in the DD4T databind framework, as it works from the datastructure it gets. Based on the datamodel it tries to find view models, which it instantiates and then maps the data from the datastructure to each field. This means that cannot work for either interfaces or abstract classes.

A poor man's solution would be to simply have a List and a List property in your parent model.

In the mean time, this is added as a feature request for the next dd4t version.

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.