Lazy Query Container
Lazy Query Container addon provides container with support for normal reads and lazy loading reads from sources like WebService client, JDBC connection, JPA context, NoSQL database or Java services.
Lazy Query Container (LQC) is a Vaadin framework addon which provides lazy loading container for Vaadin tables. LQC includes non lazy container versions as well which use same query implementations as lazy containers.
See Ilves Framework for practical LQC implementation examples. The link can be found in the link list on the right. Use key word LazyQueryContainer in the search box to find the right classes.
All containers support buffered reads and writes of items. Data reads and writes are delegated through Query interface to an application specific business delegate.
The QueryFactory and Query interface implementations are designed to be application specific and should invoke a data source like WebService client, JDBC connection, JPA context or Java service class to read and write data items. The data source has to support querying data in batches.
Lazy Query Container supports the following features: non lazy loading, lazy loading, caching, sorting, filtering, statistics, item property as ID, item index as ID and adding & removing properties (columns). Lazy Query Container also supports adding items, modifying items and removing items. The changes are buffered and can be either commited or discarded. Row status icons and status column generator are also included to allow for visualization of row states. See the demo for hands on experience on the features.
Containers:
- QueryContainer and LazyQueryContainer for custom data sources.
- EntityContainer and LazyEntityContainer for JPA. ** Vaadin Container Filters are mapped to JPA 2.0 Criteria API queries. ** Both container and application managed transactions are supported. ** Both attached and detached entities are supported. ** Nested properties are fully supported.
Other utility classes (see Wiki page for examples):
- CompositeItem for combining beans and dynamic properties.
- BeanQueryFactory for queries loading JavaBeans. ** Nested properties are fully supported.
Sample code
package com.logica.portlet.example; import javax.persistence.EntityManager; import org.vaadin.addons.lazyquerycontainer.Query; import org.vaadin.addons.lazyquerycontainer.QueryDefinition; import org.vaadin.addons.lazyquerycontainer.QueryFactory; public class MovieQueryFactory implements QueryFactory { private EntityManager entityManager; private QueryDefinition definition; public MovieQueryFactory(EntityManager entityManager) { super(); this.entityManager = entityManager; } @Override public void setQueryDefinition(QueryDefinition definition) { this.definition=definition; } @Override public Query constructQuery(Object[] sortPropertyIds, boolean[] sortStates) { return new MovieQuery(entityManager,definition,sortPropertyIds,sortStates); } } package com.logica.portlet.example; import java.util.ArrayList; import java.util.List; import javax.persistence.EntityManager; import org.vaadin.addons.lazyquerycontainer.Query; import org.vaadin.addons.lazyquerycontainer.QueryDefinition; import com.logica.example.jpa.Movie; import com.vaadin.data.Item; import com.vaadin.data.util.BeanItem; public class MovieQuery implements Query { private EntityManager entityManager; private QueryDefinition definition; private String criteria=""; public MovieQuery(EntityManager entityManager, QueryDefinition definition,Object[] sortPropertyIds, boolean[] sortStates) { super(); this.entityManager = entityManager; this.definition = definition; for(int i=0;i<sortPropertyIds.length;i++) { if(i==0) { criteria=" ORDER BY"; } else { criteria+=","; } criteria+=" m."+sortPropertyIds[i]; if(sortStates[i]) { criteria+=" ASC"; } else { criteria+=" DESC"; } } } @Override public Item constructItem() { return new BeanItem<Movie>(new Movie()); } @Override public int size() { javax.persistence.Query query = entityManager.createQuery("SELECT count(m) from Movie as m"); return (int)((Long) query.getSingleResult()).longValue(); } @Override public List<Item> loadItems(int startIndex, int count) { javax.persistence.Query query = entityManager.createQuery("SELECT m from Movie as m"+criteria); query.setFirstResult(startIndex); query.setMaxResults(count); List<Movie> movies=query.getResultList(); List<Item> items=new ArrayList<Item>(); for(Movie movie : movies) { items.add(new BeanItem<Movie>(movie)); } return items; } @Override public void saveItems(List<Item> addedItems, List<Item> modifiedItems, List<Item> removedItems) { throw new UnsupportedOperationException(); } @Override public boolean deleteAllItems() { throw new UnsupportedOperationException(); } }
package org.vaadin.addons.lazyquerycontainer.example.jpa; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Map; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import org.vaadin.addons.lazyquerycontainer.EntityContainer; import org.vaadin.addons.lazyquerycontainer.LazyQueryView; import org.vaadin.addons.lazyquerycontainer.QueryItemStatus; import org.vaadin.addons.lazyquerycontainer.QueryItemStatusColumnGenerator; import com.vaadin.Application; import com.vaadin.ui.AbstractSelect.MultiSelectMode; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.Button.ClickListener; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Panel; import com.vaadin.ui.Table; import com.vaadin.ui.TextField; import com.vaadin.ui.VerticalLayout; import com.vaadin.ui.Window; import com.vaadin.ui.themes.Runo; /** * Example application demonstrating the Lazy Query Container features. * @author Tommi S.E. Laukkanen */ @SuppressWarnings("rawtypes") public class VaadinApplication extends Application implements ClickListener { private static final long serialVersionUID = 1L; public static final String PERSISTENCE_UNIT = "vaadin-lazyquerycontainer-example"; private Button refreshButton; private Button editButton; private Button saveButton; private Button cancelButton; private Button addItemButton; private Button removeItemButton; private Table table; private EntityContainer<Task> entityContainer; private ArrayList<Object> visibleColumnIds = new ArrayList<Object>(); private ArrayList<String> visibleColumnLabels = new ArrayList<String>(); private TextField nameFilterField; @Override public void init() { Window mainWindow = new Window("Lazycontainer Application"); VerticalLayout mainLayout = new VerticalLayout(); mainLayout.setMargin(true); mainLayout.setSpacing(true); mainWindow.setContent(mainLayout); Panel filterPanel = new Panel(); filterPanel.addStyleName(Runo.PANEL_LIGHT); HorizontalLayout filterLayout = new HorizontalLayout(); filterLayout.setMargin(false); filterLayout.setSpacing(true); filterPanel.setContent(filterLayout); mainWindow.addComponent(filterPanel); Panel buttonPanel = new Panel(); buttonPanel.addStyleName(Runo.PANEL_LIGHT); HorizontalLayout buttonLayout = new HorizontalLayout(); buttonLayout.setMargin(false); buttonLayout.setSpacing(true); buttonPanel.setContent(buttonLayout); mainWindow.addComponent(buttonPanel); Panel buttonPanel2 = new Panel(); buttonPanel2.addStyleName(Runo.PANEL_LIGHT); HorizontalLayout buttonLayout2 = new HorizontalLayout(); buttonLayout2.setMargin(false); buttonLayout2.setSpacing(true); buttonPanel2.setContent(buttonLayout2); mainWindow.addComponent(buttonPanel2); nameFilterField = new TextField("Name"); filterPanel.addComponent(nameFilterField); refreshButton = new Button("Refresh"); refreshButton.addListener(this); buttonPanel.addComponent(refreshButton); editButton = new Button("Edit"); editButton.addListener(this); buttonPanel.addComponent(editButton); saveButton = new Button("Save"); saveButton.addListener(this); saveButton.setEnabled(false); buttonPanel2.addComponent(saveButton); cancelButton = new Button("Cancel"); cancelButton.addListener(this); cancelButton.setEnabled(false); buttonPanel2.addComponent(cancelButton); addItemButton = new Button("Add Row"); addItemButton.addListener(this); addItemButton.setEnabled(false); buttonPanel2.addComponent(addItemButton); removeItemButton = new Button("Remove Row"); removeItemButton.addListener(this); removeItemButton.setEnabled(false); buttonPanel2.addComponent(removeItemButton); visibleColumnIds.add(LazyQueryView.PROPERTY_ID_ITEM_STATUS); visibleColumnIds.add("name"); visibleColumnIds.add("reporter"); visibleColumnIds.add("assignee"); visibleColumnIds.add(LazyQueryView.DEBUG_PROPERTY_ID_QUERY_INDEX); visibleColumnIds.add(LazyQueryView.DEBUG_PROPERTY_ID_BATCH_INDEX); visibleColumnIds.add(LazyQueryView.DEBUG_PROPERTY_ID_BATCH_QUERY_TIME); visibleColumnLabels.add(""); visibleColumnLabels.add("Name"); visibleColumnLabels.add("Reporter"); visibleColumnLabels.add("Assignee"); visibleColumnLabels.add("Query"); visibleColumnLabels.add("Batch"); visibleColumnLabels.add("Time [ms]"); EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT); EntityManager entityManager = entityManagerFactory.createEntityManager(); entityContainer = new EntityContainer<Task>(entityManager, true, Task.class, 100, new Object[] { "name" }, new boolean[] { true }); entityContainer.addContainerProperty(LazyQueryView.PROPERTY_ID_ITEM_STATUS, QueryItemStatus.class, QueryItemStatus.None, true, false); entityContainer.addContainerProperty("name", String.class, "", true, true); entityContainer.addContainerProperty("reporter", String.class, "", true, true); entityContainer.addContainerProperty("assignee", String.class, "", true, true); entityContainer.addContainerProperty(LazyQueryView.DEBUG_PROPERTY_ID_QUERY_INDEX, Integer.class, 0, true, false); entityContainer.addContainerProperty(LazyQueryView.DEBUG_PROPERTY_ID_BATCH_INDEX, Integer.class, 0, true, false); entityContainer.addContainerProperty(LazyQueryView.DEBUG_PROPERTY_ID_BATCH_QUERY_TIME, Integer.class, 0, true, false); for (int i=0; i<10000; i++) { Task entity = entityContainer.addEntity(); entity.setName("task-"+Integer.toString(i)); entity.setAssignee("assignee-"+Integer.toString(i)); entity.setReporter("reporter-"+Integer.toString(i)); } entityContainer.commit(); table = new Table(); table.setCaption("JpaQuery"); table.setPageLength(25); table.setContainerDataSource(entityContainer); table.setColumnWidth("name", 135); table.setColumnWidth("reporter", 135); table.setColumnWidth("assignee", 135); table.setVisibleColumns(visibleColumnIds.toArray()); table.setColumnHeaders(visibleColumnLabels.toArray(new String[0])); table.setColumnWidth(LazyQueryView.PROPERTY_ID_ITEM_STATUS, 16); table.addGeneratedColumn(LazyQueryView.PROPERTY_ID_ITEM_STATUS, new QueryItemStatusColumnGenerator(this)); table.setEditable(false); table.setMultiSelect(true); table.setMultiSelectMode(MultiSelectMode.DEFAULT); table.setSelectable(true); table.setWriteThrough(true); mainWindow.addComponent(table); setMainWindow(mainWindow); } private void setEditMode(boolean editMode) { if (editMode) { table.setEditable(true); table.setSortDisabled(true); refreshButton.setEnabled(false); editButton.setEnabled(false); saveButton.setEnabled(true); cancelButton.setEnabled(true); addItemButton.setEnabled(true); removeItemButton.setEnabled(true); nameFilterField.setEnabled(false); } else { table.setEditable(false); table.setSortDisabled(false); refreshButton.setEnabled(true); editButton.setEnabled(true); saveButton.setEnabled(false); cancelButton.setEnabled(false); addItemButton.setEnabled(false); removeItemButton.setEnabled(false); nameFilterField.setEnabled(true); } }
Links
Compatibility
Was this helpful? Need more help?
Leave a comment or a question below. You can also join
the chat on Discord or
ask questions on StackOverflow.
Version
Bugs fixed: https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/79 https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/76
- Released
- 2016-01-30
- Maturity
- STABLE
- License
- Apache License 2.0
Compatibility
- Framework
- Vaadin 7.0+
- Vaadin 6.3+ in 1.0.0
- Vaadin 6.6 in 1.2.9
- Vaadin 6.5 in 1.2.9
- Vaadin 6.4 in 1.2.9
- Vaadin 6.3 in 1.2.9
- Vaadin 6.2 in 1.2.9
- Vaadin 6.1 in 1.2.9
- Vaadin 6.0 in 1.2.9
- Vaadin 6.0+ in 1.0.1
- Vaadin 7.1+ in 2.1.0
- Vaadin 7.4+ in 7.4.0.0
- Browser
- Browser Independent