Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

LazyQueryContainer with own QueryFactory

Peter Varsanyi
5 years ago Apr 19, 2016 11:13am
Peter Varsanyi
5 years ago Apr 19, 2016 1:19pm

Ok, this is the second times I answerd my own question. :) Tradition. :)
(It's not because I'm so smart. On the contrary.)
But I leave this posts in the forum, because it - maybe - will be useful someone.
Ok, this NOT the whole solution for my original problem... The "solution" is generated an other (similar) exception.
So finally I modified my code which is resolved my problem (the Query - I marked new or modified lines with this: //@CHANGED):

public class AuditlogsExtQuery implements Query {

    private EntityManager entityManager;
    private QueryDefinition definition; // not used
    private String criteria = "";
    private ConverterUtils<AuditlogsExt> converterUtils;
    private String rowPropertyMap;
    [color=#B22222]private Long startId;               //@CHANGED[/color]

    public AuditlogsExtQuery(EntityManager entityManager, QueryDefinition definition,
            Object sortPropertyIds, boolean sortStates) {
        super();
        System.out.println("init");
        this.entityManager = entityManager;
        this.definition = definition;
        this.converterUtils = new ConverterUtils<>();
        this.rowPropertyMap = new String{"id", "event", "source", "target", "clientInfo"};
        [color=#B22222]this.startId = getMaxId();      //@CHANGED[/color]

        for (int i = 0; i < sortPropertyIds.length; i++) {
            if (i == 0) {
                criteria = " ORDER BY";
            } else {
                criteria += ",";
            }
            criteria += " a." + sortPropertyIds[i];
            if (sortStates[i]) {
                criteria += " ASC";
            } else {
                criteria += " DESC";
            }
        }
    }

    @Override
    public int size() {
        System.out.println("size");
        return this.entityManager.createQuery("SELECT count(a) FROM Auditlogs a WHERE a.validto > CURRENT_DATE", Long.class)
                .getSingleResult()
                .intValue();
    }

    @Override
    public List<Item> loadItems(int startIndex, int count) {
        System.out.println("loaditems - startIndex: " + startIndex + " - count: " + count);
        javax.persistence.Query query = entityManager.createQuery("SELECT a.id, a.event, a.source, a.target, a.clientInfo FROM Auditlogs a JOIN Users u ON a.usersId = u.id WHERE a.validto > CURRENT_DATE " + criteria);
        query.setFirstResult(startIndex);
        query.setMaxResults(count);

        List<Object> result = query.getResultList();
        List<AuditlogsExt> resultExt = this.converterUtils.convertToEntityList(AuditlogsExt.class, result, this.rowPropertyMap);
        List<Item> items = new ArrayList<Item>();
        for (AuditlogsExt alext : resultExt) {
            BeanItem bi = new BeanItem<AuditlogsExt>(alext);
            items.add(bi);
        }

        return items;
    }

    @Override
    public void saveItems(List<Item> addedItems, List<Item> modifiedItems, List<Item> removedItems) {
        throw new UnsupportedOperationException("Not supported.");
    }

    @Override
    public boolean deleteAllItems() {
        throw new UnsupportedOperationException("Not supported.");
    }

    @Override
    public Item constructItem() {
        //System.out.println("constructItem");
        [color=#B22222]return new BeanItem<AuditlogsExt>(new AuditlogsExt(this.startId++)); //@CHANGED[/color]
    }

[color=#B22222]    private Long getMaxId() {  //@CHANGED
        return this.entityManager.createQuery("SELECT MAX(a.id) FROM Auditlogs a", Long.class)
                .getSingleResult()
                .longValue();
    }[/color]

}[/i][/i]

The scroll down works prefectly.BUT! When I scroll up to the top - after some page down - it gives me a fancy exception:  :(

[...]
loaditems - startIndex: 1050 - count: 50
loaditems - startIndex: 1100 - count: 50
loaditems - startIndex: 1150 - count: 50
loaditems - startIndex: 1200 - count: 50
loaditems - startIndex: 1250 - count: 50
loaditems - startIndex: 1300 - count: 50
loaditems - startIndex: 50 - count: 50
loaditems - startIndex: 100 - count: 50
ápr. 19, 2016 2:56:40 DU com.vaadin.server.DefaultErrorHandler doDefault
SEVERE:
java.lang.NullPointerException
    at org.vaadin.addons.lazyquerycontainer.LazyIdList.get(LazyIdList.java:91)
    at org.vaadin.addons.lazyquerycontainer.LazyQueryContainer.getIdByIndex(LazyQueryContainer.java:230)
    at com.vaadin.data.ContainerHelpers.getItemIdsUsingGetIdByIndex(ContainerHelpers.java:88)
    at org.vaadin.addons.lazyquerycontainer.LazyQueryContainer.getItemIds(LazyQueryContainer.java:655)
    at com.vaadin.server.communication.data.RpcDataProviderExtension.pushRowData(RpcDataProviderExtension.java:367)
    at com.vaadin.server.communication.data.RpcDataProviderExtension.access$900(RpcDataProviderExtension.java:65)
    at com.vaadin.server.communication.data.RpcDataProviderExtension$2.requestRows(RpcDataProviderExtension.java:293)
    at sun.reflect.GeneratedMethodAccessor60.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[...]

So I ask - not just from myself :) - What did I do wrong? :)

 

Peter Varsanyi
5 years ago Apr 20, 2016 8:16am