📄 administrationbean.java
字号:
/*
* @author : Neelesh
* @Version : 1.0
*
* Development Environment : Oracle 9i JDeveloper
* Name of the File : AdministrationBean.java
* Creation/Modification History :
*
* Neelesh 26-Dec-2002 Created
*
*/
package oracle.otnsamples.vsm.services;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;
import javax.ejb.DuplicateKeyException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.NamingException;
import oracle.otnsamples.util.KeyFactory;
import oracle.otnsamples.util.MailContent;
import oracle.otnsamples.util.Mailer;
import oracle.otnsamples.util.ServiceLocator;
import oracle.otnsamples.util.Utilities;
import oracle.otnsamples.vsm.JAASManager;
import oracle.otnsamples.vsm.Constants;
import oracle.otnsamples.vsm.entities.CategoryAttributeLocal;
import oracle.otnsamples.vsm.entities.CategoryAttributeLocalHome;
import oracle.otnsamples.vsm.entities.CategoryAttributePK;
import oracle.otnsamples.vsm.entities.CategoryLocal;
import oracle.otnsamples.vsm.entities.CategoryLocalHome;
import oracle.otnsamples.vsm.entities.CategoryPK;
import oracle.otnsamples.vsm.entities.CustomerLocal;
import oracle.otnsamples.vsm.entities.CustomerLocalHome;
import oracle.otnsamples.vsm.entities.GuestbookLocal;
import oracle.otnsamples.vsm.entities.GuestbookLocalHome;
import oracle.otnsamples.vsm.entities.ShopDetailLocal;
import oracle.otnsamples.vsm.entities.ShopDetailLocalHome;
import oracle.otnsamples.vsm.entities.ShopDetailPK;
import oracle.otnsamples.vsm.entities.ShopLocal;
import oracle.otnsamples.vsm.entities.ShopLocalHome;
import oracle.otnsamples.vsm.services.data.Category;
import oracle.otnsamples.vsm.services.data.Guestbook;
import oracle.otnsamples.vsm.services.data.Shop;
import oracle.otnsamples.vsm.services.data.ShopDetail;
/**
* This is the implementation of Administration Service bean. The bean provides
* implementations for all the administrative services defined in the remote
* interface. The bean interacts with various local entity beans to retrieve
* and store data to the persistent store.The entity beans referenced in this
* bean are described in ejb-jar.xml
*/
public class AdministrationBean implements SessionBean {
/**
* Default create method
*/
public void ejbCreate() {
}
/**
* Container call back, called just after instance of this bean is activated
*/
public void ejbActivate() {
}
/**
* Container call back, called just before instance of this bean is
* passivated
*/
public void ejbPassivate() {
}
/**
* Container call back, called by container before it ends the life of the
* session object.
*/
public void ejbRemove() {
}
/**
* Set the associated session context. The container calls this method after
* the instance creation.
*
* @param ctx - A SessionContext interface for the instance.
*/
public void setSessionContext(SessionContext ctx) {
}
/**
* This is a business method, to find a category by its id
*
* @param <b>catID</b> category id
* @param <b>langID</b> language id
*
* @return <b>Category</b> - Category object
*
* @throws <b>CategoryException</b> If the category with the id does not
* exist
*/
public Category findCategory(String catID, String langID)
throws CategoryException {
try {
if(
catID == null || "".equals(catID.trim()) || langID == null ||
"".equals(langID.trim())) {
throw new CategoryException(
"Category id and language id cannot be null or empty",
"error.category.null");
}
// find category home
CategoryLocalHome home = getCategoryLocalHome();
// find the category
CategoryLocal categoryBean =
home.findByCategoryAndLanguage(catID, langID);
// create a value object and return it
return createCategoryValueObject(categoryBean);
} catch(FinderException ex) {
throw new CategoryException(
"Category with Id " + catID +
" does not exist", "error.category.notpresent");
} catch(Exception ex) {
ex.printStackTrace();
throw new CategoryException(
"Unable to find category because " +
ex.getMessage(), "error.category.unknown");
}
}
/**
* This is a business method, to modify a category in VSM.
*
* @param <b>cat</b> - Category object which contains updated details.
*
* @throws <b>CategoryException</b> If the new category could not be updated
*/
public void updateCategory(Category category) throws CategoryException {
try {
// sanity check
if(
category == null || category.getLangId() == null ||
category.getName() == null ||
"".equals(category.getLangId().trim()) ||
"".equals(category.getName().trim())) {
throw new CategoryException(
"Category data is empty or null.",
"error.category.null");
}
// check if there are other categories[with different id] with the same name
if(!category.getId().equals(checkForDuplicateCategoryNames(category))) {
throw new CategoryException(
"Category with the same name exists",
"error.category.duplicatename");
}
String[] attributes = category.getAttributes();
ArrayList newAttributePKList = new ArrayList();
// find category home
CategoryLocalHome home = getCategoryLocalHome();
// find the category object using the key
CategoryLocal categoryBean =
home.findByPrimaryKey(new CategoryPK(
category.getId(),
category.getLangId()));
// update the category entity using the value object
categoryBean.setCatName(category.getName());
// get the current set of attributes using CMR method
Collection attribCollection = categoryBean.getAttributes();
// Remove all attributes
Iterator attribIter = attribCollection.iterator();
CategoryAttributeLocal categoryAttribute = null;
CategoryAttributePK pk = null;
// this loop removes all the old attributes
ArrayList currentAttribs = new ArrayList();
// remove old attributes
while(attribIter.hasNext()) {
categoryAttribute = (CategoryAttributeLocal) attribIter.next();
//remove attribute from relation
attribIter.remove();
//remove attribute from persistence
categoryAttribute.remove();
}
// find attributes home
CategoryAttributeLocalHome attributeHome =
getCategoryAttributeLocalHome();
// Now add all the new attributes
if(attributes != null) {
for(int i = attributes.length - 1; i >= 0; i--) {
// create the new attribute
categoryAttribute =
attributeHome.create(
category.getId(), category.getLangId(),
attributes [ i ]);
// add the new attribute to the relation
categoryAttribute.setCategory(categoryBean);
}
}
} catch(Exception ex) {
ex.printStackTrace();
throw new CategoryException("Unable to update category because " +
ex.getMessage(),"error.category.unknown");
}
}
/**
* This is a business method, to add a new category to the VSM.
*
* @param <b>cat</b> - Category object which contains details of the new
* category
*
* @throws <b>CategoryException</b> If the new category could not be added
*/
public String addCategory(Category cat) throws CategoryException {
try {
// sanity check
if(
cat == null || cat.getLangId() == null || cat.getName() == null ||
"".equals(cat.getLangId().trim()) ||
"".equals(cat.getName().trim())) {
throw new CategoryException(
"Category data is empty or null.",
"error.category.null");
}
// Find Entity home
CategoryLocalHome home = getCategoryLocalHome();
if(checkForDuplicateCategoryNames(cat) != null) {
throw new CategoryException(
"Category with the same name exists",
"error.category.duplicatename");
}
// Get a new primary key
String catId = KeyFactory.getKey();
// Create new category
CategoryLocal newCat = home.create(catId, cat.getLangId(), cat.getName());
// Find category attribute entity home
CategoryAttributeLocalHome attributeHome =
getCategoryAttributeLocalHome();
// Now we need to set the attributes, get the attributes from supplied category
String[] attributes = cat.getAttributes();
if(attributes != null) {
// add new attributes to collection, after creating them
ArrayList attribCollection = new ArrayList();
for(int i = attributes.length - 1; i >= 0; i--) {
if(attributes [ i ] != null && !"".equals(attributes [ i ].trim())) {
attribCollection.add(attributeHome.create(
catId, cat.getLangId(),
attributes [ i ]));
}
}
// set the new set of attributes to the new category, to ensure proper
// relationship between the new category and its attributes
newCat.setAttributes(attribCollection);
}
return catId;
} catch(DuplicateKeyException ex) {
ex.printStackTrace();
throw new CategoryException("Category already exists","error.category.duplicatename");
} catch(Exception ex) {
ex.printStackTrace();
throw new CategoryException("Unable to add category because " +
ex.getMessage(),"error.category.unknown");
}
}
/**
* This is a business method, to update status of pending shop requests,
* categorised by language.
*
* @param <b>shopIDs</b> - A long array of shop ids whose status has changed
* @param <b>status</b> - A string array of new shop status, corresponds to
* shopIDs
* @param <b>langID</b> - Indicates the preferred language of the shop owner.
* status array should be a series of Ps,Rs or As, indicating a
* Pending request, a Rejected request and an Approved request
*
* @throws <b>ShopException</b> If shop status could not be modified
*/
public void manageShopRequests(
String[] shopIDs, String[] status,
String langID) throws ShopException {
try {
// find shop home
ShopLocalHome home = getShopLocalHome();
ShopLocal shop = null;
// for each id, find the shop and update status, send mail to the shop owner
for(int i = shopIDs.length - 1; i >= 0; i--) {
if(
status [ i ] != null &&
(
status [ i ].equals(Constants.APPROVED) ||
status [ i ].equals(Constants.REJECTED) ||
status [ i ].equals(Constants.PENDING)
)) {
shop = home.findByPrimaryKey(shopIDs [ i ]);
shop.setStatus(status [ i ]);
// notify the owner about the status of the request
sendMail(status [ i ], shop.getOwner());
// if the status is "R", indicating rejected, the shop should be removed
if(Constants.REJECTED.equalsIgnoreCase(status [ i ])) {
shop.remove();
} else if(Constants.APPROVED.equalsIgnoreCase(status [ i ])) {
// if the shop is approved, change the role
shop.getOwner().setRole("shopowner");
try{
JAASManager userManager= (JAASManager)ServiceLocator.getLocator().getService("java:/comp/env/UserManager");
userManager.dropUser(shop.getOwnerName());
userManager.addUser(shop.getOwnerName(),shop.getOwner().getPassword(),"shopowner");
} catch (Exception jaasex){
jaasex.printStackTrace();
}
}
}
}
} catch(Exception ex) {
ex.printStackTrace();
throw new ShopException("Unable to alter status because " +
ex.getMessage(),"error.shop.unknown");
}
}
/**
* This is a business method, to retrieve all entries in the guestbook for
* the given language.
*
* @param <b>langID</b> String language id
*
* @return <b>Guestbook[]</b> - An array of Guestbook objects returns null if
* there are no entries in the guestbook
*
* @throws <b>GuestbookException</b> If entries cannot be retrieved
*/
public Guestbook[] viewGuestbookEntries(String langID)
throws GuestbookException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -