📄 netstoreejbimpl.java
字号:
package netstore.service.ejb;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import org.odmg.*;
import org.apache.ojb.odmg.*;
import netstore.businessobjects.CustomerBO;
import netstore.businessobjects.ItemBO;
import netstore.catalog.view.ItemDetailView;
import netstore.catalog.view.ItemSummaryView;
import netstore.customer.view.UserView;
import netstore.framework.exceptions.AccountLockedException;
import netstore.framework.exceptions.DatastoreException;
import netstore.framework.exceptions.ExpiredPasswordException;
import netstore.framework.exceptions.InvalidLoginException;
/**
* This is a simple Session Bean implementation of the Netstore service
*/
public class NetstoreEJBImpl implements SessionBean, INetstore {
private SessionContext ctx;
private Implementation odmg = null;
private Database db = null;
public UserView authenticate( String email, String password )
throws InvalidLoginException, ExpiredPasswordException,
AccountLockedException, DatastoreException {
// Query the database for a user that matches the credentials
List results = null;
try{
OQLQuery query = odmg.newOQLQuery( );
// Set the OQL select statement
String queryStr = "select customer from " + CustomerBO.class.getName( );
queryStr += " where email = $1 and password = $2";
query.create(queryStr);
// Bind the input parameters
query.bind( email );
query.bind( password );
// Retrieve the results
results = (List)query.execute( );
}catch( Exception ex ){
ex.printStackTrace( );
throw DatastoreException.datastoreError(ex);
}
// If no results were found, must be an invalid login attempt
if ( results.isEmpty( ) ){
throw new InvalidLoginException( );
}
// Should only be a single customer that matches the parameters
CustomerBO customer = (CustomerBO)results.get(0);
// Make sure the account is not locked
String accountStatusCode = customer.getAccountStatus( );
if ( accountStatusCode != null && accountStatusCode.equals( "L" ) ){
throw new AccountLockedException( );
}
// Populate the value object from the Customer business object
UserView userView = new UserView( );
userView.setId( customer.getId().toString() );
userView.setFirstName( getGBString(customer.getFirstName() ));
userView.setLastName(getGBString( customer.getLastName() ));
userView.setEmailAddress( customer.getEmail() );
userView.setCreditStatus( customer.getCreditStatus() );
return userView;
}
public List getFeaturedItems( ) throws DatastoreException {
List results = null;
try{
OQLQuery query = odmg.newOQLQuery( );
// Set the OQL select statement
query.create( "select featuredItems from " + ItemBO.class.getName( ) );
results = (List)query.execute( );
}catch( Exception ex ){
ex.printStackTrace( );
throw DatastoreException.datastoreError(ex);
}
List items = new ArrayList( );
Iterator iter = results.iterator( );
while (iter.hasNext( )){
ItemBO itemBO = (ItemBO)iter.next( );
ItemSummaryView newView = new ItemSummaryView( );
newView.setId( itemBO.getId().toString() );
newView.setName(getGBString( itemBO.getDisplayLabel() ));
newView.setUnitPrice( itemBO.getBasePrice() );
newView.setSmallImageURL( itemBO.getSmallImageURL() );
newView.setDescription(getGBString( itemBO.getDescription() ));
items.add( newView );
}
return items;
}
public ItemDetailView getItemDetailView( String itemId )
throws DatastoreException {
List results = null;
try{
OQLQuery query = odmg.newOQLQuery( );
// Set the OQL select statement
String queryStr = "select item from " + ItemBO.class.getName( );
queryStr += " where id = $1";
query.create(queryStr);
query.bind(itemId);
// Execute the query
results = (List)query.execute( );
}catch( Exception ex ){
ex.printStackTrace( );
throw DatastoreException.datastoreError(ex);
}
//
if (results.isEmpty( ) ){
throw DatastoreException.objectNotFound( );
}
ItemBO itemBO = (ItemBO)results.get(0);
// Build a ValueObject for the Item
ItemDetailView view = new ItemDetailView( );
view.setId( itemBO.getId().toString() );
view.setDescription(getGBString( itemBO.getDescription()));
view.setLargeImageURL( itemBO.getLargeImageURL() );
view.setName(getGBString( itemBO.getDisplayLabel()) );
view.setProductFeature(getGBString( itemBO.getFeature1() ));
view.setUnitPrice( itemBO.getBasePrice() );
view.setTimeCreated( new Timestamp(System.currentTimeMillis() ));
view.setModelNumber( itemBO.getModelNumber() );
return view;
}
/**
* Opens the database and prepares it for transactions.
*/
private void init() throws DatastoreException {
try{
// get odmg facade instance
odmg = OJB.getInstance();
db = odmg.newDatabase();
//open database
db.open("default", Database.OPEN_READ_WRITE);
}catch( Exception ex ){
System.out.println( ex );
throw DatastoreException.datastoreError(ex);
}
}
public void ejbCreate( ) throws CreateException {
try {
init( );
}catch ( DatastoreException e ) {
throw new CreateException(e.getMessage( ));
}
}
public void ejbRemove( ) {
try {
if (db != null) {
db.close( );
}
}catch ( ODMGException e ) {}
}
public void setSessionContext( SessionContext assignedContext ) {
ctx = assignedContext;
}
public void ejbActivate( ) {
// Nothing to do for a stateless bean
}
public void ejbPassivate( ) {
// Nothing to do for a stateless bean
}
private static String getGBString(String s){
try{
return new String(s.getBytes("ISO-8859-1"),"GB2312");
}catch(Exception e){return null;}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -