List of usage examples for org.apache.commons.beanutils BeanToPropertyValueTransformer BeanToPropertyValueTransformer
public BeanToPropertyValueTransformer(String propertyName, boolean ignoreNull)
From source file:org.broadleafcommerce.core.web.catalog.taglib.SearchFilterItemTag.java
private void doProductMultiSelect(JspWriter out, List<Product> products) throws JspException, IOException { BeanToPropertyValueTransformer valueTransformer = new BeanToPropertyValueTransformer(property, true); BeanToPropertyValueTransformer displayTransformer; if (propertyDisplay != null) { displayTransformer = new BeanToPropertyValueTransformer(propertyDisplay, true); } else {//from w ww . j a v a2 s.c om displayTransformer = valueTransformer; } HashMap<Object, Integer> countMap = new HashMap<Object, Integer>(); HashMap<Object, Object> valueDisplayMap = new HashMap<Object, Object>(); for (Product product : products) { Object value = valueTransformer.transform(product); Object display = displayTransformer.transform(product); valueDisplayMap.put(value, display); Integer integer = countMap.get(value); if (integer == null) { countMap.put(value, new Integer(1)); } else { countMap.put(value, new Integer(integer + 1)); } } String propertyCss = property.replaceAll("[\\.\\[\\]]", "_"); out.println("<ul class='searchFilter-" + propertyCss + "'>"); for (Object value : countMap.keySet()) { Object display = valueDisplayMap.get(value); out.println("<li value='" + value + "'><input type=\"checkbox\" class=\"searchFilter-" + propertyCss + "Checkbox\" name=\"" + property + "\" value=\"" + value + "\"/> " + "<span class='searchFilter-" + propertyCss + "Display'>" + display + "</span>" + " <span class='searchFilter" + propertyCss + "-count'>(" + countMap.get(value).toString() + ")</span></li>"); } out.println("</ul>"); out.println("<script>" + " var " + propertyCss + "Checked = 0;\r\n" + " $('.searchFilter-" + propertyCss + "Checkbox').click(function() {\r\n " + " var value = $(this).attr('value');\r\n" + " var checkbox = $(this).find(':checkbox');\r\n" + " if (" + propertyCss + "Checked == 0) {\r\n" + " $('.searchFilter-" + propertyCss + " li').each(function(){$(this).addClass('searchFilterDisabledSelect')});\r\n" + " $(this).removeClass('searchFilterDisabledSelect');\r\n" + " checkbox.attr('checked',true);\r\n" + " " + propertyCss + "Checked++;\r\n" + " } else if (checkbox.attr('checked') == true) {\r\n" + " $(this).addClass('searchFilterDisabledSelect');\r\n" + " if (" + propertyCss + "Checked == 1) {\r\n" + " // unchecking the only checked category, so reactivate all categories\r\n" + " $('.searchFilter-" + propertyCss + " li').each(function(){$(this).removeClass('searchFilterDisabledSelect')});\r\n" + " } \r\n" + " checkbox.attr('checked',false);\r\n" + " " + propertyCss + "Checked--;\r\n" + " } else {\r\n" + " $(this).removeClass('searchFilterDisabledSelect');\r\n" + " checkbox.attr('checked',true);\r\n" + " " + propertyCss + "Checked++;\r\n" + " }\r\n" + " updateSearchFilterResults();\r\n" + " } );" + " $('.searchFilter-" + propertyCss + "Display').click(function() {\r\n " + " var value = $(this).attr('value');\r\n" + " var liObj = $(this).parent(); \r\n" + " var checkbox = liObj.find(':checkbox');\r\n" + " if (" + propertyCss + "Checked == 0) {\r\n" + " $('.searchFilter-" + propertyCss + " li').each(function(){liObj.addClass('searchFilterDisabledSelect')});\r\n" + " liObj.removeClass('searchFilterDisabledSelect');\r\n" + " checkbox.attr('checked',true);\r\n" + " " + propertyCss + "Checked++;\r\n" + " } else if (checkbox.attr('checked') == true) {\r\n" + " liObj.addClass('searchFilterDisabledSelect');\r\n" + " if (" + propertyCss + "Checked == 1) {\r\n" + " // unchecking the only checked category, so reactivate all categories\r\n" + " $('.searchFilter-" + propertyCss + " li').each(function(){liObj.removeClass('searchFilterDisabledSelect')});\r\n" + " } \r\n" + " checkbox.attr('checked',false);\r\n" + " " + propertyCss + "Checked--;\r\n" + " } else {\r\n" + " liObj.removeClass('searchFilterDisabledSelect');\r\n" + " checkbox.attr('checked',true);\r\n" + " " + propertyCss + "Checked++;\r\n" + " }\r\n" + " updateSearchFilterResults();\r\n" + " } );" + "</script>"); }
From source file:org.broadleafcommerce.core.web.catalog.taglib.SearchFilterItemTag.java
private void doSliderRange(JspWriter out) throws JspException, IOException { List<Product> products = ((SearchFilterTag) getParent()).getProducts(); Money min = null;/*from ww w . ja va2 s . c om*/ Money max = null; BeanToPropertyValueTransformer valueTransformer = new BeanToPropertyValueTransformer(property, true); for (Product product : products) { Money propertyObject = (Money) valueTransformer.transform(product); if (propertyObject == null) { min = new Money(0D); max = new Money(0D); } else { min = propertyObject.min(min); max = propertyObject.max(max); } } String propertyCss = property.replaceAll("[.\\[\\]]", "_"); out.println("<div id='searchFilter-" + propertyCss + "'></div>"); out.println("Range:"); out.println("<input type=\"text\" id=\"min-" + propertyCss + "\" name='min-" + property + "' value='" + min.getCurrency().getSymbol() + min.getAmount().toPlainString() + "'/> - "); out.println("<input type=\"text\" id=\"max-" + propertyCss + "\" name='max-" + property + "' value='" + max.getCurrency().getSymbol() + max.getAmount().toPlainString() + "'/> <br/>"); out.println(" <script type=\"text/javascript\">\r\n" + " $(function() {\r\n" + " $(\"#searchFilter-" + propertyCss + "\").slider({\r\n" + " range: true,\r\n" + " min: " + min.getAmount().toPlainString() + ", max: " + max.getAmount().toPlainString() + "," + " values: [" + min.getAmount().toPlainString() + "," + max.getAmount().toPlainString() + "]," + " slide: function(event, ui) {\r\n" + " $(\"#min-" + propertyCss + "\").val('" + min.getCurrency().getSymbol() + "' + ui.values[0] );\r\n" + " $(\"#max-" + propertyCss + "\").val('" + max.getCurrency().getSymbol() + "' + ui.values[1]);\r\n" + " }\r\n" + " });\r\n" + " });\r\n" + " $('#searchFilter-" + propertyCss + "').bind('slidechange', updateSearchFilterResults); \r\n" + " </script>"); }
From source file:org.broadleafcommerce.core.web.search.SearchFilterUtil.java
/** * filterProducts iterates over the products for each allowed parameter, filtering out products that do * not match the values passed in via the parameters argument. There are 2 ways that a product can be * filtered out, corresponding to the multiSelect and sliderRange displayTypes on {@link SearchFilterItemTag}. * For multiSelect items, the method will remove the product if the property specified in allowedParameters's * toString() method returns a String equal to one of the Strings in the corresponding String[] in parameters. * For sliderRange items, the property on the product must be of type {@link Money}. The product will be filtered * out if it's property is greater than the Money value parsed out of max-(property name) or smaller than the Money * value parsed from min-(property name) * @param products the list of products to filter * @param parameters the parameters passed to the controller. Generally request.getParameterMap() * @param allowedParameters an array of the allowed parameters to filter on *///from www . j a v a 2 s . c o m public static void filterProducts(List<Product> products, Map<String, String[]> parameters, String[] allowedParameters) { for (String parameter : allowedParameters) { BeanToPropertyValueTransformer reader = new BeanToPropertyValueTransformer(parameter, true); if (parameters.containsKey(parameter)) { // we're doing a multi-select for (Iterator<Product> itr = products.iterator(); itr.hasNext();) { Product product = itr.next(); if (!ArrayUtils.contains(parameters.get(parameter), reader.transform(product).toString())) { itr.remove(); } } } else if (parameters.containsKey("min-" + parameter)) { String minMoney = parameters.get("min-" + parameter)[0]; String maxMoney = parameters.get("max-" + parameter)[0]; Money minimumMoney = new Money(minMoney.replaceAll("[^0-9.]", "")); Money maximumMoney = new Money(maxMoney.replaceAll("[^0-9.]", "")); for (Iterator<Product> itr = products.iterator(); itr.hasNext();) { Product product = itr.next(); Money objectValue = (Money) reader.transform(product); if (objectValue.lessThan(minimumMoney) || objectValue.greaterThan(maximumMoney)) { itr.remove(); } } } } }