Skip to content

Opening a Compound Master from another Compound Master

elvin-fms edited this page Feb 9, 2021 · 2 revisions

Here you will learn how to provide integration of one compound master with another compound master.

At the moment TG plugin generates commented out code stubs for invoking either a simple or a compound master for "New" and "Edit" actions in the embedded entity centre within a compound master. It is up to the developer to uncomment the appropriate stub, and complete the implementation.

A good example of this scenario are two items from the Organisational structure: Division and Sector. Both entities are complex entities that have compound masters developed for their representation.

Division contains various Sectors, which are then divided into different Zones and have also lists of Workshops linked to each particular Sector.

Step 1.

Locate code that creates an embedded centre for the entity that should be represented with a compound master. In this case it is method private EntityCentre<Sector> createSectorCentre().

Remove the stubs referencing StandardActions.NEW_WITH_MASTER_ACTION and StandardActions.EDIT_ACTION.

Uncomment the code referencing Compound.openNewWithMaster() and Compound.openEdit():

final EntityActionConfig newSectorActionWithMaster = Compound.openNewWithMaster(OpenSectorMasterAction.class, "add-circle-outline", Sector.ENTITY_TITLE, actionAddDesc(Sector.ENTITY_TITLE), SECTOR_DIMS);
final EntityActionConfig editSectorAction = Compound.openEdit(OpenSectorMasterAction.class, Sector.ENTITY_TITLE, actionEditDesc(Sector.ENTITY_TITLE), SECTOR_DIMS);

Step 2.

IOpenSectorMasterAction has to be extended to include division into the fetch provider and should look like this:

 static final IFetchProvider<OpenSectorMasterAction> FETCH_PROVIDER = EntityUtils.fetch(OpenSectorMasterAction.class)
            // key is needed to be correctly autopopulated by newly saved compound master entity (ID-based restoration of entity-typed key)
            // key.division is needed to create new Sector from Division compound master
            .with("key.division");

Step 3.

SectorProducer is not required and should be deleted.

OpenSectorMasterActionProducer has to be improved to handle situations when a sector is created from Sector embedded centre on Division compound master.

    protected OpenSectorMasterAction provideDefaultValues(final OpenSectorMasterAction openAction) {
        if (keyOfMasterEntityInstanceOf(Division.class)) {
            // '+' action on Sector embedded centre on Division compound master
            final Sector newSector = co(Sector.class).new_();
            // Stale Division obtained from the key of the master entity ...
            final Division staleDivision = keyOfMasterEntity(Division.class);
            // ... need to re-fetch it afresh and using the appropriate fetch model
            EntityUtils.fetchEntityForPropOf(staleDivision, "division", co(Sector.class))
                    // most likely it will be present - why wouldn't it?
                    .ifPresent(freshDivision -> {
                        newSector.setDivision(freshDivision);
                        newSector.getProperty("division").validationResult().ifFailure(Result::throwRuntime);
                        newSector.getProperty("division").setEditable(false);
                    });

            openAction.setKey(newSector);
            return openAction;
        } else {
            return super.provideDefaultValues(openAction);
        }
    }
Clone this wiki locally