Search code examples
cuba-platform

Open window with filter in Cuba Platform controller


I have some links on my dashboard, which link to myCustomers or allCostomerThatRegisteredYesterday. I have used the application-folder. For the usability is a dashboard with quicklinks even better.

How can I call openWindow("screen-id",WindowType,filterMap?) with a filter?


Solution

  • You have two options here:

    1. You can use a Query Filter (see https://doc.cuba-platform.com/manual-6.1/manual.html#datasource_query_filter). So your datasource definition will look like this:
        <collectionDatasource id="booksDs"
                              class="com.company.opentest.entity.Book"
                              view="_local">
            <query>
                <![CDATA[select e from opentest$Book e]]>
                <filter>
                    <and>
                        <c>e.author.id = :param$bookAuthor</c>
                    </and>
                </filter>
            </query>
        </collectionDatasource>
    

    In this case, if you pass a bookAuthor param in the params argument of the openWindow method, this query filter will be applied.

        Map<String, Object> params = new HashMap<>();
        params.put("bookAuthor", author);
        openWindow("opentest$Book.browse", WindowManager.OpenType.NEW_TAB, params);
    
    1. If you want to use a Generic filter component and set the predefined filter entity to it, then it is a bit more complex. You'll have to analyze the parameters map that is passed to the screen init() method, and if there is a special parameter there, you'll have find the required filter entity (sec$Filter) by the JPQL query. Then set this entity to the Filter component (Filter#setFilterEntity(...)), specify filter parameters (Filter#setParamValue(...)) and apply the filter (Filter#apply()).

    Getting filter parameter name (author84104) is described at the setParamValue() method javadoc and at the filter component documentation.

    public class BookBrowse extends AbstractLookup {
    
        @Inject
        private Filter filter;
    
        @Inject
        private DataManager dataManager;
    
        private Author bookAuthorParam;
    
        @Override
        public void init(Map<String, Object> params) {
            super.init(params);
            bookAuthorParam = (Author) params.get("bookAuthor");
        }
    
        @Override
        public void ready() {
            super.ready();
            if (bookAuthorParam != null) {
                setAuthorFilter(bookAuthorParam);
            }
        }
    
        private void setAuthorFilter(Author bookAuthor) {
            FilterEntity filterEntity = findFilterEntity();
            if (filterEntity != null) {
                filter.setFilterEntity(filterEntity);
                filter.setParamValue("author84104", bookAuthor);
                filter.apply(false);
            }
        }
    
        private FilterEntity findFilterEntity() {
            LoadContext<FilterEntity> ctx = new LoadContext<>(FilterEntity.class)
                    .setView("app");
            ctx.setQueryString("select f from sec$Filter f where f.componentId = :componentId and f.name = :name")
                    .setParameter("componentId", "[opentest$Book.browse].filter")
                    .setParameter("name", "By author");
            return dataManager.load(ctx);
        }
    }