⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adminfacadeejb.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);
   }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -