home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-21 | 21.4 KB | 583 lines |
- /*
- * PhoneBaseAppJDBC.java
- * Version 1.0
- *
- * Written by Long Huynh 7.30.96
- *
- * Copyright (c) 1996 Symantec. All Rights Reserved.
- *
- */
-
- import java.awt.*;
- import java.util.Properties;
- import java.lang.Thread;
- import java.sql.*;
- import com.symantec.tools.tsunami.scale.SCLSession;
- import com.symantec.tools.tsunami.scale.SCLRelView;
-
- public class PhoneBaseAppJDBC extends java.applet.Applet implements Runnable{
-
- public void init() {
-
- // Initialize Background and Text Color
- this.setBackground(Color.white);
- this.setForeground(Color.red);
-
- super.init();
-
- // Initialize Properties
- props.put("user","dba");
- props.put("password","sql");
-
- // Initialize TextFields
- firstfield = new TextField();
- firstfield.setBackground(current_color);
-
- lastfield = new TextField();
- lastfield.setBackground(current_color);
-
- passfield = new TextField();
- passfield.setBackground(current_color);
- passfield.disable();
-
- waitfield = new TextField();
- waitfield.setBackground(current_color);
- waitfield.disable();
- waitfield.setText("Ready.");
-
- // Initialize TextArea
- output = new TextArea();
- output.setEditable(false);
-
- // Initialize Choices
- deptchoice = new Choice();
- deptchoice.addItem("All");
- deptchoice.select(0);
- deptchoice.disable();
-
- sitechoice = new Choice();
- sitechoice.addItem("All");
- sitechoice.select(0);
- sitechoice.disable();
-
- esnchoice = new Choice();
- esnchoice.addItem("All");
- esnchoice.select(0);
- esnchoice.disable();
-
- // Initialize CheckBoxGroup
- options = new CheckboxGroup();
- quick_search = new Checkbox("Search Names Only (Quick Search)");
- quick_search.setState(true);
-
- // Initialize Buttons
- searchbutton = new Button("Search for Extension");
- clearbutton = new Button("Clear Form");
-
- // Initialize Labels
- firstlabel = new Label("First Name:");
- lastlabel = new Label("Last Name:");
- deptlabel = new Label("Department:");
- sitelabel = new Label("City:");
- esnlabel = new Label("Zip Code:");
- statuslabel = new Label("Status:");
- messagelabel = new Label("Feedback:");
-
- //{{INIT_CONTROLS
- setLayout(null);
-
- // Add CheckboxGroup
- add(quick_search);
- quick_search.reshape(insets().left+15, insets().top+5, 250, 20);
-
- // Add First Name
- add(firstlabel);
- firstlabel.reshape(insets().left+2, insets().top+30, 80, 20);
- add(firstfield);
- firstfield.reshape(insets().left+85, insets().top+30, 160, 20);
- firstfield.requestFocus();
-
- // Add Last Name
- add(lastlabel);
- lastlabel.reshape(insets().left+250, insets().top+30, 80, 20);
- add(lastfield);
- lastfield.reshape(insets().left+335, insets().top+30, 160, 20);
-
- // Add Department
- add(deptlabel);
- deptlabel.reshape(insets().left+2, insets().top+55, 80, 20);
- add(deptchoice);
- deptchoice.reshape(insets().left+85, insets().top+55, 160, 20);
-
- // Add Site
- add(sitelabel);
- sitelabel.reshape(insets().left+250, insets().top+55, 80, 20);
- add(sitechoice);
- sitechoice.reshape(insets().left+335, insets().top+55, 160, 20);
-
- // Add ESN
- add(esnlabel);
- esnlabel.reshape(insets().left+2, insets().top+80, 80, 20);
- add(esnchoice);
- esnchoice.reshape(insets().left+85, insets().top+80, 160, 20);
-
- // Add Search for Extension Button
- add(searchbutton);
- searchbutton.reshape(insets().left+260, insets().top+80, 140, 20);
-
- // Add Clear Button
- add(clearbutton);
- clearbutton.reshape(insets().left+405, insets().top+80, 90, 20);
-
- // Add Waitfield
- add(statuslabel);
- statuslabel.reshape(insets().left+2, insets().top+105, 80, 20);
- add(waitfield);
- waitfield.reshape(insets().left+85, insets().top+105, 415, 20);
-
- // Add Passfield
- add(messagelabel);
- messagelabel.reshape(insets().left+2, insets().top+130, 80, 20);
- add(passfield);
- passfield.reshape(insets().left+85, insets().top+130, 415, 20);
-
- // Add Output
- add(output);
- output.reshape(insets().left+15, insets().top+155, 485, 250);
-
- // Show opening messages
- opening();
-
- show();
- //}}
- }
-
- // add_choices()
- private void add_choices() {
- try {
- Statement stmt = server.createStatement();
- ResultSet rs_temp;
-
- try {
- waitfield.setText("Please wait while PhoneBase is loading...");
- passfield.setText("Opening database '" + db_name + "'");
- rs_temp = stmt.executeQuery(
- //SCLRelView dept = Tsunamiserver.createView(db, "dept",
- "SELECT DISTINCT " + dept_SQL + " FROM " + db_name + " ORDER BY " + dept_SQL
- );
- // ,0, 200, true, true, props);
- passfield.setText("Adding department choices...");
- rs_temp.next();
- while (rs_temp.getString(1) == null) rs_temp.next();
- deptchoice.addItem(rs_temp.getString(1));
- while (rs_temp.next()) { deptchoice.addItem(rs_temp.getString(1)); }
- deptchoice.enable();
- }
- catch (SQLException open_error) {
- passfield.setText("Could not load department choices!");
- sleep();
- }
- }
- catch (SQLException e) {
- passfield.setText("Unable to create statemenr for deptchoice!");
- sleep();
- }
-
- try {
- Statement stmt = server.createStatement();
- ResultSet rs_temp;
-
- try {
- passfield.setText("Adding site choices...");
- rs_temp = stmt.executeQuery(
- //SCLRelView site = Tsunamiserver.createView(db, "site",
- "SELECT DISTINCT " + site_SQL + " FROM " + db_name + " ORDER BY " + site_SQL
- );
- // ,0, 50, true, true, props);
- rs_temp.next();
- while (rs_temp.getString(1) == null) rs_temp.next();
- sitechoice.addItem(rs_temp.getString(1));
- while (rs_temp.next()) { sitechoice.addItem(rs_temp.getString(1)); }
- sitechoice.enable();
- }
- catch (SQLException open_error) {
- passfield.setText("Could not load site choices!");
- sleep();
- }
- }
- catch (SQLException e) {
- passfield.setText("Unable to create statemenr for sitechoice!");
- sleep();
- }
-
- try {
- Statement stmt = server.createStatement();
- ResultSet rs_temp;
-
- try {
- passfield.setText("Adding esn choice...");
- rs_temp = stmt.executeQuery(
- //SCLRelView esn = Tsunamiserver.createView(db, "esn",
- "SELECT DISTINCT " + esn_SQL + " FROM " + db_name + " ORDER BY " + esn_SQL
- );
- // ,0, 50, true, true, props);
- rs_temp.next();
- while (rs_temp.getString(1) == null) rs_temp.next();
- esnchoice.addItem(rs_temp.getString(1));
- while (rs_temp.next()) { esnchoice.addItem(rs_temp.getString(1)); }
- esnchoice.enable();
- }
- catch (SQLException open_error) {
- passfield.setText("Could not load esn choices!");
- sleep();
- }
-
- waitfield.setText("Ready.");
- }
- catch (SQLException e) {
- passfield.setText("Unable to create statemenr for sitechoice!");
- sleep();
- }
- }
-
- // close_server()
- private void close_server() {
- passfield.setText("Closing server " + server_url + "...");
- try {
- server.close();
- passfield.setText("Closing server " + server_url + "... Connection closed!");
- good_connection = false;
- }
- catch (Exception close_error) {
- passfield.setText("Could not close " + server_url);
- sleep();
- }
- }
-
- // connect_server()
- private void connect_server() {
- passfield.setText("Opening server " + server_url + "...");
- try {
- tsunami_driver = (Driver)Class.forName(tsunami_driver_name).newInstance();
- passfield.setText("Opening server " + server_url + "... Connection successful!");
- dmanager.registerDriver(tsunami_driver);
- server = dmanager.getConnection(server_url, props);
- good_connection = true;
- }
- catch (SQLException open_error) {
- passfield.setText("Could not open server " + server_url);
- sleep();
- }
- catch (InstantiationException e) {
- passfield.setText(e.getMessage());
- sleep();
- }
- catch (ClassNotFoundException e) {
- passfield.setText(e.getMessage());
- sleep();
- }
- catch (IllegalAccessException e) {
- passfield.setText(e.getMessage());
- sleep();
- }
- }
-
- // generate_SQL()
- private String generate_SQL() {
- waitfield.setText("Search initiated...");
- String from = new String("FROM " + db_name + " ");
- boolean first_exist = false, last_exist = false, not_first_one = false;
- boolean dept_exist = false, site_exist = false, esn_exist = false;
- int wild_return = has_wildcard();
-
- if (firstfield.getText().length() != 0) first_exist = true;
- if (lastfield.getText().length() != 0) last_exist = true;
- if (deptchoice.getSelectedIndex() != 0) dept_exist = true;
- if (sitechoice.getSelectedIndex() != 0) site_exist = true;
- if (esnchoice.getSelectedIndex() != 0) esn_exist = true;
-
- if (firstfield.getText().compareTo("*") == 0) first_exist = false;
- if (lastfield.getText().compareTo("*") == 0) last_exist = false;
-
- if (first_exist || last_exist || dept_exist || site_exist || esn_exist) {
- from = from.concat("WHERE");
-
- if ( (first_exist) && ((wild_return == 0) || (wild_return == 2)) ){
- not_first_one = true;
- from = from.concat(" " + first_SQL + " = '" + firstfield.getText() + "'");
- }
- else if ( (first_exist) && ((wild_return == 1) || (wild_return == 3)) ) {
- not_first_one = true;
- from = from.concat(" " + first_SQL + " LIKE '"
- + (firstfield.getText()).replace('*', '%').replace('?', '_') + "'");
- }
-
- if ( (last_exist) && ((wild_return == 0) || (wild_return == 1)) ) {
- if (not_first_one) { from = from.concat(" AND"); }
- from = from.concat(" " + last_SQL + " = '" + lastfield.getText() + "'");
- }
- else if ( (last_exist) && ((wild_return == 2) || (wild_return == 3)) ) {
- if (not_first_one) { from = from.concat(" AND"); }
- not_first_one = true;
- from = from.concat(" " + last_SQL + " LIKE '"
- + (lastfield.getText()).replace('*', '%').replace('?', '_') + "'");
- }
-
- if (dept_exist) {
- if (not_first_one) { from = from.concat(" AND"); }
- from = from.concat(" " + dept_SQL + " = '" + deptchoice.getSelectedItem() + "'");
- }
- if (site_exist) {
- if (not_first_one) { from = from.concat(" AND"); }
- from = from.concat(" " + site_SQL + " = '" + sitechoice.getSelectedItem() + "'");
- }
- if (esn_exist) {
- if (not_first_one) { from = from.concat(" AND"); }
- from = from.concat(" " + esn_SQL + " = '" + esnchoice.getSelectedItem() + "'");
- }
- }
- return (select + from);
- }
-
- // db_search()
- private void db_search() {
- int counter = 0;
-
- // Show Status
- waitfield.setText("Please wait while PhoneBase is loading...");
-
- // Print the results
- String searchfirst = new String(firstfield.getText());
- String searchlast = new String(lastfield.getText());
-
- output.setText(" PhoneBase Search Result\n");
- output.appendText("====================================\n");
-
- try {
- Statement stmt = server.createStatement();
- ResultSet rs_temp;
-
- try {
- passfield.setText("Opening database '" + db_name + "'");
- rs_temp = stmt.executeQuery(
- //SCLRelView sr = Tsunamiserver.createView(db, "sr",
- generate_SQL());
- // ,0, 50, true, true, props);
-
- rs_temp.next();
- output.appendText("Located: " + rs_temp.getString(2) + ", " + rs_temp.getString(1)
- + " at extension " + rs_temp.getString(3) + "\n");
- counter++;
-
- while(rs_temp.next()) {
- output.appendText("Located: " + rs_temp.getString(2) + ", "
- + rs_temp.getString(1) + " at extension " + rs_temp.getString(3) + "\n");
- counter++;
- }
- output.appendText("\n" + counter + " hit(s) found.\n");
- }
- catch (Exception filter_error) {
- passfield.setText("Could not continue with search algorithm!");
- }
- }
- catch (Exception filter_error) {
- passfield.setText("Could not continue with search algorithm!");
- }
-
- if (counter == 0) output.appendText("\nNo hits found.\n\n");
-
- output.appendText("====================================\n");
-
- close_server();
-
- waitfield.setText("Ready.");
- }
-
- // has_wildcard()
- private int has_wildcard() {
- int num_of_wildcard = 0;
-
- if ((firstfield.getText()).indexOf("*", 0) != -1) { num_of_wildcard = 1; }
- else if ((firstfield.getText()).indexOf("?", 0) != -1) { num_of_wildcard = 1; }
- if ((lastfield.getText()).indexOf("*", 0) != -1) {
- if (num_of_wildcard == 1) num_of_wildcard = 3;
- else num_of_wildcard = 2;
- }
- else if ((lastfield.getText()).indexOf("?", 0) != -1) {
- if (num_of_wildcard == 1) num_of_wildcard = 3;
- else num_of_wildcard = 2;
- }
- return num_of_wildcard;
- }
-
- // is_empty()
- private boolean is_empty() {
- if (((firstfield.getText()).length() == 0) && ((lastfield.getText()).length() == 0))
- return true;
- else return false;
- }
-
- // opening()
- private void opening() {
- output.setText("Welcome to PhoneBaseJDBC '96 Build 09\n");
- output.appendText("==============================================\n");
- output.appendText("In light of not being able to connect to Excel,\n");
- output.appendText("this search program will use Watcom's Sample\n");
- output.appendText("Changes include:\n");
- output.appendText(" - Employee's Dept_id instead of Department (Name)\n");
- output.appendText(" - Employee's Residential City instead of Work Site\n");
- output.appendText(" - Employee's ZipCode instead of Work ESN\n\n");
- output.appendText("(Changes are external only. Internally, there may still)\n");
- output.appendText("(be labels called esn and site. )\n\n");
- output.appendText("This program, however, still retains:\n");
- output.appendText(" - Conditional searches (where clause).\n");
- output.appendText(" - Supports wildcard searches via '*'.\n");
- output.appendText(" - Supports single-place wildcard searches via '?'.\n");
- output.appendText("==============================================\n");
- }
-
- // run()
- public void run() {
- int cntr = 0;
-
- while (kicker != null) {
- cntr++;
- if ((cntr%1000) == 0) first = true;
- try {kicker.sleep(25);} catch (InterruptedException e) {}
- }
- }
-
- // sleep();
- private void sleep() {
- close_server();
- waitfield.setText("Exception caught! Please notify maintenance of exception below.");
- firstfield.disable();
- this.disable();
- // for (int i = 0; i < 3; i++) i = 1;
- }
-
- // start()
- public void start() {
- if (kicker == null) {
- kicker = new Thread(this);
- kicker.setPriority(kicker.MIN_PRIORITY);
- kicker.start();
- }
- }
-
- // stop()
- public void stop() {
- kicker = null;
- super.stop();
- }
-
- // handleEvent()
- public boolean handleEvent(Event event) {
- Object evt = event.target;
-
- if (event.id == Event.KEY_PRESS) {
- if (event.key == 9) {
- if (evt == firstfield) {lastfield.requestFocus();}
- else if (evt == lastfield) {deptchoice.requestFocus();}
- else if (evt == deptchoice) {sitechoice.requestFocus();}
- else if (evt == sitechoice) {esnchoice.requestFocus();}
- else if (evt == esnchoice) {searchbutton.requestFocus();}
- else if (evt == searchbutton) {clearbutton.requestFocus();}
- else if (evt == clearbutton) {passfield.requestFocus();}
- else if (evt == passfield) {firstfield.requestFocus();}
- else if (evt == output) {firstfield.requestFocus(); }
- else { InterruptedException e; return false;}
- }
- else if ((event.key == 10) && (!is_empty())) {
- start();
- searchbutton.disable();
- connect_server();
- if (good_connection) { db_search(); }
- searchbutton.enable();
- }
- else return super.handleEvent(event);
- return true;
- }
- else if ((event.id == Event.ACTION_EVENT) && (evt instanceof Button)) {
- if (evt == clearbutton) {
- firstfield.setText("");
- lastfield.setText("");
- deptchoice.select(0);
- sitechoice.select(0);
- esnchoice.select(0);
- firstfield.requestFocus();
- }
- else if ((evt == searchbutton) && (!is_empty())) {
- start();
- searchbutton.disable();
- connect_server();
- if (good_connection) { db_search(); }
- searchbutton.enable();
- }
- else { InterruptedException e; return false; }
- return true;
- }
- else if ((event.id == Event.ACTION_EVENT) && (evt instanceof Checkbox)) {
- if ((evt == quick_search) && (quick_search.getState() == false)) {
- quick_search.setState(false);
- searchbutton.disable();
-
- connect_server();
- if (good_connection) {
- add_choices();
- close_server();
- }
-
- searchbutton.enable();
- }
- if ((evt == quick_search) && (quick_search.getState() == true)) {
- quick_search.setState(true);
- searchbutton.disable();
-
- deptchoice.select(0);
- deptchoice.disable();
- sitechoice.select(0);
- sitechoice.disable();
- esnchoice.select(0);
- esnchoice.disable();
-
- searchbutton.enable();
- }
- else { InterruptedException e; return false; }
- return true;
- }
- else if (event.id == Event.WINDOW_DESTROY) { System.exit(0); return true; }
- else return super.handleEvent(event);
- }
-
- //{{DECLARE_CONTROLS
- private boolean first = false, good_connection = false;
- private Color current_color = Color.white;
- private String tsunami_driver_name = "com.symantec.tools.tsunami.jdbc.Driver";
- private String server_url = "jdbc:scale://localhost:8889/Watcom/Sample/Sample";
- //private String server_url = "jdbc:scale://155.64.37.27:8889/Watcom/Sample/Sample";
- private String db = "WATCOM/Sample/Sample";
- private String db_name = "employee";
- private String first_SQL = "emp_fname";
- private String last_SQL = "emp_lname";
- private String dept_SQL = "dept_id";
- private String site_SQL = "city";
- private String esn_SQL = "zip_code";
- private String select = new String("SELECT emp_fname, emp_lname, phone ");
-
- Thread kicker = null;
- TextField firstfield, lastfield, passfield, waitfield;
- TextArea output;
- Choice deptchoice, sitechoice, esnchoice;
- CheckboxGroup options;
- Checkbox quick_search;
- Button searchbutton, clearbutton;
- Label firstlabel, lastlabel, deptlabel, sitelabel, esnlabel, statuslabel, messagelabel;
-
- Driver tsunami_driver;
- DriverManager dmanager;
- Connection server;
- SCLSession Tsunamiserver;
- Properties props = new Properties();
- }
-