📄 catalogmanagerbean.java
字号:
package com.jdon.estore.catalog;
import javax.ejb.*;
import com.jdon.servicelocator.ejb.ServiceLocator;
import com.jdon.controller.events.EventModel;
import com.jdon.estore.model.Category;
import com.jdon.estore.catalog.dao.CatalogDAO;
import com.jdon.sequence.SequenceGeneratorLocalHome;
import com.jdon.sequence.SequenceGeneratorLocal;
import com.jdon.controller.model.PageIterator;
import java.util.*;
import com.jdon.estore.JNDINames;
import org.apache.log4j.Logger;
public class CatalogManagerBean implements SessionBean {
private final static Logger logger = Logger.getLogger(CatalogManagerBean.class);
SessionContext sessionContext;
private CategoryHome chome;
private CategoryDetailsHome cdhome;
private SequenceGeneratorLocalHome sequenceHome;
private CatalogEJBLocalHome catalogEJBHome;
private int categoryAllCount = 0;
private Map pageIteratorCache = new HashMap();
public void ejbCreate() throws CreateException {
try {
ServiceLocator sl = new ServiceLocator();
chome = (CategoryHome) sl.getLocalHome(JNDINames.
CATEGORY_HOME);
cdhome = (CategoryDetailsHome) sl.getLocalHome(JNDINames.
CATEGORY_DETAILS_HOME);
sequenceHome = (SequenceGeneratorLocalHome)sl.getLocalHome(
JNDINames.SEQUENCEGENERATOR_HOME);
catalogEJBHome = (CatalogEJBLocalHome)sl.getLocalHome(JNDINames.CATALOG_EJBHOME);
} catch (Exception ex) {
logger.error("create error:" + ex);
throw new CreateException();
}
}
public int getNewId(String name) {
try {
SequenceGeneratorLocal seq = sequenceHome.create();
return seq.nextSequenceNumber(name);
} catch (Exception ex) {
throw new EJBException("Error generating id for : " + name + ex);
}
}
public void createCategory(EventModel em) throws Exception {
Category category = (Category) em.getModel();
try {
String Id = Integer.toString(getNewId(JNDINames.SEQUENCE_NAME));
CategoryLocal categoryLocal = chome.create(Id);
CategoryDetails categoryDetails = cdhome.create(Id, category.getName(),
category.getDescription());
//设置Category与CategoryDetails的1:1关系
categoryLocal.setCategoryDetails(categoryDetails);
//因为新增了新的记录,数据库记录总数变化,复位为0
categoryAllCount = 0;
category.setCatId(Id); //Id是自动生产的,放入EvenModel中。
} catch (Exception ex) {
logger.error(ex);
em.setErrors("db.error");
}
}
public void updateCategory(EventModel em) throws Exception {
Category category = (Category) em.getModel();
try {
logger.debug(" --> got categotyLocal by Id = " + category.getCatId());
CategoryLocal categoryLocal = chome.findByPrimaryKey(category.getCatId());
logger.debug(" --> got categotyDetails");
CategoryDetails categoryDetails = categoryLocal.getCategoryDetails();
categoryDetails.setName(category.getName());
categoryDetails.setDescription(category.getDescription());
} catch (Exception ex) {
logger.error(ex);
em.setErrors("db.error");
}
}
//if cacasde delete is true , this will delete all product;
public void deleteCategory(EventModel em) throws Exception {
Category category = (Category) em.getModel();
try {
chome.remove(category.getCatId());
//cacasde delete is true
//因为删除了新的记录,数据库记录总数变化,复位为0
categoryAllCount = 0;
} catch (Exception ex) {
logger.error(" --->> customer create error:" + ex);
em.setErrors("db.error");
}
}
public Category getCategoryById(String id) {
logger.debug(" looking for id=" + id);
try {
CatalogEJBLocal catalogEJB = catalogEJBHome.create();
return catalogEJB.getCategory(id);
} catch (FinderException ex) {
logger.warn(ex);
} catch (Exception ex) {
logger.error(ex);
}
return null;
}
public Category getCategoryByName(String name) {
logger.debug(" looking for name=" + name);
try {
CategoryDetails categoryDetails = cdhome.findByName(name);
Category category = getCategoryDeatils(categoryDetails);
return category;
} catch (FinderException ex) {
logger.warn(ex);
} catch (Exception ex) {
logger.error(ex);
}
return null;
}
public Collection getCategoryByAll() {
try {
Collection categoryDetailsList = cdhome.findByAll();
Iterator iter = categoryDetailsList.iterator();
Collection list = new ArrayList();
while (iter.hasNext()) {
CategoryDetails categoryDetails = (CategoryDetails) iter.next();
Category category = getCategoryDeatils(categoryDetails);
list.add(category);
}
return list;
} catch (FinderException ex) {
logger.warn(ex);
} catch (Exception ex) {
logger.error(ex);
}
return null;
}
private Category getCategoryDeatils(CategoryDetails categoryDetails) {
Category category = new Category();
category.setCatId(categoryDetails.getCatId());
category.setName(categoryDetails.getName());
category.setDescription(categoryDetails.getDescription());
return category;
}
public PageIterator getCategories(int start, int count) {
PageIterator pageIterator = null;
try {
String pIKey = getPIKey(start);//获得key
//首先从缓冲中获取
pageIterator = (PageIterator)pageIteratorCache.get(pIKey);
if (pageIterator == null){
//从数据库读取
CatalogEJBLocal catalogEJB = catalogEJBHome.create();
pageIterator = catalogEJB.getCategories(start, count);
pageIteratorCache.put(pIKey, pageIterator);
}else{
logger.debug("read pageIterator from cache ..........");
}
pageIterator.setAllCount(getCategoryAllCount());
} catch (Exception ex) {
logger.warn("getCategories error: " + ex);
}
return pageIterator;
}
private String getPIKey(int start) {
StringBuffer buffer = new StringBuffer("PageIterator");
buffer.append(start);
return buffer.toString();
}
public int getCategoryAllCount() {
try {
if (categoryAllCount == 0){
CatalogEJBLocal catalogEJB = catalogEJBHome.create();
categoryAllCount = catalogEJB.getCategoryAllCount();
}else{
logger.debug("read categoryAllCount from cache ..........");
}
} catch (Exception ex) {
logger.error("getCategoryAllCount() error");
}
return categoryAllCount;
}
public void ejbRemove() {
/**@todo Complete this method*/
}
public void ejbActivate() {
/**@todo Complete this method*/
}
public void ejbPassivate() {
/**@todo Complete this method*/
}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -