【Java开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了Java开源代码-ApplicationBean.java的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
/*
* JCatalog Project
*/
package catalog.view.bean;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
//
import javax.faces.model.SelectItem;
import javax.faces.FacesException;
//
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
//
import catalog.model.businessobject.Category;
/**
* The managed bean with application scope.
* <p>
* It is used as an application scope cache.
* In JSF, the properties are set by bean management facility.
*
* @author <a href="mailto:derek_3710167@qq.com">Derek Y. Shen</a>
*/
public class ApplicationBean extends BaseBean {
//the logger for this class
private Log logger = LogFactory.getLog(this.getClass());
//list of SelectItems for all categories
private List categorySelectItems;
//the number of products per page
private int productsPerPage;
//the uri for product image directory
private static String productImageDirUri;
//the uri for image upload result page
private static String imageUploadResultPage;
/**
* Default Constructor.
*
* @throws FacesException If internal error occurs while retrieves categories
*/
public ApplicationBean() {
this.categorySelectItems = new ArrayList();
this.logger.debug("ApplicationBean is created");
}
/**
* Initializes the ApplicationBean.
* <p>
* load all the categories.
*
* @see BaseBean#init()
*/
protected void init() {
try {
this.logger.debug("Initializes ApplicationBean");
List list = this.serviceLocator.getCatalogService().getAllCategories();
for (int i=0; i<list.size(); i++) {
Category c = (Category)list.get(i);
this.categorySelectItems.add(new SelectItem(c.getId(), c.getName(), c.getDescription()));
}
} catch (Exception e) {
String msg = "Could not initialize ApplicationBean " + e.toString();
this.logger.error(msg);
throw new FacesException(msg);
}
this.logger.debug("Application bean is initialized");
}
public List getCategorySelectItems() {
return this.categorySelectItems;
}
public int getProductsPerPage() {
return this.productsPerPage;
}
public void setProductsPerPage(int newProductsPerPage) {
this.productsPerPage = newProductsPerPage;
}
public static String getProductImageDirUri() {
return productImageDirUri;
}
public void setProductImageDirUri(String newProductImageDirUri) {
productImageDirUri = newProductImageDirUri;
this.logger.debug("productImageUri is set with value of " + newProductImageDirUri);
}
public static String getImageUploadResultPage() {
return imageUploadResultPage;
}
public void setImageUploadResultPage(String newImageUploadResultPage) {
imageUploadResultPage = newImageUploadResultPage;
this.logger.debug("imageUploadResultPage is set with value of " + newImageUploadResultPage);
}
public String getDummyVariable() {
return null;
}
/**
* Get the category name by category id.
*
* @param id the category id
* @return the category name associated with the category id
*/
public String getCategoryName(String id) {
if (id != null && this.categorySelectItems != null) {
Iterator ite = this.categorySelectItems.iterator();
while(ite.hasNext()) {
SelectItem i = (SelectItem)ite.next();
if (id.equals(i.getValue())) {
return i.getLabel();
}
}
}
return null;
}
}