How to create datasource programmatically

Hi everyone,
I have a similar problem. I have a One-to-One relation between a main entity (say Person) connect with an optional second one (say ArtistInfo).
(schematic)
Person 1 → 1/0 ArtistInfo

I use a PickerField to visually connect Entities. (as in Docs).

When I click on the lens in PickerField (in parent editor) it opens an editor for ArtistInfo entity (child editor), That’s OK.
I want to display (not modify) information from Person entity modified in parent Editor.

I tried different solution:

  1. In ArtistInfo screen CollecionDatasource with query
    select p from project$Person p where p.artist.id = ds$ArtistInfo

that should return 0 or 1 results, but I think I can’t connect a FieldGroup to CollectionDatasource

  1. Passing additional parameters in PickerField OpenAction of the parent editor

----- Code Snippet Father Editor----

@Override
public void ready() {
    Person p = getItem();
    Map<String,Object> params = new HashMap<String,Object>();
    params.put("person",p);
    PkpArtist.getOpenAction().setEditScreenParams(params);
}

----- Code Snippet ----

and retrieving in Child Editor.

----- Code Snippet Child Editor ----

@Override
void init(Map<String, Object> params) {
    p = (Person) params.get("person")  --> Works great
    // Datasource Creation
    personDS = new DsBuilder(getDsContext()).setJavaClass(Person.class)
            .setViewName(View.LOCAL)
            .setId("personDs").buildDatasource()
    personDS.setItem(p)
}

@Override
void ready() {
    //personDS.setItem(p)
    personDS.refresh()
}

----- Code Snippet ----

Here in ArtistInfo screen XML is defined a single instance datasource connected to Field Group that is not initialized at editor opening (single instance DS does not get data from DB) So I created programmatically and tried to setItem() in init() or ready()
with no success.

How can I solve this?

Thanks in advance,
Fabrizio

Hello @f.demassis

You want to display the Person entity in one more FieldGroup that is located in ArtistInfo editor screen, am I right?

Regards,
Daniil.

Yes you are right.
Thank you in advance.

Fabrizio

In simple case you can just manually pass parent entity values into FieldGroup fields with fieldGroup.setFieldValue(fieldConfig, value).

I’ve prepared small sample for you. Please take a look at it and ask question if you have some.

image

dyn-fg-init.zip (76.9 KB)

Regards,
Daniil.

Hello i’ ll tried your example but wih no success.
Ok for optimal parameter passing between two editors using ParamsMap.of that works perfectly, but i still have null pointer Exception in personDS.setItem(parameter) because personDS is null.
It seems it is not created by XMA declaration. I tried again to move setItem() in ready method, but no success. Follows my groovy snippet of code for ArtistInfoEdit.groovy and PersonDS xml definition.

Thank yuo in advance for your time,
Fabrizio

------------------------------------- Groovy --------------------------------------------------- ArtistInfoEdit.groovy
private Person p

@Override
void init(Map<String, Object> params) {

    super.init(params)
    p = (Person) params.get("person")
}


private void  initPersonLayout(Person p) {
    personDS.setItem(p)   <<<< personDS=null

    def setField = { FieldGroup.FieldConfig fc ->
        def value = personDS.getItem().getValueEx(fc.getProperty())
        PersonfieldGroup.setFieldValue(fc, value)
    }

    PersonfieldGroup.getFields().each {setField it}
    PersonfieldGroup.setVisible(true)
}

@Override
void ready() {
    if (p != null)
        initPersonLayout(p)
}

------------------------------------- Groovy ---------------------------------------------------
------------------------------------- XML --------------------------------------------------- ArtistInfo-edit.xml

------------------------------------- XML ---------------------------------------------------

I solved it, It was banally a problem of variable name of datasource.
Thank you.

Fabrizio