mercredi 27 janvier 2016

Filter deep list fields using metadata

Situation

In my application I have a bunch of classes and some of them have translated versions.

Example:

There is a class News having a list of NewsTranslation. NewsTranslations' fields are language and title.

I also have an Event and EventTranslation (with language field) and News has a field of Event type.

Classes(fields) summary:

  • News(Event, NewsTranslations) implements Multilingual
  • Event(EventTranslations) implements Miltilingual
  • NewsTranslation(language, title) implements Translation
  • EventTranslation(language, titie) implements Translation

What i want to do is

create a method that will take News and language String as arguments and filter the News object deeply so that every translation list will be filtered for language.

I need a generic solution that will work for other scenarios by traversing the object deeply and filter all Translation list.

What i tried/thought of

I know how to do it the easy way, for example using reflection to recursively search all fields for appropriate type, but i don't think it's the optimal one.

I was thinking about using annotation on class to specify paths to translations, and then get the values with reflections. Example:

@TranslationPaths({
@TranslationPath("newsTranslations"),
@TranslationPath("event.eventTranslations")})
public class News {
...
}

But maybe you can suggest a better solution? Maybe a library that does that kind of stuff?





Aucun commentaire:

Enregistrer un commentaire