10

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 ?

5
  • 1
    It's a bit of a hack, but at the moment the only way of doing it. Might be good to raise an issue on Github so I can add a nicer mechanism.
    – Raimond
    Mar 1, 2017 at 12:59
  • ok, thanks for the feedback, will put something in github Mar 1, 2017 at 13:05
  • I guess that's the answer then @Raimond - but - what would be the approach if there were two different MM schema requiring binding - let's say one was for an Image and another specific to PDFs to hold explicit multimedia metadata? Mar 1, 2017 at 13:14
  • Do undefined1, undefined2 etc. Mar 1, 2017 at 13:40
  • 1
    The only way of determining that is by looking at the published content currently travelling through the databind mechanism. So - you have to be smart with your modeling. I think the real way to solve it is to add an identifier or indeed schema title in DD4T Templates to do this properly.
    – Raimond
    Mar 1, 2017 at 13:41

0

Your Answer

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

Browse other questions tagged or ask your own question.