Directory

← Back

CriteriaBuilder

UI to create SQL "Where" criteria, or Container's Filter

Author

Rating

Popularity

<100

This component allow user to filter data. You can add "where" condition to SQL-query, or set Filter in container, or write your own specific builder (hibernate criteria builder)

Sample code

	 	final Collection<CriteriaField> fields = new HashSet<CriteriaField>();
	 	fields.add(new CriteriaField("name", "Name"));
	 	fields.add(new CriteriaField("code", "Code", ClassField.INTEGER));	 	
	 	fields.add(new CriteriaField("summa", "Summa", ClassField.FLOAT));
	 	fields.add(new CriteriaField("date", "Date", ClassField.DATE));
	 	fields.add(new CriteriaField("city", "City"));	 	
               criteriaBuilder = new CriteriaBuilder(fields);   

// and put builder somewhere like this
// layout.addComponent(criteriaBuilder);
	private void applyFilter(){
		if (!criteriaBuilder.validate()) return; // user has entered invalid data!
		Filter filter = CriteriaContainerFilterBuilder.getFilter(criteriaBuilder);
		if (filter!=null){
		  removeFilters();	
		  container.addContainerFilter(filter); // this Datasource
		// this only Label for example
		  sqlWhere.setCaption(CriteriaSqlTextBuilder.getSql(criteriaBuilder)); 
		}  
	}
	
	private void removeFilters(){
		container.removeAllContainerFilters();
	}
public class CriteriabuildertestUI extends UI {

	CriteriaBuilder criteriaBuilder;
	ContainerHierarchicalWrapper itemContainerWrapper;
	Table table;
	BeanItemContainer<TestItem> container;
	CriteriaBuilderWindow criteriaBuilderWindow;
	
	@WebServlet(value = "/*", asyncSupported = true)
	@VaadinServletConfiguration(productionMode = false, ui = CriteriabuildertestUI.class)
	public static class Servlet extends VaadinServlet {
	}

	@Override
	protected void init(VaadinRequest request) {
		final VerticalLayout layout = new VerticalLayout();
		final HorizontalLayout btnLayout = new HorizontalLayout();
		layout.setMargin(true);
		setContent(layout);

		table = new Table(){
		    protected String formatPropertyValue(Object rowId,
		            Object colId, Property property) {
		        // Format by property type
		        if (property.getType() == Date.class) {
		            SimpleDateFormat df =
		                new SimpleDateFormat("dd-MM-yyyy");
		            return df.format((Date)property.getValue());
		        }
		        else if (property.getType() == Double.class){
		          return String.format("%.2f", (Double)property.getValue());
		        }
		        else
		        return super.formatPropertyValue(rowId, colId, property);
		}};
		container=getTestData();
		table.setContainerDataSource(container);
		table.setWidth("100%");
	    layout.addComponent(table);
	    layout.addComponent(btnLayout);
		
	 	final Collection<CriteriaField> fields = new HashSet<CriteriaField>();
	 	fields.add(new CriteriaField("name", "Name"));
	 	fields.add(new CriteriaField("code", "Code", ClassField.INTEGER));	 	
	 	fields.add(new CriteriaField("summa", "Summa", ClassField.FLOAT));
	 	fields.add(new CriteriaField("date", "Date", ClassField.DATE));
	 	fields.add(new CriteriaField("city", "City"));	 	        
		criteriaBuilderWindow = new CriteriaBuilderWindow(fields); 
		criteriaBuilderWindow.setModal(true);
		criteriaBuilderWindow.setApplyEvent(new CriteriaBuilderWindowListener() {					
			@Override
			public void execute() {
				CriteriaBuilder criteriaBuilder=criteriaBuilderWindow.getCriteriaBuilder();
				itemContainerWrapper=criteriaBuilder.getItemContainerWrapper();
				Filter filter = CriteriaContainerFilterBuilder.getFilter(criteriaBuilder);
				if (filter!=null){
				  removeFilters();	
				  container.addContainerFilter(filter); // this is Datasource
				}  
			}
		});
		
        Button showFilterBtn = new Button ("Show Criteria Window");
        showFilterBtn.addClickListener(new ClickListener(){			
			@Override			
			public void buttonClick(ClickEvent event) {				

				addWindow(criteriaBuilderWindow);
			}
		});
        btnLayout.addComponent(showFilterBtn); 	
        
        Button resetBtn = new Button ("Reset");
        resetBtn.addClickListener(new ClickListener() {			
			@Override			
			public void buttonClick(ClickEvent event) {
				removeFilters(); 						
			}
		});
        btnLayout.addComponent(resetBtn);   
        
        
	}
	
	private void removeFilters(){
		container.removeAllContainerFilters();
	}	
	
	
	public BeanItemContainer<TestItem> getTestData(){
		BeanItemContainer<TestItem> container = new BeanItemContainer<TestItem>(TestItem.class);
		DateFormat dateFormat = SimpleDateFormat.getDateInstance();
		try {
			container.addBean(new TestItem("Den",5,1501.12, dateFormat.parse("14.1.2015"),"Odessa"));
			container.addBean(new TestItem("Alex",4,2504.11, dateFormat.parse("15.1.2015"),"Kiev"));
			container.addBean(new TestItem("Ksenia",2,901.52, dateFormat.parse("5.1.2015"),"Odessa"));
			container.addBean(new TestItem("Vladimir",1,3901.89, dateFormat.parse("15.12.2014"),"Odessa"));
			container.addBean(new TestItem("Anna",12,1901.89, dateFormat.parse("23.11.2014"),"Zaporozh'e"));
			container.addBean(new TestItem("Anna",12,2301.14, dateFormat.parse("20.12.2014"),"Zaporozh'e"));
			container.addBean(new TestItem("Vladimir",1,4311.00, dateFormat.parse("6.1.2015"),"Odessa"));
			container.addBean(new TestItem("Ksenia",2,1201.02, dateFormat.parse("25.11.2014"),"Odessa"));
			container.addBean(new TestItem("Stephan",3,7040.00, dateFormat.parse("22.11.2014"),"Zhmerinka"));
			container.addBean(new TestItem("Vlad",3,3040.00, dateFormat.parse("22.01.2015"),"Dnepropetrovsk"));
			container.addBean(new TestItem("Sam",6,1050.00, dateFormat.parse("28.01.2015"),"Dnepropetrovsk"));
			container.addBean(new TestItem("Sam",6,3050.00, dateFormat.parse("02.02.2015"),"Dnepropetrovsk"));
			container.addBean(new TestItem("Kat",7,2150.00, dateFormat.parse("05.01.2015"),"Kiev"));
			container.addBean(new TestItem("Kat",7,4150.00, dateFormat.parse("05.11.2014"),"Kiev"));
			container.addBean(new TestItem("Peter",8,7150.00, dateFormat.parse("12.12.2014"),"Vinnitsa"));
			container.addBean(new TestItem("Peter",8,6150.00, dateFormat.parse("18.12.2014"),"Vinnitsa"));
			container.addBean(new TestItem("Peter",8,6150.00, dateFormat.parse("11.01.2015"),"Vinnitsa"));
			container.addBean(new TestItem("Andrey",10,2150.00, dateFormat.parse("15.01.2015"),"Odessa"));
			container.addBean(new TestItem("Nataly",11,1190.50, dateFormat.parse("11.01.2015"),"Zaporozh'e"));
	
		} catch (ParseException e) {			
			e.printStackTrace();
		}						
		return container;
	}	

}

Compatibility

(Loading compatibility data...)

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

  • "Not flag" in groups work correctly in CriteriaContainerFilterBuilder
  • Some JavaDocs added
Released
2015-02-04
Maturity
EXPERIMENTAL
License
Apache License 2.0

Compatibility

Framework
Vaadin 7.3+
Browser
N/A

CriteriaBuilder - Vaadin Add-on Directory

UI to create SQL "Where" criteria, or Container's Filter CriteriaBuilder - Vaadin Add-on Directory
This component allow user to filter data. You can add "where" condition to SQL-query, or set Filter in container, or write your own specific builder (hibernate criteria builder)
Online