I have a situation where I need to define a parameter which actually might be of different objects.
Let say I have a main object :
class MyStation{
private Object point;
}
But point param. could be an other foreign key(an object I have created) or simply a String.
From the UI point of view, I want that user could easly select the object, ex: if foreign key then a lookup field, otherwise a simple field to type the string value.
Do you have any suggestion how can I do it in the most efficient way ?
If you are using the FieldGroup component for the MyStation entity the recommended way is to use the custom field mechanism. The main idea is to mark your field as custom and generate it in screen controller. Here is an example:
// create a component depends on type of the "point" field
TextField pointFieldComponent = componentsFactory.createComponent(TextField.class);
// setup the component
FieldConfig pointField = fieldGroup.getFieldNN("point");
pointField.setComponent(pointFieldComponent);
You can find an example of this mechanism in the createGroupField method of the UserEditor class.
Documentation for custom fields in FieldGroup: link.
If your are using separate field just generate your field, setup it and add to layout.
From the UI I’m getting it, could you suggest me with any example form the Java OOP or Database side?
Should I create an abstract class end extend objects for all my parameters types?
So for the main entity :
there is a parameter named “point”
this parameter can be of different types that the user can select from drop down menu
types can be a foreign key of an other entity (lookup drop down menu), any other object, string or int
only one value type can be stored(so it’s an OR) into the database
It’s similar to the car example :
if you select one brand then the second dropdown will allow you to select the sub-brand options, the only difference here is that, if from the lookup dropdown menu there is no option, then the user can free text the value manually of the id.
…at the end in the database I want only to store 2 columns corresponding to my “point” param.
2 columns : pointType, pointValue
typePoint: corresponds to my different objects, defined in an enum pointValue : is the object value of type as defined in typePoint (EntityA, EntityB, EntityC, EntityD or EntityE)
Just want to avoid to have many columns in my database, since I use only one of 5 total…
What would be the best solution ? Do you have any example about this?
Okay, so in this case I suggest that you create an entity Point and use this entity as a type of Station field. Then you should create extensions of the Point entity: StringPoint, IntPoint, etc.
And because of this custom relation you will have to write your own custom logic that will load, process and store these entities.
@tsarev, I have a question :
I realised that my example is slightly different that the cuba’s example. I will need to use also a Non Persistent entity which is crated from an other Persistent entity.
Let say I have Full Address Entity, data is stored on the database.
In my other entity Station : I need to have a startPoint which is based on selected type, it could be an Address (entity) or a country (string) which is a distinct query of country field.
I want to create something like this because I dont have only one Station but many, and I want to avoid repetitive creation of Point object, since I can have startPoint and endPoint, also sendingPoint… all of Point type.
The Point object basically is made of 2 different lookups, 1st to select the type and the 2nd to select the value( 1st: Country 2: List of distinct countries taken from Address entity ; 1st Region 2: List of distinct regions taken from Address entity)…
Could you please give me any hit how I can do it in the best and easiest way?
The problem is that Address entity usually has 100K rows, and 5-K to 10K per country and more for region. So if the user wants to include all country adresses, imagine the user need to select 10K address, doesn’t work on this case!
That’s why I want to let the user select the country/region… and store in the database as-is (country name for non-persistent entity and UUID as string for persistent entity), it’s fine since it’s master data. (I can create later a query to flat data down on low level then publish it into the REST API)
In this case I think I can not avoid the complex solution : 1st select the type then select the value…
Do you know how I can build the logic of Point view ( with LookupFields ) and inherit it on other UI screens ?