TableRandomize

Java

This application creates a random key for a table for purposes of selection for analysis or marketing. /* * TableRandomize.java * * Copyright (c) 2013 Alfred Wheeler * All rights reserved. * * This software is the proprietary * information of Alfred Wheeler * */ import java.io.*; import java.io.BufferedReader; import java.io.BufferedWriter; import java.sql.*; import java.text.*; import java.text.DecimalFormat; import java.util.*; import java.util.Collections; import java.util.Date; import java.util.GregorianCalendar; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; import java.util.Properties; import java.util.Random; /** * The purpose of this application is to add a random key to a table * * SYNTAX *  java [-cp ] TableRandomize [-h|-help] [-m ] [-L|Log ] <[-T|Table] table name> * * EXAMPLES *  java TableRandomize -h *             Display help page * *   java TableRandomize -m 1000 -L /path/fileName -T table_name *             Log the progress to file at intevals of 1000 records * *   java TableRandomize table_name *             Just add the random key to the table * * @param  -h *             Optional. Usage message. * @param -m *             Optional. Number of records to process before writing progress message to log. * @param -L  *             Optional. Desired log file path and name. * @param -T  *             Required. File to read the records from. * * @author      10/16/2013 created by Alfred Wheeler. * * @version     0.1 * */ /*  * Notes : * 1. Add unique_id and random_key columns to table. * 2. Populate unique_id sequentially. * 3. Here is a PostgreSQL example : * *    CREATE TABLE random_key_table_name AS   *    SELECT *    primary_key_id, *    (100000 + (row_number OVER )) AS unique_id, *    null AS random_key *   FROM table_name *   ;  * 4. The unique_id is redundant, but convenient... */ public class TableRandomize { public static String className = "TableRandomize"; public static long logModulo = 10000;  // Default increment for log entries public static String tableName = "";   // Table Name public static String logName = "";     // Log File Path and Name public static String propName =        // Properties File Path and Name System.getProperty("user.dir") + "/properties/Demo.properties"; // Properties file public static boolean argHelp             = false; public static boolean argModulo           = false; public static boolean argTable            = false; public static boolean argLogFile          = false; public static boolean propRecordCount     = false; public static void main( String[] args ) throws IOException, SQLException, Exception { String usage = "Usage: java [-cp ] " + className + " [-h|-help] [-m ] [-L|Log ] <[-T|Table] table name>"; long dt = 0; long datetimeStart = 0; long datetimeEnd = 0; File propFile = null; Properties appProp = null;         // Application/class properties Connection conn = null; Statement st = null; ResultSet rs = null; String sql = ""; long tableIndex = 0; long value = 0;                    // candidate for random key long uniqueId = 0; long rows = 0;                     // rows on table int errorCount = 0; String msg = "";                   // message String s = "";                     // utility variable int i = 0;                         // utility variable Iterator ptr = null;               // utility variable for lists /* 		 * Get arguments from command line */ 		if ( args.length < 1 ) { System.out.println; System.out.println( usage ); System.out.println; System.exit(1); } 		for ( i = 0; i < args.length; i++ ) { if ( !argHelp ) { // Have we already got it? if ( args[i].equals ( "-h" ) || args[i].equals ( "-help" ) || args[i].equals ( "-?" ) ) { 					argHelp = true; } 			} 			if ( !argModulo ) { // Have we already got it? if ( args[i].equals ( "-m" ) || args[i].equals ( "-modulo" ) ) { // Log increment modulus argModulo = true; if ( args.length > ( i + 1 ) ) { try { logModulo = Long.parseLong( args[i+1] ); } 						catch ( Exception e ) { msg = "*** " + className + ": ERROR - Invalid value specified as logModulo, " + args[i+1] + "."; System.out.println; System.out.println( msg ); errorCount++; } 					} 				} 			} else { if ( args[i].equals ( "-m" ) || args[i].equals ( "-modulo" ) ) { msg = "*** " + className + ": ERROR - Duplicate logModulo option specified."; System.out.println; System.out.println( msg ); errorCount++; } 			} 			if ( !argLogFile ) { // Have we already got it? if ( args[i].equals ( "-L" ) || args[i].equals ( "-Log" ) ) { argLogFile = true; if ( args.length > ( i + 1 ) ) { logName = args[i+1]; } 				} 			} else { if ( args[i].equals ( "-L" ) || args[i].equals ( "-Log" ) ) { msg = "*** " + className + ": ERROR - Duplicate Log option specified."; System.out.println; System.out.println( msg ); errorCount++; } 			} 			if ( !argTable ) { // Have we already got it? if ( args[i].equals ( "-T" ) || args[i].equals ( "-Table" ) ) { argTable = true; if ( args.length > ( i + 1 ) ) { tableName = args[i+1]; } 				} 			} else { if ( args[i].equals ( "-T" ) || args[i].equals ( "-Table" ) ) { msg = "*** " + className + ": ERROR - Duplicate TableName option specified."; System.out.println; System.out.println( msg ); errorCount++; } 			} 			if ( !argTable ) { // Have we already got it? if ( !( // Are we looking at an option? ( args[i].equals ( "-h" ) ) || ( args[i].equals ( "-help"  ) ) || ( args[i].equals ( "-m" ) ) || ( args[i].equals ( "-modulo" ) ) || ( args[i].equals ( "-L" ) ) || ( args[i].equals ( "-Log" ) ) || ( args[i].equals ( "-T" ) ) || ( args[i].equals ( "-Table" ) ) // Insurance... ) ) { 					if ( i > 0 ) { // We need to watch out for the dreaded "Array index out of bounds error" if ( !( // We do not want any value associated with these options! ( args[i-1].equals ( "-m" ) ) || ( args[i-1].equals ( "-modulo" ) ) || ( args[i-1].equals ( "-L" ) ) || ( args[i-1].equals ( "-Log" ) ) || ( args[i-1].equals ( "-T" ) ) || ( args[i-1].equals ( "-Table" ) ) // Insurance... ) ) { 							argTable = true; tableName = args[i]; } 					} else { // Apparently, the first argument must be the table name! argTable = true; tableName = args[i]; } 				} 			} 		} 		/* 		 * Exit point */ 		if ( errorCount > 0 ) { System.exit(1); } 		/* 		 * Get the database connection values, etc... from the properties file * ( This eventually will be externalized to reduce code clutter... ) */ 		propFile = new File( propName ); if ( !propFile.isFile ) { System.out.println( propName + " does not exist or is not in the specified directory." ); errorCount++; System.exit(1); } 		appProp = loadProperties( propName ); /* 		 * Note : Command line arguments override properties file values */ 		if ( !argModulo ) { try { logModulo = Long.parseLong( appProp.getProperty("LogModulo") ); argModulo = true; } 			catch ( Exception e ) { // This would be an odd but real problem. msg = "*** " + className + ": ERROR - Invalid value specified for logModulo, " + s + "."; System.out.println; System.out.println( msg ); errorCount++; System.exit(1); } 		} 		if ( !argLogFile ) { logName = appProp.getProperty( className + ".LogFileName" ); if ( logName != null ) { argLogFile = true; } 		} //		printProperties( appProp ); String dbDriver = "" + appProp.getProperty("dbDriver"); String dbUrl   = "" + appProp.getProperty("dbUrl"); String dbUid   = "" + appProp.getProperty("dbUid"); String dbUpw   = "" + appProp.getProperty("dbUpw"); /* 		 * Identify the log */ 		System.out.println; if ( argLogFile ) { writeOut ( logName, "" ); } System.out.println; if ( argLogFile ) { writeOut ( logName, "" ); } System.out.println( "--" ); if ( argLogFile ) { writeOut ( logName, "--" ); } System.out.println( "-- LOG for : " + className ); if ( argLogFile ) { writeOut ( logName, ( "-- LOG FILE for : " + className ) ); } System.out.println( "-- " ); if ( argLogFile ) { writeOut ( logName, "-- " ); } System.out.println( "-- USER NAME   : " + System.getProperty( "user.name" ) ); if ( argLogFile ) { writeOut ( logName, ( "-- USER NAME   : " + System.getProperty( "user.name" ) ) ); } System.out.println( "-- " ); if ( argLogFile ) { writeOut ( logName, "-- " ); } if ( argLogFile ) { System.out.println( "-- LOG FILE PATH-NAME : " + logName ); } if ( argLogFile ) { System.out.println( "-- " ); if ( argLogFile ) { writeOut ( logName, "-- " ); } } System.out.println( "-- TABLE NAME : " + tableName ); if ( argLogFile ) { writeOut ( logName, ( "-- TABLE NAME : " + tableName ) ); } System.out.println( "-- " ); if ( argLogFile ) { writeOut ( logName, "-- " ); } for (i=0;i<args.length;i++) { System.out.println( "-- ARGS[" + i + "] : " + args[i] ); if ( argLogFile ) { writeOut ( logName, ( "-- ARGS[" + i + "] : " + args[i] ) ); } } 		System.out.println( "--" ); if ( argLogFile ) { writeOut ( logName, "--" ); } System.out.println; if ( argLogFile ) { writeOut ( logName, "" ); } /* 		 * Get start time for performance summary report / stats */ 		datetimeStart = dateTimeStamp( "START DATE-TIME" ); //		System.out.println; if ( argLogFile ) { writeOut ( logName, "" ); } // Vertical spacing /* 		 * Show records count of the table */ 		rows = recordCount( dbDriver, dbUrl, dbUid, dbUpw, tableName ); System.out.println( "-- RECORDS ON TABLE " + tableName + " : " + rows ); if ( argLogFile ) { writeOut ( logName, ( "-- RECORDS ON TABLE " + tableName + " : " + rows ) ); } System.out.println( "-- " ); if ( argLogFile ) { writeOut ( logName, "-- " ); } dt = dateTimeStamp( "DATE-TIME STAMP" ); /* 		 * Get/Set the initial value for the Unique ID index */ 		sql = "SELECT ( MIN( unique_id ) - 100000 ) AS next_table_index " + "FROM " + tableName + " " + "WHERE random_key = 0 " ; //		System.out.print("sql = " + sql + " \n"); try { Class.forName(dbDriver); conn = DriverManager.getConnection(dbUrl, dbUid, dbUpw); st = conn.createStatement; rs = st.executeQuery( sql ); if (rs.next) { tableIndex = Integer.parseInt( rs.getString("next_table_index") ); } 		} // end try catch ( SQLException ex ) { System.out.println ( "---" ); System.out.println ( "--                      FATAL ERROR IN SQL" ); System.out.println ( "---" ); System.out.println ( "-- " ); while ( ex != null ) { System.out.println ("-- SQLState: " + ex.getSQLState ); System.out.println ("-- Message: " + ex.getMessage ); System.out.println ("-- Vendor:  " + ex.getErrorCode ); ex = ex.getNextException ; System.out.println ( "-- " ); } 			System.out.println ( "---" ); } // end catch catch ( java.lang.Exception ex ) { /* 			 * Got some other type of exception. Dump it. */ 			System.out.println ( "---" ); ex.printStackTrace ; System.out.println("exception: " + ex.getMessage ); } // end catch finally { rs.close; st.close; conn.close; } // end finally /* 		 * Crank out the random values and apply them to the table */ 		while ( tableIndex < rows ) { uniqueId = tableIndex + 100000; value = makeRandomKey( dbDriver, dbUrl, dbUid, dbUpw, tableName ); if ( 0 == tableIndex % 1000 ) { System.out.println ( "-- unique id = " + uniqueId + ", random key = " + value ); } 			sql = "UPDATE " + tableName + " SET random_key = " + value + " WHERE unique_id = " + uniqueId ; //			System.out.print("sql = " + sql + " \n"); executeSQL( dbDriver, dbUrl, dbUid, dbUpw, sql ); tableIndex++; logUpdate( tableIndex, "RECORDS PROCESSED = " ); } 		datetimeEnd = dateTimeStamp( "END DATE-TIME" ); /* 		 * Determine total duration of script in minutes */ 		msg = duration( datetimeStart, datetimeEnd ); System.out.println( " " ); if ( argLogFile ) { writeOut ( logName, " " ); } System.out.print( msg ); if ( argLogFile ) { writeOut ( logName, ( msg ) ); } System.out.println( " " ); if ( argLogFile ) { writeOut ( logName, " " ); } //		/* //		 * Produce performance summary //		 */ // //		msg = performance( rows, datetimeStart, datetimeEnd ); //		System.out.println( " " ); if ( argLogFile ) { writeOut ( logName, " " ); } //		System.out.print( msg ); if ( argLogFile ) { writeOut ( logName, ( msg ) ); } //		System.out.println( " " ); if ( argLogFile ) { writeOut ( logName, " " ); } } // main /** 	 * Just execute the SQL statement */ 	private static void executeSQL( String dbDriver, String dbUrl, String dbUid, String dbUpw, String sql ) throws SQLException { Connection conn = null; Statement st = null; try { Class.forName(dbDriver); conn = DriverManager.getConnection(dbUrl, dbUid, dbUpw); st = conn.createStatement; st.execute( sql ); } // end try catch ( SQLException ex ) { System.out.println ( "---" ); System.out.println ( "--                      FATAL ERROR IN SQL" ); System.out.println ( "---" ); System.out.println ( "-- " ); while ( ex != null ) { System.out.println ("-- SQLState: " + ex.getSQLState ); System.out.println ("-- Message: " + ex.getMessage ); System.out.println ("-- Vendor:  " + ex.getErrorCode ); ex = ex.getNextException ; System.out.println ( "-- " ); } 			System.out.println ( "---" ); } // end catch catch ( java.lang.Exception ex ) { /* 			 * Got some other type of exception. Dump it. */ 			System.out.println ( "---" ); ex.printStackTrace ; System.out.println("exception: " + ex.getMessage ); } // end catch finally { st.close; conn.close; } // end finally } // executeSQL /** 	 * Create a random key */ 	private static long makeRandomKey( String dbDriver, String dbUrl, String dbUid, String dbUpw, String tableName ) throws SQLException { long value = 0; String it = null; boolean found = true; String sql = ""; Connection conn = null; Statement st = null; ResultSet rs = null; Random randomGenerator = new Random; // Seed the random generator while ( found != false ) { value = randomGenerator.nextInt(1000000000); // Number < 1,000,000,000 sql = "SELECT 'TRUE' AS result " + "FROM " + tableName + " " + "WHERE random_key = " + value + " " ; //			System.out.print("sql = " + sql + " \n"); try { Class.forName(dbDriver); conn = DriverManager.getConnection(dbUrl, dbUid, dbUpw); st = conn.createStatement; rs = st.executeQuery( sql ); if (rs.next) { it = rs.getString("result"); } 			} // end try catch ( SQLException ex ) { System.out.println ( "---" ); System.out.println ( "--                      FATAL ERROR IN SQL" ); System.out.println ( "---" ); System.out.println ( "-- " ); while ( ex != null ) { System.out.println ("-- SQLState: " + ex.getSQLState ); System.out.println ("-- Message: " + ex.getMessage ); System.out.println ("-- Vendor:  " + ex.getErrorCode ); ex = ex.getNextException ; System.out.println ( "-- " ); } 				System.out.println ( "---" ); } // end catch catch ( java.lang.Exception ex ) { /* 				 * Got some other type of exception. Dump it. */ 				System.out.println ( "---" ); ex.printStackTrace ; System.out.println("exception: " + ex.getMessage ); } // end catch finally { rs.close; st.close; conn.close; } // end finally if ( found != true ) { found = false; } 			if ( it == null ) { found = false; } 			else { if ( !it.equals("TRUE") ) { found = false; } 			} 		} 		return (value); } // makeRandomKey /** 	 * Write ( append ) a line to the specified file. */ 	private static void writeOut( String lpn, String s ) { BufferedWriter out = null; String msg = ""; boolean fileIsOpen = false; try { out = new BufferedWriter( new FileWriter( lpn, true ) ); // The value true indicates append. fileIsOpen = true; } 		catch ( IOException e ) { msg = "*** " + className + ": ERROR - cannot open file " + lpn + " for writing."; System.out.println; System.out.println( msg ); System.exit(1); } 		if ( fileIsOpen == true ) { try { out.write( s + "\n" ); } 			catch ( IOException e ) { msg = "*** " + className + ": ERROR - cannot write to file, " + lpn + "."; System.out.println; System.out.println( msg ); System.exit(1); // Must be a real problem... } 			try { out.flush; out.close; } 			catch ( Exception e ) { msg = "*** " + className + ": ERROR - Could not close file, " + lpn + "."; System.out.println; System.out.println( msg ); System.exit(1); } 		} 	} // writeOut /** 	 * Writes a progress entry to the specified log. */ 	private static void logUpdate( long count, String msg ) { String progress_time = ""; String processed = ""; if ( 0 == ( count % logModulo ) ) { progress_time = fmtDTS ( new Date( System.currentTimeMillis ), "MM/DD/YYYY HH:MI:SS" ); // Gets the current date-time processed = msg + " " + count; System.out.println; System.out.println( ( "-- " + processed + ", DATETIME = " + progress_time ) ); if ( argLogFile ) { writeOut ( logName, ( "-- " + processed + ", DATETIME = " + progress_time ) ); } } 	} // logUpdate /** 	 * Writes a user formatted general message entry the specified log. */ 	private static void logMsg( String msg ) { String progress_time = ""; progress_time = fmtDTS ( new Date( System.currentTimeMillis ), "MM/DD/YYYY HH:MI:SS" ); // Gets the current date-time System.out.println; System.out.println( ( "-- " + msg + ", DATETIME = " + progress_time ) ); if ( argLogFile ) { writeOut ( logName, ( "-- " + msg + ", DATETIME = " + progress_time ) ); } } // logMsg /** 	 * Counts the records on a database table * 	 * @return     recordss long */ 	private static long recordCount( String dbDriver, String dbUrl, String dbUid, String dbUpw, String tableName ) throws SQLException { long records = 0; Connection conn = null; Statement st = null; ResultSet rs = null; String sql = "SELECT COUNT(*) AS records FROM " + tableName ; //		System.out.print("sql = " + sql + " \n"); try { Class.forName(dbDriver); conn = DriverManager.getConnection(dbUrl, dbUid, dbUpw); st = conn.createStatement; rs = st.executeQuery( sql ); if (rs.next) { records = Long.parseLong( rs.getString("records") ); } 		} // end try catch ( SQLException ex ) { System.out.println ( "---" ); System.out.println ( "--                      FATAL ERROR IN SQL" ); System.out.println ( "---" ); System.out.println ( "-- " ); while ( ex != null ) { System.out.println ("-- SQLState: " + ex.getSQLState ); System.out.println ("-- Message: " + ex.getMessage ); System.out.println ("-- Vendor:  " + ex.getErrorCode ); ex = ex.getNextException ; System.out.println ( "-- " ); } 			System.out.println ( "---" ); } // end catch catch ( java.lang.Exception ex ) { /* 			 * Got some other type of exception. Dump it. */ 			System.out.println ( "---" ); ex.printStackTrace ; System.out.println("exception: " + ex.getMessage ); } // end catch finally { rs.close; st.close; conn.close; } // end finally return (records); } // recordCount /** 	 * Determines if a substring exists within a given string, * and if found, then where in the string it is. * If not found, returns -1. * 	 * @return     position int */ 	private static int inStr( String searchString, String targetString, int offset ) { int position = 0; int result = -1; String substringOfSearchString = ""; if ( ( offset < 0 ) || ( offset >= searchString.length ) ) { } else { position = offset; while ( position < searchString.length ) { substringOfSearchString = searchString.substring( position,( position + targetString.length ) ); if ( substringOfSearchString == targetString ) { result = position; } 				if ( result < 0 ) { position++; } 			} 		} 		return result; } 	/** 	 * Left pads string, s, with string, c, usually * a single character, for some repetitions, n. 	 * * @return     leftPaddedString String */ 	public static String lpad ( String s, int n, String c ) { while ( n > 0 ) { s = c + s; 			n--; } 		return s; 	} /** 	 * Right pads string, s, with string, c, usually * a single character, for some repetitions, n. 	 * * @return     rightPaddedString String */ 	public static String rpad ( String s, int n, String c ) { while ( n > 0 ) { s = s + c; 			n--; } 		return s; 	} /** 	 * Produces Date-Time Stamp and optionally * writes it to the log file. * 	 * @return     dateTime long */ 	private static long dateTimeStamp ( String msg ) { String dateFormat = "MM/DD/YYYY HH:MI:SS"; String dateString = dateFormat; long dt; GregorianCalendar calendar = new GregorianCalendar; dt = System.currentTimeMillis; // Gets the current date-time calendar.setTime ( new Date( dt ) ); int hour = calendar.get(calendar.HOUR_OF_DAY); int minute = calendar.get(calendar.MINUTE); int second = calendar.get(calendar.SECOND); int dayOfMonth = calendar.get(calendar.DAY_OF_MONTH); int month = calendar.get(calendar.MONTH); int year = calendar.get(calendar.YEAR); DecimalFormat twoDigitFormat = new DecimalFormat; twoDigitFormat.setMinimumIntegerDigits(2); String secondString = twoDigitFormat.format((second)); String minuteString = twoDigitFormat.format((minute)); String hourString  = twoDigitFormat.format((hour)); String dayString   = twoDigitFormat.format((dayOfMonth)); String monthString = twoDigitFormat.format((month+1)); String yearString  = Integer.toString(year); /* 		 * Parse out dateFormat string and populate dateString */ 		int ss = dateFormat.indexOf("SS"); int mi = dateFormat.indexOf("MI"); int hh = dateFormat.indexOf("HH"); int dd = dateFormat.indexOf("DD"); int mm = dateFormat.indexOf("MM"); int yyyy = dateFormat.indexOf("YYYY"); if (ss >= 0) { if (ss > 0) { dateString = dateString.substring(0,ss) + secondString + dateString.substring(ss+2); } 			else { dateString = secondString + dateString.substring(ss+2); } 		} 		if (mi >= 0) { if (mi > 0) { dateString = dateString.substring(0,mi) + minuteString + dateString.substring(mi+2); } 			else { dateString = minuteString + dateString.substring(mi+2); } 		} 		if (hh >= 0) { if (hh > 0) { dateString = dateString.substring(0,hh) + hourString + dateString.substring(hh+2); } 			else { dateString = hourString + dateString.substring(hh+2); } 		} 		if (dd >= 0) { if (dd > 0) { dateString = dateString.substring(0,dd) + dayString + dateString.substring(dd+2); } 			else { dateString = dayString + dateString.substring(dd+2); } 		} 		if (mm >= 0) { if (mm > 0) { dateString = dateString.substring(0,mm) + monthString + dateString.substring(mm+2); } 			else { dateString = monthString + dateString.substring(mm+2); } 		} 		if (yyyy >= 0) { if (yyyy > 0) { dateString = dateString.substring(0,yyyy) + yearString + dateString.substring(yyyy+4); } 			else { dateString = yearString + dateString.substring(yyyy+4); } 		} 		else { // yyyy will be < 0, if "YYYY" is not in dateFormat string. int yy = dateFormat.indexOf("YY"); if (yy >= 0) { if (yy > 0) { dateString = dateString.substring(0,yy) + yearString.substring(2) + dateString.substring(yy+2); } 				else { dateString = yearString.substring(2) + dateString.substring(yy+2); } 			} 		} //		if ( argLogFile ) { writeOut ( logName, "" ); } if ( !(msg.toUpperCase).equals( "QUIET" ) ) { System.out.println( "--" ); if ( argLogFile ) { writeOut ( logName, "--" ); } System.out.println( "-- " + msg + " : " + dateString ); if ( argLogFile ) { writeOut ( logName, ( "-- " + msg + " : " + dateString ) ); } System.out.println( "--" ); if ( argLogFile ) { writeOut ( logName, "--" ); } } 		return ( dt ); } // dateTimeStamp /** 	 * Format a date time value to user specification * for logging, calculations, and reporting. * 	 * @return     dateTime String */ 	private static String fmtDTS ( Date d, String dateFormat ) { String dateString; dateString = dateFormat; GregorianCalendar calendar = new GregorianCalendar; calendar.setTime ( d ); int hour = calendar.get(calendar.HOUR_OF_DAY); int minute = calendar.get(calendar.MINUTE); int second = calendar.get(calendar.SECOND); int dayOfMonth = calendar.get(calendar.DAY_OF_MONTH); int month = calendar.get(calendar.MONTH); int year = calendar.get(calendar.YEAR); DecimalFormat twoDigitFormat = new DecimalFormat; twoDigitFormat.setMinimumIntegerDigits(2); String secondString = twoDigitFormat.format((second)); String minuteString = twoDigitFormat.format((minute)); String hourString  = twoDigitFormat.format((hour)); String dayString   = twoDigitFormat.format((dayOfMonth)); String monthString = twoDigitFormat.format((month+1)); String yearString  = Integer.toString(year); /* 		 * Parse out dateFormat string and populate dateString */ 		int ss = dateFormat.indexOf("SS"); int mi = dateFormat.indexOf("MI"); int hh = dateFormat.indexOf("HH"); int dd = dateFormat.indexOf("DD"); int mm = dateFormat.indexOf("MM"); int yyyy = dateFormat.indexOf("YYYY"); if (ss >= 0) { if (ss > 0) { dateString = dateString.substring(0,ss) + secondString + dateString.substring(ss+2); } 			else { dateString = secondString + dateString.substring(ss+2); } 		} 		if (mi >= 0) { if (mi > 0) { dateString = dateString.substring(0,mi) + minuteString + dateString.substring(mi+2); } 			else { dateString = minuteString + dateString.substring(mi+2); } 		} 		if (hh >= 0) { if (hh > 0) { dateString = dateString.substring(0,hh) + hourString + dateString.substring(hh+2); } 			else { dateString = hourString + dateString.substring(hh+2); } 		} 		if (dd >= 0) { if (dd > 0) { dateString = dateString.substring(0,dd) + dayString + dateString.substring(dd+2); } 			else { dateString = dayString + dateString.substring(dd+2); } 		} 		if (mm >= 0) { if (mm > 0) { dateString = dateString.substring(0,mm) + monthString + dateString.substring(mm+2); } 			else { dateString = monthString + dateString.substring(mm+2); } 		} 		if (yyyy >= 0) { if (yyyy > 0) { dateString = dateString.substring(0,yyyy) + yearString + dateString.substring(yyyy+4); } 			else { dateString = yearString + dateString.substring(yyyy+4); } 		} 		else { // yyyy will be < 0, if "YYYY" is not in dateFormat string. int yy = dateFormat.indexOf("YY"); if (yy >= 0) { if (yy > 0) { dateString = dateString.substring(0,yy) + yearString.substring(2) + dateString.substring(yy+2); } 				else { dateString = yearString.substring(2) + dateString.substring(yy+2); } 			} 		} 		return ( dateString ); } // fmtDTS /** 	 * Produce a brief performance summary * for logging and reporting. * 	 * @return     briefReport String */ 	private static String performance ( long inRecords, long start, long end ) { String msg = ""; double minutesElapsed = 0.1; long recordsPerHour = 0; /* 		 * Determine total duration of application in minutes */ 		Long r = new Long( inRecords ); Long e = new Long( end ); Long s = new Long( start ); minutesElapsed = Math.round( ( ( e.doubleValue - s.doubleValue ) / ( 60 * 1000D ) ) * 10 ) / 10; if ( minutesElapsed < 0.1 ) { minutesElapsed = 0.1; } 		String userName = System.getProperty("user.name"); Date sdt = new Date( start ); Date edt = new Date( end ); /* 		 * Determine records per hour */ 		Double rph = new Double( ( r.doubleValue / ( ( e.doubleValue - s.doubleValue ) / ( 60 * 60 * 1000D ) ) ) ); msg += "\n"; msg += "--\n"; msg += "-- PERFORMANCE SUMMARY\n"; msg += "--\n"; msg += "-- User Name . . . . . . . . " + userName + "\n"; msg += "-- Program . . . . . . . . . " + className + "\n"; msg += "-- Started . . . . . . . . . " + fmtDTS( sdt, "YYYY-MM-DD HH:MI:SS" ) + "\n"; msg += "-- Ended . . . . . . . . . . " + fmtDTS( edt, "YYYY-MM-DD HH:MI:SS" ) + "\n"; msg += "-- Run duration . . . . . . . " + minutesElapsed + " minutes\n"; msg += "-- Records per hour . . . . . " + rph.longValue + "\n"; msg += "-- Total records read . . . . " + inRecords + "\n"; msg += "--\n"; return( msg ); } // performance /** 	 * Determine total duration of application in minutes * 	 * @return     durationReport String */ 	private static String duration( long start, long end ) { String msg = ""; double minutesElapsed = 0.1; long recordsPerHour = 0; /* 		 * Determine total duration of application in minutes */ 		Long e = new Long( end ); Long s = new Long( start ); minutesElapsed = Math.round( ( ( e.doubleValue - s.doubleValue ) / ( 60 * 1000D ) ) * 10 ) / 10; if ( minutesElapsed < 0.1 ) { minutesElapsed = 0.1; } 		msg += "\n"; msg += "--\n"; msg += "-- DURATION : " + minutesElapsed + " minutes\n"; //		msg += "--\n"; msg += "-- END LOG\n"; msg += "\n"; return( msg ); } // duration /** 	 * Show properties (for debugging) */ 	private static void printProperties( Properties p ) { System.out.println; System.out.println("--"); System.out.println("-- Properties"); System.out.println("--"); System.out.println; p.list( System.out ); System.out.println; System.out.println("--"); System.out.println; } // printProperties /** 	 * Load properties from a file * 	 * @return     applicationProperties Properties */ 	private static Properties loadProperties( String fileName ) { InputStream propsFile; Properties tempProp = new Properties; try { propsFile = new FileInputStream(fileName); tempProp.load(propsFile); propsFile.close; } 		catch (IOException ioe) { System.out.println("I/O Exception."); ioe.printStackTrace; System.exit(0); } 		return tempProp; } // loadProperties } // End - class