📄 categorybean.java
字号:
package com.jsfabc.jsh.view.bean;
import com.jsfabc.jsh.model.bo.Article;
import com.jsfabc.jsh.model.bo.BuyBill;
import com.jsfabc.jsh.model.bo.Category;
import com.jsfabc.jsh.model.service.ArticleService;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.application.FacesMessage;
import com.jsfabc.jsh.model.exception.DbException;
import com.jsfabc.jsh.utils.MessageFactory;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.faces.model.SelectItem;
public class CategoryBean {
//保存用户的一些基本信息供后面使用
private Integer categoryId;
//所有类别
private List categorys;
//用品类别列表用于填充下拉列表
private List categoryList;
//用品列表
private List articleList;
//用于保存一个会话范围的类别
private Category category;
//一个类别的所有用品
private List articles;
//被选择的用品列表
private List selectedArticles;
//被选择的用品标识
private Integer [] articleIds;
//采购清单
private BuyBill buyBill;
//采购清单列表
private List buyBills;
//用品服务
private ArticleService articleService;
//构造函数
public CategoryBean() {
}
public Integer getCategoryId() {
return this.categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public Category getCategory() {
return this.category;
}
public void setCategory(Category category) {
this.category = category;
}
//类别的取得与设置
public List getCategorys(){
return categorys;
}
public void setCategorys(List newValue){
this.categorys=newValue;
}
public List getCategoryList(){
return categoryList;
}
public void setCategoryList(List newValue){
this.categoryList=newValue;
}
public List getArticleList(){
return articleList;
}
public void setArticleList(List newValue){
this.articleList=newValue;
}
public List getArticles(){
return articles;
}
public void setArticles(List newValue){
this.articles=newValue;
}
public List getSelectedArticles(){
return selectedArticles;
}
public void setSelectedArticles(List newValue){
this.selectedArticles=newValue;
}
public Integer [] getArticleIds(){
return this.articleIds;
}
public void setArticleIds(Integer [] newValue){
this.articleIds=newValue;
}
public BuyBill getBuyBill(){
return buyBill;
}
public void setBuyBill(BuyBill newValue){
this.buyBill=newValue;
}
public List getBuyBills(){
return buyBills;
}
public void setBuyBills(List newValue){
this.buyBills=newValue;
}
//创建日志对象
Log log=LogFactory.getLog(this.getClass());
//依赖注入
public void setArticleService(ArticleService newValue){
this.articleService=newValue;
}
public String categoryListAction(){
FacesContext facesContext= FacesContext.getCurrentInstance();
//添加类别列表,以便填充下拉列表。
//该列表也可用在应用程序初始化时保存在一个应用程序范围内的bean的方法实现,
//不过这样就会在更新类别项目后,要重启应用程序的服务器,让其重新初始化
//才能读到更新后的值,这里不采用这种方式
this.categoryList=new ArrayList();
try {
this.categorys=articleService.findAllCategorys();
for (int i=0; i<categorys.size(); i++) {
category=(Category)categorys.get(i);
this.categoryList.add(new SelectItem(category.getCategoryId().toString(),category.getCategoryName()));
}
return "success";
}
catch(DbException de){
//捕获DbException异常
FacesMessage msg=MessageFactory.getMessage
(facesContext,"error_database_error","");
msg.setSeverity(FacesMessage.SEVERITY_WARN);
facesContext.addMessage(null,msg);
return "failure";
}
catch(Exception e){
//捕获其他异常
FacesMessage msg=MessageFactory.getMessage
(facesContext,"error_unexpected","");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
facesContext.addMessage(null,msg);
return "failure";
}
}
public void categoryChanged(ValueChangeEvent event) {
//取得faces上下文
FacesContext facesContext= FacesContext.getCurrentInstance();
try{
//获得选取的类别
Category selectedCategory=new Category();
Integer categoryId=(Integer)event.getNewValue();
selectedCategory.setCategoryId(categoryId);
this.category=selectedCategory;
this.articles=articleService.findCategoryArticles(selectedCategory);
this.articleList=new ArrayList();
for (int i=0; i<articles.size(); i++) {
Article article=(Article)articles.get(i);
this.articleList.add(new SelectItem(article.getArticleId(),article.getArticleName()));
}
}
catch(DbException de){
//捕获DbException异常
FacesMessage msg=MessageFactory.getMessage
(facesContext,"error_database_error","");
msg.setSeverity(FacesMessage.SEVERITY_WARN);
facesContext.addMessage(null,msg);
}
catch(Exception e){
//捕获其他异常
FacesMessage msg=MessageFactory.getMessage
(facesContext,"error_unexpected","");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
facesContext.addMessage(null,msg);
}
}
//取得被选择的用品
public List getSelectedArticles(Integer [] ids) {
if (ids.length>0 && this.articles != null) {
Iterator ite = this.articles.iterator();
List selectedArticles=new ArrayList();
while(ite.hasNext()) {
Article article = (Article)ite.next();
for(int i=0;i<ids.length;i++){
if (ids[i].equals(article.getArticleId())) {
selectedArticles.add(article);
}
}
}
return selectedArticles;
}
return null;
}
public String showArticlesAction(){
this.selectedArticles=getSelectedArticles(articleIds);
return "next";
}
public String showRequisitionAction(){
//取得faces上下文
FacesContext facesContext= FacesContext.getCurrentInstance();
for(int i=0;i<selectedArticles.size();i++){
//作为申请的附件,购买清单
Article article=(Article)selectedArticles.get(i);
Double subTotalPrice=article.getCurrentPrice()*article.getRequisitionNumber();
article.setSubTotalPrice(subTotalPrice);
}
return "next";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -