📄 productbean.java
字号:
package cn.hxex.library.view.bean;
import java.util.ArrayList;
import java.util.List;
import cn.hxex.library.exception.LibraryException;
import cn.hxex.library.model.Category;
import cn.hxex.library.model.Product;
import cn.hxex.library.view.util.FacesUtils;
import cn.hxex.library.view.util.ViewUtils;
public class ProductBean extends BaseBean
{
private String id;
private String name;
private String description;
private String categoryId;
private List productBeans = new ArrayList();
public String getCategoryId()
{
return categoryId;
}
public void setCategoryId(String categoryId)
{
this.categoryId = categoryId;
}
public String getDescription()
{
return description;
}
/**
* @param description
* The description to set.
*/
public void setDescription(String description)
{
this.description = description;
}
/**
* @return Returns the id.
*/
public String getId()
{
return id;
}
/**
* @param id
* The id to set.
*/
public void setId(String id)
{
this.id = id;
}
/**
* @return Returns the name.
*/
public String getName()
{
return name;
}
/**
* @param name
* The name to set.
*/
public void setName(String name)
{
this.name = name;
}
/**
* @return Returns the productBeans.
*/
public List getProductBeans()
{
return productBeans;
}
/**
* @param productBeans
* The productBeans to set.
*/
public void setProductBeans(List productBeans)
{
this.productBeans = productBeans;
}
protected void init()
{
try
{
queryProducts();
}
catch( LibraryException ex )
{
}
}
/**
* Backing bean action to create a new product.
*
* @return the navigation result
*/
public String createAction()
{
this.logger.info("Product createAction is invoked!");
try
{
Product product = convertToProduct();
Category category = this.getServiceLocator().getCategoryService()
.getCategory(this.categoryId);
product.setCategory(category);
this.getServiceLocator().getProductService().saveProduct(product);
// remove the productBean inside the cache
this.logger.info("remove ProductBean from cache");
FacesUtils.resetManagedBean(BeanNames.PRODUCT_BEAN);
String params[] = { this.name };
FacesUtils.addInfoMessageById("productBean_create_product_success",
params);
return NavigationResults.SUCCESS;
}
catch (LibraryException e)
{
FacesUtils.addErrorMessageById(e.getMessage());
return NavigationResults.RETRY;
}
}
public String editAction()
{
if (this.id != null)
{
try
{
Product product = this.getServiceLocator().getProductService()
.getProduct(this.id);
converToProductBean(this, product);
return NavigationResults.PRODUCT_EDIT;
}
catch (LibraryException ex)
{
FacesUtils.addErrorMessageById(ex.getMessage());
}
}
return NavigationResults.PRODUCT_LIST;
}
public String updateAction()
{
try
{
Product product = convertToProduct();
Category category = this.getServiceLocator().getCategoryService()
.getCategory(this.categoryId);
product.setCategory(category);
this.getServiceLocator().getProductService().updateProduct(product);
String params[] = { this.name };
FacesUtils.addInfoMessageById("productBean_update_product_success",
params);
queryProducts();
return NavigationResults.PRODUCT_LIST;
}
catch (LibraryException e)
{
FacesUtils.addErrorMessageById(e.getMessage());
return NavigationResults.RETRY;
}
}
public String deleteAction()
{
this.logger.info("Product deleteAction is invoked!");
try
{
this.getServiceLocator().getProductService().deleteProduct(this.id);
FacesUtils.addInfoMessageById("productBean_delete_product_success");
queryProducts();
}
catch (LibraryException ex)
{
FacesUtils.addErrorMessageById(ex.getMessage());
}
return NavigationResults.PRODUCT_LIST;
}
/**
* Backing bean action to search productBeans by category.
*
* @return the navigation result
*/
public String listByCategoryAction()
{
this.logger.info("Product listByCategoryAction is invoked!");
return NavigationResults.PRODUCT_LIST;
}
private Product convertToProduct()
{
Product product = new Product();
product.setId(this.id);
product.setName(this.name);
product.setDescription(this.description);
return product;
}
public static ProductBean converToProductBean(ProductBean productBean,
Product product)
{
productBean.setId(product.getId());
productBean.setName(product.getName());
productBean.setDescription(product.getDescription());
productBean.setCategoryId(product.getCategory().getId());
return productBean;
}
private void queryProducts() throws LibraryException
{
if( this.categoryId==null )
return;
Category category = this.getServiceLocator().getCategoryService().getCategory(categoryId);
this.setProductBeans( ViewUtils.convertToList( category.getProducts() ) );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -