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

📄 processpaymentbean.java

📁 This book shows you how to use JBoss to develop EJB projects.
💻 JAVA
字号:
package com.titan.processpayment;

import com.titan.domain.*;

import java.sql.*;

import javax.ejb.*;
import javax.annotation.Resource;
import javax.sql.DataSource;
import javax.ejb.EJBException;
import org.jboss.annotation.security.SecurityDomain;
import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;

@Stateless
@SecurityDomain("TitanIdentityDB")     
@RolesAllowed("AUTHORIZED_MERCHANT")
public class ProcessPaymentBean implements ProcessPaymentRemote, 
                                           ProcessPaymentLocal 
{
   
   final public static String CASH = "CASH";
   final public static String CREDIT = "CREDIT";
   final public static String CHECK = "CHECK";
    
   @Resource(mappedName="java:/DefaultDS") DataSource dataSource;

   @Resource(name="min") int minCheckNumber = 100;
     
   @PermitAll
   public boolean byCash(Customer customer, double amount)
      throws PaymentException 
   {
      return process(customer.getId(), amount, CASH, null, -1, null, null);
   }
    
   @RolesAllowed("CHECK_FRAUD_ENABLED")
   public boolean byCheck(Customer customer, CheckDO check, double amount)
      throws PaymentException 
   {
      if (check.checkNumber > minCheckNumber) 
      {
         return process(customer.getId(), amount, CHECK, 
                        check.checkBarCode, check.checkNumber, null, null);
      }
      else 
      {
         throw new PaymentException("Check number is too low. Must be at least "+minCheckNumber);
      }
   }
   public boolean byCredit(Customer customer, CreditCardDO card, 
                           double amount) throws PaymentException 
   {
      if (card.expiration.before(new java.util.Date())) 
      {
         throw new PaymentException("Expiration date has passed");
      }
      else 
      {
         return process(customer.getId(), amount, CREDIT, null,
                        -1, card.number, new java.sql.Date(card.expiration.getTime()));
      }
   }
   private boolean process(int customerID, double amount, String type, 
                           String checkBarCode, int checkNumber, String creditNumber, 
                           java.sql.Date creditExpDate) throws PaymentException 
   {
     
      Connection con = null;
        
      PreparedStatement ps = null;
     
      try 
      {
         con = dataSource.getConnection();
         ps = con.prepareStatement
            ("INSERT INTO payment (customer_id, amount, type,"+ 
             "check_bar_code,check_number,credit_number,"+
             "credit_exp_date) VALUES (?,?,?,?,?,?,?)");
         ps.setInt(1,customerID);
         ps.setDouble(2,amount);
         ps.setString(3,type);
         ps.setString(4,checkBarCode);
         ps.setInt(5,checkNumber);
         ps.setString(6,creditNumber);
         ps.setDate(7,creditExpDate);
         int retVal = ps.executeUpdate();
         if (retVal!=1) 
         {
            throw new EJBException("Payment insert failed");
         }         
         return true;
      } 
      catch(SQLException sql) 
      {
         throw new EJBException(sql);
      } 
      finally 
      {
         try 
         {
            if (ps != null) ps.close();
            if (con!= null) con.close();
         } 
         catch(SQLException se) 
         {
            se.printStackTrace();
         }
      }
   }
}

⌨️ 快捷键说明

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