AdminFacadeEJB.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:6k
源码类别:
Java编程
开发平台:
Java
- package day21ex.adminfacade;
- import java.util.*;
- import java.rmi.*;
- import javax.rmi.*;
- import javax.naming.*;
- import javax.ejb.*;
- import day21ex.enrollment.*;
- import day21ex.order.*;
- import day21ex.orderlineitem.*;
- import day21ex.enrollmentcart.*;
- import day21ex.course.*;
- import day21ex.student.*;
- import day21ex.mailer.*;
- public class AdminFacadeEJB implements SessionBean {
- private SessionContext ctx;
- Context initialContext = null;
- public AdminFacadeEJB() {
- print("The container created this instance.n");
- }
- public Collection getOrdersByStatus(String status)
- throws AdminFacadeException {
- Collection collection = new ArrayList();
- OrderLocal order;
- StudentLocal student;
- try {
- OrderLocalHome home= getOrderLocalHome();
- Collection orders = home.findByStatus(status);
- Iterator it = orders.iterator();
- while( it != null && it.hasNext() ) {
- order = (OrderLocal) it.next();
- try {
- student = order.getStudent();
- collection.add( new OrderDetails( order.getOrderId(),
- student.getStudentId(), order.getOrderDate(),
- order.getAmount(), order.getStatus() ) );
- } catch (Exception e) {
- e.printStackTrace();
- System.err.println(e);
- }
- }
- } catch(FinderException fe) {
- print("FinderException while getOrdersByStatus :" +
- fe.getMessage() + "n" );
- throw new AdminFacadeException("Unable to find Orders"+
- " of status : " + status +
- fe.getMessage());
- } catch(NamingException ne) {
- print("NamingException while getOrdersByStatus :" +
- ne.getMessage() + "n");
- throw new AdminFacadeException("NamingException while looking "
- + "for Orders of status " + status +
- ne.getMessage());
- } catch(Exception e) {
- print("Exception while getOrdersByStatus :" +
- e.getMessage() + "n");
- throw new AdminFacadeException("Exception while looking "
- + "for Orders of status " + status +
- e.getMessage());
- }
- return collection;
- }
- public void approveOrder(String orderId)
- throws AdminFacadeException {
- try {
- OrderLocalHome home= getOrderLocalHome();
- OrderLocal order = home.findByPrimaryKey(orderId);
- order.setStatus("Approved");
- /* enroll the student in all the courses in the order */
- Collection lineItems = order.getLineItems();
- StudentLocal student = order.getStudent();
- EnrollmentLocalHome enrollmentHome =
- getEnrollmentLocalHome();
- Iterator it = lineItems.iterator();
- while( it.hasNext() ) {
- OrderLineItemLocal lineItem = (OrderLineItemLocal) it.next();
- CourseLocal course = lineItem.getCourse();
- EnrollmentLocal enrollment =
- enrollmentHome.create(student.getStudentId(), course.getCourseId());
- }
- try {
- MailerHome mailerHome = getMailerHome();
- Mailer mailer = mailerHome.create();
- String message = "Your order id:" + order.getOrderId() +
- " is approved for enrollment";
- print("Sending email to:" + student.getEmailAddress() +
- ":" + message);
- mailer.sendMail(student.getEmailAddress(), message);
- } catch(Exception e) {
- System.err.println("Could not send e-mail.");
- e.printStackTrace();
- }
- } catch(Exception e) {
- e.printStackTrace();
- print("Exception while approveOrder :" +
- e.getMessage() + "n");
- throw new AdminFacadeException("Exception while approveOrder :" +
- e.getMessage() + "n");
- }
- }
- private OrderLocalHome getOrderLocalHome() throws NamingException {
- InitialContext initial = new InitialContext();
- Object o = initial.lookup("day21ex/Order");
- return ((OrderLocalHome) o);
- }
- private MailerHome getMailerHome() throws NamingException {
- InitialContext initial = new InitialContext();
- Object o = initial.lookup("day21ex/Mailer");
- return ((MailerHome) o);
- }
- private EnrollmentLocalHome getEnrollmentLocalHome()
- throws NamingException {
- InitialContext initial = new InitialContext();
- Object o = initial.lookup("day21ex/EnrollmentLocal");
- return ((EnrollmentLocalHome) o);
- }
- public void setSessionContext(SessionContext ctx) {
- print("The container called the setSessionContext method ");
- print("to associate session bean instance with its context.n");
- this.ctx = ctx;
- }
- public void ejbCreate() throws CreateException {
- print("The container called the ejbCreate method.n");
- }
- public void ejbActivate() {
- print("This instance has just been reactivated.n");
- }
- public void ejbPassivate() {
- print("The container intends to passivate the instance.n");
- }
- public void ejbRemove() {
- print("This instance is in the process of being removed ");
- print("by the container.n");
- }
- void print ( String str ) {
- System.out.print(str);
- }
- }