Jatin Shashoo February 2016

Implementing regional specific fields in Entities in Java project

In my project, there is a need to add regional specific fields in whichever entity required. These fields will be visible when the project is run in the particular region. I guess this could be done either by using profiles in pom.xml OR by using web.xml.

For example :

Employee has name, salary, age. There would be an additional field 'regionalAge' which would hold age in terms of Hijri calendar.

Considering that we enter these details manually on a jsp, I want to show the regional specific fields only when required.

I want to implement this by using Aspect Oriented Programming. The current implementation is not very developer-friendly. Any suggestions?

P.S. I am new to AOP and hope to learn the same via this example.

Q) How has this been presently implemented?

A) A new entity has been created say : RegionalData. This entity has a limited number of fields which are numbered like regionalField1, 2 and so on. This entity needs to be embedded into whichever entity that requires regional specific fields inside of it. Persisting the same has been handled.

There is an xml file say : regionalSpecificMapping.xml in which the 'jspName' in which I wish to use it in and the 'source entity name' are mapped. There also is a regional mapping table that does the same and maps name of the field to source entity name and to the regional Field number #1/2/3.

This results in creation a limited number of columns that are created in the table of the entity in which we embedded the RegionalData. In these columns, regional specific values are stored.

I am looking for another approach as the current one will at some point fail to include more fields and would have to include more fields again.

Answers


AlexR February 2016

The first and the simplest approach is having all regional fields in your class but making them nullable. For example Employee class will have "regionalAge" but it will be initialized only if it is relevant for current region. In you presentation layer (jsp) you should show only fields that are not null.

As a variant of this solution you can use Optional either from java 8 (if you are using java 8) or guava (if you still in previous version).

Second solution is to use base Employee class and extend it for specific regions. Employee is implemented and packaged into your core project. But you can implement several extensions that provide their own classes that extend Employee and add their fields. You can also define factory that creates entities and re-define this factory in each sub-project, so instantiation of your entities could be done using generic way.

Post Status

Asked in February 2016
Viewed 2,646 times
Voted 5
Answered 1 times

Search




Leave an answer