Manipulate fields programmatically when creating a record

Hello,
I have implemented the following class “updateNomComplet ()” to concatain the field “nomComplet” and “nom”.
This works perfectly when the record is already created. The problem is when we are creating a record (not yet comited - while entering information in the form) it does not work and I don’t see how to do it. Thank you for your help.

package com.company.performancecenter.web.departement;

import com.company.performancecenter.web.objectif.ObjectifEdit;
import com.haulmont.cuba.gui.model.*;
import com.haulmont.cuba.gui.screen.*;
import com.company.performancecenter.entity.Departement;
import com.company.performancecenter.entity.Objectif;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@UiController("performancecenter_Departement.browse")
@UiDescriptor("departement-browse.xml")
@LookupComponent("table")
//@LoadDataBeforeShow
public class DepartementBrowse extends MasterDetailScreen<Departement> {
    // Debug
    Logger logger = LoggerFactory.getLogger(ObjectifEdit.class);

    // Injections
    @Inject
    private CollectionLoader<Departement> departementsLd;
    @Inject
    private InstanceContainer<Departement> departementsDc;
   // private CollectionContainer<Departement> departementsDc;
    @Inject
    private CollectionLoader<Objectif> objectifsDl;

    @Subscribe
    protected void onBeforeShow(BeforeShowEvent event) { departementsLd.load(); }

    @Subscribe(id = "departementsDc", target = Target.DATA_CONTAINER)
    protected void onDepartementsDcItemChange(InstanceContainer.ItemChangeEvent<Departement> event) {
        logger.info("Initialisation de l'application : InstanceContainer");
        objectifsDl.setParameter("departement", event.getItem());
        objectifsDl.load();
    }
    @Subscribe
    protected void onInit(InitEvent event) {
        logger.info("Initialisation de l'application : Initialisation (event Init)");
        departementsDc.addItemPropertyChangeListener(event2 -> {
           updateNomComplet();
        });
    }

    public void updateNomComplet() {
        String nomComplet ="/";
        Departement departement = departementsDc.getItem();

        if (departement.getLParentDepartement() != null) {
            nomComplet = departement.getLParentDepartement().getNomComplet() + "/" + departement.getNom();
        }
        else
        {
            nomComplet = "/" + departement.getNom();
        }
        departement.setNomComplet(nomComplet);
        // DEBUG
        logger.info("RESOLUTION DU NOM COMPLET");
        logger.info("NOM         : " + String.valueOf(departement.getNom()));
        logger.info("NOM COMPLET : " + String.valueOf(departement.getNomComplet()));
    }
}

Please provide a small test project demonstrating the problem and steps how to reproduce it.

Hello and thank you for answer.
I created an example to reproduce the problem and found the defect.
Indeed, there was confusion in the view between an “InstanceContainer” and a “CollectionContainer”.
In the source code of the controller I published, the injection “departementsDc” refers to an “InstanceContainer” while in the view “departementsDc” is a “CollectionContainer”.

Thank you !

1 Like