Env.:Spring 2.5 Hibernate 3 icefaces 1.7
I try to convert all the products items to product beans depends param_catelog id.
but I always get null value in converting method.
I use two backing bean 1.productbean 2.productlistbean
Here is part of backing bean
#backing bean-productbean
Code:
public class ProductBean extends BaseBean {
//the product id
private String id ;
//the product name
private String name;
//the product description
private String description;
//the product price
private double price;
//the product width
private double width;
//the product height
private double height;
//the category ids associated with the product
private Set productCategories;
//the category id selected by the user on the page
private List selectedCategoryIds;
.
.
.
.
.
public Set getCategoryIds() {
return this.productCategories;
}
public void setCategoryIds(Set newCategoryIds) {
this.productCategories = newCategoryIds;
if (this.productCategories != null) {
this.selectedCategoryIds = ViewUtils.convertToList(this.productCategories);
}
}
public List getSelectedCategoryIds() {
return this.selectedCategoryIds;
}
public void setSelectedCategoryIds(List newSelectedCategoryIds) {
this.selectedCategoryIds = newSelectedCategoryIds;
this.productCategories = ViewUtils.convertToSet(selectedCategoryIds);
}
#backing bean-productlistbean
Code:
public class ProductListBean extends BaseBean {
private static final int DEFAULT_PRODUCTS_PER_PAGE = 6;
private List productBeans;
private List currentProductBeans;
private Map productBeansMap;
private String currentCategoryId;
private String currentCategoryName;
private int productsPerPage;
private int totalPages;
private int pageNo;
public ProductListBean() {
this.productBeans = new ArrayList();
this.currentProductBeans = new ArrayList();
this.productBeansMap = new HashMap();
this.logger.debug("ProductListBean is created");
}
protected void init() {
try {
List products = this.serviceLocator.getCatalogService().getAllProducts();
//getCatalogService().getAllProducts();
log.info("products size = " + products.size());
for (int i=0; i<products.size(); i++) {
Product product = (Product)products.get(i);
ProductBean productBean = ProductBuilder.createProductBean(product);
productBean.setServiceLocator(this.serviceLocator);
this.productBeans.add(productBean);
}
this.currentProductBeans = this.productBeans;
this.setProductsPerPage();
this.buildProductBeansMap();
} catch (Exception e) {
String msg = "Could not initialize ProductListBean";
log.info("Could not initialize ProductListBean", e);
throw new FacesException(msg, e);
}
this.log.info("ProductListBean is initialized"+currentProductBeans);
}
public String searchByCategoryAction() {
this.currentCategoryId = FacesUtils.getRequestParameter(RequestParamNames.CATEGORY_ID);
String pageNoString = FacesUtils.getRequestParameter(RequestParamNames.PAGE_NO);
try {
this.pageNo = Integer.parseInt(pageNoString);
} catch (Exception e) {
this.pageNo = 1;
}
this.totalPages = 0;
this.currentProductBeans = new ArrayList();
this.logger.debug("searchByCategoryAction is invoked");
log.info("categoryId = " + this.currentCategoryId);
log.info("pageNo = " + this.pageNo);
if (this.currentCategoryId == null || this.currentCategoryId.equals("")) {
//get all products, no pagination
this.currentProductBeans = this.productBeans;
return NavigationResults.PRODUCT_LIST;
}
else {
//catalog
Map categoryProductBeans = (Map)this.productBeansMap.get(currentCategoryId);
log.info("categoryProductBeans Map = " + categoryProductBeans);
if (categoryProductBeans != null) {
this.totalPages = categoryProductBeans.size();
log.info("total pages = " + this.totalPages);
List productBeans = (List)categoryProductBeans.get(new Integer(this.pageNo));
log.info("List productbeans = " + this.productBeans.size());
if (productBeans != null) {
this.currentProductBeans = productBeans;
}
}
log.info("currentProductBeans size = " + this.currentProductBeans.size());
//set current category name
this.currentCategoryName = FacesUtils.getApplicationBean().getCategoryName(this.currentCategoryId);
log.info("currentCategoryName = " + this.currentCategoryName);
return NavigationResults.CATALOG;
}
}
private void buildProductBeansMap() {
log.info("buildProductBeansMap is invoked");
this.productBeansMap = new HashMap();
Map categoryProductsMap = new HashMap();
for (int i=0; i<this.productBeans.size(); i++) {
ProductBean product = (ProductBean)this.productBeans.get(i);
log.info("product_buildMap"+product.getId());
Set categoryIds = product.getCategoryIds();
log.info("product_categoryIds"+categoryIds);
Iterator ite = categoryIds.iterator();
while (ite.hasNext()) {
String categoryId = (String)ite.next();
if (! categoryProductsMap.containsKey(categoryId)) {
//new category
List list = new ArrayList();
list.add(product);
categoryProductsMap.put(categoryId, list);
}
else {
List list = (List) categoryProductsMap.get(categoryId);
list.add(product);
}
}
}
Iterator ite = categoryProductsMap.keySet().iterator();
while (ite.hasNext()) {
Object category = ite.next();
List productBeans = (List)categoryProductsMap.get(category);
this.productBeansMap.put(category, this.pagination
****log result****
Code:
2009-06-23 11:37:50,173: buildProductBeansMap is invoked
2009-06-23 11:37:50,173: product_buildMapL001
2009-06-23 11:37:50,173: product_categoryIds[]
2009-06-23 11:37:50,173: product_buildMapL002
2009-06-23 11:37:50,173: product_categoryIds[]
2009-06-23 11:37:50,173: product_buildMapL003
2009-06-23 11:37:50,173: product_categoryIds[]
2009-06-23 11:37:50,173: product_buildMapL004
2009-06-23 11:37:50,173: product_categoryIds[]
2009-06-23 11:37:50,173: product_buildMapL005
2009-06-23 11:37:50,173: product_categoryIds[]
2009-06-23 11:37:50,173: product_buildMapL006
2009-06-23 11:37:50,173: product_categoryIds[]
2009-06-23 11:37:50,173: product_buildMapL007
2009-06-23 11:37:50,173: product_categoryIds[]
2009-06-23 11:37:50,173: product_buildMapL008
2009-06-23 11:37:50,173: product_categoryIds[]
2009-06-23 11:37:50,173: ProductListBean is initialized[id=L001 name=Product1, id=L002 name=Product2, id=L003 name=Product3, id=L004 name=Product4, id=L005 name=Product5, id=L006 name=Product6, id=L007 name=Product7, id=L008 name=Product8]
2009-06-23 11:37:50,173: service locator is set
2009-06-23 11:37:50,173: categoryId = 1
2009-06-23 11:37:50,173: pageNo = 1
2009-06-23 11:37:50,173: categoryProductBeans Map = null
2009-06-23 11:37:50,173: currentProductBeans size = 0
2009-06-23 11:37:50,173: currentCategoryName = null
as you known, buildProductBeansMap array has null value. also categoryProductBeans Map = null
so looked the production bean and call ViewUtil pacage
***ViewUtil****
Code:
import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
public class ViewUtils {
/**
* Convert a list to a set.
*
* @param orig the list to be converted
* @return the set converted from the list
*/
public static Set convertToSet(List orig) {
Set result = new HashSet();
if (orig != null) {
Iterator ite = orig.iterator();
while (ite.hasNext()) {
result.add(ite.next());
}
}
return result;
}
/**
* Convert a set to a list.
* @param orig the set to be converted
* @return the list converted from the set
*/
public static List convertToList(Set orig) {
List result = new ArrayList();
if (orig != null) {
Iterator ite = orig.iterator();
while (ite.hasNext()) {
result.add(ite.next());
}
}
return result;
}
}
would you help me how to fix this problem . thanks in advances