📄 netstoreserviceimpl.java
字号:
package netstore.service;
import java.sql.Timestamp;
import java.util.List;
import java.util.ArrayList;
import netstore.catalog.view.ItemDetailView;
import netstore.catalog.view.ItemSummaryView;
import netstore.framework.security.IAuthentication;
import netstore.customer.view.UserView;
import netstore.businessobjects.*;
// Import the exceptions used
import netstore.framework.exceptions.DatastoreException;
import netstore.framework.exceptions.InvalidLoginException;
import netstore.framework.exceptions.ExpiredPasswordException;
import netstore.framework.exceptions.AccountLockedException;
import javax.servlet.ServletContext;
// Import the implementation specific packages
import org.odmg.*;
import org.apache.ojb.odmg.*;
public class NetstoreServiceImpl implements INetstoreService{
ServletContext servletContext = null;
// Implementation specific references
Implementation odmg = null;
Database db = null;
/**
* Create the service, which includes initializing the persistence
* framework.
*/
public NetstoreServiceImpl() throws DatastoreException {
super();
init();
}
public void setServletContext( ServletContext ctx ){
this.servletContext = ctx;
}
public ServletContext getServletContext(){
return servletContext;
}
/**
* Return a list of items that are featured.
*/
public List getFeaturedItems() throws DatastoreException {
// Start a transaction
Transaction tx = odmg.newTransaction();
tx.begin();
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();
tx.commit();
}catch( Exception ex ){
// Rollback the transaction
tx.abort();
ex.printStackTrace();
throw DatastoreException.datastoreError(ex);
}
int size = results.size();
List items = new ArrayList();
for( int i = 0; i < size; i++ ){
ItemBO itemBO = (ItemBO)results.get(i);
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;
}
/**
* Return an detailed view of an item based on the itemId argument.
*/
public ItemDetailView getItemDetailView( String itemId )
throws DatastoreException{
// Start a transaction
Transaction tx = odmg.newTransaction();
tx.begin();
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 transaction
results = (List)query.execute();
tx.commit();
}catch( Exception ex ){
// Rollback the transaction
tx.abort();
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;
}
/**
* Authenticate the user's credentials and either return a UserView for the
* user or throw one of the security exceptions.
*/
public UserView authenticate(String email, String password) throws
InvalidLoginException,ExpiredPasswordException,AccountLockedException,
DatastoreException {
// Start a transaction
Transaction tx = odmg.newTransaction();
tx.begin();
// 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 and commit the transaction
results = (List)query.execute();
tx.commit();
}catch( Exception ex ){
// Rollback the transaction
tx.abort();
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;
}
/**
* Log the user out of the system.
*/
public void logout(String email){
// Do nothing with right now, but might want to log it for auditing reasons
}
public void destroy(){
// Do nothing for this example
}
/**
* Opens the database and prepares it for transactions
*/
private void init() throws DatastoreException {
// get odmg facade instance
odmg = OJB.getInstance();
db = odmg.newDatabase();
//open database
try{
db.open("default", Database.OPEN_READ_WRITE);
}catch( Exception ex ){
System.out.println( ex );
throw DatastoreException.datastoreError(ex);
}
}
/*
* test NetstoreServiceImpl
*/
public static void main(String args[])throws Exception{
NetstoreServiceImpl service=new NetstoreServiceImpl();
List l=service.getFeaturedItems();
ItemSummaryView view=( ItemSummaryView)l.get(0);
System.out.println(view.getId());
ItemDetailView view2= service.getItemDetailView("110");
System.out.println(view2.getDescription()+";"+view2.getProductFeature());
UserView view3=service.authenticate("weiqin@yahoo.com","weiqin");
System.out.println(view3.getFirstName()+view3.getLastName());
}
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 + -