Hi Im parsing text files then using hibernate to store them to mysql.
Eveytime I call saveOrUpdate on my FinancialAccount object, I get:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException
with no details like a -1 or 6 to tell me where. I am doing much subString(start, end),
but shouldn't the exception take place when the String is actually read from the text not when I am
trying to save it using hibernate?
The first piece of code main method:
[code]
System.out.println(nadpath);
File naddir = new File(nadpath);
if (naddir.exists()) {
nadFiles = naddir.list();
System.out.println(nadFiles);
} else {
errors.append("TradeBlotter - /var/local/Quartz/TRADEDATA/"
+ coid + "/nad/ does not exist. ");
}
if (nadFiles.length>0) {
parsedNAD = new LinkedList<String>();
FinancialAccountClass fa = new FinancialAccountClass();
// We send each file to FinancialAccount.java
for (int i = 0; i < nadFiles.length; i++) {
System.out.println("File:"+nadFiles[i]);
System.out.println(i);
boolean parsed = fa.parseFile(nadpath + nadFiles[i],
coid);
System.out.println("Result Parsed:" + parsed);
parsedNAD.add(nadFiles[i]);
}
} else {
System.out
.println("TradeBlotter - There were no NAD files for client: "
+ coid);
}
[/code]
uses:
[code]
public boolean parseFile(String filepath, String coid) {
// This is used to configure logging with Apache log4j.
PropertyConfigurator.configure("/var/local/Quartz/TradeBlotter/lib/log4j.properties");
final Logger log = Logger.getLogger("FinancialAccountClass.class");
Session session = null;
boolean parsed = false;
int count = 0;
// This is for reading our state codes from a properties file.
// DST-STATES.PROPERTIES.
FileInputStream fis = null;
try {
// fis = new FileInputStream("C:\\Documents and
// Settings\\mike\\Desktop\\dst-data\\DST-STATES.properties");
fis = new FileInputStream(
"/var/local/Quartz/TradeBlotter/lib/DST-STATES.properties");
} catch (FileNotFoundException fnf) {
fnf.printStackTrace();
System.out.println("TradeBlotter - DST STATE code prop file not found.");
log.error("TradeBlotter - DST STATE code prop file not found.");
}
Properties props = new Properties();
try {
props.load(fis);
} catch (IOException io) {
io.printStackTrace();
System.out.println("TradeBlotter - Problem reading in DST STATE properties file.");
log.error("TradeBlotter - Problem reading in DST STATE properties file.");
}
// 3 LINES PER CLIENT
String LINE1 = "";
String LINE2 = "";
String LINE3 = "";
// /////////////////////////////////////////
// These match our db.
String DEALERNUMBER = "";
String BRANCHNUMBER = "";
String ACCOUNTNAME = "";
String ESTABLISHEDDATE = "";
String LASTMAINTENANCEDATE = "";
String ACCOUNTTYPE;
String ADDRESS = "";
String ZIPCODE1 = "";
String ZIPCODE2 = "";
String STATECODE = "";
String STATE = "";
String CUSIP = "";
String SWP;
String SUBACCOUNT = "";
String NAV;
String INTERESTEDPARTY;
String REPID = "";
String ACCOUNTNUMBER = "";
String SSN = "";
String SSNSTATUS = "";
String REGLINE1 = "";
String REGLINE2 = "";
String REGLINE3 = "";
String REGLINE4 = "";
String REGLINE5 = "";
String REGLINE6 = "";
String REGLINE7 = "";
// //////////////////////////////////////////
// Later use...
String HEADER = "";
//String FOOTER = "";
// //////////////
// Read the file.
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(filepath));
} catch (FileNotFoundException fnf) {
fnf.printStackTrace();
System.out.println("TradeBlotter - Could not load the file " + filepath + ".");
log.error("TradeBlotter - Could not load the file " + filepath + ".");
}
String line;
LinkedList<String> clients = new LinkedList<String>();
// First we read the whole file.
// Add each line to a LinkedList<String>.
try {
while ((line = reader.readLine()) != null) {
clients.add(line);
}
} catch (IOException io2) {
io2.printStackTrace();
System.out.println("TradeBlotter - Problem reading from file " + filepath + ".");
log.error("TradeBlotter - Problem reading from file " + filepath + ".");
}
// For future use.
HEADER = clients.getFirst();
clients.removeFirst();
clients.removeLast();
// Lets get the fundname from the HEADER.
// fundname is in the header between two asterisks(*).
String FUNDNAME = StringUtils.substringBetween(HEADER, "*", "*");
System.out.println("fundname " + FUNDNAME);
// This is a place holder for our second round of data.Intermediate step
Hashtable<String, String> records = new Hashtable<String, String>();
// This Hashtable is for our final db insertion.
// /Hashtable<String, String> ready = new Hashtable<String, String>();
int size = clients.size();
int numOfClients = size / 3;
System.out.println("Number of Clients: " + numOfClients);
System.out.println("Rows read from file: " + size);
System.out.println("------------------------------------------");
for (int i = 0; i < size; i += 3) {
// Option 1
// System.out.println(i);
// Printout for testing..
// System.out.println(clients.get(i)+clients.get(i+1)+clients.get(i+2));
/*
* System.out.println(clients.get(i));
* System.out.println(clients.get(i+1));
* System.out.println(clients.get(i+2));
*/
// System.out.println("------------------------------");
// String data = clients.get(i)+clients.get(i+1)+clients.get(i+2);
// System.out.println(data);
// Option 2
records.put("line1" + count, clients.get(i));
records.put("line2" + count, clients.get(i + 1));
records.put("line3" + count, clients.get(i + 2));
count++;
}// end for loop.
// reset our count variable to zero.
// if you do not, the count of rows actually inserted will be
// incorrectly calculated.
for (int i = 0; i < records.size(); i++) {
if ((records.get("line1" + i) != null)
&& (records.get("line2" + i) != null)
&& (records.get("line3" + i) != null)) {
LINE1 = records.get("line1" + i);
DEALERNUMBER = LINE1.substring(6, 13).trim();
BRANCHNUMBER = LINE1.substring(13, 22).trim();
//CUSIP = LINE1.substring(22, 31);
ESTABLISHEDDATE = LINE1.substring(59, 67).trim();
LASTMAINTENANCEDATE = LINE1.substring(67, 75).trim();
ACCOUNTTYPE = LINE1.substring(86, 87).trim();
STATECODE = LINE1.substring(90, 93).trim();
STATE = props.getProperty(STATECODE);
SSN = LINE1.substring(93, 102).trim();
try {
SSN = CryptWorker.encrypt(SSN, key);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
System.out.println("c1");
log.error(e.getMessage());
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
System.out.println("c2");
log.error(e.getMessage());
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
System.out.println("c3");
log.error(e.getMessage());
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
System.out.println("c4");
log.error(e.getMessage());
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
System.out.println("c5");
log.error(e.getMessage());
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
System.out.println("c6");
log.error(e.getMessage());
e.printStackTrace();
}catch(Exception e){
log.error(e.getMessage());
e.printStackTrace();
System.out.println("c7");
}
SSNSTATUS = LINE1.substring(102, 103).trim();
SWP = LINE1.substring(103, 104).trim();
ZIPCODE1 = LINE1.substring(115, 120).trim();
ZIPCODE2 = LINE1.substring(120, 124).trim();
NAV = LINE1.substring(155, 156).trim();
INTERESTEDPARTY = LINE1.substring(157, 158).trim();
ACCOUNTNUMBER = LINE1.substring(38, 58).trim();
SUBACCOUNT = LINE1.substring(111, 112).trim();
if (SUBACCOUNT.equals("0")) {
SUBACCOUNT = "TRUE";
} else {
SUBACCOUNT = "FALSE";
}
// Create the account name=fundname+accountnumber.
ACCOUNTNAME = FUNDNAME + ACCOUNTNUMBER;
LINE2 = records.get("line2" + i);
REGLINE1 = LINE2.substring(10, 45).trim();
REGLINE2 = LINE2.substring(45, 80).trim();
REGLINE3 = LINE2.substring(80, 115).trim();
REGLINE4 = LINE2.substring(115, 150).trim();
LINE3 = records.get("line3" + i).trim();
REGLINE5 = LINE3.substring(6, 41).trim();
REGLINE6 = LINE3.substring(41, 76).trim();
REGLINE7 = LINE3.substring(76, 111).trim();
REPID = LINE3.substring(111, 120).trim();
System.out.println("ACCOUNT NUMBER: " + ACCOUNTNUMBER);
System.out.println("REPID: " + REPID);
System.out.println("DEALER NUMBER: " + DEALERNUMBER);
System.out.println("DEALER BRANCH: " + BRANCHNUMBER);
System.out.println("ESTABLISHED DATE: " + ESTABLISHEDDATE);
System.out.println("LAST MAINT. DATE: " + LASTMAINTENANCEDATE);
System.out.println("ACCOUNT TYPE: " + ACCOUNTTYPE);
//System.out.println("CUSIP: " + CUSIP);
System.out.println("STATE CODE: " + STATECODE);
System.out.println("STATE: " + STATE);
System.out.println("SSN: " + SSN);
System.out.println("SSN STATUS CODE: " + SSNSTATUS);
System.out.println("SYSTEMATIC WITHDRAWAL PLAN: " + SWP);
System.out.println("ZIP CODE: " + ZIPCODE1 + " " + ZIPCODE2);
System.out.println("NAV: " + NAV);
System.out.println("INT. PARTY: " + INTERESTEDPARTY);
System.out.println("SUBACCOUNT: " + SUBACCOUNT);
System.out.println("ADDRESS: ");
System.out.println(REGLINE1);
System.out.println(REGLINE2);
System.out.println(REGLINE3);
System.out.println(REGLINE4);
System.out.println(REGLINE5);
System.out.println(REGLINE6);
System.out.println(REGLINE7);
ADDRESS = REGLINE1 + "\n" + REGLINE2 + "\n" + REGLINE3 + "\n"
+ REGLINE4 + "\n" + REGLINE5 + "\n" + REGLINE6 + "\n"
+ REGLINE7;
System.out
.println("------------------------------------------------------------");
//Here we should encrypt the ssn.
FinancialAccountId id = new FinancialAccountId(ACCOUNTNUMBER,
FUNDNAME, SSN);
FinancialAccount fa = new FinancialAccount();
fa.setId(id);
fa.setDealernumber(DEALERNUMBER);
fa.setAccountname(ACCOUNTNAME);
fa.setBranchnumber(BRANCHNUMBER);
fa.setEstablisheddate(ESTABLISHEDDATE);
fa.setLastmaintenancedate(LASTMAINTENANCEDATE);
fa.setAccounttype(ACCOUNTTYPE);
fa.setAddress(ADDRESS);
fa.setState(STATE);
fa.setZipcode(ZIPCODE1 + " "+ ZIPCODE2);
fa.setSwp(SWP);
fa.setNav(NAV);
fa.setInterestedparty(INTERESTEDPARTY);
fa.setRepid(REPID);
fa.setBalance(new BigDecimal("00.00"));
fa.setSubaccount(SUBACCOUNT);
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.saveOrUpdate(fa);
// This closes the session.
// If you call it again, you'll get an exception
session.getTransaction().commit();
parsed = true;
}
}
try {
} catch (HibernateException he) {
he.printStackTrace();
log.error("Database Exception creating Financial Accounts for "+coid+". Hibernate Exception "+he.getMessage());
}
return parsed;
} //end static void main.
[/code][/code]
|