📄 profilemanagementbean.java
字号:
/*
* @author : Sujatha
* @Version : 1.0
*
* Development Environment : Oracle9i JDeveloper
* Name of the File : ProfileManagementBean.java
* Creation/Modification History :
*
* Sujatha 10-Dec-2002 Created
*
*/
package oracle.otnsamples.vsm.services;
// Java Utility classes
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.Properties;
import java.util.Date;
import java.util.Calendar;
// EJB imports
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.ObjectNotFoundException;
import javax.ejb.FinderException;
// EJB Home and local interfaces
import oracle.otnsamples.vsm.JAASManager;
import oracle.otnsamples.vsm.entities.CustomerLocalHome;
import oracle.otnsamples.vsm.entities.CustomerLocal;
import oracle.otnsamples.vsm.entities.CountryLocalHome;
import oracle.otnsamples.vsm.entities.CountryLocal;
import oracle.otnsamples.vsm.entities.CountryPK;
import oracle.otnsamples.vsm.entities.ShopLocalHome;
import oracle.otnsamples.vsm.entities.ShopDetailLocalHome;
import oracle.otnsamples.vsm.entities.ShopLocal;
// Credit card validation web service related import
import oracle.otnsamples.ws.CreditCardValidationServiceStub;
// Value objects imports
import oracle.otnsamples.vsm.services.data.Profile;
import oracle.otnsamples.vsm.services.data.Shop;
// OTN Utilities for finding services, sending mails, formatting dates and currency,
// generating primary key etc.
import oracle.otnsamples.util.MailContent;
import oracle.otnsamples.util.Mailer;
import oracle.otnsamples.util.ServiceLocator;
import oracle.otnsamples.util.KeyFactory;
import oracle.otnsamples.util.Utilities;
import oracle.otnsamples.vsm.Constants;
/**
* This is the implementation of Profile Management Service bean.
* The bean provides implementations for all the profile management 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 ProfileManagementBean implements SessionBean {
private SessionContext ctx ;
/**
* Initializes the bean
*/
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 <b>ctx</b> - A SessionContext interface for the instance.
*/
public void setSessionContext(SessionContext ctx) {
this.ctx = ctx;
}
/**
* This business method registers a new mall user
* @param <b>profile</b> - Information about the user to be registered
* @exception <b>ProfileException</b> if the user cannot be registered
*/
public void register( Profile profile ) throws ProfileException {
// Check if the profile information has been specified
if( profile == null || profile.getUserName().trim().equals("")) {
throw new ProfileException("Profile information is null", "error.profile.null");
}
// Check if the credit card expiry date is in the past. Raise exception if so.
// Parse the entered date
Date entry = Utilities.parse(profile.getCardExpiryDate(), "MM/yyyy");
// Get the current date and set the time to zero to enable only
// date comparison without time
Calendar temp = Calendar.getInstance();
temp.set(Calendar.AM_PM, Calendar.AM);
temp.set(Calendar.HOUR, 0);
temp.set(Calendar.MINUTE, 0);
temp.set(Calendar.SECOND, 0);
temp.set(Calendar.MILLISECOND, 0);
Date currentDate = temp.getTime();
if ( currentDate.after(entry) ) {
throw new ProfileException("Invalid credit card expiry date",
"error.profile.invalidccdate");
}
CustomerLocalHome userHome = null;
// Check if the username already exists
try {
userHome = getUserLocalHome();
CustomerLocal user = userHome.findByPrimaryKey(profile.getUserName());
throw new ProfileException("User Name already exists","error.profile.duplicateuser");
} catch( FinderException ex ) {
// User does not exist. Continue with registration
}
// Validate the credit card number specified by user using Web Service
try {
CreditCardValidationServiceStub ccServices =
new CreditCardValidationServiceStub();
ccServices.validateCard( profile.getCardNumber() );
} catch( Exception e) {
e.printStackTrace();
throw new ProfileException("Invalid credit card number",
"error.profile.invalidccnumber");
}
try {
// Encrypt (string reversal) the credit card number before storing the value
StringBuffer encryptedString = new StringBuffer( profile.getCardNumber() );
// Add the user information through the customer entity bean
CustomerLocal user = userHome.create( profile.getUserName(), profile.getFirstName(),
profile.getLastName(), profile.getEmail(),
profile.getAddress(), profile.getCity(),
profile.getState(), profile.getCountryID(),
new Integer(profile.getZip()), profile.getPhone(), "shopuser",
profile.getPassword(), profile.getCardProvider(),
encryptedString.reverse().toString(),
Utilities.parse(profile.getCardExpiryDate(), "MM/yyyy" ) );
user.setCountry(this.getCountryLocalHome().
findByPrimaryKey(new CountryPK(profile.getCountryID())));
JAASManager userManager= (JAASManager)ServiceLocator.getLocator().getService("java:/comp/env/UserManager");
userManager.addUser(profile.getUserName(),profile.getPassword(),"shopuser");
} catch( Exception ex ) {
ex.printStackTrace();
ctx.setRollbackOnly();
try{
JAASManager userManager= (JAASManager)ServiceLocator.getLocator().getService("java:/comp/env/UserManager");
userManager.dropUser(profile.getUserName());
} catch (Exception jaasex){
jaasex.printStackTrace();
}
if( ex instanceof ProfileException )
throw new ProfileException("Unable to register user because ",
((ProfileException)ex).getMessageCode());
else
throw new ProfileException( "Unable to register user because " + ex.getMessage(), "error.profile.registration" );
}
}
/**
* This business method retrieves the profile information of a given user
* @param <b>userName</b> - userName for which the profile information
* needs to be retrieved
* @return <b>Profile</b> - the profile information of the user
* @exception <b>ProfileException</b> if the profile cannot be retrieved
*/
public Profile getProfile( String userName ) throws ProfileException {
// Check if the username has been specified
if( userName == null || userName.trim().equals("") ) {
throw new ProfileException("User name is null", "error.profile.usernull");
}
Profile profile = null;
try {
// Look up the customer bean
CustomerLocalHome userHome = getUserLocalHome();
// Search if the name entered by the user exists in the users table
CustomerLocal user = userHome.findByPrimaryKey(userName);
// Create a new value object with the profile information
profile = new Profile( user.getUserName(), user.getFirstName(),
user.getLastName(), user.getEmail(),
user.getAddress(), user.getCity(),
user.getState(), user.getCtryID(),
user.getZip().toString(), user.getPhone(),
user.getRole(), user.getPassword(),
user.getCardProvider(), user.getCardNumber(),
Utilities.format(user.getCardExpiryDate(),"MM/yyyy"));
// Decrypt (string reversal) the credit card number before storing the value
StringBuffer decryptedString = new StringBuffer( profile.getCardNumber() );
profile.setCardNumber(decryptedString.reverse().toString());
} catch( Exception ex ) {
throw new ProfileException( "Unable to retrieve user profile because " +
ex.getMessage(), "error.profile.retrieval" );
}
// Return back the value object created.
return profile;
}
/**
* This business method updates the profile information of a user
* @param <b>profile</b> - New information of the user whose profile
* has to to updated
* @exception <b>ProfileException</b> if the profile information cannot
* be updated
*/
public void updateProfile(Profile profile) throws ProfileException {
// Check if profile information has been specified
if( profile == null ) {
throw new ProfileException("Profile information is null", "error.profile.null");
}
// Check if the credit card expiry date is in the past. Raise exception if so.
// Parse the entered date
Date entry = Utilities.parse(profile.getCardExpiryDate(), "MM/yyyy");
// Get the current date and set the time to zero to enable only
// date comparison without time
Calendar temp = Calendar.getInstance();
temp.set(Calendar.AM_PM, Calendar.AM);
temp.set(Calendar.HOUR, 0);
temp.set(Calendar.MINUTE, 0);
temp.set(Calendar.SECOND, 0);
temp.set(Calendar.MILLISECOND, 0);
temp.set(Calendar.DATE,1);
Date currentDate = temp.getTime();
if ( currentDate.after(entry) ) {
throw new ProfileException("Invalid credit card expiry date",
"error.profile.invalidccdate");
}
// Validate the credit card number using the credit card web service
try {
CreditCardValidationServiceStub ccServices = new CreditCardValidationServiceStub();
ccServices.validateCard( profile.getCardNumber() );
} catch( Exception ex) {
throw new ProfileException( "Invalid credit card number",
"error.profile.invalidccnumber");
}
try {
// Lookup the customer bean
CustomerLocalHome userHome = getUserLocalHome();
// Locate the customer bean using the username
CustomerLocal user = userHome.findByPrimaryKey(profile.getUserName());
// Update the customer bean using the values in the value object
user.setFirstName(profile.getFirstName());
user.setLastName(profile.getLastName());
user.setEmail(profile.getEmail());
user.setAddress(profile.getAddress());
user.setCity(profile.getCity());
user.setState(profile.getState());
user.setCtryID(profile.getCountryID());
user.setZip(new Integer(profile.getZip()));
user.setPhone(profile.getPhone());
user.setCardProvider(profile.getCardProvider());
user.setCountry(this.getCountryLocalHome().
findByPrimaryKey(new CountryPK(profile.getCountryID())));
user.setCardExpiryDate(Utilities.parse(profile.getCardExpiryDate(), "MM/yyyy" ));
// Encrypt (string reversal) the credit card number before storing the value
StringBuffer encryptedString = new StringBuffer( profile.getCardNumber() );
user.setCardNumber(encryptedString.reverse().toString());
} catch( Exception ex ) {
throw new ProfileException( "Unable to update user profile because " +
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -