You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beehive.apache.org by ek...@apache.org on 2006/01/28 03:01:07 UTC
svn commit: r373065 - in /beehive/trunk/samples/netui-samples:
src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/
web/ui/datagrid/sortandfilter/
Author: ekoneil
Date: Fri Jan 27 18:00:58 2006
New Revision: 373065
URL: http://svn.apache.org/viewcvs?rev=373065&view=rev
Log:
Add filter support to the data grid sort / filter sample.
Now, it's time to clean this up and add "real" sample data. :)
BB: self
Test: netui-samples clean / build pass
Added:
beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterByProperty.java (with props)
beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterPredicate.java (with props)
beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterPredicateCompanyName.java (with props)
beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterPredicateCustomerID.java (with props)
beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/SortByProperty.java (contents, props changed)
- copied, changed from r372916, beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/SortByPropertyEngine.java
Removed:
beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/SortByPropertyEngine.java
Modified:
beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/CustomerBean.java
beehive/trunk/samples/netui-samples/web/ui/datagrid/sortandfilter/Controller.java
Modified: beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/CustomerBean.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/CustomerBean.java?rev=373065&r1=373064&r2=373065&view=diff
==============================================================================
--- beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/CustomerBean.java (original)
+++ beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/CustomerBean.java Fri Jan 27 18:00:58 2006
@@ -17,8 +17,11 @@
*/
package org.apache.beehive.samples.netui.ui.datagrid.sortandfilter;
+import java.util.LinkedList;
+import java.util.List;
+
public class CustomerBean {
-
+
private int _customerId;
private String _companyName;
private String _contactName;
@@ -117,5 +120,21 @@
public void setFax(String fax) {
_fax = fax;
+ }
+
+ public static List<CustomerBean> createSampleData() {
+ LinkedList<CustomerBean> _customers = new LinkedList<CustomerBean>();
+ for(int i = 0; i < 20; i++) {
+ CustomerBean c = new CustomerBean();
+ c.setAddress(i + " Fake Street");
+ c.setCity("San Francisco");
+ c.setCompanyName("The Apache Software Founcation");
+ c.setContactName("JoJo Shabado " + i);
+ c.setContactTitle("Jo Coder");
+ c.setCountry("USA");
+ c.setCustomerId(i);
+ _customers.add(c);
+ }
+ return _customers;
}
}
Added: beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterByProperty.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterByProperty.java?rev=373065&view=auto
==============================================================================
--- beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterByProperty.java (added)
+++ beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterByProperty.java Fri Jan 27 18:00:58 2006
@@ -0,0 +1,55 @@
+/*
+ Copyright 2004-2006 The Apache Software Foundation.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+ $Header:$
+*/
+package org.apache.beehive.samples.netui.ui.datagrid.sortandfilter;
+
+import java.util.List;
+import java.util.LinkedList;
+
+import org.apache.beehive.netui.databinding.datagrid.api.filter.Filter;
+
+/**
+ * Simple, <b>demonstration-only</b> implementation of a filtering algorithm to show the use
+ * of the sort and filter functionality of the data grid.
+ */
+public class FilterByProperty {
+
+ private LinkedList<FilterPredicate> _filters = null;
+
+ public void addPredicate(FilterPredicate filterPredicate) {
+ if(_filters == null)
+ _filters = new LinkedList<FilterPredicate>();
+ _filters.add(filterPredicate);
+ }
+
+ public <T> List<T> filter(List<T> dataSet) {
+ if(dataSet == null || dataSet.size() == 0)
+ return null;
+
+ List<T> filtered = new LinkedList<T>();
+ for(T item : dataSet) {
+ boolean pass = true;
+ for(FilterPredicate filterPredicate : _filters) {
+ if(!filterPredicate.check(item))
+ pass = false;
+ }
+ if(pass)
+ filtered.add(item);
+ }
+ return filtered;
+ }
+}
Propchange: beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterByProperty.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterPredicate.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterPredicate.java?rev=373065&view=auto
==============================================================================
--- beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterPredicate.java (added)
+++ beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterPredicate.java Fri Jan 27 18:00:58 2006
@@ -0,0 +1,45 @@
+/*
+ Copyright 2004-2006 The Apache Software Foundation.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+ $Header:$
+*/
+package org.apache.beehive.samples.netui.ui.datagrid.sortandfilter;
+
+import org.apache.beehive.netui.databinding.datagrid.api.filter.FilterOperation;
+import org.apache.beehive.netui.databinding.datagrid.api.filter.FilterOperationHint;
+
+/**
+ *
+ */
+public abstract class FilterPredicate {
+
+ private FilterOperationHint _filterOperationHint;
+ private Object _value;
+
+ public FilterPredicate(FilterOperationHint filterOperation, Object value) {
+ _value = value;
+ _filterOperationHint = filterOperation;
+ }
+
+ public abstract boolean check(Object object);
+
+ protected FilterOperationHint getFilterOperationHint() {
+ return _filterOperationHint;
+ }
+
+ protected Object getValue() {
+ return _value;
+ }
+}
Propchange: beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterPredicate.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterPredicateCompanyName.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterPredicateCompanyName.java?rev=373065&view=auto
==============================================================================
--- beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterPredicateCompanyName.java (added)
+++ beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterPredicateCompanyName.java Fri Jan 27 18:00:58 2006
@@ -0,0 +1,41 @@
+/*
+ Copyright 2004-2006 The Apache Software Foundation.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+ $Header:$
+*/
+package org.apache.beehive.samples.netui.ui.datagrid.sortandfilter;
+
+import org.apache.beehive.netui.databinding.datagrid.api.filter.FilterOperationHint;
+
+/**
+ *
+ */
+public class FilterPredicateCompanyName
+ extends FilterPredicate {
+
+ public FilterPredicateCompanyName(FilterOperationHint filterOperation, Object companyName) {
+ super(filterOperation, companyName);
+ }
+
+ public boolean check(Object object) {
+ assert object instanceof CustomerBean;
+
+ CustomerBean customerBean = (CustomerBean)object;
+ if(getFilterOperationHint() == FilterOperationHint.CONTAINS)
+ return customerBean.getCompanyName().contains(getValue().toString());
+ else throw new IllegalStateException("Filter predicate does not support the operation " +
+ getFilterOperationHint().toString());
+ }
+}
Propchange: beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterPredicateCompanyName.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterPredicateCustomerID.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterPredicateCustomerID.java?rev=373065&view=auto
==============================================================================
--- beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterPredicateCustomerID.java (added)
+++ beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterPredicateCustomerID.java Fri Jan 27 18:00:58 2006
@@ -0,0 +1,41 @@
+/*
+ Copyright 2004-2006 The Apache Software Foundation.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+ $Header:$
+*/
+package org.apache.beehive.samples.netui.ui.datagrid.sortandfilter;
+
+import org.apache.beehive.netui.databinding.datagrid.api.filter.FilterOperationHint;
+
+/**
+ *
+ */
+public class FilterPredicateCustomerID
+ extends FilterPredicate {
+
+ public FilterPredicateCustomerID(FilterOperationHint filterOperation, Object value) {
+ super(filterOperation, value);
+ }
+
+ public boolean check(Object object) {
+ assert object instanceof CustomerBean;
+
+ CustomerBean customerBean = (CustomerBean)object;
+ if(getFilterOperationHint() == FilterOperationHint.EQUAL)
+ return customerBean.getCustomerId() == getValue();
+ else throw new IllegalStateException("Filter predicate does not support the operation " +
+ getFilterOperationHint().toString());
+ }
+}
\ No newline at end of file
Propchange: beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/FilterPredicateCustomerID.java
------------------------------------------------------------------------------
svn:eol-style = native
Copied: beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/SortByProperty.java (from r372916, beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/SortByPropertyEngine.java)
URL: http://svn.apache.org/viewcvs/beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/SortByProperty.java?p2=beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/SortByProperty.java&p1=beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/SortByPropertyEngine.java&r1=372916&r2=373065&rev=373065&view=diff
==============================================================================
--- beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/SortByPropertyEngine.java (original)
+++ beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/SortByProperty.java Fri Jan 27 18:00:58 2006
@@ -37,7 +37,7 @@
* for production applications -- it's just enables example that show how sorting and filtering work in the
* NetUI data grid.
*/
-public class SortByPropertyEngine {
+public class SortByProperty {
public List sort(Sort sort, List list) {
if (list == null || list.size() == 0)
@@ -84,7 +84,7 @@
Object value = null;
try {
- value = propAccessor.invoke(object, null);
+ value = propAccessor.invoke(object, (Object[])null);
}
catch (IllegalAccessException e) {
throw new IllegalStateException("Exception occurred invoking property getter \"" + propAccessor.getName() + "\"");
Propchange: beehive/trunk/samples/netui-samples/src/org/apache/beehive/samples/netui/ui/datagrid/sortandfilter/SortByProperty.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: beehive/trunk/samples/netui-samples/web/ui/datagrid/sortandfilter/Controller.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/samples/netui-samples/web/ui/datagrid/sortandfilter/Controller.java?rev=373065&r1=373064&r2=373065&view=diff
==============================================================================
--- beehive/trunk/samples/netui-samples/web/ui/datagrid/sortandfilter/Controller.java (original)
+++ beehive/trunk/samples/netui-samples/web/ui/datagrid/sortandfilter/Controller.java Fri Jan 27 18:00:58 2006
@@ -27,14 +27,16 @@
import org.apache.beehive.netui.databinding.datagrid.api.DataGridConfig;
import org.apache.beehive.netui.databinding.datagrid.api.DataGridStateFactory;
import org.apache.beehive.netui.databinding.datagrid.api.DataGridState;
+import org.apache.beehive.netui.databinding.datagrid.api.DataGridConfigFactory;
import org.apache.beehive.netui.databinding.datagrid.api.sort.Sort;
import org.apache.beehive.netui.databinding.datagrid.api.filter.FilterOperationHint;
import org.apache.beehive.netui.databinding.datagrid.api.filter.Filter;
import org.apache.beehive.netui.databinding.datagrid.api.filter.FilterTypeHint;
-import org.apache.beehive.netui.databinding.datagrid.api.filter.FilterOperation;
-import org.apache.beehive.netui.databinding.datagrid.runtime.sql.SQLSupport;
import org.apache.beehive.samples.netui.ui.datagrid.sortandfilter.CustomerBean;
-import org.apache.beehive.samples.netui.ui.datagrid.sortandfilter.SortByPropertyEngine;
+import org.apache.beehive.samples.netui.ui.datagrid.sortandfilter.SortByProperty;
+import org.apache.beehive.samples.netui.ui.datagrid.sortandfilter.FilterByProperty;
+import org.apache.beehive.samples.netui.ui.datagrid.sortandfilter.FilterPredicateCustomerID;
+import org.apache.beehive.samples.netui.ui.datagrid.sortandfilter.FilterPredicateCompanyName;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
@@ -51,12 +53,14 @@
extends PageFlowController {
private static final Log LOG = LogFactory.getLog(Controller.class);
- private static String GRID_NAME_CUSTOMERS = "customers";
+ private static final String GRID_NAME_CUSTOMERS = "customers";
+ private static final String FILTER_EXPRESSION_CUSTOMERID = "customerid";
+ private static final String FILTER_EXPRESSION_COMPANYNAME = "companyname";
private transient DataGridConfig _dataGridConfig = null;
private transient DataGridURLBuilder _urlBuilder = null;
- private LinkedList<CustomerBean> _customers = null;
- private CustomerOperations _customerOperations = null;
+ private List<CustomerBean> _customers = null;
+ private CustomerFilterForm _customerFilterForm = null;
private DataGridState _dataGridState = null;
@Jpf.Action
@@ -70,34 +74,53 @@
return decorateGridForward(new Forward("grid-page"), lookupCustomers());
}
- @Jpf.Action()
+ @Jpf.Action
public Forward sort() {
saveDataGridState();
return decorateGridForward(new Forward("grid-page"), lookupCustomers());
}
- @Jpf.Action(useFormBean="_customerOperations")
- public Forward filter(CustomerOperations customerOperations) {
+ @Jpf.Action(useFormBean="_customerFilterForm")
+ public Forward filter(CustomerFilterForm customerFilterForm) {
saveDataGridState();
return decorateGridForward(new Forward("grid-page"), lookupCustomers());
}
+ private Forward decorateGridForward(Forward forward, List<CustomerBean> customers) {
+ assert forward != null;
+ forward.addActionOutput("customers", customers);
+ return forward;
+ }
+
private List<CustomerBean> lookupCustomers() {
DataGridState dataGridState = DataGridStateFactory.getInstance(getRequest()).getDataGridState(GRID_NAME_CUSTOMERS);
List<CustomerBean> dataSet = _customers;
- final List sorts = dataGridState.getSortModel().getSorts();
- final List filters = dataGridState.getFilterModel().getFilters();
-
+ /* implement sorting */
+ final List<Sort> sorts = (List<Sort>)dataGridState.getSortModel().getSorts();
if(sorts != null && sorts.size() < 2) {
+
Sort sort = (Sort)sorts.get(0);
- SortByPropertyEngine sorter = new SortByPropertyEngine();
+ SortByProperty sorter = new SortByProperty();
dataSet = sorter.sort(sort, _customers);
}
- if(filters != null) {
- /* todo: implement filtering */
+ /* implement filtering */
+ if(_customerFilterForm != null) {
+ final List<Filter> filters = _customerFilterForm.getFilters();
+ if(filters != null && filters.size() > 0) {
+ FilterByProperty filterer = new FilterByProperty();
+ for(Filter filter : filters) {
+ if(filter.getFilterExpression().equals(FILTER_EXPRESSION_CUSTOMERID))
+ filterer.addPredicate(new FilterPredicateCustomerID(filter.getOperationHint(),
+ Integer.parseInt(filter.getValue().toString())));
+ if(filter.getFilterExpression().equals(FILTER_EXPRESSION_COMPANYNAME))
+ filterer.addPredicate(new FilterPredicateCompanyName(filter.getOperationHint(),
+ filter.getValue()));
+ }
+ dataSet = filterer.filter(dataSet);
+ }
}
return dataSet;
@@ -107,34 +130,21 @@
_dataGridState = DataGridStateFactory.getInstance(getRequest()).getDataGridState(GRID_NAME_CUSTOMERS);
}
- private Forward decorateGridForward(Forward forward, List<CustomerBean> customers) {
- assert forward != null;
- forward.addActionOutput("customers", customers);
- return forward;
- }
-
@Override
protected void onCreate() {
LOG.debug("page flow created");
- _customers = new LinkedList<CustomerBean>();
- for(int i = 0; i < 20; i++) {
- CustomerBean c = new CustomerBean();
- c.setAddress(i + " Fake Street");
- c.setCity("San Francisco");
- c.setCompanyName("The Apache Software Founcation");
- c.setContactName("JoJo Shabado " + i);
- c.setContactTitle("Jo Coder");
- c.setCountry("USA");
- c.setCustomerId(i);
- _customers.add(c);
- }
+ _customers = CustomerBean.createSampleData();
+
+ _dataGridConfig = DataGridConfigFactory.getInstance();
+
+ /* initialize the CustomerFilterForm page flow scoped form bean */
+ _customerFilterForm = new CustomerFilterForm();
+ _customerFilterForm.setDataGridConfig(_dataGridConfig);
}
- public static class CustomerOperations
+ public static class CustomerFilterForm
implements java.io.Serializable {
- private static final String FILTER_EXPRESSION_CUSTOMERID = "customerid";
- private static final String FILTER_EXPRESSION_CUSTOMERNAME = "companyname";
private String _customerId = null;
private String _companyName = null;
@@ -163,33 +173,36 @@
public List<Filter> getFilters() {
LinkedList<Filter> filters = new LinkedList<Filter>();
+ Filter filter = null;
if(_customerId != null && !_customerId.equals("")) {
- Filter filter = buildFilter(FILTER_EXPRESSION_CUSTOMERID, _customerId, FilterTypeHint.STRING);
+ filter = buildFilter(FILTER_EXPRESSION_CUSTOMERID,
+ _customerId,
+ FilterTypeHint.NUMERIC,
+ FilterOperationHint.EQUAL);
filters.add(filter);
}
if(_companyName != null && !_companyName.equals("")) {
- Filter filter = buildFilter(FILTER_EXPRESSION_CUSTOMERNAME, _companyName, FilterTypeHint.STRING);
+ filter = buildFilter(FILTER_EXPRESSION_COMPANYNAME,
+ _companyName,
+ FilterTypeHint.STRING,
+ FilterOperationHint.CONTAINS);
filters.add(filter);
}
return filters;
}
- private Filter buildFilter(String filterExpression, Object value, FilterTypeHint typeHint) {
+ private Filter buildFilter(String filterExpression,
+ Object value,
+ FilterTypeHint typeHint,
+ FilterOperationHint operationHint) {
+
Filter filter = _dataGridConfig.createFilter();
filter.setFilterExpression(filterExpression);
-
- /* todo: this lookup makes things tightly coupled to a query language */
- /* todo: operation lookup is hard */
- /*
- the fix is to use the FilterOperationHint on the Filter object itself as a way to
- bypass *requiring* the FilterOperation to be set
- */
- FilterOperation fOp = SQLSupport.mapFilterHintToOperation(FilterOperationHint.CONTAINS);
- filter.setOperation(fOp);
filter.setTypeHint(typeHint);
filter.setValue(value);
+ filter.setOperationHint(operationHint);
return filter;
}
}