home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / TRIAL / JBUILDER / JVISBRKR.Z / CreditHistoryRandomData.java < prev    next >
Encoding:
Java Source  |  1998-05-08  |  11.5 KB  |  270 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 to generate random data for a specified applicant.
  25. //--------------------------------------------------------------------------------------------------
  26.  
  27. package borland.reference.creditapproval.server;
  28.  
  29. import borland.sql.dataset.*;
  30. import borland.jbcl.dataset.*;
  31. import borland.jbcl.control.*;
  32. import java.util.*;
  33. import borland.reference.creditapproval.CORBAInterface.*;
  34.  
  35. /**
  36. * CreditHistoryRandomData creates simple random data for an applicant.
  37. * This random data includes:
  38. *  1) Credit History Record
  39. *  2) Several Account Records
  40. */
  41. public class CreditHistoryRandomData implements DataModule{
  42.   private static CreditHistoryRandomData myDM;
  43.   private Database acmeDb;
  44.   ResourceBundle res = Res.getBundle("borland.reference.creditapproval.server.Res");
  45.  
  46.   public CreditHistoryRandomData() {
  47.     try {
  48.       jbInit();
  49.     }
  50.     catch (Exception e) {
  51.       e.printStackTrace();
  52.     }
  53.   }
  54.  
  55.   private void jbInit() throws Exception{
  56.   }
  57.  
  58.   static public CreditHistoryRandomData getDataModule() {
  59.     if (myDM == null)
  60.       myDM = new CreditHistoryRandomData();
  61.     return myDM;
  62.   }
  63.  
  64.   /**
  65.   * Use the HISTORYID table to generate a unique history ID.
  66.   * If a row does not exist in this, one is inserted.
  67.   */
  68.   private int generateHistoryId() throws Exception {
  69.  
  70.     int historyId, count;
  71.  
  72.     java.sql.Statement countStatement = acmeDb.createStatement() ;
  73.     java.sql.ResultSet countResults = countStatement.executeQuery("SELECT COUNT(*) FROM HISTORYID");
  74.  
  75.     // Move to first row in the result set! If a row does not
  76.     // exist, throw an exception.
  77.     if (countResults.next()) count = countResults.getInt("COUNT");
  78.     else throw new Exception( res.getString("History_ID_Error") );
  79.  
  80.     // If no rows exist, insert one now so that it can be used to generate a unique ID
  81.     if (count < 1 ) countStatement.executeUpdate("INSERT INTO HISTORYID VALUES(0)");
  82.  
  83.     // Increment the HISTORYID to obtain a unique number.
  84.     java.sql.Statement statement = acmeDb.createStatement() ;
  85.     statement.executeUpdate("UPDATE HISTORYID SET HISTORYID = HISTORYID + 1");
  86.     java.sql.ResultSet results = statement.executeQuery("SELECT HISTORYID FROM HISTORYID");
  87.     if (results.next()) historyId = results.getInt("HISTORYID");
  88.     else throw new Exception( res.getString("History_ID_Error") );
  89.  
  90.     results.close();
  91.  
  92.     return historyId;
  93.   }
  94.  
  95.   /**
  96.   * Creates a record in the Credit History table for the specified applicant.
  97.   */
  98.   private void insertCreditHistoryRecord(applicantInfoStruct appInfo, int historyId )
  99.     throws Exception {
  100.  
  101.     String query = "INSERT INTO CREDITHISTORY (HISTORYID, " +
  102.                    "FIRSTNAME, MI, LASTNAME, DOB, PHONE, ADDR1, ADDR2, CITY, STATE, " +
  103.                    "POSTALCODE, COUNTRY, IDENTIFICATION ) " +
  104.                    "VALUES( ?, ?, ?, ?, ?, ?, " +
  105.                    "?, ?, ?, ?, ?, ?, ?)";
  106.  
  107.     java.sql.PreparedStatement newRecordStatement =
  108.                acmeDb.createPreparedStatement( query );
  109.  
  110.     // Populate the ? in the prepared statement with actual values.
  111.     // This relies on the JDBC driver to format data-types correctly,
  112.     // such as dates, for the back-end RDBMS.
  113.     newRecordStatement.setInt(     1, historyId);
  114.     newRecordStatement.setString(  2, appInfo.firstName);
  115.     newRecordStatement.setString(  3, appInfo.MI );
  116.     newRecordStatement.setString(  4, appInfo.lastName );
  117.     newRecordStatement.setDate(    5, java.sql.Date.valueOf( appInfo.DOB ));
  118.     newRecordStatement.setString(  6, appInfo.phone );
  119.     newRecordStatement.setString(  7, appInfo.address1 );
  120.     newRecordStatement.setString(  8, appInfo.address2 );
  121.     newRecordStatement.setString(  9, appInfo.city );
  122.     newRecordStatement.setString( 10, appInfo.state );
  123.     newRecordStatement.setString( 11, appInfo.postalCode );
  124.     newRecordStatement.setString( 12, appInfo.country );
  125.     newRecordStatement.setString( 13, appInfo.PID );
  126.  
  127.     newRecordStatement.execute();
  128.     newRecordStatement.close();
  129.  
  130.   }
  131.  
  132.   /**
  133.   *  Creates random accounts based on the applicants monthly income.
  134.   */
  135.   private void generateRandomAccounts( applicantInfoStruct appInfo,
  136.                int historyId ) throws Exception {
  137.  
  138.     int institutionCount;
  139.     java.util.Random randomNumber = new java.util.Random();
  140.  
  141.     java.sql.Statement statement = acmeDb.createStatement() ;
  142.     java.sql.ResultSet results = statement.executeQuery("SELECT INSTITUTIONID FROM INSTITUTIONID");
  143.  
  144.     if (results.next()) institutionCount = results.getInt("INSTITUTIONID");
  145.     else throw new Exception( res.getString("Random_accounts") );
  146.  
  147.     // Generate a random percentage of total spending in the range of 30%- 50%
  148.     double maxPercentage = randomNumber.nextDouble() * 0.20 + 0.25;
  149.     double percentLeft = maxPercentage - appInfo.rentMortgagePayment / appInfo.monthlyIncome;
  150.     if (percentLeft <= 0) return;
  151.     double monthlyPaymentsLeft = appInfo.monthlyIncome * percentLeft;
  152.     double carPayment = (int) (monthlyPaymentsLeft / 2.0);
  153.     double creditCardPayment = (int) (monthlyPaymentsLeft / (4.0 * (int) (randomNumber.nextDouble() * 2.0 + 1.0)));
  154.     double personalLoanPayment = (int) (monthlyPaymentsLeft - (creditCardPayment + carPayment));
  155.  
  156.     // Create a prepared statement to be used in the
  157.     // creation of CCARD, AUTO and LOAN accounts...
  158.     String query = "INSERT INTO ACCOUNT(HISTORYID, " +
  159.                    "ACCOUNTID, TYPE, STATUS, INSTITUTIONID, " +
  160.                    "ACCOUNTNUMBER, BALANCE, MONTHLYPAYMENT, LIMIT, PASTDUE120, " +
  161.                    "PASTDUE90, PASTDUE60, PASTDUE30, TOTALPAYMENTS ) " +
  162.                    "VALUES(  ?, ?, ?, ?, ?, ?, " +
  163.                    "?, ?, ?, ?, ?, ?, ?, ?)";
  164.  
  165.     java.sql.PreparedStatement newRecordStatement =
  166.                acmeDb.createPreparedStatement( query );
  167.  
  168.     // Create Car Payment Account
  169.     newRecordStatement.setInt(     1, historyId);
  170.     newRecordStatement.setInt(     2, 1);
  171.     newRecordStatement.setString(  3, "AUTO" );
  172.     newRecordStatement.setString(  4, "OPEN" );
  173.     newRecordStatement.setInt(     5, (int) (randomNumber.nextDouble() * institutionCount + 1.0));
  174.     newRecordStatement.setString(  6, Integer.toString((int)(randomNumber.nextDouble() * 10000000.0)) );
  175.     double balance = (int) (randomNumber.nextDouble() * 10000.0);
  176.     newRecordStatement.setDouble(  7, balance);
  177.     newRecordStatement.setDouble(  8, carPayment );
  178.     newRecordStatement.setDouble(  9, 0 );
  179.     newRecordStatement.setShort(  10, (short) (randomNumber.nextFloat() * 2) );
  180.     newRecordStatement.setShort(  11, (short) (randomNumber.nextFloat() * 2) );
  181.     newRecordStatement.setShort(  12, (short) (randomNumber.nextFloat() * 2) );
  182.     newRecordStatement.setShort(  13, (short) (randomNumber.nextFloat() * 2) );
  183.     newRecordStatement.setShort(  14, (short) (randomNumber.nextFloat() * 30 + 20));
  184.     newRecordStatement.executeUpdate();
  185.  
  186.     // Create Credit Card Account
  187.     newRecordStatement.setInt(  2, 2);
  188.     newRecordStatement.setString(  3, "CCARD" );
  189.     newRecordStatement.setString(  4, "OPEN" );
  190.     newRecordStatement.setInt(  5, (int) (randomNumber.nextDouble() * institutionCount + 1));
  191.     newRecordStatement.setString(  6, Integer.toString((int)(randomNumber.nextDouble() * 10000000)) );
  192.     balance = (int) (randomNumber.nextDouble() * 5000d);
  193.     newRecordStatement.setDouble(  7, balance);
  194.     newRecordStatement.setDouble(  8, creditCardPayment );
  195.     newRecordStatement.setDouble(  9, balance * (int) (randomNumber.nextDouble() * 3 + 1) );
  196.     newRecordStatement.setShort(  10, (short) (randomNumber.nextFloat() * 2) );
  197.     newRecordStatement.setShort(  11, (short) (randomNumber.nextFloat() * 2) );
  198.     newRecordStatement.setShort(  12, (short) (randomNumber.nextFloat() * 2) );
  199.     newRecordStatement.setShort(  13, (short) (randomNumber.nextFloat() * 2) );
  200.     newRecordStatement.setShort(  14, (short) (randomNumber.nextFloat() * 30 + 20));
  201.     newRecordStatement.executeUpdate();
  202.  
  203.     // Create Personal Loan Account if monthly payment is greater than $1
  204.     if ( personalLoanPayment > 1f ) {
  205.       newRecordStatement.setInt(  2, 3);
  206.       newRecordStatement.setString(  3, "LOAN" );
  207.       newRecordStatement.setString(  4, "OPEN" );
  208.       newRecordStatement.setInt(  5, (int) (randomNumber.nextDouble() * institutionCount + 1));
  209.       newRecordStatement.setString(  6, Integer.toString((int)(randomNumber.nextDouble() * 10000000)) );
  210.       balance = (int) (randomNumber.nextDouble() * 7500);
  211.       newRecordStatement.setDouble(  7,  balance);
  212.       newRecordStatement.setDouble(  8, personalLoanPayment );
  213.       newRecordStatement.setDouble(  9, 0 );
  214.       newRecordStatement.setShort(  10, (short) (randomNumber.nextFloat() * 2) );
  215.       newRecordStatement.setShort(  11, (short) (randomNumber.nextFloat() * 2) );
  216.       newRecordStatement.setShort(  12, (short) (randomNumber.nextFloat() * 2) );
  217.       newRecordStatement.setShort(  13, (short) (randomNumber.nextFloat() * 2) );
  218.       newRecordStatement.setShort(  14, (short) (randomNumber.nextFloat() * 30 + 20));
  219.       newRecordStatement.executeUpdate();
  220.     }
  221.     newRecordStatement.close();
  222.  }
  223.  
  224.   /**
  225.   *  Create random data for the specified applicant. Determine what % of their income
  226.   *  will be allocated to monthly payments and the generate some random accounts.
  227.   */
  228.   public int generateRandomData(applicantInfoStruct appInfo, Database acmeDb)
  229.     throws Exception {
  230.  
  231.     int historyId;
  232.  
  233.     this.acmeDb = acmeDb;
  234.  
  235.     // Turn off autoCommit so that the following SQL is executed in the
  236.     // same transaction...
  237.     this.acmeDb.start();
  238.     this.acmeDb.setAutoCommit( false );
  239.  
  240.     try {
  241.  
  242.       // Generate a new history ID
  243.       historyId = generateHistoryId();
  244.  
  245.       // Populate the Credit History Table with the applicants personal information
  246.       insertCreditHistoryRecord( appInfo, historyId );
  247.       generateRandomAccounts( appInfo, historyId );
  248.  
  249.       // Commit the transaction
  250.       this.acmeDb.commit();
  251.  
  252.     } catch( Exception e) {
  253.       System.err.println(e);
  254.  
  255.       // The transaction needs to be rolled-back
  256.       // to ensure all DB changes are discarded.
  257.       try{
  258.         this.acmeDb.rollback();
  259.       } catch( Exception rollbackException ) {
  260.         System.out.println(rollbackException);
  261.       }
  262.       // Throw a new exception so that the caller is aware
  263.       // that an error occurred.
  264.       throw new Exception(res.getString("random_create_error") );
  265.     }
  266.     return historyId;
  267.   }
  268. }
  269.  
  270.