📄 administrationbean.java
字号:
try {
// find guestbook entity home
GuestbookLocalHome home = getGuestbookLocalHome();
// find all entries in guestbook
Collection guestbookCollection = home.findByLanguage(langID);
// Iterate through the collection and create an array of guestbook value objects
Guestbook[] guestbookEntries =
new Guestbook[ guestbookCollection.size() ];
Iterator iter = guestbookCollection.iterator();
int i = 0;
GuestbookLocal guestbookEntry = null;
while(iter.hasNext()) {
guestbookEntry = (GuestbookLocal) iter.next();
// copy the values from the entity to the value object
guestbookEntries [ i ] =
new Guestbook(
guestbookEntry.getId(), guestbookEntry.getUserName(),
guestbookEntry.getEmail(), guestbookEntry.getRating(),
guestbookEntry.getComment(),
guestbookEntry.getEntryDate(), langID);
i++;
}
return guestbookEntries;
} catch(FinderException ex) {
return null;
} catch(Exception ex) {
throw new GuestbookException("Unable to get guestbook entries because " +
ex.getMessage(),"error.guestbook.unknown");
}
}
/**
* This is a business method, to remove an entry from the guestbook
*
* @param <b>guestbookID</b> - Id of the guestbook entry
*
* @throws <b>GuestbookException</b> If entries cannot be removed
*/
public void deleteGuestbookEntries(String guestbookID)
throws GuestbookException {
try {
// find home of guestbook entity
GuestbookLocalHome home = getGuestbookLocalHome();
// find the entry with the key and remove it
home.findByPrimaryKey(guestbookID).remove();
} catch(Exception ex) {
throw new GuestbookException("Unable to remove guestbook entries because " +
ex.getMessage(),"error.guestbook.unknown");
}
}
/**
* This is a business method, to retrieve all pending shop requests
*
* @param <b>langId</b> - Pending requests with this language id will be
* retrieved
*
* @return <b>Shop[]</b> - An array of Shop objects which are yet to be
* approved. returns null if there are no such shops
*
* @throws <b>ShopException</b> If shops cannot be retrieved
*/
public Shop[] getPendingRequests(String langID)
throws ShopException {
try {
// find shop home
ShopLocalHome home = getShopLocalHome();
ShopLocal shopBean = null;
// find all shops whose status is pending-indicated by "P"
Collection shopCollection = home.findByStatus(Constants.PENDING);
// create an array of Shop value objects using this collection
Iterator iter = shopCollection.iterator();
Shop[] shops = new Shop[ shopCollection.size() ];
Shop shop = null;
ShopDetailLocal shopDetailBean = null;
ShopDetailLocalHome shopDetailHome = getShopDetailLocalHome();
int i = 0;
while(iter.hasNext()) {
shopBean = (ShopLocal) iter.next();
shopDetailBean =
shopDetailHome.findByPrimaryKey(new ShopDetailPK(
shopBean.getId(),
langID));
shop = new Shop();
shop.addShopDetail(new ShopDetail(
shopDetailBean.getLangID(),
shopDetailBean.getShopName(),
shopDetailBean.getDescription()));
// set all attributes for this value object
shop.setUserName(shopBean.getOwnerName());
shop.setId(shopBean.getId().toString());
shop.setCategoryId(shopBean.getCatID());
shop.setRegDate(shopBean.getRegDate());
shop.setStatus(shopBean.getStatus());
shops [ i ] = shop;
i++;
}
return shops;
} catch(FinderException ex) {
ex.printStackTrace();
return null;
} catch(Exception ex) {
ex.printStackTrace();
throw new ShopException("Unable to get pending shop requests because " +
ex.getMessage(),"error.shop.unknown");
}
}
/**
* This is a business method, to get all categories in VSM
*
* @param <b>langID</b> - Categories with this language id will be retrieved
*
* @return <b>Category[]</b> - An array of Category objects
*
* @throws <b>CategoryException</b> If the categories could not be retrieved
*/
public Category[] getAllCategories(String langID)
throws CategoryException {
try {
// find category home
CategoryLocalHome home = getCategoryLocalHome();
// Reference to a category
CategoryLocal categoryBean = null;
// find all categories
Collection catCollection = home.findByLanguage(langID);
// Iterate through the collection of categories and create a value object
// for each category, and set them in a Category objects array
Iterator iter = catCollection.iterator();
Category[] cats = new Category[ catCollection.size() ];
int i = 0;
while(iter.hasNext()) {
categoryBean = (CategoryLocal) iter.next();
cats [ i ] = createCategoryValueObject(categoryBean);
i++;
}
return cats;
} catch(FinderException ex) {
ex.printStackTrace();
return new Category[ 0 ];
} catch(Exception ex) {
ex.printStackTrace();
throw new CategoryException("Unable to find categories because " +
ex.getMessage(),"error.category.unknown");
}
}
/*
* Method to get the home for GuestbookLocal bean
*/
private GuestbookLocalHome getGuestbookLocalHome()
throws NamingException {
return (GuestbookLocalHome) ServiceLocator.getLocator().getService("java:comp/env/ejb/GuestbookLocal");
}
/*
* Method to get the home for CustomerLocal bean
*/
private CustomerLocalHome getUserLocalHome() throws NamingException {
return (CustomerLocalHome) ServiceLocator.getLocator().getService("java:comp/env/ejb/UserLocal");
}
/*
* Method to get the home for CategoryAttributeLocal bean
*/
private CategoryAttributeLocalHome getCategoryAttributeLocalHome()
throws NamingException {
return (CategoryAttributeLocalHome) ServiceLocator.getLocator().getService("java:comp/env/ejb/CategoryAttributeLocal");
}
/*
* Method to get the home for ShopLocal bean
*/
private ShopLocalHome getShopLocalHome() throws NamingException {
return (ShopLocalHome) ServiceLocator.getLocator().getService("java:comp/env/ejb/ShopLocal");
}
/*
* Method to get the home for ShopDetailLocal bean
*/
private ShopDetailLocalHome getShopDetailLocalHome()
throws NamingException {
return (ShopDetailLocalHome) ServiceLocator.getLocator().getService("java:comp/env/ejb/ShopDetailLocal");
}
/*
* Method to get the home for CategoryLocal bean
*/
private CategoryLocalHome getCategoryLocalHome()
throws NamingException {
return (CategoryLocalHome) ServiceLocator.getLocator().getService("java:comp/env/ejb/CategoryLocal");
}
/*
* Creates a category value object from a category entity
*
* @param <b> categoryBean</b> Category entity
*
* @return <b>Category</b> a Category value object
*/
private Category createCategoryValueObject(CategoryLocal categoryBean) {
Category category = new Category();
category.setId(categoryBean.getId());
category.setName(categoryBean.getCatName());
//category.setCatID(categoryBean.getCatID());
Collection attribs = categoryBean.getAttributes();
String[] attributes = new String[ attribs.size() ];
Iterator attribIter = attribs.iterator();
int i = 0;
while(attribIter.hasNext()) {
attributes [ i ] =
((CategoryAttributeLocal) attribIter.next()).getLabel();
i++;
}
category.setAttributes(attributes);
category.setLangId(categoryBean.getLanguageID());
return category;
}
/*
* Returns the email id of the VSM administrator
*
* @return <b>String</b> -email id of VSM Administrator returns null if it
* cannot be found.
*/
private String getAdminEmail() {
try {
// find customer home,
CustomerLocalHome home = getUserLocalHome();
// find the ADMIN entity and return the email id
return home.findByPrimaryKey("admin").getEmail();
} catch(Exception ex) {
ex.printStackTrace();
return null;
}
}
/*
* Sends mail to the owner notifying the owner about shop approval/rejection
*
* @param <b>status</b> Status of the shop -A for approved, R for Rejected
* @param <b>owner</b> the User entity representing the owner
*/
private void sendMail(String status, CustomerLocal owner) {
try {
String message = null;
String subject = null;
// send a rejected mail if status is Rejected
Properties emailMessages = Utilities.loadParams("EmailMessages");
if(Constants.REJECTED.equalsIgnoreCase(status)) {
message = emailMessages.getProperty("admin.email.rejectmessage");
subject = emailMessages.getProperty("admin.email.rejectsubject");
} else {
if(Constants.APPROVED.equalsIgnoreCase(status)) {
// send an approved mail if status is A
message = emailMessages.getProperty("admin.email.approvalmessage");
subject = emailMessages.getProperty("admin.email.approvalsubject");
} else {
// do nothing if the status is something else
return;
}
}
// to address
String to = owner.getEmail();
// from address- this is the email id of VSM admin, indicated by
// the username "admin"
String from = getAdminEmail();
MailContent mailContent = new MailContent();
// set the from. to, subject and body.
mailContent.setFrom(from);
mailContent.setTo(to);
mailContent.setSubject(subject);
mailContent.setBody(message);
// User Mailer to send the mail.
Mailer.sendMail(mailContent);
} catch(Exception ex) {
ex.printStackTrace();
}
}
/*
* The method checks if there is another category with the same name as the
* supplied category.
*
* @param <b>cat</b> The category to be inserted/updated
*
* @return <b>String</b> null if there is no category with the same name and
* different id. The id of the matching category otherwise
*
* @throws CategoryException if there was an error while checking the
* categories.
*/
private String checkForDuplicateCategoryNames(Category cat)
throws CategoryException {
try {
// check if there is a category with the same name
Iterator iter =
getCategoryLocalHome().findByLanguage(cat.getLangId()).iterator();
CategoryLocal existingCat = null;
while(iter.hasNext()) {
existingCat = (CategoryLocal) iter.next();
// if there is a category with the same name,
if(cat.getName().equalsIgnoreCase(existingCat.getCatName())) {
return existingCat.getId();
}
}
} catch(FinderException ex) {
// there are no categories with same name
} catch(Exception ex) {
ex.printStackTrace();
throw new CategoryException(
"Unknown error when checking duplicate" +
" category names",
"error.category.checkduplicates");
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -