i improved the speed of file parsing, be making less database hits
This commit is contained in:
@@ -37,13 +37,14 @@ import com.TwentyCodes.java.OrderProcessor.ProgressListener;
|
||||
*/
|
||||
public class OrderDB {
|
||||
|
||||
// private final String CREATE_TABLE = "CREATE TABLE Orders (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, 'item' blob);";
|
||||
private final String CREATE_TABLE = "CREATE TABLE Orders (id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, item blob);";
|
||||
|
||||
private final String INSERT = "insert into Orders (item) values (?);";
|
||||
private ProgressListener mListener;
|
||||
private PreparedStatement prep;
|
||||
// private String dbLocation;
|
||||
private boolean isLoadingFile = false;
|
||||
private ArrayList<Order> list;
|
||||
|
||||
/**
|
||||
* Creates a new OrderDB
|
||||
@@ -57,15 +58,15 @@ public class OrderDB {
|
||||
// Class.forName("org.sqlite.JDBC");
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
Connection conn = getConnection();
|
||||
// Statement stat = conn.createStatement();
|
||||
Statement stat = conn.createStatement();
|
||||
|
||||
// stat.executeUpdate("drop table if exists Orders;");
|
||||
// try {
|
||||
// stat.executeUpdate(CREATE_TABLE);
|
||||
// } catch (SQLException e) {
|
||||
// //most likely the table already exist
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
try {
|
||||
stat.executeUpdate(CREATE_TABLE);
|
||||
} catch (SQLException e) {
|
||||
//most likely the table already exist
|
||||
e.printStackTrace();
|
||||
}
|
||||
conn.close();
|
||||
}
|
||||
|
||||
@@ -101,10 +102,12 @@ public class OrderDB {
|
||||
/*
|
||||
* check the current database for an orders with a matching order number
|
||||
*/
|
||||
for(Order item : getAllOrders())
|
||||
for(Order item : list)
|
||||
if(item.getGoogleOrderNumber() == order.getGoogleOrderNumber())
|
||||
throw new OrderExistsException(order.toString());
|
||||
|
||||
list.add(order);
|
||||
|
||||
saveOrder(order);
|
||||
}
|
||||
|
||||
@@ -129,7 +132,7 @@ public class OrderDB {
|
||||
ObjectInputStream ins;
|
||||
while (rs.next()) {
|
||||
if(mListener != null && ! isLoadingFile)
|
||||
mListener.onProgressUpdate(row++, count);
|
||||
mListener.onProgressUpdate("Getting All Orders", false, row++, count);
|
||||
bais = new ByteArrayInputStream(rs.getBytes("item"));
|
||||
ins = new ObjectInputStream(bais);
|
||||
list.add((Order) ins.readObject());
|
||||
@@ -181,8 +184,11 @@ public class OrderDB {
|
||||
public void load(File file) throws SQLException, IOException, ClassNotFoundException{
|
||||
|
||||
// Scanner scan = new Scanner(file);
|
||||
prep = null;
|
||||
list = null;
|
||||
|
||||
CSVParser csvParser = new CSVParser(new FileInputStream(file));
|
||||
list = new ArrayList<Order>(getAllOrders());
|
||||
|
||||
int line = 1;
|
||||
int lineCount = countLines(file);
|
||||
@@ -191,25 +197,32 @@ public class OrderDB {
|
||||
Connection conn = getConnection();
|
||||
prep = conn.prepareStatement(INSERT);
|
||||
conn.setAutoCommit(true);
|
||||
|
||||
while(csvParser.lastLineNumber() < lineCount){
|
||||
|
||||
if(mListener != null)
|
||||
mListener.onProgressUpdate(line++, lineCount);
|
||||
|
||||
if(Main.DEBUG)
|
||||
System.out.println("\non line: "+ line);
|
||||
|
||||
try {
|
||||
addOrder(new Order(csvParser.getLine()));
|
||||
} catch (NumberFormatException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvalidDateFormatException e) {
|
||||
e.printStackTrace();
|
||||
} catch (OrderExistsException e) {
|
||||
while (csvParser.lastLineNumber() < lineCount) {
|
||||
|
||||
if (mListener != null)
|
||||
mListener.onProgressUpdate(file.toString() + " ~ Parsing Order: "+ line +" of "+ lineCount, false, line++, lineCount);
|
||||
|
||||
if (Main.DEBUG)
|
||||
System.out.println("\non line: " + line);
|
||||
|
||||
try {
|
||||
addOrder(new Order(csvParser.getLine()));
|
||||
} catch (NumberFormatException e) {
|
||||
e.printStackTrace();
|
||||
} catch (OrderExistsException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvalidDateFormatException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
prep.close();
|
||||
conn.close();
|
||||
isLoadingFile = false;
|
||||
}
|
||||
@@ -233,6 +246,7 @@ public class OrderDB {
|
||||
|
||||
prep.setBytes(1, bos.toByteArray());
|
||||
prep.executeUpdate();
|
||||
// prep.addBatch();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -265,7 +279,7 @@ public class OrderDB {
|
||||
ObjectInputStream ins;
|
||||
while (rs.next()) {
|
||||
if(mListener != null && ! isLoadingFile)
|
||||
mListener.onProgressUpdate(row++, count);
|
||||
mListener.onProgressUpdate("Searching for: "+text, false, row++, count);
|
||||
bais = new ByteArrayInputStream(rs.getBytes("item"));
|
||||
ins = new ObjectInputStream(bais);
|
||||
Order order = (Order) ins.readObject();
|
||||
|
||||
@@ -17,6 +17,6 @@ public interface ProgressListener {
|
||||
* @param progress
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void onProgressUpdate(int progress, int max);
|
||||
public void onProgressUpdate(String string, boolean isIndeterminate, int progress, int max);
|
||||
|
||||
}
|
||||
@@ -158,12 +158,12 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
|
||||
* @author ricky barrette
|
||||
*/
|
||||
@Override
|
||||
public void onProgressUpdate(int progress, int max) {
|
||||
public void onProgressUpdate(String string, boolean isIndeterminate, int progress, int max) {
|
||||
mProgressBar.setMaximum(max);
|
||||
mProgressBar.setValue(progress);
|
||||
mProgressBar.setString(mCurrentFile + " ~ Parsing Order: "+ progress +" of "+mProgressBar.getMaximum());
|
||||
mProgressBar.setString(string);
|
||||
if(mProgressBar.isIndeterminate()) {
|
||||
mProgressBar.setIndeterminate(false);
|
||||
mProgressBar.setIndeterminate(isIndeterminate);
|
||||
pack();
|
||||
}
|
||||
if(progress >= mProgressBar.getMaximum()){
|
||||
|
||||
@@ -56,7 +56,7 @@ public class OrderPane extends JScrollPane {
|
||||
int total = 0;
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
if(mListener != null)
|
||||
mListener.onProgressUpdate(i, list.size());
|
||||
mListener.onProgressUpdate("Processing Order: "+ i +" of "+ list.size(), false, i, list.size());
|
||||
switch (list.get(i).getFinancialStatus()) {
|
||||
case CANCELLED:
|
||||
updateTextPane(list.get(i).toString(), TextStyle.RED);
|
||||
|
||||
@@ -257,17 +257,18 @@ public class SearchPanel extends JPanel implements ActionListener, ProgressListe
|
||||
* @author ricky barrette
|
||||
*/
|
||||
@Override
|
||||
public void onProgressUpdate(int progress, int max) {
|
||||
public void onProgressUpdate(String string, boolean isIndeterminate, int progress, int max) {
|
||||
mProgressBar.setMaximum(max);
|
||||
mProgressBar.setValue(progress);
|
||||
mProgressBar.setIndeterminate(isIndeterminate);
|
||||
mProgressBar.setString(string);
|
||||
|
||||
if(Main.DEBUG)
|
||||
System.out.println("search progress = "+ progress);
|
||||
|
||||
progress++;
|
||||
if(mProgressBar.isIndeterminate()) {
|
||||
mProgressBar.setIndeterminate(false);
|
||||
mFrame.pack();
|
||||
}
|
||||
|
||||
if(isProcessing){
|
||||
mProgressBar.setString("Processing Order: "+ mListSize +" of "+ progress);
|
||||
try {
|
||||
mProgressBar.setValue(progress + MainWindow.db.getCount());
|
||||
} catch (SQLException e) {
|
||||
|
||||
@@ -79,9 +79,11 @@ public class ShowAllPanel extends JPanel implements ActionListener, ProgressList
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProgressUpdate(int progress, int max) {
|
||||
public void onProgressUpdate(String string, boolean isIndeterminate, int progress, int max) {
|
||||
mProgressBar.setMaximum(max);
|
||||
mProgressBar.setValue(progress);
|
||||
mProgressBar.setIndeterminate(isIndeterminate);
|
||||
mProgressBar.setString(string);
|
||||
if(progress >= mProgressBar.getMaximum()){
|
||||
mFrame.pack();
|
||||
mProgressBar.setString("Done");
|
||||
|
||||
Reference in New Issue
Block a user