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:


  @Override
    @Transactional
    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 -> {
            factoryProductionStageDs.clear();
            FactoryProductionStage stage = metadata.create(FactoryProductionStage.class);
            List<FactoryProductionStage> lists = entityLoadService.getFactoryProductionStageList(factoryDs.getItem());

            if(!lists.isEmpty()){
                BigDecimal poQty=new BigDecimal(0);
                for (FactoryProductionStage line : lists) {
                    stage.setFactoryProductionProcess(line.getFactoryProductionProcess());
                    stage.setName(line.getName());
                    stage.setOrdinalPosition(line.getOrdinalPosition());
                    factoryProductionStageDs.addItem(stage);
                }
            }
        });

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


            factoryProductionStageDs.clear();
            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.