Migrating custom filter from Cuba to Jmix 1.5

Hello:

I am migrating a Cuba 7.2 application to Jmix 1.5. I wrote a custom filter that I want to migrate without using cubaFilter if I can avoid it. The filter generated custom JPQL and set it to the dataloader using a filterBeforeFilterAppliedHandler. That event doesn’t exist in a Jmix filter.

This is the Cuba code:

    @Install(to = "filter", subject = "beforeFilterAppliedHandler")
    private boolean filterBeforeFilterAppliedHandler() {
        String sql = "select e from merges_Gmailq e where 1 = 1 ";

        String statusFilter = (String) filter.getParamValue("typeFilter62988");
        Integer minDays = (Integer) filter.getParamValue("minDays82894");
        Integer maxDays = (Integer) filter.getParamValue("maxDays33946");

        if (statusFilter == null) {
            statusFilter = MailqFilterEnum.UNASSIGNED_ASSIGNED.getId();
            filter.setParamValue("typeFilter62988", MailqFilterEnum.UNASSIGNED_ASSIGNED);
        }

        if (minDays == null) minDays = 0;
        if (maxDays == null) maxDays = 30;

        switch (Objects.requireNonNull(MailqFilterEnum.fromId(statusFilter))) {
            case ALL:
                break;
            case ASSIGNED:
                sql += " and (coalesce(e.status,'U') = 'A')";
                break;
            case UNASSIGNED_ASSIGNED:
                sql += " and (coalesce(e.status,'U') = 'U' or (coalesce(e.status,'U') = 'A' and e.assignedTo = '" + userSession.getUser().getLogin().toUpperCase() + "'))";
                break;
            case UNASSIGNED:
                sql += " and (coalesce(e.status,'U') = 'U')";
                break;
            case ASSIGNED_LAST30:
                sql += " and (coalesce(e.status,'U) = 'A' and e.assignedTo = '" + userSession.getUser().getLogin().toUpperCase() + "' and e.arrivalTime >= {d '" + LocalDate
                        .now().minus(Period.ofDays(30)).format(DateTimeFormatter.ISO_DATE) + "'})";
                break;
            case ASSIGNED_ME:
                sql += " and (coalesce(e.status,'U') = 'A' and e.assignedTo = '" + userSession.getUser().getLogin().toUpperCase() + "')";
                break;
            case DELETED:
                sql += " and (coalesce(e.status,'U') = 'D')";
                break;
            case COMPLETED:
                sql += " and (coalesce(e.status,'U') = 'C')";
                break;
        }
        if (minDays == 0) {
            sql += " and @between(e.arrivalTime, now - " + maxDays.toString() + ", now + 1" + ",day)";
        } else {
            minDays--;
            sql += " and @between(e.arrivalTime, now - " + maxDays.toString() + ", now - " + minDays.toString() + ",day)";
        }
        sql += " order by e.arrivalTime desc";
        filter.getDataLoader().setQuery(sql);
        return true;
    }
        <filter id="filter"
          applyTo="gmailMessagesTable"
          dataLoader="gmailMessagesDl">
            <properties include=".*"/>
            <custom name="statusFilter" paramClass="com.paslists.merges.entity.enums.MailqFilterEnum" caption="Filter" inExpr="false"/>
            <custom name="minDays" paramClass="java.lang.Integer" caption="Min Days" inExpr="false"/>
            <custom name="maxDays" paramClass="java.lang.Integer" caption="Max Days" inExpr="false"/>
        </filter>

As you can see, the custom filter uses the current user name and some generated SQL for various conditions. The SQL generated is dependent on the filter values chosen, so I don’t see a way to do this in the XML.

Should I do this in a Preload event on the data loader attached to the filter?

Is there a recommended solution?

Thanks in advance…

Hello,

Yes, PreLoadEvent is the most suitable place to do it.