From 8749006ea5d225865f2c940c6fd4e20a5b69b17e Mon Sep 17 00:00:00 2001 From: ricky barrette Date: Wed, 9 Feb 2011 18:50:45 +0000 Subject: [PATCH] added a checkout button to the Main Panel. It allows the user to "checkout" a remote database to a local database removed the warning label from main panel, and moved all messages to the progress bar --- OrderProcessor/.orderprocessor.properties | 4 +- .../java/OrderProcessor/DB/OrderDB.java | 93 +++++++++++++++---- .../java/OrderProcessor/UI/MainWindow.java | 46 ++++++--- OrderProcessor/version infomation/changelog | 4 + OrderProcessor/version infomation/version | 2 +- 5 files changed, 115 insertions(+), 34 deletions(-) diff --git a/OrderProcessor/.orderprocessor.properties b/OrderProcessor/.orderprocessor.properties index 6cd9176..6a74533 100644 --- a/OrderProcessor/.orderprocessor.properties +++ b/OrderProcessor/.orderprocessor.properties @@ -1,7 +1,7 @@ #---Order Processor Settings--- -#Tue Feb 08 11:33:05 EST 2011 +#Wed Feb 09 13:48:50 EST 2011 database_name=Orders -server_parrword=3qR0VY2ngKq+4rxX59oRtmHOIBKKLb96 +server_parrword=Ekf4syi+03SEjxLksxRbuuxV4qpKog5G save_password=true use_remote_server=true server_location=tcdevsvn1 diff --git a/OrderProcessor/src/com/TwentyCodes/java/OrderProcessor/DB/OrderDB.java b/OrderProcessor/src/com/TwentyCodes/java/OrderProcessor/DB/OrderDB.java index 85c32b3..d32160c 100644 --- a/OrderProcessor/src/com/TwentyCodes/java/OrderProcessor/DB/OrderDB.java +++ b/OrderProcessor/src/com/TwentyCodes/java/OrderProcessor/DB/OrderDB.java @@ -35,6 +35,7 @@ import com.TwentyCodes.java.OrderProcessor.Order; import com.TwentyCodes.java.OrderProcessor.OrderExistsException; import com.TwentyCodes.java.OrderProcessor.ProgressListener; import com.TwentyCodes.java.OrderProcessor.Status; +import com.TwentyCodes.java.OrderProcessor.UI.MainWindow; import com.TwentyCodes.java.OrderProcessor.UI.SettingsPanel; /** @@ -51,7 +52,7 @@ public class OrderDB { private PreparedStatement prep; private String dbLocation; private boolean isLoadingFile = false; - private ArrayList list; + private ArrayList mList; private boolean isUsingRemoteDatabase; /** @@ -144,7 +145,7 @@ public class OrderDB { /* * check the current database for an orders with a matching order number */ - for(Order item : list) + for(Order item : mList) if(item.getGoogleOrderNumber() == order.getGoogleOrderNumber()) if(item.getFulfillmentStatus() == Status.NEW ) @@ -152,11 +153,65 @@ public class OrderDB { else throw new OrderExistsException(order.toString()); - list.add(order); + mList.add(order); saveOrder(order); } + /** + * copies remote database to local DB + * @throws SQLException + * @throws IOException + * @throws ClassNotFoundException + * @author ricky barrette + */ + public void checkoutDatabase() throws SQLException, IOException, ClassNotFoundException{ + //get all from remote DB + ArrayList list = this.getAllOrders(); + //switch over to local DB + Class.forName("org.sqlite.JDBC"); + dbLocation = System.getProperty("user.home") +"/.TwentyCodesOrders"; + + isUsingRemoteDatabase = false; + + //connect to local DB + Connection conn = this.getConnection(); + Statement stat = conn.createStatement(); + +// //remove old table +// stat.executeUpdate("drop table if exists Orders;"); + + //create tabe if it needs to be created + try { + stat.executeUpdate(CREATE_TABLE_LOCAL); + } catch (Exception e1) { + e1.printStackTrace(); + } + + mList = this.getAllOrders(); + + //the insert statement + prep = conn.prepareStatement(INSERT); + conn.setAutoCommit(true); + + //copy the orders over + int i = 1; + for(Order item : list) { + if(mListener != null) + mListener.onProgressUpdate("Checking out order "+ i++ +" of "+ list.size(), false, i, list.size()); + try { + addOrder(item); + } catch (OrderExistsException e) { + e.printStackTrace(); + } + } + + //reload the remote DB + MainWindow.loadDatabase(); + + mList = null; + } + /** * counts the number of lines in a csv file * @param file to be read @@ -171,7 +226,7 @@ public class OrderDB { cnt++; return cnt; } - + /** * deletes the old order that IS BEING replaced * @throws SQLException @@ -244,19 +299,6 @@ public class OrderDB { Collections.sort(list); return list; } - - /** - * Gets all orders from the database - * @param conn to database - * @return ResultSet containing all orders - * @throws SocketException - * @throws SQLException - * @author ricky barrette - */ - private ResultSet getOrders(Connection conn) throws SocketException, SQLException { - Statement stat = conn.createStatement(); - return stat.executeQuery("select * from Orders;"); - } /** * gets the connection to the data base @@ -314,6 +356,19 @@ public class OrderDB { return count; } + /** + * Gets all orders from the database + * @param conn to database + * @return ResultSet containing all orders + * @throws SocketException + * @throws SQLException + * @author ricky barrette + */ + private ResultSet getOrders(Connection conn) throws SocketException, SQLException { + Statement stat = conn.createStatement(); + return stat.executeQuery("select * from Orders;"); + } + /** * gets the properties from the properties file * @return properties @@ -344,10 +399,10 @@ public class OrderDB { public void load(File file) throws SQLException, IOException, ClassNotFoundException{ prep = null; - list = null; + mList = null; CSVParser csvParser = new CSVParser(new FileInputStream(file)); - list = new ArrayList(getAllOrders()); + mList = new ArrayList(getAllOrders()); int line = 1; int lineCount = countLines(file); diff --git a/OrderProcessor/src/com/TwentyCodes/java/OrderProcessor/UI/MainWindow.java b/OrderProcessor/src/com/TwentyCodes/java/OrderProcessor/UI/MainWindow.java index e933a2b..322c660 100644 --- a/OrderProcessor/src/com/TwentyCodes/java/OrderProcessor/UI/MainWindow.java +++ b/OrderProcessor/src/com/TwentyCodes/java/OrderProcessor/UI/MainWindow.java @@ -46,11 +46,12 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen private String mCurrentFile; private UncaughtExceptionHandler mExceptionReport = new UncaughtExceptionHandler(this.getClass()); private static JLabel orderCountLabel; - private static JLabel mWarningLabel; +// private static JLabel mWarningLabel; private static JButton mConnectButton; private static JPasswordField mPassWord; private static boolean isUsingRemoteServer; private JLabel lblDatabasePassword; + private static JButton mCheckout; /** * Creates a new MainWindow @@ -98,7 +99,7 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen mLoadFileButton = new JButton("Load File", new ImageIcon(getClass().getResource("/database_add.png"))); mLoadFileButton.addActionListener(this); - mWarningLabel = new JLabel(""); +// mWarningLabel = new JLabel(""); orderCountLabel = new JLabel(""); panel.add(new JLabel("Total Orders in the database:")); @@ -118,7 +119,12 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen mConnectButton.addActionListener(this); panel.add(mConnectButton); - panel.add(mWarningLabel); + mCheckout = new JButton("Checkout Remote Database", new ImageIcon(getClass().getResource("/database_down.png"))); + mCheckout.addActionListener(this); + mCheckout.setEnabled(false); + panel.add(mCheckout); + +// panel.add(mWarningLabel); //progress bar mProgressBar = new JProgressBar(); @@ -161,28 +167,25 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen try { db = isUsingRemoteServer ? new OrderDB(new String(mPassWord.getPassword())) : new OrderDB(); orderCountLabel.setText(db.getCount()+""); - mWarningLabel.setText("Connected"); + mProgressBar.setString("Connected"); mPassWord.setEnabled(false); } catch (SQLException e) { e.printStackTrace(); - mWarningLabel.setText(e.getMessage()); - mWarningLabel.setVisible(true); + mProgressBar.setString(e.getMessage()); mConnectButton.setEnabled(true); orderCountLabel.setText(""); } catch (SocketException e) { - mWarningLabel.setText(e.getMessage()); - mWarningLabel.setVisible(true); + mProgressBar.setString(e.getMessage()); mConnectButton.setEnabled(true); orderCountLabel.setText(""); } catch (NullPointerException e){ - mWarningLabel.setText("Database not avilable"); - mWarningLabel.setVisible(true); + mProgressBar.setString("Database not avilable"); mConnectButton.setEnabled(true); orderCountLabel.setText(""); } catch (ClassNotFoundException e) { e.printStackTrace(); } - mProgressBar.setString(mWarningLabel.getText()); + mCheckout.setEnabled( (isUsingRemoteServer && ! mConnectButton.isEnabled()) ); mProgressBar.setIndeterminate(false); } }); @@ -233,6 +236,25 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen if(e.getSource() == mConnectButton){ loadDatabase(); } + + if(e.getSource() == mCheckout) + java.util.concurrent.Executors.newSingleThreadExecutor().submit(new Runnable() { + public void run() { + db.setOnProgressListerner(MainWindow.this); + try { + db.checkoutDatabase(); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + }); } /** @@ -257,7 +279,7 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen } catch (SQLException e) { e.printStackTrace(); } catch (SocketException e) { - mWarningLabel.setText(e.getMessage()); + mProgressBar.setString(e.getMessage()); } } } diff --git a/OrderProcessor/version infomation/changelog b/OrderProcessor/version infomation/changelog index f833cde..3f68bee 100644 --- a/OrderProcessor/version infomation/changelog +++ b/OrderProcessor/version infomation/changelog @@ -57,3 +57,7 @@ slight modification to order pane updateTextPane() s 1.1.6 b 51 added status PAYMENT_DECLINED + +1.1.7 b 52 +added a checkout button to the Main Panel. It allows the user to "checkout" a remote database to a local database +removed the warning label from main panel, and moved all messages to the progress bar diff --git a/OrderProcessor/version infomation/version b/OrderProcessor/version infomation/version index 00bcb15..c1420e8 100644 --- a/OrderProcessor/version infomation/version +++ b/OrderProcessor/version infomation/version @@ -1 +1 @@ -1.1.6 b 51 \ No newline at end of file +1.1.7 b 52 \ No newline at end of file