|
1 | 1 | package merhoo.admin; |
2 | 2 |
|
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.io.IOException; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Map; |
| 9 | + |
3 | 10 | /** |
4 | | - * Hello world! |
5 | | - * |
| 11 | + * App is the basic admin app. For now, all it does is connect to the database |
| 12 | + * and then disconnect |
6 | 13 | */ |
7 | | -public class App |
| 14 | +public class App |
8 | 15 | { |
9 | | - public static void main( String[] args ) |
10 | | - { |
11 | | - System.out.println( "Hello World!" ); |
| 16 | + /** |
| 17 | + * Print the menu for our program |
| 18 | + */ |
| 19 | + static void menu() { |
| 20 | + System.out.println("Main Menu"); |
| 21 | + System.out.println(" [T] Create tblData"); |
| 22 | + System.out.println(" [D] Drop tblData"); |
| 23 | + System.out.println(" [1] Query for a specific row"); |
| 24 | + System.out.println(" [*] Query for all rows"); |
| 25 | + System.out.println(" [-] Delete a row"); |
| 26 | + System.out.println(" [+] Insert a new row"); |
| 27 | + System.out.println(" [~] Update a row"); |
| 28 | + System.out.println(" [q] Quit Program"); |
| 29 | + System.out.println(" [?] Help (this message)"); |
| 30 | + } |
| 31 | + /** |
| 32 | + * Ask the user to enter a menu option; repeat until we get a valid option |
| 33 | + * |
| 34 | + * @param in A BufferedReader, for reading from the keyboard |
| 35 | + * |
| 36 | + * @return The character corresponding to the chosen menu option |
| 37 | + */ |
| 38 | + static char prompt(BufferedReader in) { |
| 39 | + // The valid actions: |
| 40 | + String actions = "TD1*-+~q?"; |
| 41 | + |
| 42 | + // We repeat until a valid single-character option is selected |
| 43 | + while (true) { |
| 44 | + System.out.print("[" + actions + "] :> "); |
| 45 | + String action; |
| 46 | + try { |
| 47 | + action = in.readLine(); |
| 48 | + } catch (IOException e) { |
| 49 | + e.printStackTrace(); |
| 50 | + continue; |
| 51 | + } |
| 52 | + if (action.length() != 1) |
| 53 | + continue; |
| 54 | + if (actions.contains(action)) { |
| 55 | + return action.charAt(0); |
| 56 | + } |
| 57 | + System.out.println("Invalid Command"); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Ask the user to enter a String message |
| 63 | + * |
| 64 | + * @param in A BufferedReader, for reading from the keyboard |
| 65 | + * @param message A message to display when asking for input |
| 66 | + * |
| 67 | + * @return The string that the user provided. May be "". |
| 68 | + */ |
| 69 | + static String getString(BufferedReader in, String message) { |
| 70 | + String s; |
| 71 | + try { |
| 72 | + System.out.print(message + " :> "); |
| 73 | + s = in.readLine(); |
| 74 | + } catch (IOException e) { |
| 75 | + e.printStackTrace(); |
| 76 | + return ""; |
| 77 | + } |
| 78 | + return s; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Ask the user to enter an integer |
| 83 | + * |
| 84 | + * @param in A BufferedReader, for reading from the keyboard |
| 85 | + * @param message A message to display when asking for input |
| 86 | + * |
| 87 | + * @return The integer that the user provided. On error, it will be -1 |
| 88 | + */ |
| 89 | + static int getInt(BufferedReader in, String message) { |
| 90 | + int i = -1; |
| 91 | + try { |
| 92 | + System.out.print(message + " :> "); |
| 93 | + i = Integer.parseInt(in.readLine()); |
| 94 | + } catch (IOException e) { |
| 95 | + e.printStackTrace(); |
| 96 | + } catch (NumberFormatException e) { |
| 97 | + e.printStackTrace(); |
| 98 | + } |
| 99 | + return i; |
| 100 | + } |
| 101 | + |
| 102 | + public static void main( String[] args ) |
| 103 | + { |
| 104 | + Map<String, String> env = System.getenv(); |
| 105 | + String ip = env.get("POSTGRES_IP"); |
| 106 | + String port = env.get("POSTGRES_PORT"); |
| 107 | + String user = env.get("POSTGRES_USER"); |
| 108 | + String pass = env.get("POSTGRES_PASS"); |
| 109 | + |
| 110 | + // Get a fully-configured connection to the database, or exit |
| 111 | + // immediately |
| 112 | + Database db = Database.getDatabase(ip, port, user, pass); |
| 113 | + if (db == null) |
| 114 | + return; |
| 115 | + // Start our basic command-line interpreter: |
| 116 | + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); |
| 117 | + while (true) { |
| 118 | + // Get the user's request, and do it |
| 119 | + // |
| 120 | + // NB: for better testability, each action should be a separate |
| 121 | + // function call |
| 122 | + char action = prompt(in); |
| 123 | + if (action == '?') { |
| 124 | + menu(); |
| 125 | + } else if (action == 'q') { |
| 126 | + break; |
| 127 | + } else if (action == 'T') { |
| 128 | + db.createTable(); |
| 129 | + } else if (action == 'D') { |
| 130 | + db.dropTable(); |
| 131 | + } else if (action == '1') { |
| 132 | + int id = getInt(in, "Enter the row ID"); |
| 133 | + if (id == -1) |
| 134 | + continue; |
| 135 | + Database.RowData res = db.selectOne(id); |
| 136 | + if (res != null) { |
| 137 | + System.out.println(" [" + res.mId + "] " + res.mSubject); |
| 138 | + System.out.println(" --> " + res.mMessage); |
| 139 | + } |
| 140 | + } else if (action == '*') { |
| 141 | + ArrayList<Database.RowData> res = db.selectAll(); |
| 142 | + if (res == null) |
| 143 | + continue; |
| 144 | + System.out.println(" Current Database Contents"); |
| 145 | + System.out.println(" -------------------------"); |
| 146 | + for (Database.RowData rd : res) { |
| 147 | + System.out.println(" [" + rd.mId + "] " + rd.mSubject); |
| 148 | + } |
| 149 | + } else if (action == '-') { |
| 150 | + int id = getInt(in, "Enter the row ID"); |
| 151 | + if (id == -1) |
| 152 | + continue; |
| 153 | + int res = db.deleteRow(id); |
| 154 | + if (res == -1) |
| 155 | + continue; |
| 156 | + System.out.println(" " + res + " rows deleted"); |
| 157 | + } else if (action == '+') { |
| 158 | + String subject = getString(in, "Enter the subject"); |
| 159 | + String message = getString(in, "Enter the message"); |
| 160 | + if (subject.equals("") || message.equals("")) |
| 161 | + continue; |
| 162 | + int res = db.insertRow(subject, message); |
| 163 | + System.out.println(res + " rows added"); |
| 164 | + } else if (action == '~') { |
| 165 | + int id = getInt(in, "Enter the row ID :> "); |
| 166 | + if (id == -1) |
| 167 | + continue; |
| 168 | + String newMessage = getString(in, "Enter the new message"); |
| 169 | + int res = db.updateOne(id, newMessage); |
| 170 | + if (res == -1) |
| 171 | + continue; |
| 172 | + System.out.println(" " + res + " rows updated"); |
| 173 | + } |
12 | 174 | } |
| 175 | + // Always remember to disconnect from the database when the program |
| 176 | + // exits |
| 177 | + db.disconnect(); |
| 178 | + } |
13 | 179 | } |
0 commit comments