I created a new OrderDB.java that uses sqlite to store its orders
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
|
||||
<classpathentry kind="lib" path="/home/ricky/Downloads/sqlitejdbc-v056.jar"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
|
||||
@@ -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<Order> getAllOrders() throws SQLException, IOException, ClassNotFoundException {
|
||||
|
||||
Connection conn = getConnection();
|
||||
Statement stat = conn.createStatement();
|
||||
ResultSet rs = stat.executeQuery("select * from Orders;");
|
||||
ArrayList<Order> list = new ArrayList<Order>();
|
||||
|
||||
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<Order> search(String text) throws SQLException, IOException, ClassNotFoundException {
|
||||
ArrayList<Order> array = getAllOrders();
|
||||
long orderNumber = -1;
|
||||
try {
|
||||
orderNumber = Long.parseLong(text);
|
||||
} catch (Exception e) {
|
||||
// TO NOTHING
|
||||
}
|
||||
ArrayList<Order> list = new ArrayList<Order>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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<Date>{
|
||||
|
||||
public class Date implements Comparable<Date>, Serializable{
|
||||
|
||||
private static final long serialVersionUID = 5691938526283433531L;
|
||||
private byte mHour;
|
||||
private byte mMinute;
|
||||
private byte mSecond;
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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<Order>{
|
||||
public class Order implements Comparable<Order>, Serializable{
|
||||
|
||||
private static final long serialVersionUID = 810485062967800884L;
|
||||
private String mItemName;
|
||||
private long mGoogleOrderNumber;
|
||||
private Date mOrderCreationDate;
|
||||
|
||||
@@ -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;
|
||||
|
||||
/**
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -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<Order> list = MainWindow.db.search(mTextField.getText());
|
||||
ArrayList<Order> 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user