home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / TRIAL / JBUILDER / JVISBRKR.Z / CreditCardIssuer.java < prev    next >
Encoding:
Java Source  |  1998-05-08  |  10.5 KB  |  280 lines

  1. /*
  2.  * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
  3.  * 
  4.  * This SOURCE CODE FILE, which has been provided by Borland as part
  5.  * of a Borland product for use ONLY by licensed users of the product,
  6.  * includes CONFIDENTIAL and PROPRIETARY information of Borland.  
  7.  *
  8.  * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
  9.  * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
  10.  * THE PRODUCT.
  11.  *
  12.  * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
  13.  * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
  14.  * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
  15.  * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
  16.  * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
  17.  * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
  18.  * CODE FILE.
  19.  */
  20. //--------------------------------------------------------------------------------------------------
  21. // CORBA Reference Application
  22. // Copyright (c) 1997 by Borland International, All Rights Reserved
  23. //
  24. // Contains logic necessary to issue credit cards.
  25. //--------------------------------------------------------------------------------------------------
  26.  
  27. package borland.reference.creditapproval.server;
  28.  
  29. import borland.sql.dataset.*;
  30. import borland.jbcl.dataset.*;
  31. import java.util.*;
  32. import borland.reference.creditapproval.CORBAInterface.*;
  33.  
  34. /**
  35. * CreditCardIssuer issues a credit card to a specified applicant.
  36. *
  37. * Credit Cards are issues using the following business logic:
  38. *
  39. * 1) Calculate Credit Card Number: Increment a database
  40. *   counter and add a constant
  41. *    New Number = (Credit Card Counter + 1) + 9000000000000000
  42. * 2) Calculate Expiration Date: Add 2 years to the current Month/Year
  43. *    Expiration Date = Current Month / (Current Year + 2)
  44. */
  45. public class CreditCardIssuer implements DataModule{
  46.   private static CreditCardIssuer myDM;
  47.   Database cliffhangerDb = new Database();
  48.  
  49.   // Dataset used to create new customer ID
  50.   QueryDataSet nextCustomerIDDataSet = new QueryDataSet();
  51.   ResourceBundle res = Res.getBundle("borland.reference.creditapproval.server.Res");
  52.  
  53.   public CreditCardIssuer() throws Exception {
  54.     try {
  55.       jbInit();
  56.     }
  57.     catch (Exception e) {
  58.       e.printStackTrace();
  59.       throw e;
  60.     }
  61.   }
  62.  
  63.   /**
  64.   *  Initialize Objects
  65.   */
  66.   private void jbInit() throws Exception{
  67.     cliffhangerDb.setConnection(new borland.sql.dataset.ConnectionDescriptor("jdbc:odbc:Cliffhanger", "SYSDBA", "masterkey", false, "sun.jdbc.odbc.JdbcOdbcDriver"));
  68.     cliffhangerDb.setSQLDialect(borland.sql.dataset.SQLDialect.INTERBASE);
  69.     nextCustomerIDDataSet.setQuery(new borland.sql.dataset.QueryDescriptor(cliffhangerDb, "SELECT NEXTID FROM spNextCustomerID", null, true, Load.ALL));
  70.   }
  71.  
  72.   /**
  73.   *  Connect/Disconnect to/from the database based on 'connect' parameter.
  74.   */
  75.   void connect(boolean open) throws DataSetException {
  76.     if (open) {
  77.       if (!cliffhangerDb.isOpen()) new TimedConnect(cliffhangerDb, 2, 8 );
  78.     }
  79.     else {
  80.       if (cliffhangerDb.isOpen()) {
  81.         try {
  82.           cliffhangerDb.closeConnection();
  83.         } catch (Exception e) {
  84.           System.out.println( e );
  85.         }
  86.       }
  87.     }
  88.   }
  89.  
  90.   static public CreditCardIssuer getDataModule() throws Exception {
  91.     if (myDM == null)
  92.       myDM = new CreditCardIssuer();
  93.     return myDM;
  94.   }
  95.  
  96.   /**
  97.   * Create an Expiration date and return it as a string in
  98.   * JDBC date escape format.
  99.   */
  100.   String generateExpirationDate() throws Exception {
  101.     java.util.GregorianCalendar calendar = new java.util.GregorianCalendar();
  102.     String expirationDate;
  103.     expirationDate = Integer.toString( calendar.get(java.util.Calendar.YEAR) + 2 ) +
  104.              "-" + Integer.toString( calendar.get(java.util.Calendar.MONTH) + 1 ) + "-01";
  105.     return expirationDate;
  106.   }
  107.  
  108.   /**
  109.   * Increment the database credit card counter and add a constant
  110.   * to produce a new credit card number.
  111.   */
  112.   String generateCardNumber() throws Exception {
  113.  
  114.     int nextCount = 0;
  115.  
  116.     // Issue an SQL statement to increment the Credit Card Number
  117.     java.sql.Statement statement = cliffhangerDb.createStatement() ;
  118.     statement.executeUpdate("UPDATE CARDNUMBER SET CARDNUMBER = CARDNUMBER + 1");
  119.  
  120.  
  121.     java.sql.ResultSet results = statement.executeQuery("SELECT CARDNUMBER FROM CARDNUMBER");
  122.  
  123.     // Move to first row in the result set! If a row does not
  124.     // exist, throw an exception.
  125.     if (results.next()) nextCount = results.getInt("CARDNUMBER");
  126.     else throw new Exception( res.getString("Database_error") );
  127.  
  128.     results.close();
  129.  
  130.     // Add the constant to generate a new, properly formed, Credit Card Number
  131.     String count = Integer.toString( nextCount );
  132.     String number = "9000000000000000";
  133.     number = number.substring( 0, (number.length() - count.length())) + count;
  134.  
  135.     return number;
  136.   }
  137.  
  138.   /**
  139.    * Method to format a customer name given the first name, middle initial,
  140.    * and last name.
  141.    */
  142.   private String formatCustomerName(String firstName, String mi, String lastName) {
  143.     // Format the customer name
  144.     if (mi.compareTo("") == 0)
  145.       return (firstName + " " + lastName);
  146.     else
  147.       return (firstName + " " + mi + ". " + lastName);
  148.   }
  149.  
  150.   /**
  151.   * Create an account record in the Cliffhanger Database for the new
  152.   * credit card holder.
  153.   */
  154.   public void generateAccountRecord(applicantInfoStruct appInfo,
  155.                               creditApprovalStruct creditCardInfo )
  156.                               throws Exception {
  157.  
  158.     // Call the stored procedure to get a new Customer ID
  159.     int customerID = getNextCustomerID();
  160.  
  161.     // Insert a new account record
  162.     String accountQuery = "INSERT INTO ACCOUNT (ID, CREDITCARDNUMBER, " +
  163.                    "EXPIRATIONDATE, LIMIT, " +
  164.                    "IDENTIFICATION, DOB, STATUS, BALANCE) " +
  165.                    "VALUES( ?, ?, ?, ?, ?, ?, 'OPEN', 0)";
  166.  
  167.  
  168.     java.sql.PreparedStatement newAccountStatement =
  169.                cliffhangerDb.createPreparedStatement( accountQuery );
  170.     // Populate the ? in the prepared statements with actual values.
  171.     // This relies on the JDBC driver to format data-types correctly,
  172.     // such as dates, for the back-end RDBMS.
  173.     newAccountStatement.setInt(     1, customerID);
  174.     newAccountStatement.setString(  2, creditCardInfo.creditCardNumber );
  175.     newAccountStatement.setDate(    3, java.sql.Date.valueOf( creditCardInfo.expirationDate ));
  176.     newAccountStatement.setDouble(  4, creditCardInfo.limit );
  177.     newAccountStatement.setString(  5, appInfo.PID );
  178.     newAccountStatement.setDate(    6, java.sql.Date.valueOf( appInfo.DOB ));
  179.  
  180.     newAccountStatement.execute();
  181.     newAccountStatement.close();
  182.  
  183.  
  184.     // Insert a new Customer Record
  185.     String customerQuery = "INSERT INTO CUSTOMER (ID, FIRSTNAME, MI, " +
  186.                    "LASTNAME, PHONE, ADDR1, ADDR2, CITY, " +
  187.                    "STATE, POSTALCODE, COUNTRY, SHIPNAME, SHIPADDR1, " +
  188.                    "SHIPADDR2, SHIPCITY, SHIPSTATE, SHIPPOSTALCODE, " +
  189.                    "SHIPCOUNTRY, FAX, EMAIL ) " +
  190.                    "VALUES( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " +
  191.                    "?, ?, ?, ?, ?, ?, ?, ?, '', '' )";
  192.  
  193.     java.sql.PreparedStatement newCustomerStatement =
  194.                cliffhangerDb.createPreparedStatement( customerQuery );
  195.  
  196.     newCustomerStatement.setInt(     1, customerID);
  197.     newCustomerStatement.setString(  2, appInfo.firstName);
  198.     newCustomerStatement.setString(  3, appInfo.MI );
  199.     newCustomerStatement.setString(  4, appInfo.lastName );
  200.     newCustomerStatement.setString(  5, appInfo.phone );
  201.     newCustomerStatement.setString(  6, appInfo.address1 );
  202.     newCustomerStatement.setString(  7, appInfo.address2 );
  203.     newCustomerStatement.setString(  8, appInfo.city );
  204.     newCustomerStatement.setString(  9, appInfo.state );
  205.     newCustomerStatement.setString( 10, appInfo.postalCode );
  206.     newCustomerStatement.setString( 11, appInfo.country );
  207.     newCustomerStatement.setString( 12, formatCustomerName( appInfo.firstName,
  208.                                             appInfo.MI,
  209.                                             appInfo.lastName ));
  210.     newCustomerStatement.setString( 13, appInfo.address1 );
  211.     newCustomerStatement.setString( 14, appInfo.address2 );
  212.     newCustomerStatement.setString( 15, appInfo.city );
  213.     newCustomerStatement.setString( 16, appInfo.state );
  214.     newCustomerStatement.setString( 17, appInfo.postalCode );
  215.     newCustomerStatement.setString( 18, appInfo.country );
  216.  
  217.     newCustomerStatement.execute();
  218.     newCustomerStatement.close();
  219.   }
  220.  
  221.   /**
  222.   * Issue a credit credit card to the specified applicant.
  223.   */
  224.   public void issueCreditCard(applicantInfoStruct appInfo,
  225.                               creditApprovalStruct creditCardInfo )
  226.                               throws Exception {
  227.  
  228.    try{
  229.  
  230.       // Turn off autoCommit so that all of the SQL statements
  231.       // in the folllowing method invocations are part of the
  232.       // same transaction.
  233.       cliffhangerDb.setAutoCommit( false );
  234.  
  235.       // Generate credit card information
  236.       creditCardInfo.creditCardNumber = generateCardNumber();
  237.       creditCardInfo.expirationDate   = generateExpirationDate();
  238.       generateAccountRecord( appInfo, creditCardInfo );
  239.  
  240.       // Commit the transaction
  241.       cliffhangerDb.commit();
  242.  
  243.     } catch( Exception e) {
  244.       System.err.println(e);
  245.  
  246.       // The transaction needs to be rolled-back
  247.       // to ensure all DB changes are discarded.
  248.       try{
  249.         cliffhangerDb.rollback();
  250.       } catch( Exception rollbackException ) {
  251.         System.out.println(rollbackException);
  252.       }
  253.       // Throw a new exception so that the caller is aware
  254.       // that an error occurred.
  255.       throw new Exception(res.getString("An_error_occurred") +
  256.                           res.getString("Please_try_again_"));
  257.     }
  258.   }
  259.  
  260.   /**
  261.    * Method to get the next customer ID.
  262.    * This calls a stored procedure on the database server spNextCustomerID
  263.    * that returns a single row with the next Customer ID generated by the server.
  264.    */
  265.   private int getNextCustomerID() throws Exception {
  266.     int result = 0;
  267.     try {
  268.       nextCustomerIDDataSet.open();
  269.       nextCustomerIDDataSet.refresh();
  270.       result = nextCustomerIDDataSet.getInt("NEXTID");
  271.       nextCustomerIDDataSet.close();
  272.     }
  273.     catch (Exception ex) {
  274.       ex.printStackTrace();
  275.       throw ex;
  276.     }
  277.     return result;
  278.   }
  279. }
  280.