postValidate() and preCommit() replacement in v7?

What is the equivalent for postValidate() in v7.0 screens? I used to have this:

    @Override
    protected void postValidate(ValidationErrors errors) {
        if (getItem().getRtype().equals(RtypeEnum.GlobalSuppress) ||
                getItem().getRtype().equals(RtypeEnum.FrontEndSuppress) ||
                getItem().getRtype().equals(RtypeEnum.OfferTypeSuppress) ||
                getItem().getRtype().equals(RtypeEnum.OfferSuppress)) {
            if (getItem().getSrcode() == null) {
                errors.add("Please choose a Suppress Reason for a Suppress record.");
            }
        }
        for (Item item : itemsDs.getItems()) {
            if (item.getSourceId() == null) {
                errors.add("Please choose a Source Code or choose 'UNKNOWN'");
            }
            if (item.getStepid() != null && item.getEntrynumber() == null) {
                errors.add("Missing Entry Number.  Please make sure that you have a unique entry number for every puzzle entry.");
            }

            if (item.getStepid() == null && item.getEntrynumber() != null) {
                errors.add("Missing Game/Step.  Plesae make sure that every entry has a puzzle game/step assigned.");
            }
        }
        super.postValidate(errors);
    }

Similary, for preCommit():

    @Override
    protected boolean preCommit() {

        if (!getItem().getRtype().equals(RtypeEnum.GlobalSuppress) &&
                !getItem().getRtype().equals(RtypeEnum.FrontEndSuppress) &&
                !getItem().getRtype().equals(RtypeEnum.OfferTypeSuppress) &&
                !getItem().getRtype().equals(RtypeEnum.OfferSuppress) &&
                getItem().getSrcode() != null) {
            getItem().setSrcode(null);
        }

        //  Load all suppitems for this customer, if there are any

        LoadContext<Suppitem> loadContext = LoadContext.create(Suppitem.class)
                .setQuery(LoadContext.createQuery("select c from rade$Suppitem c where c.customer.id = :customer")
                        .setParameter("customer", getItem()))
                .setView(View.LOCAL);
        List<Suppitem> suppitems = dataManager.loadList(loadContext);

        // Delete any suppitems that already exist

        if (suppitems.size() > 0) {
            for (Suppitem item: suppitems) {
                dataManager.remove(item);
            }
        }

        // Add back the selected suppitems, if there are any

        if (getItem().getRtype().equals(RtypeEnum.OfferTypeSuppress) || getItem().getRtype().equals(RtypeEnum.OfferSuppress)) {
            Collection chosen = (Collection) optSuppitems.getLookupSelectedItems();
            for (Object value : chosen) {
                Suppitem suppitem = metadata.create(Suppitem.class);
                suppitem.setCustomer(getItem());
                if (lookupRtype.getValue().equals(RtypeEnum.OfferTypeSuppress)) {
                    suppitem.setOffertype((String) value);
                } else {
                    suppitem.setOffer((Offer) value);
                }
                dataManager.commit(suppitem);
            }
        }

        return super.preCommit();
    }

How would I code this instead?

Hi Eric!

Instead of postValidate you can use validateAdditionalRules(ValidationErrors errors).

preCommit is now an event of a DataContext. You can subscribe on it with @Subscribe annotation. Look at following code:

@Subscribe(target = Target.DATA_CONTEXT)
private void onPreCommit(DataContext.PreCommitEvent event) {

    if (!getEditedEntity().getRtype().equals(RtypeEnum.GlobalSuppress) &&
            !getEditedEntity().getRtype().equals(RtypeEnum.FrontEndSuppress) &&
            !getEditedEntity().getRtype().equals(RtypeEnum.OfferTypeSuppress) &&
            !getEditedEntity().getRtype().equals(RtypeEnum.OfferSuppress) &&
            getEditedEntity().getSrcode() != null) {
        getEditedEntity().setSrcode(null);
    }

    //  Load all suppitems for this customer, if there are any

    LoadContext<Suppitem> loadContext = LoadContext.create(Suppitem.class)
            .setQuery(LoadContext.createQuery("select c from rade$Suppitem c where c.customer.id = :customer")
                    .setParameter("customer", getEditedEntity()))
            .setView(View.LOCAL);
    List<Suppitem> suppitems = dataManager.loadList(loadContext);

    // Delete any suppitems that already exist

    if (suppitems.size() > 0) {
        for (Suppitem item: suppitems) {
            dataManager.remove(item);
        }
    }

    // Add back the selected suppitems, if there are any

    if (getEditedEntity().getRtype().equals(RtypeEnum.OfferTypeSuppress) || getEditedEntity().getRtype().equals(RtypeEnum.OfferSuppress)) {
        Collection chosen = (Collection) optSuppitems.getLookupSelectedItems();
        for (Object value : chosen) {
            Suppitem suppitem = metadata.create(Suppitem.class);
            suppitem.setCustomer(getEditedEntity());
            if (lookupRtype.getValue().equals(RtypeEnum.OfferTypeSuppress)) {
                suppitem.setOffertype((String) value);
            } else {
                suppitem.setOffer((Offer) value);
            }
            dataManager.commit(suppitem);
        }
    }
    
//        use it instead of return false
//        event.preventCommit();
}

You don’t need to remember how to use the @Subscribe annotation, as CUBA Studio generates it for you. Press Alt+Insert (Cmd+N on macOS) and select Subscribe to event, then choose an event of interest.

1 Like

Before I heard back from you, I created a Bean Validator instead for the postValidate, which seems to work OK. :wink:

As for the preCommit - thanks for the hint! I will try it out.