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
* @author ricky barrette
* @param password
* @throws ClassNotFoundException
* @throws SQLException
* @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 {
@@ -77,11 +114,7 @@ public class OrderDB {
dbLocation = System.getProperty("user.home") +"/.TwentyCodesOrders";
}
Connection conn;
if(password == null)
conn = getConnection();
else
conn = getConnection(password);
Connection conn = getConnection(password);
Statement stat = conn.createStatement();
@@ -149,9 +182,10 @@ public class OrderDB {
private void deleteOrder(Order order) throws SQLException, IOException, ClassNotFoundException{
int row = 1;
int count = getCount();
Connection conn = getConnection();
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select * from Orders;");
ResultSet rs = getOrders(conn);
ByteArrayInputStream bais;
ObjectInputStream ins;
Order item;
@@ -189,9 +223,10 @@ public class OrderDB {
int row = 1;
int count = getCount();
Connection conn = getConnection();
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select * from Orders;");
ResultSet rs = getOrders(conn);
ArrayList<Order> list = new ArrayList<Order>();
ByteArrayInputStream bais;
@@ -210,6 +245,19 @@ public class OrderDB {
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
* @return
@@ -221,40 +269,31 @@ public class OrderDB {
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");
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);
}
if(isUsingRemoteDatabase)
return getConnection(passwordEncryptor.decrypt(props.getProperty(SettingsPanel.SERVER_PASSWORD)));
else
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 {
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");
if(props.getProperty(SettingsPanel.SERVER_USERNAME).length() > 0)
url += "?user="+ props.getProperty(SettingsPanel.SERVER_USERNAME);
if(password.length() > 0)
url += "&password="+ password;
return DriverManager.getConnection(url);
}
else
return DriverManager.getConnection("jdbc:sqlite:"+ dbLocation+".db");
}
/**
@@ -283,7 +322,7 @@ public class OrderDB {
private Properties getProperties() {
Properties props = new java.util.Properties();
try {
FileInputStream in = new FileInputStream(".settings.propertys");
FileInputStream in = new FileInputStream(SettingsPanel.PROPERTIES_FILE_NAME);
props.load(in);
in.close();
return props;

View File

@@ -31,6 +31,8 @@ import com.TwentyCodes.java.OrderProcessor.UncaughtExceptionHandler;
import com.TwentyCodes.java.OrderProcessor.DB.OrderDB;
import javax.swing.JPasswordField;
import org.jasypt.util.text.BasicTextEncryptor;
/**
* 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 JButton mConnectButton;
private static JPasswordField mPassWord;
private static boolean isUsingRemoteServer;
private JLabel lblDatabasePassword;
/**
@@ -108,6 +111,7 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
mPassWord = new JPasswordField();
mPassWord.setColumns(10);
mPassWord.setEnabled(false);
panel.add(mPassWord);
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);
return mainPanel;
}
/**
* loads an instance of the order database using the most current settings
* @author ricky barrette
*/
public static void loadDatabase() {
BasicTextEncryptor passwordEncryptor = new BasicTextEncryptor();
passwordEncryptor.setPassword(OrderDB.PASSWORD);
isUsingRemoteServer = false;
try {
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())
mPassWord.setText(p.getProperty(SettingsPanel.SERVER_PASSWORD));
mPassWord.setText(passwordEncryptor.decrypt(p.getProperty(SettingsPanel.SERVER_PASSWORD)));
} catch (IOException e) {
e.printStackTrace();
}
@@ -146,7 +159,7 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
//order count labels
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()+"");
mWarningLabel.setText("Connected");
mPassWord.setEnabled(false);
@@ -258,7 +271,7 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
private static Properties getProperties() throws IOException{
// create and load default properties
Properties props = new Properties();
FileInputStream in = new FileInputStream(".settings.propertys");
FileInputStream in = new FileInputStream(SettingsPanel.PROPERTIES_FILE_NAME);
props.load(in);
in.close();
return props;

View File

@@ -44,13 +44,11 @@ public class OrderPane extends JScrollPane {
/**
* displays orders
*
* @param list
* of orders to display
* @param list of orders to display
* @author ricky barrette
*/
public void displayOrders(ArrayList<Order> list) {
ArrayList<String> styles = new ArrayList<String>();
ArrayList<TextStyle> styles = new ArrayList<TextStyle>();
mOutput.setText(null);
BigDecimal possible = new BigDecimal(0);
BigDecimal actual = new BigDecimal(0);
@@ -65,24 +63,24 @@ public class OrderPane extends JScrollPane {
try {
switch (list.get(i).getFulfillmentStatus()) {
case WILL_NOT_DELIVER:
styles.add(TextStyle.RED.toString());
styles.add(TextStyle.RED);
break;
case NEW:
styles.add(TextStyle.BLUE.toString());
styles.add(TextStyle.BLUE);
newOrders++;
possibleNew = possibleNew.add(new BigDecimal(list.get(i).getOrderAmount()));
break;
default:
styles.add(TextStyle.REGULAR.toString());
styles.add(TextStyle.REGULAR);
total++;
actual = actual.add(new BigDecimal((list.get(i).getAmountCharged())));
}
} catch (Exception e) {
//This is just in case I missed a order status again
styles.add(TextStyle.ITALIC);
e.printStackTrace();
styles.add(TextStyle.REGULAR.toString());
}
possible = possible.add(new BigDecimal((list.get(i).getOrderAmount())));
}
updateTextPane(list, styles);
@@ -99,14 +97,14 @@ public class OrderPane extends JScrollPane {
* @param styles for the orders
* @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();
addStylesToDocument(doc);
// Load the text pane with styled text.
try {
for (int i = 0; i < list.size(); i++) {
doc.insertString(doc.getLength(), "\n"+list.get(i).toString(),
doc.getStyle(styles.get(i)));
doc.getStyle(styles.get(i).toString()));
}
} catch (BadLocationException ble) {
System.err.println("Couldn't insert initial text into text pane.");
@@ -114,29 +112,26 @@ public class OrderPane extends JScrollPane {
mOutput.setDocument(doc);
}
/**
* @return the text contained by this order pane
* @author ricky barrette
*/
public String getText(){
return mOutput.getText();
}
/**
* adds the following string to the textpane
*
* adds the following string to the order pane
* @param string
* @author ricky barrette
*/
private void updateTextPane(String string, TextStyle style) {
String[] initString = { "\n" + string };
String[] initStyles = { style.toString() };
StyledDocument doc = mOutput.getStyledDocument();
addStylesToDocument(doc);
// Load the text pane with styled text.
try {
for (int i = 0; i < initString.length; i++) {
doc.insertString(doc.getLength(), initString[i],
doc.getStyle(initStyles[i]));
}
doc.insertString(doc.getLength(), string, doc.getStyle(style.toString()));
} catch (BadLocationException ble) {
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
*
* @param doc
* @author ricky barrette
*/
@@ -160,6 +154,7 @@ public class OrderPane extends JScrollPane {
Style s = doc.addStyle("ITALIC", regular);
StyleConstants.setItalic(s, true);
StyleConstants.setBackground(s, Color.YELLOW);
s = doc.addStyle("BOLD", regular);
StyleConstants.setBold(s, true);
@@ -175,7 +170,6 @@ public class OrderPane extends JScrollPane {
/**
* sets a listener for db progress updates
*
* @param listener
* @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 SAVE_PASSWORD = "save_password";
public static final String DATABASE_NAME = "database_name";
public static final String PROPERTIES_FILE_NAME = ".orderprocessor.properties";
private JTextField mServerLocation;
private JTextField mPortNumber;
private JTextField mUserName;
@@ -179,7 +180,7 @@ public class SettingsPanel extends JPanel implements ActionListener {
mProps = new Properties();
try {
FileInputStream in = new FileInputStream(".settings.propertys");
FileInputStream in = new FileInputStream(PROPERTIES_FILE_NAME);
mProps.load(in);
in.close();
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){
try {
FileOutputStream out = new FileOutputStream(".settings.propertys");
FileOutputStream out = new FileOutputStream(PROPERTIES_FILE_NAME);
p.store(out, "---Order Processor Settings---");
out.close();
} 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 a password field to the main panel to allow for easy password entry
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