paremeter of customer data source

Is it possible sending parameter to custom data source class that will be used from the Studio designer? Thanks for any example.

Hi,
There are two ways to send parameters in the custom data source.

  1. Creates fields in data source class, fill fields by specified parameter values in screen class and use this values in the getEntities method of custom data source
  2. Or you can manually refresh data source in screen class by using CollectionDatasource#refresh(java.util.Map). Map of params is passed to the getEntities method of custom data source

I can make examples if needed

Hi Andrey
Thanks, that will be very good with examples.

Hi Mortoza
There is a sample data source class, that uses parameters.


public class CustomEntityDatasource extends CustomCollectionDatasource<CustomEntity, UUID> {

    protected String name;

    @Override
    protected Collection<CustomEntity> getEntities(Map<String, Object> params) {
        DataManager dataManager = AppBeans.get(DataManager.class);
        LoadContext loadContext = new LoadContext(CustomEntity.class)
                .setView(View.LOCAL);
        if (params.containsKey("name")) {
            loadContext.setQueryString("select e from customdatasourse$CustomEntity e where e.name = :name")
                    .setParameter("name", params.get("name"));
        } else if (name != null) {
            loadContext.setQueryString("select e from customdatasourse$CustomEntity e where e.name = :name")
                    .setParameter("name", name);
        }
        else {
            loadContext.setQueryString("select e from customdatasourse$CustomEntity e");
        }
        return dataManager.loadList(loadContext);
    }

    public void setName(String name) {
        this.name = name;
    }
}

I pass parameters from screen class:


public class CustomEntityBrowse extends AbstractLookup {
    @Inject
    private CustomEntityDatasource customEntitiesDs;

    @Override
    public void init(Map<String, Object> params) {
        super.init(params);
        customEntitiesDs.setName("abc");
    }
}

or


public class CustomEntityBrowse extends AbstractLookup {
    @Inject
    private CustomEntityDatasource customEntitiesDs;

    @Override
    public void init(Map<String, Object> params) {
        super.init(params);
        customEntitiesDs.refresh(ParamsMap.of("name", "abc"));
    }
}

customDatasourse.zip (29.6K)

1 Like

Hi

Is it possible to add a filter to this custom datasource?

Thank you

In a custom datasource you can load data in an arbitrary way - from middleware service, from external web services, creating entities on the fly, etc. Whereas filter component just modifies JPQL which is sent to DataManager. So you can set a filter to your datasource (if it contains a JPQL query), but its your responsibility to handle how the filter should affect your data loading process. The filter is available via the getQueryFilter() method.