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
Lazy Query Container - Vaadin Add-on Directory
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.Sources at Github
Discussion Forum
Wiki Page
Usage Examples
Lazy Query Container version 1.0.0
First release.
Lazy Query Container version 1.0.1
Easier construction, more JavaDoc comments and debug properties for viewing query statistics.
Lazy Query Container version 1.1.0
Added buffered edit functionality and status column generator for visual tracking of row states. Kudos to Volker Krebs for providing ideas and code sparring to this extension.
Lazy Query Container version 1.1.1
New rows are now added to the beginning. Kudos to Volker Krebs for contributing this change.
Lazy Query Container version 1.1.2
New rows are now added to the beginning. Kudos to Volker Krebs for contributing this change.
Lazy Query Container version 1.1.3
Updated icon loading of row status column generator to work in custom applications located in different packages. Improved cache handling to avoid evicting edited rows from cache.
Lazy Query Container version 1.1.4
Fixed cache eviction of removed rows. Added CompositeItem class for combining several items to single item to allow for example use of debug properties with BeanItem.
Lazy Query Container version 1.1.5
Added generics JpaQueryFactory implementation to enable easy use with JPA. See example and Wiki page for details. Only application managed transactions are supported currently. If you need to use transaction manager please request support to be added in LQC forum or issue tracker.
Lazy Query Container version 1.1.6
Added support for selecting whether application managed or container managed transactions are used with JpaQueryFactory.
Lazy Query Container version 1.1.7
Added BeanQueryFactory and AbstractBeanQuery for faster implementation of queries with JavaBeans as value objects. See Wiki page for details.
Lazy Query Container version 1.1.8
Replaced AbstractBeanQuery default constructor with properly parametrized constructor so that implementations can use constructor to initialize for example SQL select queries for all loadBeans() invocations.
Lazy Query Container version 1.1.9
Fixed:
https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/#issue/3
https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/#issue/4
Lazy Query Container version 1.2.0
1) Renamed DefaultQueryDefinition to LazyQueryDefinition.
2) Added EntityContainer with support for setting where PSQL criteria and corresponding parameter map.
3) Deprecated JpaQueryFactory and JpaQuery.
Lazy Query Container version 1.2.1
Fixed size to be read only once per query with JPA.
Lazy Query Container version 1.2.2
Fixed:
https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/#issue/5
https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/#issue/6
Lazy Query Container version 1.2.3
Fixed: https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/closed#issue/6
Lazy Query Container version 1.2.4
Changes related to Transactional Container Vaadin addon.
Lazy Query Container version 1.2.5
Features:
Added possibility to choose whether entities are detached from persistence context when EntityContainer loads them and whether they are symmetrically reattached.
Fixed:
https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/closed/#issue/9
https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/closed/#issue/10
https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/closed/#issue/11
https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/closed/#issue/12
Lazy Query Container version 1.2.6
Added features:
* QueryView has now methods for accessing added, modified and removed items.
Fixed issues:
* https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/#issue/13
Lazy Query Container version 1.2.7
Fixed:
https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/closed/#issue/14
https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/closed/#issue/14
Lazy Query Container version 1.2.8
Fixed: https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/closed/#issue/16
Lazy Query Container version 1.2.9
Fixed bugs:
https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/18
https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/19
https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/20
Lazy Query Container version 1.2.11
Fixed https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/23.
Lazy Query Container version 1.3.0
Kudos to fatroom for adding Vaadin version 7 support.
Please report bugs and problems.
Lazy Query Container version 1.3.1
Switched pom Java version to 1.7. Moved root pom to root folder. Set root pom as parent to the child poms and moved shared definitions to root pom. Updated Cobertura and Checkstyle maven plugin versions. Updated checkstyle checks.xml. Reformatted source code files. Fixed a bug which caused examples to throw exception with Vaadin 7.0.3.
Lazy Query Container version 2.0.0
- Added support for Vaadin Container filtering feature.
- Update Query interface to support better Container filtering.
- Implemented EntityContainer to use Vaadin Container filtering and internally JPA 2.0 Criteria AP.
- Renamed EntityContainer to LazyEntityContainer.
- Added QueryContainer and EntityContainer which can be used in non lazy manner with same query backend as LCQs.
- Added supported for using item property as ID in container in addition to Index based IDs.
- Brushed up Container constructors so that different container implementations have similar logic in constructor parameter ordering.
- Fixed open bugs.
Lazy Query Container version 2.0.1
Fixed the pom to not have parent pom.
Lazy Query Container version 2.0.2
Added support for nested properties in Vaadin Filter to JPA Criteria 2.0 API conversion.
Lazy Query Container version 2.0.3
Exposed QueryDefinition default filter add and remove methods to directly to Container API.
Lazy Query Container version 2.0.4
Bug fixes.
Lazy Query Container version 2.0.5
Bug fix to filters. Conveniency getEntity method and EntityContainer compliant constructor to LazyEntityContainer.
Lazy Query Container version 2.0.6
Full nested property support. Earlier nested properties were only supported for filtering and sorting. Please note that you need to add explicitly nested properties to container and set max nested property depth to query definition to be greater than zero. Kudos to Eduardo Frazão for contributing to this feature.
Lazy Query Container version 2.0.7
Added null aggregate support to nested properties.
Lazy Query Container version 2.0.8
- Fixed #43 Since version 2.0.0 in Vaadin 7.0+ Table addItem throws IndexOutOfBoundsException.
- Pulled fix or #44 Between filter fix. There were used two end values for the comparison.
+++ Kudos to terramexx for this fix.
- Pulled fix for #45 nextItemId/prevItemId should return null instead of throwing IndexOutOfBoundsException.
+++ Kudos to Thomas Raehalme for this fix.
Lazy Query Container version 2.0.9
Applied fix for Vaadin LCQ container issue #46. Kudos to michaelhwalther.
Lazy Query Container version 2.1.0
Updated to Vaadin 7.1+
Lazy Query Container version 2.1.15
LazyIdList changed to be serializable (author: Michael Simons)
Lazy Query Container version 2.1.16
Updated Vaadin version and fixed javadoc warnings and errors.
Lazy Query Container version 7.3.3.6
- Changed versioning scheme to be 'vaadin-version.lzq-minor-version'.
- Fixed issues #64 and #61.
- Refactored out some deprecated code dependencies.
Lazy Query Container version 7.4.0.0
Updated dependencies:
* Updated Vaadin version to 7.4.0.
* Changed JPA API version to 2.1 and changed scope to provided.
* Removed log4j dependency.
Fixed issues:
* https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/66
* https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/69
Lazy Query Container version 7.4.0.1
Fixed refresh problem with grid sorting and filters. Fix contributed by Martin Vysny.
https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/71
Lazy Query Container version 7.6.1.3
Bugs fixed:
https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/79
https://github.com/tlaukkan/vaadin-lazyquerycontainer/issues/76