used similar code to remove redundant code

created a static variable for PROPERTIES_FILE_NAME
added a case to the order pane to highlight any orders with null statuses
slight modification to order pane updateTextPane() s
This commit is contained in:
2011-01-29 14:18:28 +00:00
parent 4be7476de7
commit 4ebe4a9910
7 changed files with 156 additions and 94 deletions

View File

@@ -0,0 +1,9 @@
#---Order Processor Settings---
#Sat Jan 29 09:08:26 EST 2011
server_parrword=ghHagtj3wfMjx/hHetKlFmvfd9ho54c4
database_name=Orders
save_password=false
server_location=tcdevsvn1
use_remote_server=false
server_port_number=3306
server_user_name=ricky.barrette

View File

@@ -56,12 +56,49 @@ public class OrderDB {
/** /**
* Creates a new OrderDB * Creates a new OrderDB
* @author ricky barrette
* @param password
* @throws ClassNotFoundException * @throws ClassNotFoundException
* @throws SQLException * @throws SQLException
* @throws SocketException * @throws SocketException
* @throws Exception * @author ricky barrette
*/
public OrderDB() throws ClassNotFoundException, SQLException, SocketException {
Properties props = getProperties();
isUsingRemoteDatabase = Boolean.parseBoolean(props.getProperty(SettingsPanel.IS_USING_REMOTE_SERVER, "false"));
if(isUsingRemoteDatabase) {
Class.forName("com.mysql.jdbc.Driver");
}
else {
Class.forName("org.sqlite.JDBC");
dbLocation = System.getProperty("user.home") +"/.TwentyCodesOrders";
}
Connection conn = getConnection();
Statement stat = conn.createStatement();
// stat.executeUpdate("drop table if exists Orders;");
try {
if(isUsingRemoteDatabase)
stat.executeUpdate(CREATE_TABLE);
else
stat.executeUpdate(CREATE_TABLE_LOCAL);
} catch (SQLException e) {
//most likely the table already exist
e.printStackTrace();
}
conn.close();
}
/**
* Creates a new OrderDB
* @param password for the database
* @throws ClassNotFoundException
* @throws SQLException
* @throws SocketException
* @author ricky barrette
*/ */
public OrderDB(String password) throws ClassNotFoundException, SQLException, SocketException { public OrderDB(String password) throws ClassNotFoundException, SQLException, SocketException {
@@ -77,11 +114,7 @@ public class OrderDB {
dbLocation = System.getProperty("user.home") +"/.TwentyCodesOrders"; dbLocation = System.getProperty("user.home") +"/.TwentyCodesOrders";
} }
Connection conn; Connection conn = getConnection(password);
if(password == null)
conn = getConnection();
else
conn = getConnection(password);
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
@@ -149,9 +182,10 @@ public class OrderDB {
private void deleteOrder(Order order) throws SQLException, IOException, ClassNotFoundException{ private void deleteOrder(Order order) throws SQLException, IOException, ClassNotFoundException{
int row = 1; int row = 1;
int count = getCount(); int count = getCount();
Connection conn = getConnection(); Connection conn = getConnection();
Statement stat = conn.createStatement(); ResultSet rs = getOrders(conn);
ResultSet rs = stat.executeQuery("select * from Orders;");
ByteArrayInputStream bais; ByteArrayInputStream bais;
ObjectInputStream ins; ObjectInputStream ins;
Order item; Order item;
@@ -189,9 +223,10 @@ public class OrderDB {
int row = 1; int row = 1;
int count = getCount(); int count = getCount();
Connection conn = getConnection(); Connection conn = getConnection();
Statement stat = conn.createStatement(); ResultSet rs = getOrders(conn);
ResultSet rs = stat.executeQuery("select * from Orders;");
ArrayList<Order> list = new ArrayList<Order>(); ArrayList<Order> list = new ArrayList<Order>();
ByteArrayInputStream bais; ByteArrayInputStream bais;
@@ -210,6 +245,19 @@ public class OrderDB {
return 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 * gets the connection to the data base
* @return * @return
@@ -221,40 +269,31 @@ public class OrderDB {
BasicTextEncryptor passwordEncryptor = new BasicTextEncryptor(); BasicTextEncryptor passwordEncryptor = new BasicTextEncryptor();
passwordEncryptor.setPassword(PASSWORD); passwordEncryptor.setPassword(PASSWORD);
if(isUsingRemoteDatabase){ if(isUsingRemoteDatabase)
String url = "jdbc:mysql://"+props.getProperty(SettingsPanel.SERVER_LOCATION)+":"+props.getProperty(SettingsPanel.SERVER_PORT_NUMBER, "3306")+"/"+props.getProperty(SettingsPanel.DATABASE_NAME,"Orders"); return getConnection(passwordEncryptor.decrypt(props.getProperty(SettingsPanel.SERVER_PASSWORD)));
if(props.getProperty(SettingsPanel.SERVER_USERNAME).length() > 0)
url += "?user="+ props.getProperty(SettingsPanel.SERVER_USERNAME);
if(props.getProperty(SettingsPanel.SERVER_PASSWORD).length() > 0)
url += "&password="+ passwordEncryptor.decrypt(props.getProperty(SettingsPanel.SERVER_PASSWORD));
return DriverManager.getConnection(url);
}
else else
return DriverManager.getConnection("jdbc:sqlite:"+ dbLocation+".db"); return DriverManager.getConnection("jdbc:sqlite:"+ dbLocation+".db");
} }
/**
* gets the connection to the database
* @param password users password for the database
* @return connection
* @throws SQLException
* @author ricky barrette
*/
private Connection getConnection(String password) throws SQLException { private Connection getConnection(String password) throws SQLException {
Properties props = getProperties(); Properties props = getProperties();
BasicTextEncryptor passwordEncryptor = new BasicTextEncryptor();
passwordEncryptor.setPassword(PASSWORD);
if(isUsingRemoteDatabase){
String url = "jdbc:mysql://"+props.getProperty(SettingsPanel.SERVER_LOCATION)+":"+props.getProperty(SettingsPanel.SERVER_PORT_NUMBER, "3306")+"/"+props.getProperty(SettingsPanel.DATABASE_NAME,"Orders"); String url = "jdbc:mysql://"+props.getProperty(SettingsPanel.SERVER_LOCATION)+":"+props.getProperty(SettingsPanel.SERVER_PORT_NUMBER, "3306")+"/"+props.getProperty(SettingsPanel.DATABASE_NAME,"Orders");
if(props.getProperty(SettingsPanel.SERVER_USERNAME).length() > 0) if(props.getProperty(SettingsPanel.SERVER_USERNAME).length() > 0)
url += "?user="+ props.getProperty(SettingsPanel.SERVER_USERNAME); url += "?user="+ props.getProperty(SettingsPanel.SERVER_USERNAME);
if(password.length() > 0)
url += "&password="+ password; url += "&password="+ password;
return DriverManager.getConnection(url); return DriverManager.getConnection(url);
}
else
return DriverManager.getConnection("jdbc:sqlite:"+ dbLocation+".db");
} }
/** /**
@@ -283,7 +322,7 @@ public class OrderDB {
private Properties getProperties() { private Properties getProperties() {
Properties props = new java.util.Properties(); Properties props = new java.util.Properties();
try { try {
FileInputStream in = new FileInputStream(".settings.propertys"); FileInputStream in = new FileInputStream(SettingsPanel.PROPERTIES_FILE_NAME);
props.load(in); props.load(in);
in.close(); in.close();
return props; return props;

View File

@@ -31,6 +31,8 @@ import com.TwentyCodes.java.OrderProcessor.UncaughtExceptionHandler;
import com.TwentyCodes.java.OrderProcessor.DB.OrderDB; import com.TwentyCodes.java.OrderProcessor.DB.OrderDB;
import javax.swing.JPasswordField; import javax.swing.JPasswordField;
import org.jasypt.util.text.BasicTextEncryptor;
/** /**
* this is the main window and class of this application * this is the main window and class of this application
*/ */
@@ -47,6 +49,7 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
private static JLabel mWarningLabel; private static JLabel mWarningLabel;
private static JButton mConnectButton; private static JButton mConnectButton;
private static JPasswordField mPassWord; private static JPasswordField mPassWord;
private static boolean isUsingRemoteServer;
private JLabel lblDatabasePassword; private JLabel lblDatabasePassword;
/** /**
@@ -108,6 +111,7 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
mPassWord = new JPasswordField(); mPassWord = new JPasswordField();
mPassWord.setColumns(10); mPassWord.setColumns(10);
mPassWord.setEnabled(false);
panel.add(mPassWord); panel.add(mPassWord);
mConnectButton = new JButton("Connect", new ImageIcon(getClass().getResource("/repeat.png"))); mConnectButton = new JButton("Connect", new ImageIcon(getClass().getResource("/repeat.png")));
@@ -122,18 +126,27 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
mainPanel.add(mProgressBar, BorderLayout.SOUTH); mainPanel.add(mProgressBar, BorderLayout.SOUTH);
return mainPanel; return mainPanel;
} }
/**
* loads an instance of the order database using the most current settings
* @author ricky barrette
*/
public static void loadDatabase() { public static void loadDatabase() {
BasicTextEncryptor passwordEncryptor = new BasicTextEncryptor();
passwordEncryptor.setPassword(OrderDB.PASSWORD);
isUsingRemoteServer = false;
try { try {
Properties p = getProperties(); Properties p = getProperties();
mPassWord.setEnabled(! Boolean.parseBoolean(p.getProperty(SettingsPanel.SAVE_PASSWORD, "false")));
isUsingRemoteServer = Boolean.parseBoolean(p.getProperty(SettingsPanel.IS_USING_REMOTE_SERVER, "false"));
mPassWord.setEnabled((! Boolean.parseBoolean(p.getProperty(SettingsPanel.SAVE_PASSWORD, "false"))) && (isUsingRemoteServer));
if(! mPassWord.isEnabled()) if(! mPassWord.isEnabled())
mPassWord.setText(p.getProperty(SettingsPanel.SERVER_PASSWORD)); mPassWord.setText(passwordEncryptor.decrypt(p.getProperty(SettingsPanel.SERVER_PASSWORD)));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -146,7 +159,7 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
//order count labels //order count labels
try { try {
db = new OrderDB(mPassWord.isEnabled() ? new String(mPassWord.getPassword()) : null); db = isUsingRemoteServer ? new OrderDB(new String(mPassWord.getPassword())) : new OrderDB();
orderCountLabel.setText(db.getCount()+""); orderCountLabel.setText(db.getCount()+"");
mWarningLabel.setText("Connected"); mWarningLabel.setText("Connected");
mPassWord.setEnabled(false); mPassWord.setEnabled(false);
@@ -258,7 +271,7 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
private static Properties getProperties() throws IOException{ private static Properties getProperties() throws IOException{
// create and load default properties // create and load default properties
Properties props = new Properties(); Properties props = new Properties();
FileInputStream in = new FileInputStream(".settings.propertys"); FileInputStream in = new FileInputStream(SettingsPanel.PROPERTIES_FILE_NAME);
props.load(in); props.load(in);
in.close(); in.close();
return props; return props;

View File

@@ -44,13 +44,11 @@ public class OrderPane extends JScrollPane {
/** /**
* displays orders * displays orders
* * @param list of orders to display
* @param list
* of orders to display
* @author ricky barrette * @author ricky barrette
*/ */
public void displayOrders(ArrayList<Order> list) { public void displayOrders(ArrayList<Order> list) {
ArrayList<String> styles = new ArrayList<String>(); ArrayList<TextStyle> styles = new ArrayList<TextStyle>();
mOutput.setText(null); mOutput.setText(null);
BigDecimal possible = new BigDecimal(0); BigDecimal possible = new BigDecimal(0);
BigDecimal actual = new BigDecimal(0); BigDecimal actual = new BigDecimal(0);
@@ -65,24 +63,24 @@ public class OrderPane extends JScrollPane {
try { try {
switch (list.get(i).getFulfillmentStatus()) { switch (list.get(i).getFulfillmentStatus()) {
case WILL_NOT_DELIVER: case WILL_NOT_DELIVER:
styles.add(TextStyle.RED.toString()); styles.add(TextStyle.RED);
break; break;
case NEW: case NEW:
styles.add(TextStyle.BLUE.toString()); styles.add(TextStyle.BLUE);
newOrders++; newOrders++;
possibleNew = possibleNew.add(new BigDecimal(list.get(i).getOrderAmount())); possibleNew = possibleNew.add(new BigDecimal(list.get(i).getOrderAmount()));
break; break;
default: default:
styles.add(TextStyle.REGULAR.toString()); styles.add(TextStyle.REGULAR);
total++; total++;
actual = actual.add(new BigDecimal((list.get(i).getAmountCharged()))); actual = actual.add(new BigDecimal((list.get(i).getAmountCharged())));
} }
} catch (Exception e) { } catch (Exception e) {
//This is just in case I missed a order status again
styles.add(TextStyle.ITALIC);
e.printStackTrace(); e.printStackTrace();
styles.add(TextStyle.REGULAR.toString());
} }
possible = possible.add(new BigDecimal((list.get(i).getOrderAmount()))); possible = possible.add(new BigDecimal((list.get(i).getOrderAmount())));
} }
updateTextPane(list, styles); updateTextPane(list, styles);
@@ -99,14 +97,14 @@ public class OrderPane extends JScrollPane {
* @param styles for the orders * @param styles for the orders
* @author ricky barrette * @author ricky barrette
*/ */
private void updateTextPane(ArrayList<Order> list, ArrayList<String> styles) { private void updateTextPane(ArrayList<Order> list, ArrayList<TextStyle> styles) {
StyledDocument doc = mOutput.getStyledDocument(); StyledDocument doc = mOutput.getStyledDocument();
addStylesToDocument(doc); addStylesToDocument(doc);
// Load the text pane with styled text. // Load the text pane with styled text.
try { try {
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
doc.insertString(doc.getLength(), "\n"+list.get(i).toString(), doc.insertString(doc.getLength(), "\n"+list.get(i).toString(),
doc.getStyle(styles.get(i))); doc.getStyle(styles.get(i).toString()));
} }
} catch (BadLocationException ble) { } catch (BadLocationException ble) {
System.err.println("Couldn't insert initial text into text pane."); System.err.println("Couldn't insert initial text into text pane.");
@@ -114,29 +112,26 @@ public class OrderPane extends JScrollPane {
mOutput.setDocument(doc); mOutput.setDocument(doc);
} }
/**
* @return the text contained by this order pane
* @author ricky barrette
*/
public String getText(){ public String getText(){
return mOutput.getText(); return mOutput.getText();
} }
/** /**
* adds the following string to the textpane * adds the following string to the order pane
*
* @param string * @param string
* @author ricky barrette * @author ricky barrette
*/ */
private void updateTextPane(String string, TextStyle style) { private void updateTextPane(String string, TextStyle style) {
String[] initString = { "\n" + string };
String[] initStyles = { style.toString() };
StyledDocument doc = mOutput.getStyledDocument(); StyledDocument doc = mOutput.getStyledDocument();
addStylesToDocument(doc); addStylesToDocument(doc);
// Load the text pane with styled text. // Load the text pane with styled text.
try { try {
for (int i = 0; i < initString.length; i++) { doc.insertString(doc.getLength(), string, doc.getStyle(style.toString()));
doc.insertString(doc.getLength(), initString[i],
doc.getStyle(initStyles[i]));
}
} catch (BadLocationException ble) { } catch (BadLocationException ble) {
System.err.println("Couldn't insert initial text into text pane."); System.err.println("Couldn't insert initial text into text pane.");
} }
@@ -146,7 +141,6 @@ public class OrderPane extends JScrollPane {
/** /**
* adds supported styles to the document * adds supported styles to the document
*
* @param doc * @param doc
* @author ricky barrette * @author ricky barrette
*/ */
@@ -160,6 +154,7 @@ public class OrderPane extends JScrollPane {
Style s = doc.addStyle("ITALIC", regular); Style s = doc.addStyle("ITALIC", regular);
StyleConstants.setItalic(s, true); StyleConstants.setItalic(s, true);
StyleConstants.setBackground(s, Color.YELLOW);
s = doc.addStyle("BOLD", regular); s = doc.addStyle("BOLD", regular);
StyleConstants.setBold(s, true); StyleConstants.setBold(s, true);
@@ -175,7 +170,6 @@ public class OrderPane extends JScrollPane {
/** /**
* sets a listener for db progress updates * sets a listener for db progress updates
*
* @param listener * @param listener
* @author ricky barrette * @author ricky barrette
*/ */

View File

@@ -43,6 +43,7 @@ public class SettingsPanel extends JPanel implements ActionListener {
public static final String SERVER_PASSWORD = "server_parrword"; public static final String SERVER_PASSWORD = "server_parrword";
public static final String SAVE_PASSWORD = "save_password"; public static final String SAVE_PASSWORD = "save_password";
public static final String DATABASE_NAME = "database_name"; public static final String DATABASE_NAME = "database_name";
public static final String PROPERTIES_FILE_NAME = ".orderprocessor.properties";
private JTextField mServerLocation; private JTextField mServerLocation;
private JTextField mPortNumber; private JTextField mPortNumber;
private JTextField mUserName; private JTextField mUserName;
@@ -179,7 +180,7 @@ public class SettingsPanel extends JPanel implements ActionListener {
mProps = new Properties(); mProps = new Properties();
try { try {
FileInputStream in = new FileInputStream(".settings.propertys"); FileInputStream in = new FileInputStream(PROPERTIES_FILE_NAME);
mProps.load(in); mProps.load(in);
in.close(); in.close();
isUsingRemoteServer = Boolean.parseBoolean(mProps.getProperty(IS_USING_REMOTE_SERVER, "false")); isUsingRemoteServer = Boolean.parseBoolean(mProps.getProperty(IS_USING_REMOTE_SERVER, "false"));
@@ -277,7 +278,7 @@ public class SettingsPanel extends JPanel implements ActionListener {
*/ */
private void writeProperties(Properties p){ private void writeProperties(Properties p){
try { try {
FileOutputStream out = new FileOutputStream(".settings.propertys"); FileOutputStream out = new FileOutputStream(PROPERTIES_FILE_NAME);
p.store(out, "---Order Processor Settings---"); p.store(out, "---Order Processor Settings---");
out.close(); out.close();
} catch (FileNotFoundException e1) { } catch (FileNotFoundException e1) {

View File

@@ -48,3 +48,9 @@ added more options in settings allowing user to pick the database name.
added an option to allow the user to save their password added an option to allow the user to save their password
added a password field to the main panel to allow for easy password entry added a password field to the main panel to allow for easy password entry
added a connect button to the main panel added a connect button to the main panel
1.1.5 b 50
used similar code to remove redundant code
created a static variable for PROPERTIES_FILE_NAME
added a case to the order pane to highlight any orders with null statuses
slight modification to order pane updateTextPane() s

View File

@@ -1 +1 @@
1.1.4 b 49 1.1.5 b 50