Loading datasource from Service #694

I have a service that returns List of entities that I have used to load the datasource in screen controller without any manipulation as the datasource is returning based on the condition I want to. As I am loading the datasource through a loop, I wonder if there is any easer way to load.

Here the service:

    public List<FactoryProductionStage> getFactoryProductionStageList(Factory factory) {
        List<FactoryProductionStage> list = new ArrayList<>();
        EntityManager em = persistence.getEntityManager();
        String queryString = "select s from mydb$Factory f JOIN f.factoryProductionProcess p JOIN p.factoryProductionStage s \n" +
                "        WHERE f.id = ?1 ORDER BY s.ordinalPosition";

        TypedQuery<FactoryProductionStage> query = em.createQuery(queryString, FactoryProductionStage.class);
        query.setParameter(1, factory.getId());
        list = query.getResultList();
        return list;

Here is how I am loading the list of entities in my datasource:

  factoryDs.addItemPropertyChangeListener(e -> {
            FactoryProductionStage stage = metadata.create(FactoryProductionStage.class);
            List<FactoryProductionStage> lists = entityLoadService.getFactoryProductionStageList(factoryDs.getItem());

                BigDecimal poQty=new BigDecimal(0);
                for (FactoryProductionStage line : lists) {

Instead of above code, I was wondering if something like follows should work:

            factoryProductionStageDs = (CollectionDatasource<FactoryProductionStage, UUID>) entityLoadService.getFactoryProductionStageDs(factoryDs.getItem());

But i see it doesn’t work. Is there anything around the corner I can try?

You can create a custom datasource class as explained here and then specify the full name of this class in the “Datasource class” field of your datasource in Studio.