home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-05-08 | 11.5 KB | 270 lines |
- /*
- * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
- *
- * This SOURCE CODE FILE, which has been provided by Borland as part
- * of a Borland product for use ONLY by licensed users of the product,
- * includes CONFIDENTIAL and PROPRIETARY information of Borland.
- *
- * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS
- * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
- * THE PRODUCT.
- *
- * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
- * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
- * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
- * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
- * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
- * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
- * CODE FILE.
- */
- //--------------------------------------------------------------------------------------------------
- // CORBA Reference Application
- // Copyright (c) 1997 by Borland International, All Rights Reserved
- //
- // Contains logic to generate random data for a specified applicant.
- //--------------------------------------------------------------------------------------------------
-
- package borland.reference.creditapproval.server;
-
- import borland.sql.dataset.*;
- import borland.jbcl.dataset.*;
- import borland.jbcl.control.*;
- import java.util.*;
- import borland.reference.creditapproval.CORBAInterface.*;
-
- /**
- * CreditHistoryRandomData creates simple random data for an applicant.
- * This random data includes:
- * 1) Credit History Record
- * 2) Several Account Records
- */
- public class CreditHistoryRandomData implements DataModule{
- private static CreditHistoryRandomData myDM;
- private Database acmeDb;
- ResourceBundle res = Res.getBundle("borland.reference.creditapproval.server.Res");
-
- public CreditHistoryRandomData() {
- try {
- jbInit();
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- private void jbInit() throws Exception{
- }
-
- static public CreditHistoryRandomData getDataModule() {
- if (myDM == null)
- myDM = new CreditHistoryRandomData();
- return myDM;
- }
-
- /**
- * Use the HISTORYID table to generate a unique history ID.
- * If a row does not exist in this, one is inserted.
- */
- private int generateHistoryId() throws Exception {
-
- int historyId, count;
-
- java.sql.Statement countStatement = acmeDb.createStatement() ;
- java.sql.ResultSet countResults = countStatement.executeQuery("SELECT COUNT(*) FROM HISTORYID");
-
- // Move to first row in the result set! If a row does not
- // exist, throw an exception.
- if (countResults.next()) count = countResults.getInt("COUNT");
- else throw new Exception( res.getString("History_ID_Error") );
-
- // If no rows exist, insert one now so that it can be used to generate a unique ID
- if (count < 1 ) countStatement.executeUpdate("INSERT INTO HISTORYID VALUES(0)");
-
- // Increment the HISTORYID to obtain a unique number.
- java.sql.Statement statement = acmeDb.createStatement() ;
- statement.executeUpdate("UPDATE HISTORYID SET HISTORYID = HISTORYID + 1");
- java.sql.ResultSet results = statement.executeQuery("SELECT HISTORYID FROM HISTORYID");
- if (results.next()) historyId = results.getInt("HISTORYID");
- else throw new Exception( res.getString("History_ID_Error") );
-
- results.close();
-
- return historyId;
- }
-
- /**
- * Creates a record in the Credit History table for the specified applicant.
- */
- private void insertCreditHistoryRecord(applicantInfoStruct appInfo, int historyId )
- throws Exception {
-
- String query = "INSERT INTO CREDITHISTORY (HISTORYID, " +
- "FIRSTNAME, MI, LASTNAME, DOB, PHONE, ADDR1, ADDR2, CITY, STATE, " +
- "POSTALCODE, COUNTRY, IDENTIFICATION ) " +
- "VALUES( ?, ?, ?, ?, ?, ?, " +
- "?, ?, ?, ?, ?, ?, ?)";
-
- java.sql.PreparedStatement newRecordStatement =
- acmeDb.createPreparedStatement( query );
-
- // Populate the ? in the prepared statement with actual values.
- // This relies on the JDBC driver to format data-types correctly,
- // such as dates, for the back-end RDBMS.
- newRecordStatement.setInt( 1, historyId);
- newRecordStatement.setString( 2, appInfo.firstName);
- newRecordStatement.setString( 3, appInfo.MI );
- newRecordStatement.setString( 4, appInfo.lastName );
- newRecordStatement.setDate( 5, java.sql.Date.valueOf( appInfo.DOB ));
- newRecordStatement.setString( 6, appInfo.phone );
- newRecordStatement.setString( 7, appInfo.address1 );
- newRecordStatement.setString( 8, appInfo.address2 );
- newRecordStatement.setString( 9, appInfo.city );
- newRecordStatement.setString( 10, appInfo.state );
- newRecordStatement.setString( 11, appInfo.postalCode );
- newRecordStatement.setString( 12, appInfo.country );
- newRecordStatement.setString( 13, appInfo.PID );
-
- newRecordStatement.execute();
- newRecordStatement.close();
-
- }
-
- /**
- * Creates random accounts based on the applicants monthly income.
- */
- private void generateRandomAccounts( applicantInfoStruct appInfo,
- int historyId ) throws Exception {
-
- int institutionCount;
- java.util.Random randomNumber = new java.util.Random();
-
- java.sql.Statement statement = acmeDb.createStatement() ;
- java.sql.ResultSet results = statement.executeQuery("SELECT INSTITUTIONID FROM INSTITUTIONID");
-
- if (results.next()) institutionCount = results.getInt("INSTITUTIONID");
- else throw new Exception( res.getString("Random_accounts") );
-
- // Generate a random percentage of total spending in the range of 30%- 50%
- double maxPercentage = randomNumber.nextDouble() * 0.20 + 0.25;
- double percentLeft = maxPercentage - appInfo.rentMortgagePayment / appInfo.monthlyIncome;
- if (percentLeft <= 0) return;
- double monthlyPaymentsLeft = appInfo.monthlyIncome * percentLeft;
- double carPayment = (int) (monthlyPaymentsLeft / 2.0);
- double creditCardPayment = (int) (monthlyPaymentsLeft / (4.0 * (int) (randomNumber.nextDouble() * 2.0 + 1.0)));
- double personalLoanPayment = (int) (monthlyPaymentsLeft - (creditCardPayment + carPayment));
-
- // Create a prepared statement to be used in the
- // creation of CCARD, AUTO and LOAN accounts...
- String query = "INSERT INTO ACCOUNT(HISTORYID, " +
- "ACCOUNTID, TYPE, STATUS, INSTITUTIONID, " +
- "ACCOUNTNUMBER, BALANCE, MONTHLYPAYMENT, LIMIT, PASTDUE120, " +
- "PASTDUE90, PASTDUE60, PASTDUE30, TOTALPAYMENTS ) " +
- "VALUES( ?, ?, ?, ?, ?, ?, " +
- "?, ?, ?, ?, ?, ?, ?, ?)";
-
- java.sql.PreparedStatement newRecordStatement =
- acmeDb.createPreparedStatement( query );
-
- // Create Car Payment Account
- newRecordStatement.setInt( 1, historyId);
- newRecordStatement.setInt( 2, 1);
- newRecordStatement.setString( 3, "AUTO" );
- newRecordStatement.setString( 4, "OPEN" );
- newRecordStatement.setInt( 5, (int) (randomNumber.nextDouble() * institutionCount + 1.0));
- newRecordStatement.setString( 6, Integer.toString((int)(randomNumber.nextDouble() * 10000000.0)) );
- double balance = (int) (randomNumber.nextDouble() * 10000.0);
- newRecordStatement.setDouble( 7, balance);
- newRecordStatement.setDouble( 8, carPayment );
- newRecordStatement.setDouble( 9, 0 );
- newRecordStatement.setShort( 10, (short) (randomNumber.nextFloat() * 2) );
- newRecordStatement.setShort( 11, (short) (randomNumber.nextFloat() * 2) );
- newRecordStatement.setShort( 12, (short) (randomNumber.nextFloat() * 2) );
- newRecordStatement.setShort( 13, (short) (randomNumber.nextFloat() * 2) );
- newRecordStatement.setShort( 14, (short) (randomNumber.nextFloat() * 30 + 20));
- newRecordStatement.executeUpdate();
-
- // Create Credit Card Account
- newRecordStatement.setInt( 2, 2);
- newRecordStatement.setString( 3, "CCARD" );
- newRecordStatement.setString( 4, "OPEN" );
- newRecordStatement.setInt( 5, (int) (randomNumber.nextDouble() * institutionCount + 1));
- newRecordStatement.setString( 6, Integer.toString((int)(randomNumber.nextDouble() * 10000000)) );
- balance = (int) (randomNumber.nextDouble() * 5000d);
- newRecordStatement.setDouble( 7, balance);
- newRecordStatement.setDouble( 8, creditCardPayment );
- newRecordStatement.setDouble( 9, balance * (int) (randomNumber.nextDouble() * 3 + 1) );
- newRecordStatement.setShort( 10, (short) (randomNumber.nextFloat() * 2) );
- newRecordStatement.setShort( 11, (short) (randomNumber.nextFloat() * 2) );
- newRecordStatement.setShort( 12, (short) (randomNumber.nextFloat() * 2) );
- newRecordStatement.setShort( 13, (short) (randomNumber.nextFloat() * 2) );
- newRecordStatement.setShort( 14, (short) (randomNumber.nextFloat() * 30 + 20));
- newRecordStatement.executeUpdate();
-
- // Create Personal Loan Account if monthly payment is greater than $1
- if ( personalLoanPayment > 1f ) {
- newRecordStatement.setInt( 2, 3);
- newRecordStatement.setString( 3, "LOAN" );
- newRecordStatement.setString( 4, "OPEN" );
- newRecordStatement.setInt( 5, (int) (randomNumber.nextDouble() * institutionCount + 1));
- newRecordStatement.setString( 6, Integer.toString((int)(randomNumber.nextDouble() * 10000000)) );
- balance = (int) (randomNumber.nextDouble() * 7500);
- newRecordStatement.setDouble( 7, balance);
- newRecordStatement.setDouble( 8, personalLoanPayment );
- newRecordStatement.setDouble( 9, 0 );
- newRecordStatement.setShort( 10, (short) (randomNumber.nextFloat() * 2) );
- newRecordStatement.setShort( 11, (short) (randomNumber.nextFloat() * 2) );
- newRecordStatement.setShort( 12, (short) (randomNumber.nextFloat() * 2) );
- newRecordStatement.setShort( 13, (short) (randomNumber.nextFloat() * 2) );
- newRecordStatement.setShort( 14, (short) (randomNumber.nextFloat() * 30 + 20));
- newRecordStatement.executeUpdate();
- }
- newRecordStatement.close();
- }
-
- /**
- * Create random data for the specified applicant. Determine what % of their income
- * will be allocated to monthly payments and the generate some random accounts.
- */
- public int generateRandomData(applicantInfoStruct appInfo, Database acmeDb)
- throws Exception {
-
- int historyId;
-
- this.acmeDb = acmeDb;
-
- // Turn off autoCommit so that the following SQL is executed in the
- // same transaction...
- this.acmeDb.start();
- this.acmeDb.setAutoCommit( false );
-
- try {
-
- // Generate a new history ID
- historyId = generateHistoryId();
-
- // Populate the Credit History Table with the applicants personal information
- insertCreditHistoryRecord( appInfo, historyId );
- generateRandomAccounts( appInfo, historyId );
-
- // Commit the transaction
- this.acmeDb.commit();
-
- } catch( Exception e) {
- System.err.println(e);
-
- // The transaction needs to be rolled-back
- // to ensure all DB changes are discarded.
- try{
- this.acmeDb.rollback();
- } catch( Exception rollbackException ) {
- System.out.println(rollbackException);
- }
- // Throw a new exception so that the caller is aware
- // that an error occurred.
- throw new Exception(res.getString("random_create_error") );
- }
- return historyId;
- }
- }
-
-