minor tweaks to how i handle progress updates, and database searchs

This commit is contained in:
2011-01-13 16:04:27 +00:00
parent 9f29f38a35
commit b2da630680
9 changed files with 139 additions and 129 deletions

View File

@@ -9,7 +9,9 @@ package com.TwentyCodes.java.OrderProcessor.DB;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
@@ -69,6 +71,22 @@ public class OrderDB {
conn.close();
}
/**
* counts the number of lines in a file
* @param file to be read
* @return number of lines in a file
* @throws IOException
* @author ricky barrette
*/
public int countLines(File file) throws IOException {
LineNumberReader reader = new LineNumberReader(new FileReader(file));
int cnt = 0;
while ((reader.readLine()) != null) ;
cnt = reader.getLineNumber();
reader.close();
return cnt;
}
/**
* added the order to the database
* @param order
@@ -100,6 +118,7 @@ public class OrderDB {
public ArrayList<Order> getAllOrders() throws SQLException, IOException, ClassNotFoundException {
int row = 1;
int count = getCount();
Connection conn = getConnection();
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select * from Orders;");
@@ -109,7 +128,7 @@ public class OrderDB {
ObjectInputStream ins;
while (rs.next()) {
if(mListener != null && ! isLoadingFile)
mListener.onProgressUpdate(row++);
mListener.onProgressUpdate(row++, count);
bais = new ByteArrayInputStream(rs.getBytes("item"));
ins = new ObjectInputStream(bais);
list.add((Order) ins.readObject());
@@ -160,6 +179,7 @@ public class OrderDB {
public void load(File file) throws SQLException, IOException, ClassNotFoundException{
Scanner scan = new Scanner(file);
int line = 1;
int lineCount = countLines(file);
isLoadingFile = true;
Connection conn = getConnection();
@@ -169,7 +189,7 @@ public class OrderDB {
while(scan.hasNextLine()){
if(mListener != null)
mListener.onProgressUpdate(line++);
mListener.onProgressUpdate(line++, lineCount);
if(Main.DEBUG)
System.out.println("\non line: "+ line);
@@ -213,30 +233,55 @@ public class OrderDB {
* 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
* @param isExclusive true if the search is excluding the string
* @return an array list containing all orders meeting search criteria
* @throws SQLException
* @throws IOException
* @throws ClassNotFoundException
* @author ricky barrette
* @throws ClassNotFoundException
* @throws IOException
* @throws SQLException
*/
public ArrayList<Order> search(String text) throws SQLException, IOException, ClassNotFoundException {
ArrayList<Order> array = getAllOrders();
public ArrayList<Order> search(String text, boolean isExclusive) throws SQLException, IOException, ClassNotFoundException {
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));
int row = 1;
int count = getCount();
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()) {
if(mListener != null && ! isLoadingFile)
mListener.onProgressUpdate(row++, count);
bais = new ByteArrayInputStream(rs.getBytes("item"));
ins = new ObjectInputStream(bais);
Order order = (Order) ins.readObject();
if (isExclusive){
if ( ! order.getItemName().equalsIgnoreCase(text)
|| (orderNumber > 0)
&& (order.getGoogleOrderNumber() == orderNumber)
|| order.getCustomerName().toLowerCase(Locale.ENGLISH).contains(text.toLowerCase(Locale.ENGLISH)))
list.add(order);
} else if ( order.getItemName().equalsIgnoreCase(text)
|| (orderNumber > 0)
&& (order.getGoogleOrderNumber() == orderNumber)
|| order.getCustomerName().toLowerCase(Locale.ENGLISH).contains(text.toLowerCase(Locale.ENGLISH)))
list.add(order);
ins.close();
bais.close();
}
rs.close();
conn.close();
if(mListener != null)
mListener.onProgressUpdate(i);
}
Collections.sort(list);
return list;
}
@@ -246,14 +291,15 @@ public class OrderDB {
* @param search string to search for
* @param start date
* @param end date
* @param isExclusive true if the search is excluding the string
* @return list of orders that meet requirements
* @throws SQLException
* @throws IOException
* @throws ClassNotFoundException
* @author ricky barrette
*/
public ArrayList<Order> search(String search, Date start, Date end) throws SQLException, IOException, ClassNotFoundException {
ArrayList<Order> prelist = search(search);
public ArrayList<Order> search(String search, Date start, Date end, boolean isExclusive) throws SQLException, IOException, ClassNotFoundException {
ArrayList<Order> prelist = search(search, isExclusive);
ArrayList<Order> list = new ArrayList<Order>();
for(Order item : prelist){

View File

@@ -34,37 +34,17 @@ public class Date implements Comparable<Date>, Serializable{
String[] date = parts[0].split("-");
String[] time = parts[1].split(":");
try {
this.mYear = Integer.parseInt(removeFirstChar(date[0]));
this.mYear = Integer.parseInt(date[0]);
this.mDay = Byte.parseByte(date[2]);
this.mMonth = Byte.parseByte(date[1]);
this.mHour = Byte.parseByte(time[0]);
this.mMinute = Byte.parseByte(time[1]);
this.mSecond = Byte.parseByte(removeLastChar(time[2]));
this.mSecond = Byte.parseByte(time[2]);
} catch (NumberFormatException e) {
e.printStackTrace();
throw new InvalidDateFormatException(newDate);
}
}
/**
* removes the first string from a string
* @param s
* @return
* @author ricky barrette
*/
private String removeFirstChar(String s) {
return s.substring(1,s.length());
}
/**
* removes the last char from a string
* @param s
* @return
* @author ricky barrette
*/
private String removeLastChar(String s) {
return s.substring(0,s.length()-1);
}
/**
* returns a human readable string that reprensts this object

View File

@@ -43,7 +43,7 @@ public class Order implements Comparable<Order>, Serializable{
lineParts = order.split(",");
this.mGoogleOrderNumber = Long.parseLong(removeLastChar(lineParts[0].split(" ")[1]));
this.mOrderCreationDate = new Date(lineParts[2]);
this.mOrderCreationDate = new Date(lineParts[2].substring(1, (lineParts[2].length() -1) ));
this.mOrderAmount = Float.parseFloat(lineParts[4]);
this.mAmountCharged = Float.parseFloat(lineParts[5]);
this.mFinancialStatus = Status.parseStatus(lineParts[6]);

View File

@@ -17,6 +17,6 @@ public interface ProgressListener {
* @param progress
* @author ricky barrette
*/
public void onProgressUpdate(int progress);
public void onProgressUpdate(int progress, int max);
}

View File

@@ -88,9 +88,9 @@ public class DatePicker extends JFrame implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
try {
mListener.onDatePicked(mResultCode, new Date("\""+
mListener.onDatePicked(mResultCode, new Date(
mCalendar.getYearChooser().getYear()+"-"+ (mCalendar.getMonthChooser().getMonth()+1) +"-"+ mCalendar.getDayChooser().getDay() +
" "+ Integer.parseInt(mHours.getText())+":"+ Integer.parseInt(mMinutes.getText())+":"+ Integer.parseInt(mSeconds.getText())+"\""));
" "+ Integer.parseInt(mHours.getText())+":"+ Integer.parseInt(mMinutes.getText())+":"+ Integer.parseInt(mSeconds.getText())));
this.dispose();
} catch (NumberFormatException e1) {
e1.printStackTrace();

View File

@@ -9,11 +9,8 @@ package com.TwentyCodes.java.OrderProcessor.UI;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.sql.SQLException;
import javax.swing.ImageIcon;
@@ -82,18 +79,16 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
setVisible(true);
pack();
try {
db = new OrderDB();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
db = new OrderDB();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fc = new JFileChooser();
fc.setFileFilter(new FileFilter());
// db.setOnProgressListerner(this);
}
/**
@@ -115,64 +110,37 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
* Prepares the progress bar, and loads the file using an executer to prevent UI hangups
*/
if(e.getSource() == mLoadFileButton){
try {
if(fc.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION){
mLoadFileButton.setEnabled(false);
db.setOnProgressListerner(this);
mProgressBar.setIndeterminate(true);
mCurrentFile = fc.getSelectedFile().toString();
mProgressBar.setString(mCurrentFile);
mProgressBar.setMaximum(countLines(fc.getSelectedFile())+1);
java.util.concurrent.Executors.newSingleThreadExecutor().submit(new Runnable() {
public void run() {
try {
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 (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
if(fc.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION){
mLoadFileButton.setEnabled(false);
db.setOnProgressListerner(this);
mProgressBar.setIndeterminate(true);
mCurrentFile = fc.getSelectedFile().toString();
mProgressBar.setString(mCurrentFile);
java.util.concurrent.Executors.newSingleThreadExecutor().submit(new Runnable() {
public void run() {
try {
db.load(fc.getSelectedFile());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
pack();
} else {
mProgressBar.setIndeterminate(false);
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
pack();
} else {
mProgressBar.setIndeterminate(false);
}
}
}
/**
* counts the number of lines in a file
* @param file to be read
* @return number of lines in a file
* @throws IOException
* @author ricky barrette
*/
public int countLines(File file) throws IOException {
LineNumberReader reader = new LineNumberReader(new FileReader(file));
int cnt = 0;
while ((reader.readLine()) != null) ;
cnt = reader.getLineNumber();
reader.close();
return cnt;
}
/**
@@ -182,8 +150,9 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
* @author ricky barrette
*/
@Override
public void onProgressUpdate(int progress) {
public void onProgressUpdate(int progress, int max) {
progress++;
mProgressBar.setMaximum(max);
mProgressBar.setValue(progress);
mProgressBar.setString(" "+mCurrentFile + " ~ Parsing Order: "+ progress +" of "+mProgressBar.getMaximum()+" ");
if(mProgressBar.isIndeterminate()) {

View File

@@ -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);
mListener.onProgressUpdate(i, list.size());
switch (list.get(i).getFinancialStatus()) {
case CANCELLED:
updateTextPane(list.get(i).toString(), TextStyle.RED);

View File

@@ -9,6 +9,8 @@ package com.TwentyCodes.java.OrderProcessor.UI;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
@@ -17,6 +19,7 @@ import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
@@ -34,7 +37,7 @@ import com.TwentyCodes.java.OrderProcessor.ProgressListener;
* This class will be the dialog that will ask the user for a specific make to show
* @author ricky barrette
*/
public class SearchDialog extends JFrame implements ActionListener, ProgressListener, MouseListener, DatePickerListener {
public class SearchDialog extends JFrame implements ActionListener, ProgressListener, MouseListener, DatePickerListener, ItemListener {
private static final long serialVersionUID = 1750326106927701404L;
private static final int END_DATE_RESULT = 1;
@@ -47,6 +50,8 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
private OrderPane mOrderPanel;
private JTextField mStartDateField;
private JTextField mEndDateField;
private boolean isExclusive = false;
private JCheckBox mExclusiveCheckBox;
/**
* Creates a new ShowAllMakeDialog
@@ -70,6 +75,11 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
mTextField.addActionListener(this);
panel.add(mTextField);
//exclusive check box
mExclusiveCheckBox = new JCheckBox("Exclusive Search");
mExclusiveCheckBox.addItemListener(this);
panel.add(mExclusiveCheckBox);
//start date
panel.add(new JLabel("Start Date:"));
mStartDateField = new JTextField();
@@ -111,7 +121,7 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
public void actionPerformed(ActionEvent e) {
if(! mStartDateField.getText().isEmpty() && !mEndDateField.getText().isEmpty())
try {
preformSearch(mTextField.getText(), new Date("\""+mStartDateField.getText()+"\""), new Date("\""+mEndDateField.getText()+"\""));
preformSearch(mTextField.getText(), new Date(mStartDateField.getText()), new Date(mEndDateField.getText()));
} catch (InvalidDateFormatException e1) {
e1.printStackTrace();
}
@@ -124,19 +134,14 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
public void run() {
mProgressBar.setString("Searching for: "+search);
mProgressBar.setStringPainted(true);
try {
mProgressBar.setMaximum(MainWindow.db.getCount());
} catch (SQLException e1) {
e1.printStackTrace();
}
MainWindow.db.setOnProgressListerner(SearchDialog.this);
ArrayList<Order> list = null;
try {
if((start != null) && (end != null)){
list = MainWindow.db.search(search, start, end);
list = MainWindow.db.search(search, start, end, isExclusive);
} else{
list = MainWindow.db.search(search);
list = MainWindow.db.search(search, isExclusive);
}
} catch (SQLException e) {
@@ -171,10 +176,12 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
* @author ricky barrette
*/
@Override
public void onProgressUpdate(int progress) {
public void onProgressUpdate(int progress, int max) {
if(Main.DEBUG)
System.out.println("search progress = "+ progress);
progress++;
if(mProgressBar.isIndeterminate()) {
mProgressBar.setIndeterminate(false);
@@ -187,8 +194,10 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
} catch (SQLException e) {
e.printStackTrace();
}
} else
} else {
mProgressBar.setValue(progress);
mProgressBar.setMaximum(max);
}
}
@@ -231,7 +240,6 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
}
@Override
public void onDatePicked(int resultCode, Date date) {
switch(resultCode){
@@ -244,5 +252,16 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
}
}
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getSource() == mExclusiveCheckBox)
if(e.getStateChange() == java.awt.event.ItemEvent.SELECTED)
isExclusive = true;
if(e.getStateChange() == java.awt.event.ItemEvent.DESELECTED)
isExclusive = false;
}
}

View File

@@ -45,11 +45,6 @@ public class ShowAllDialog extends JFrame implements ActionListener, ProgressLis
getContentPane().add(mProgressBar, BorderLayout.SOUTH);
this.getContentPane().add(mOutput, BorderLayout.CENTER);
this.getContentPane().add(ok, BorderLayout.NORTH);
try {
mProgressBar.setMaximum(MainWindow.db.getCount());
} catch (SQLException e1) {
e1.printStackTrace();
}
mProgressBar.setString("Loading from Database");
mProgressBar.setStringPainted(true);
MainWindow.db.setOnProgressListerner(this);
@@ -108,7 +103,8 @@ public class ShowAllDialog extends JFrame implements ActionListener, ProgressLis
}
@Override
public void onProgressUpdate(int progress) {
public void onProgressUpdate(int progress, int max) {
mProgressBar.setMaximum(max);
mProgressBar.setValue(progress);
if(progress >= mProgressBar.getMaximum()){
this.pack();