diff --git a/Order Processor/.classpath b/Order Processor/.classpath index 18d70f0..d0b16d0 100644 --- a/Order Processor/.classpath +++ b/Order Processor/.classpath @@ -1,6 +1,7 @@ - + + diff --git a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/DB/OrderDB.java b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/DB/OrderDB.java new file mode 100644 index 0000000..d8eb8ed --- /dev/null +++ b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/DB/OrderDB.java @@ -0,0 +1,242 @@ +/** + * OrderDB.java + * @date Jan 8, 2011 + * @author ricky barrette + * @author Twenty Codes, LLC + */ +package com.TwentyCodes.java.OrderProcessor.DB; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Locale; +import java.util.Scanner; + +import com.TwentyCodes.java.OrderProcessor.InvalidDateFormatException; +import com.TwentyCodes.java.OrderProcessor.Main; +import com.TwentyCodes.java.OrderProcessor.Order; +import com.TwentyCodes.java.OrderProcessor.OrderExistsException; +import com.TwentyCodes.java.OrderProcessor.ProgressListener; + +/** + * An SQLite Order Data Base + * @author ricky barrette + */ +public class OrderDB { + + private final String CREATE_TABLE = "CREATE TABLE Orders " + + "(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," + + " 'item' blob);"; + + private final String INSERT = "insert into Orders (item) values (?);"; + private ProgressListener mListener; + private PreparedStatement prep; + private String dbLocation; + + /** + * Creates a new OrderDB + * @author ricky barrette + * @throws ClassNotFoundException + * @throws SQLException + * @throws Exception + */ + public OrderDB() throws ClassNotFoundException, SQLException { + dbLocation = System.getProperty("user.home") +"/.TwentyCodesOrders"; + Class.forName("org.sqlite.JDBC"); + Connection conn = getConnection(); + Statement stat = conn.createStatement(); + +// stat.executeUpdate("drop table if exists Orders;"); + try { + stat.executeUpdate(CREATE_TABLE); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + conn.close(); + } + + /** + * returns the row count + * @return number orders in the database + * @throws SQLException + * @author ricky barrette + */ + public int getCount() throws SQLException{ + int count = -1; + Connection conn = getConnection(); + ResultSet rs = conn.createStatement().executeQuery("select COUNT(*) from Orders;"); + + if(rs.next()) + count = rs.getInt(1); + conn.close(); + return count; + } + + /** + * gets the connection to the data base + * @return + * @throws SQLException + * @author ricky barrette + */ + private Connection getConnection() throws SQLException{ + return DriverManager.getConnection("jdbc:sqlite:"+ dbLocation+".db"); + } + + /** + * gets all orders from the database + * @return a list of orders + * @throws SQLException + * @throws IOException + * @throws ClassNotFoundException + * @author ricky barrette + */ + public ArrayList getAllOrders() throws SQLException, IOException, ClassNotFoundException { + + Connection conn = getConnection(); + Statement stat = conn.createStatement(); + ResultSet rs = stat.executeQuery("select * from Orders;"); + ArrayList list = new ArrayList(); + + ByteArrayInputStream bais; + ObjectInputStream ins; + while (rs.next()) { + bais = new ByteArrayInputStream(rs.getBytes("item")); + ins = new ObjectInputStream(bais); + list.add((Order) ins.readObject()); + ins.close(); + } + rs.close(); + conn.close(); + Collections.sort(list); + return list; + } + + /** + * loads the orders from the specified *.CSV file + * @param file to be parsed + * @author ricky barrette + * @throws SQLException + * @throws IOException + * @throws ClassNotFoundException + * @throws OrderExistsException + */ + public void load(File file) throws SQLException, IOException, OrderExistsException, ClassNotFoundException{ + Scanner scan = new Scanner(file); + int line = 1; + + Connection conn = getConnection(); + prep = conn.prepareStatement(INSERT); + conn.setAutoCommit(true); + + while(scan.hasNextLine()){ + + if(mListener != null) + mListener.onProgressUpdate(line++); + + if(Main.DEBUG) + System.out.println("\non line: "+ line); + + try { + addOrder(new Order(scan.nextLine())); + } catch (NumberFormatException e) { + e.printStackTrace(); + } catch (InvalidDateFormatException e) { + e.printStackTrace(); + } + } + } + + /** + * saves an order to the database + * @param order + * @throws IOException + * @throws SQLException + * @author ricky barrette + */ + private void saveOrder(Order order) throws IOException, SQLException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(bos); + + oos.writeObject(order); + oos.flush(); + oos.close(); + bos.close(); + + prep.setBytes(1, bos.toByteArray()); + prep.execute(); + } + + + /** + * sets a listener for db progress updates + * @param listener + * @author ricky barrette + */ + public void setOnProgressListerner(ProgressListener listener) { + mListener = listener; + } + + /** + * returns a list of orders that pertain any information being searched for. + * possible seraches include: item name, google order number, customer name + * @param text + * @return a list containing all possible orders + * @author ricky barrette + * @throws ClassNotFoundException + * @throws IOException + * @throws SQLException + */ + public ArrayList search(String text) throws SQLException, IOException, ClassNotFoundException { + ArrayList array = getAllOrders(); + long orderNumber = -1; + try { + orderNumber = Long.parseLong(text); + } catch (Exception e) { + // TO NOTHING + } + ArrayList list = new ArrayList(); + for(int i = 0; i < array.size(); i++){ + if(array.get(i).getItemName().equalsIgnoreCase(text) + || (orderNumber > 0) && (array.get(i).getGoogleOrderNumber() == orderNumber) + || array.get(i).getCustomerName().toLowerCase(Locale.ENGLISH).contains(text.toLowerCase(Locale.ENGLISH))) + list.add(array.get(i)); + + if(mListener != null) + mListener.onProgressUpdate(i); + } + Collections.sort(list); + return list; + } + + /** + * added the order to the database + * @param order + * @throws OrderExistsException + * @throws ClassNotFoundException + * @throws IOException + * @throws SQLException + */ + public void addOrder(Order order) throws OrderExistsException, SQLException, IOException, ClassNotFoundException { + + /* + * check the current database for an orders with a matching order number + */ + for(Order item : getAllOrders()) + if(item.getGoogleOrderNumber() == order.getGoogleOrderNumber()) + throw new OrderExistsException(order.toString()); + + saveOrder(order); + } +} \ No newline at end of file diff --git a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/Date.java b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/Date.java index 623a4d2..c7ea3e1 100644 --- a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/Date.java +++ b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/Date.java @@ -6,12 +6,15 @@ */ package com.TwentyCodes.java.OrderProcessor; +import java.io.Serializable; + /** * A Date object that represents a specific date and time * @author ricky barrette */ -public class Date implements Comparable{ - +public class Date implements Comparable, Serializable{ + + private static final long serialVersionUID = 5691938526283433531L; private byte mHour; private byte mMinute; private byte mSecond; diff --git a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/FileFilter.java b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/FileFilter.java index 63398f8..b3b9692 100644 --- a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/FileFilter.java +++ b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/FileFilter.java @@ -15,14 +15,6 @@ import java.io.File; public class FileFilter extends javax.swing.filechooser.FileFilter { public final static String csv = "csv"; - - /** - * Creates a new FileFilter - * @author ricky barrette - */ - public FileFilter() { - // TODO Auto-generated constructor stub - } /** * (non-Javadoc) diff --git a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/Order.java b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/Order.java index 4769224..35ccfc8 100644 --- a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/Order.java +++ b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/Order.java @@ -6,13 +6,16 @@ package com.TwentyCodes.java.OrderProcessor; +import java.io.Serializable; + /** * This class will represent a single order * @author ricky barrette */ -public class Order implements Comparable{ +public class Order implements Comparable, Serializable{ + private static final long serialVersionUID = 810485062967800884L; private String mItemName; private long mGoogleOrderNumber; private Date mOrderCreationDate; diff --git a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/GetStatsDialog.java b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/GetStatsDialog.java index 425ac7d..b64dae5 100644 --- a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/GetStatsDialog.java +++ b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/GetStatsDialog.java @@ -47,6 +47,7 @@ public class GetStatsDialog extends JFrame implements ActionListener { private JRadioButton mOct; private JTextField textField; private JButton mOkButton; + @SuppressWarnings("unused") private Months mTimeFrame = Months.JAN; /** diff --git a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/MainWindow.java b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/MainWindow.java index 520eaf1..7e7139c 100644 --- a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/MainWindow.java +++ b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/MainWindow.java @@ -14,6 +14,7 @@ import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.LineNumberReader; +import java.sql.SQLException; import javax.swing.JButton; import javax.swing.JFileChooser; @@ -22,7 +23,8 @@ import javax.swing.JPanel; import javax.swing.JProgressBar; import com.TwentyCodes.java.OrderProcessor.FileFilter; -import com.TwentyCodes.java.OrderProcessor.OrderDB; +import com.TwentyCodes.java.OrderProcessor.DB.OrderDB; +import com.TwentyCodes.java.OrderProcessor.OrderExistsException; import com.TwentyCodes.java.OrderProcessor.ProgressListener; import com.TwentyCodes.java.OrderProcessor.UncaughtExceptionHandler; @@ -69,11 +71,18 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen setVisible(true); pack(); - db = new OrderDB(); + try { + db = new OrderDB(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + fc = new JFileChooser(); fc.setFileFilter(new FileFilter()); - db.setOnProgressListerner(this); +// db.setOnProgressListerner(this); } /** @@ -111,6 +120,18 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen db.load(fc.getSelectedFile()); } catch (FileNotFoundException e) { e.printStackTrace(); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (OrderExistsException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } } }); diff --git a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/SearchDialog.java b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/SearchDialog.java index c088b6f..52c60ab 100644 --- a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/SearchDialog.java +++ b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/SearchDialog.java @@ -10,6 +10,8 @@ import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.io.IOException; +import java.sql.SQLException; import java.util.ArrayList; import javax.swing.JButton; @@ -94,16 +96,29 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList public void run() { mProgressBar.setString("Searching for: "+mTextField.getText()); mProgressBar.setStringPainted(true); - mProgressBar.setMaximum(MainWindow.db.getSize()); + try { + mProgressBar.setMaximum(MainWindow.db.getCount()); + } catch (SQLException e1) { + e1.printStackTrace(); + } MainWindow.db.setOnProgressListerner(SearchDialog.this); mOutput.setText(null); float possible = 0; float actual = 0; int total = 0; - ArrayList list = MainWindow.db.search(mTextField.getText()); + ArrayList list = null; + try { + list = MainWindow.db.search(mTextField.getText()); + } catch (SQLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } mListSize = list.size(); - mProgressBar.setMaximum(MainWindow.db.getSize() + mListSize); + mProgressBar.setMaximum(mProgressBar.getMaximum() + mListSize); isProcessing = true; mProgressBar.setString("Processing Order: "+ 1 +" of "+ mListSize); pack(); @@ -209,7 +224,7 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList } if(isProcessing){ mProgressBar.setString("Processing Order: "+ mListSize +" of "+ progress); - mProgressBar.setValue(progress + MainWindow.db.getSize()+1); +// mProgressBar.setValue(progress + MainWindow.db.getSize()+1); } else mProgressBar.setValue(progress); }