We are currently building a java DD4T 2 implementation. We are creating viewmodels for our different schema's. One of the schemas has a componentlink field that can link to either a multimedia component or a regular component.
Based on the documentation for viewmodels, we created an viewmodel interface and then created two specific viewmodels.
The resulting code looks a bit to this (the classes contain a lot more off course, but removed all the extra code for clarity.
public class Article extends AbstractModelClass {
@ViewModelProperty
private MultiMediaItem articleImage; //this can either be a regular component or a MM component
}
public interface MultiMediaItem
{
// ommitted interface properties for clarity
}
@ViewModel(rootElementNames = { "undefined" })
public class ArticleImageMultimediaComponent extends AbstractModelClass implements MultiMediaItem
//this defines the viewmodel for the Multimedia component
{
@ViewModelProperty(entityFieldName = "FileName")
private String fileName;
}
@ViewModel(rootElementNames = { "articleImageComponent" })
//this defines the viewmodel for the regular component
public class ArticleImageRegularComponent extends AbstractModelClass implements MultiMediaItem
{
@ViewModelProperty(entityFieldName = "textFieldFilename")
private String fileName;
}
The problem i'm having is that, normally, we should use the root element names of the schemas to let the databinder detect which class to instantiate (either the ArticleImageMultimediaComponent or the ArticleImageRegularComponent).
The problem is that the multimedia component doesn't have a root element name, so initially, the value of articleImage was always null. When we added the rootelementname "undefined" to the multimedia viewmodel, it started to work, and the databinder could resolve the multimedia component and convert it to our viewmodel.
The question I have is : putting the 'undefined' rootelementname looks a bit like a hack or a workaround. I was wondering if this is the preferred approach, or if there is a better way to do this ?