Field with multi-possible objects

Hi,

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 ?

Regards,
Gent

Hello, @halili.gent

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:

A part of screen descriptor:

<fieldGroup id="myFieldGroup"
            datasource="stationDs">
    <column width="250px">
        <field property="name"/>
        <field id="point" custom="true"/>
    </column>
</fieldGroup>

A part of screen controller:

// 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.

Regards,
Daniil.

Hi @tsarev,
Thanks for your reply!

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?

Regards,
Gent

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.

If you need an example, I’ll prepare it.

Regards,
Daniil.

1 Like

Thanks @tsarev !
That would be great if you can give an example.

Regards,
Gent

Hi @tsarev,

Do you have any example, or could you give me more details on how I can do this?

Regards,
Gent

I found https://github.com/cuba-platform/sample-entity-inheritance which is very similar to what I’m looking for.

Hello. Yes, this example is similar to your case.

Do you still need an example for exactly your case?

I think the example is enough, will try to use the same logic on my example.
Thanks for you’re help!

@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?

First of all, I suggest that you use both persistent Address and Point. It will allow to avoid duplications and decrease complexity.

1st to select the type and the 2nd to select the value

You can use the MANY_TO_MANY relation for this purpose. To choose countries and regions I suggest that you use the TokenList component: documentation.

Regards,
Daniil.

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 ?

To simplify it :

Based on the example https://github.com/cuba-platform/sample-entity-inheritance, what I’m looking for is to be able to make industry ( https://github.com/cuba-platform/sample-entity-inheritance/blob/master/modules/global/src/com/company/entityinheritance/entity/Company.java#L35) as Costumer (https://github.com/cuba-platform/sample-entity-inheritance/blob/master/modules/global/src/com/company/entityinheritance/entity/Customer.java) by creating an Industry Entity as Non-Persistent…
So in Order costumer can be : Company, Person or Industry
What I don’t want to do is to create a table Industry into the database since the Industry is managed by Company.
Maybe @gorelov can help ?