FileOutputStream oOutStrm = null;
FileInputStream oInStrm = null;
BufferedInputStream oInBuff;
BufferedOutputStream oOutBuff;
// File with a description of all errors
FileWriter oBadWrtr = null;
// File with just the rows that failed to be inserted
FileWriter oDiscardWrtr = null;
// Intermediate buffer for SQL composition
StringBuffer oSQL = new StringBuffer();
StringBuffer oRow = new StringBuffer();
if (DebugFile.trace) DebugFile.writeln(" Begin inserting data...");
// ********************
// Open data input file
if (sCmd.equalsIgnoreCase("APPEND") || sCmd.equalsIgnoreCase("UPDATE") || sCmd.equalsIgnoreCase("APPENDUPDATE")) {
try {
if (DebugFile.trace) {
DebugFile.writeln(" new FileInputStream("+sInFile+")");
DebugFile.writeln(" input file length is "+String.valueOf(oInFile.length())+" bytes");
}
oInStrm = new FileInputStream(oInFile);
} catch (FileNotFoundException fnfe) {
try { oConn.close(); } catch (Exception ignore) {}
throw new ImportExportException("File not found opening stream "+sInFile, fnfe);
}
// Java direct I/O performance sucks so use an intermediate buffer
oInBuff = new BufferedInputStream(oInStrm);
// **********************************
// Open bad and discard file writters
if (sBadFile!=null) {
try {
if (DebugFile.trace) DebugFile.writeln(" new FileWriter("+sBadFile+")");
oBadWrtr = new FileWriter(sBadFile, false);
}
catch (FileNotFoundException neverthrown) {}
catch (IOException ioe) {
try { oInBuff.close(); } catch (Exception ignore) {}
try { oInStrm.close(); } catch (Exception ignore) {}
try { oConn.close(); } catch (Exception ignore) {}
throw new ImportExportException("IOException "+ioe.getMessage(), ioe);
}
}
if (sDiscardFile!=null) {
try {
if (DebugFile.trace) DebugFile.writeln(" new FileWriter("+sDiscardFile+")");
oDiscardWrtr = new FileWriter(sDiscardFile, false);
}
catch (FileNotFoundException neverthrown) {}
catch (IOException ioe) {
try { oBadWrtr.close(); } catch (Exception ignore) {}
try { oInBuff.close(); } catch (Exception ignore) {}
try { oInStrm.close(); } catch (Exception ignore) {}
try { oConn.close(); } catch (Exception ignore) {}
throw new ImportExportException("IOException "+ioe.getMessage(), ioe);
}
}
// *****************************************************************
// If RECOVERABLE mode is enabled then set AutoCommit OFF else switch
// AutoCommit ON and commit each row just after insertion
try {
oImplLoad.prepare(oConn, oColumns);
if (bRecoverable)
oConn.setAutoCommit(false);
else
oConn.setAutoCommit(true);
} catch (Exception xcpt) {
try { oBadWrtr.close(); } catch (Exception ignore) {}
try { oInBuff.close(); } catch (Exception ignore) {}
try { oInStrm.close(); } catch (Exception ignore) {}
try { oImplLoad.close(); } catch (Exception ignore) {}
try { oConn.close(); } catch (Exception ignore) {}
throw new ImportExportException(xcpt.getClass().getName()+" "+xcpt.getMessage(), xcpt);
}
if (sEntity.equalsIgnoreCase("VCARDS")) {
try {
VCardParser oPrsr = new VCardParser();
oPrsr.parse(oInBuff, sCharSet);
VCardLoader oVCrdLoad = (VCardLoader) oImplLoad;
for (HashMap<String,String> oVCard : oPrsr.vcards()) {
oVCrdLoad.put(oVCard);
try {
oVCrdLoad.store(oConn, sWorkArea, iFlags);
} catch (Exception xcpt) {
iErrorCount++;
if (DebugFile.trace) DebugFile.writeln(" "+xcpt.getClass().getName()+": at line "+String.valueOf(iLine)+" "+xcpt.getMessage());
if (bRecoverable) {
if (DebugFile.trace) DebugFile.writeln(" Connection.rollback()");
try { oConn.rollback(); } catch (SQLException ignore) {}
} // fi (bRecoverable)
if (oBadWrtr!=null) {
oBadWrtr.write(xcpt.getClass().getName()+": at card "+oVCard.get("N")+" "+xcpt.getMessage()+"\r\n");
} // fi
if (oDiscardWrtr!=null) {
oDiscardWrtr.write(oVCard.get("N")+sRowDelim);
} // fi
} // catch
} // next
if (oInBuff!=null) { oInBuff.close(); }
oInBuff=null;
if (oInStrm!=null) { oInStrm.close(); }
oInStrm=null;
if (oBadWrtr!=null) { oBadWrtr.close(); oBadWrtr=null; }
if (oDiscardWrtr!=null) { oDiscardWrtr.close(); oDiscardWrtr=null; }
oImplLoad.close();
if (bRecoverable) {
if (0==iErrorCount)
oConn.commit();
else
oConn.rollback();
}
if (DebugFile.trace) DebugFile.writeln("Connection.close()");
oConn.close();
} catch (Exception xcpt) {
if (DebugFile.trace) DebugFile.writeln(" "+xcpt.getClass().getName()+" "+xcpt.getMessage());
try { if (null!=oDiscardWrtr) oDiscardWrtr.close(); } catch (Exception ignore) {}
try { if (null!=oBadWrtr) oBadWrtr.close(); } catch (Exception ignore) {}
try { if (null!=oInBuff) oInBuff.close(); } catch (Exception ignore) {}
try { if (null!=oInStrm) oInStrm.close(); } catch (Exception ignore) {}
try { oImplLoad.close(); } catch (Exception ignore) {}
try { if (bRecoverable) oConn.rollback(); } catch (Exception ignore) {}
try { oConn.close(); } catch (Exception ignore) {}
throw new ImportExportException(xcpt.getClass().getName() + " " + xcpt.getMessage());
}
} else {
// *******************************************************
// Check that all column names on input file are valid
// and resolve column names to positions for faster access
if (DebugFile.trace) DebugFile.writeln("Resolving column names to positions...");
for (c=0; c<iColFmtsCount; c++) {
DBColumn oClmn = oColumns.getColumn(c);
if (oClmn.getSqlType()!=Types.NULL) {
int cIndex = oImplLoad.getColumnIndex(oClmn.getName());
if (-1==cIndex) {
throw new ImportExportException("SQLException column "+oClmn.getName()+" not found at base table");
} else {
oClmn.setPosition(cIndex);
}
}
} // next (c)
// ******************************************
// Read data input file and insert row by row
if (DebugFile.trace) DebugFile.writeln("Begin read data from text file...");
InputStreamReader oInRdr = null;
try {
// Use an stream reader for decoding bytes into the proper charset
if (DebugFile.trace) DebugFile.writeln(" new InputStreamReader(BufferedInputStream,"+sCharSet+")");
oInRdr = new InputStreamReader(oInBuff, sCharSet);
} catch (java.io.UnsupportedEncodingException uee) {
try { oInBuff.close(); } catch (Exception ignore) {}
try { oInStrm.close(); } catch (Exception ignore) {}
try { oConn.close(); } catch (Exception ignore) {}
throw new ImportExportException("Unsupported Encoding "+sCharSet, uee);
}
try {
if (DebugFile.trace) {
String sRowDelimChr = "", sColDelimChr = "";
for (int d=0; d<sRowDelim.length(); d++) sRowDelimChr += "Chr("+String.valueOf((int)sRowDelim.charAt(d))+")";
for (int d=0; d<sColDelim.length(); d++) sColDelimChr += "Chr("+String.valueOf((int)sColDelim.charAt(d))+")";
DebugFile.writeln(" Read input stream with row delimiter "+sRowDelimChr+" and col delimiter "+sColDelimChr);
}
int nCharCount = 0;
// Read input file by one character at a time
while (((i=oInRdr.read())!=-1) && (iErrorCount<=iMaxErrors)) {
nCharCount++;
// Skip row delimiter, let r be the relative position inside the row delimiter
// then skip as many characters readed at i as they match with row delimiter + offset r
r=0;
while (i==sRowDelim.charAt(r) && i!=-1) {
r++;
i=oInRdr.read();
if (r==iRowDelimLen) break;
} // wend
// If r>0 then the row delimiter has been reached Or
// If i==-1 then it is the last line, so insert the row
if (r==iRowDelimLen || i==-1) {
if (iLine>iSkip) {
if (DebugFile.trace) DebugFile.writeln(" Processing line "+String.valueOf(iLine));
if (bPreserveSpace)
sLine = oRow.toString();
else
sLine = oRow.toString().trim();
if (sLine.length()>0) {
if (sColDelim.length()==1)
aLine = Gadgets.split(sLine, sColDelim.charAt(0));
else
aLine = Gadgets.split(sLine, sColDelim);
// If current line does have the same number of columns as in the
// file definition then report an error or raise an exception
if (aLine.length!=iColFmtsCount) {
iErrorCount++;
if (DebugFile.trace) DebugFile.writeln(" Error: at line "+String.valueOf(iLine)+" has "+String.valueOf(aLine.length)+" columns but should have "+String.valueOf(iColFmtsCount));
if (bRecoverable) {
if (DebugFile.trace) DebugFile.writeln(" Connection.rollback()");
try { oConn.rollback(); } catch (SQLException ignore) {}
}
if (oBadWrtr!=null) {
oBadWrtr.write("Error: at line "+String.valueOf(iLine)+" has "+String.valueOf(aLine.length)+" columns but should have "+String.valueOf(iColFmtsCount)+"\r\n");
oBadWrtr.write(sLine+"\r\n");
}
if (oDiscardWrtr!=null) {
oDiscardWrtr.write(sLine+sRowDelim);
}
} else {
// Up to here a single line as been readed and is kept in aLines array
try {
oImplLoad.setAllColumnsToNull();
if (bAllCaps) {
for (c=0; c<iColFmtsCount; c++) {
oColFmt = oColumns.getColumn(c);
if (oColFmt.getSqlType()!=Types.NULL) {
String sColName = oColFmt.getName();
if (DebugFile.trace) DebugFile.writeln(" ImportLoader.put("+sColName+"("+String.valueOf(oColFmt.getPosition())+"),"+aLine[c]+")");
if (sColName.equalsIgnoreCase(DB.tx_email) ||
sColName.equalsIgnoreCase(DB.tx_email_alt) ||
sColName.equalsIgnoreCase(DB.tx_alt_email) ||
sColName.equalsIgnoreCase(DB.tx_main_email) ||
sColName.equalsIgnoreCase(DB.tx_comments) ||
sColName.equalsIgnoreCase(DB.tx_remarks) ||
sColName.equalsIgnoreCase(DB.tx_nickname) ||
sColName.equalsIgnoreCase(DB.tx_pwd) ||
sColName.equalsIgnoreCase(DB.tx_pwd_sign) ||
sColName.startsWith("url_") ||
sColName.startsWith("de_") ||
sColName.startsWith("gu_") ||
sColName.startsWith("id_") ||
sColName.startsWith("tp_") )
oImplLoad.put(oColFmt.getPosition(), oColFmt.convert(aLine[c]));
else
oImplLoad.put(oColFmt.getPosition(), oColFmt.convert(aLine[c].toUpperCase()));
} // fi (getSqlType()!=NULL)
} // next c)
} else {
for (c=0; c<iColFmtsCount; c++) {
oColFmt = oColumns.getColumn(c);
if (oColFmt.getSqlType()!=Types.NULL) {
if (DebugFile.trace) DebugFile.writeln(" ImportLoader.put("+oColFmt.getName()+"("+String.valueOf(oColFmt.getPosition())+"),"+aLine[c]+")");
oImplLoad.put(oColFmt.getPosition(), oColFmt.convert(aLine[c]));
} // fi (getSqlType()!=NULL)
} // next c)
} // fi (ALLCAPS)
if (null!=sCategory) oImplLoad.put("gu_category", sCategory);
if (null!=sGuList) oImplLoad.put("gu_list", sGuList);
oImplLoad.store(oConn, sWorkArea, iFlags);
} catch (NumberFormatException xcpt) {
iErrorCount++;
if (DebugFile.trace) DebugFile.writeln(" NumberFormatException: at line "+String.valueOf(iLine)+" "+xcpt.getMessage());
if (bRecoverable) {
if (DebugFile.trace) DebugFile.writeln(" Connection.rollback()");
try { oConn.rollback(); } catch (SQLException ignore) {}
} // fi (bRecoverable)
if (oBadWrtr!=null) {
oBadWrtr.write("NumberFormatException: at line "+String.valueOf(iLine)+" "+xcpt.getMessage()+"\r\n");
oBadWrtr.write(sLine+"\r\n");
} // fi
if (oDiscardWrtr!=null) {
oDiscardWrtr.write(sLine+sRowDelim);
} // fi
} catch (SQLException xcpt) {
iErrorCount++;
if (DebugFile.trace) DebugFile.writeln(" SQLException: at line "+String.valueOf(iLine)+" "+xcpt.getMessage());
if (bRecoverable) {
if (DebugFile.trace) DebugFile.writeln(" Connection.rollback()");
try { oConn.rollback(); } catch (SQLException ignore) {}
} // fi (bRecoverable)
if (oBadWrtr!=null) {
oBadWrtr.write("SQLException: at line "+String.valueOf(iLine)+" "+xcpt.getMessage()+"\r\n");
oBadWrtr.write(sLine+"\r\n");
} // fi
if (oDiscardWrtr!=null) {
oDiscardWrtr.write(sLine+sRowDelim);
} // fi
} // catch
} // fi (aLine.length!=iColFmtsCount)
} // fi (sLine!="")
} else {
if (DebugFile.trace) DebugFile.writeln(" Skiping line "+String.valueOf(iLine));
} // fi (iLine>iSkip)
oRow.setLength(0);
if (i!=-1) oRow.append((char)i);
iLine++;
if (-1==i) break;
} else {
oRow.append((char)i);
}// fi (r==iRowDelimLen || i==-1)
} // wend
if (null!=oInRdr) oInRdr.close();
if (DebugFile.trace) {
DebugFile.writeln("End read data from text file. "+String.valueOf(nCharCount)+" characters readed with error count "+String.valueOf(iErrorCount));
}
if (oInBuff!=null) { oInBuff.close(); }
oInBuff=null;
if (oInStrm!=null) { oInStrm.close(); }
oInStrm=null;
if (oBadWrtr!=null) { oBadWrtr.close(); oBadWrtr=null; }
if (oDiscardWrtr!=null) { oDiscardWrtr.close(); oDiscardWrtr=null; }
oImplLoad.close();
if (bRecoverable) {
if (0==iErrorCount)
oConn.commit();
else
oConn.rollback();
}
if (DebugFile.trace) DebugFile.writeln("Connection.close()");
oConn.close();
} catch (SQLException xcpt) {
if (DebugFile.trace) DebugFile.writeln(" "+xcpt.getClass().getName()+": at row "+String.valueOf(r)+" "+xcpt.getMessage());
try { if (null!=oDiscardWrtr) oDiscardWrtr.close(); } catch (Exception ignore) {}
try { if (null!=oBadWrtr) oBadWrtr.close(); } catch (Exception ignore) {}
try { if (null!=oInBuff) oInBuff.close(); } catch (Exception ignore) {}
try { if (null!=oInStrm) oInStrm.close(); } catch (Exception ignore) {}
try { oImplLoad.close(); } catch (Exception ignore) {}
try { if (bRecoverable) oConn.rollback(); } catch (Exception ignore) {}
try { oConn.close(); } catch (Exception ignore) {}
throw new ImportExportException(xcpt.getClass().getName() + " " + xcpt.getMessage() + " at row " + String.valueOf(r));
}
catch (IOException xcpt) {
if (DebugFile.trace) DebugFile.writeln(" "+xcpt.getClass().getName()+": at row "+String.valueOf(r)+" "+xcpt.getMessage());
try { if (null!=oDiscardWrtr) oDiscardWrtr.close(); } catch (Exception ignore) {}
try { if (null!=oBadWrtr) oBadWrtr.close(); } catch (Exception ignore) {}
try { if (null!=oInBuff) oInBuff.close(); } catch (Exception ignore) {}
try { if (null!=oInStrm) oInStrm.close(); } catch (Exception ignore) {}
try { oImplLoad.close(); } catch (Exception ignore) {}
try { if (bRecoverable) oConn.rollback(); } catch (Exception ignore) {}
try { oConn.close(); } catch (Exception ignore) {}
throw new ImportExportException(xcpt.getClass().getName() + " " + xcpt.getMessage() + " at row " + String.valueOf(r));
}
catch (java.text.ParseException xcpt) {
if (DebugFile.trace) DebugFile.writeln(" "+xcpt.getClass().getName()+": at row "+String.valueOf(r)+" "+xcpt.getMessage());
try { if (null!=oDiscardWrtr) oDiscardWrtr.close(); } catch (Exception ignore) {}
try { if (null!=oBadWrtr) oBadWrtr.close(); } catch (Exception ignore) {}
try { if (null!=oInBuff) oInBuff.close(); } catch (Exception ignore) {}
try { if (null!=oInStrm) oInStrm.close(); } catch (Exception ignore) {}
try { oImplLoad.close(); } catch (Exception ignore) {}
try { if (bRecoverable) oConn.rollback(); } catch (Exception ignore) {}