1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package Controllers;
21
22 import Launchers.Settings;
23 import Utilities.*;
24 import javafx.application.Platform;
25 import javafx.event.ActionEvent;
26 import javafx.fxml.FXML;
27 import javafx.geometry.Insets;
28 import javafx.scene.control.*;
29 import javafx.scene.layout.GridPane;
30 import javafx.scene.web.WebView;
31 import javafx.stage.FileChooser;
32 import javafx.stage.Stage;
33 import javafx.util.Pair;
34
35 import java.io.File;
36 import java.io.FileOutputStream;
37 import java.io.IOException;
38 import java.io.OutputStream;
39 import java.net.URL;
40 import java.util.Optional;
41 import java.util.Properties;
42
43
44
45
46
47
48
49 @SuppressWarnings("WeakerAccess")
50
51 public class SettingsController {
52
53
54
55 @FXML
56 private TextField DbLoc;
57
58
59 @FXML
60 private CheckBox Delivered;
61 @FXML
62 private CheckBox Paid;
63 @FXML
64 private TextField Name;
65 @FXML
66 private TextField Address;
67 @FXML
68 private TextField ZipCode;
69 @FXML
70 private TextField Town;
71 @FXML
72 private TextField State;
73 @FXML
74 private TextField Phone;
75 @FXML
76 private TextField Email;
77 @FXML
78 private TextField DonationsT;
79
80 @FXML
81 private ComboBox<Object> cmbxReportType;
82 @FXML
83 private TextField scoutName;
84 @FXML
85 private TextField scoutStAddr;
86 @FXML
87 private TextField scoutZip;
88 @FXML
89 private TextField scoutTown;
90 @FXML
91 private TextField scoutState;
92 @FXML
93 private TextField scoutPhone;
94 @FXML
95 private TextField scoutRank;
96 @FXML
97 private TextField logoLoc;
98 @FXML
99 private TextField pdfLoc;
100 @FXML
101 private WebView licenseWebView;
102 private Settings settings;
103
104 public SettingsController() {
105
106 }
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 @FXML
122 public void promptLogo(ActionEvent event) {
123
124 FileChooser chooser = new FileChooser();
125 FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Image files", "*.jpg", "*.gif", "*.png", "*.bmp");
126 chooser.getExtensionFilters().add(filter);
127
128 chooser.setSelectedExtensionFilter(filter);
129
130 File image = chooser.showOpenDialog(settings);
131 if (image != null) {
132 String path = image.getAbsolutePath();
133 logoLoc.setText(path);
134 }
135 }
136
137 @FXML
138 public void verifyConnection(ActionEvent event) {
139 Dialog<Pair<String, String>> dialog = new Dialog<>();
140 dialog.setTitle("Login");
141
142
143 ButtonType login = new ButtonType("Login", ButtonBar.ButtonData.OK_DONE);
144 dialog.getDialogPane().getButtonTypes().addAll(login, ButtonType.CANCEL);
145
146
147 GridPane grid = new GridPane();
148 grid.setHgap(10);
149 grid.setVgap(10);
150 grid.setPadding(new Insets(20, 150, 10, 10));
151
152 TextField userNameTextField = new TextField();
153 userNameTextField.setPromptText("Username");
154 PasswordField passwordField = new PasswordField();
155 passwordField.setPromptText("Password");
156
157 grid.add(new Label("Username:"), 0, 0);
158 grid.add(userNameTextField, 1, 0);
159 grid.add(new Label("Password:"), 0, 1);
160 grid.add(passwordField, 1, 1);
161
162
163
164 javafx.scene.Node loginButton = dialog.getDialogPane().lookupButton(login);
165 loginButton.setDisable(true);
166
167
168 userNameTextField.textProperty().addListener((observable, oldValue, newValue) -> loginButton.setDisable(newValue.trim().isEmpty()));
169
170 dialog.getDialogPane().setContent(grid);
171
172
173 Platform.runLater(() -> userNameTextField.requestFocus());
174
175
176 dialog.setResultConverter(dialogButton -> {
177 if (dialogButton == login) {
178 return new Pair<String, String>(userNameTextField.getText(), passwordField.getText());
179 }
180 return null;
181 });
182
183 Optional<Pair<String, String>> result = dialog.showAndWait();
184
185 result.ifPresent(userPass -> {
186 saveData();
187 if (DbInt.verifyLoginAndUser(userPass)) {
188 if (DbInt.testConnection()) {
189 Alert alert = new Alert(Alert.AlertType.INFORMATION);
190 alert.setHeaderText("Connection Successful");
191 alert.showAndWait();
192 } else {
193 Alert alert = new Alert(Alert.AlertType.ERROR);
194 alert.setHeaderText("Connection unsuccessful");
195 alert.setContentText("Check address, username/password");
196 alert.showAndWait();
197
198 }
199 } else {
200 Alert alert = new Alert(Alert.AlertType.ERROR);
201 alert.setHeaderText("Connection unsuccessful");
202 alert.setContentText("Check address, username/password");
203 alert.showAndWait();
204 }
205
206
207 });
208 }
209
210 @FXML
211 private void verifyAdmin(ActionEvent event) {
212 Dialog<Pair<String, String>> dialog = new Dialog<>();
213 dialog.setTitle("Verify DB");
214 dialog.setHeaderText("Please enter DB admin credentials to verify Database existance.");
215
216 ButtonType login = new ButtonType("Verify", ButtonBar.ButtonData.OK_DONE);
217 dialog.getDialogPane().getButtonTypes().addAll(login, ButtonType.CANCEL);
218
219
220 GridPane grid = new GridPane();
221 grid.setHgap(10);
222 grid.setVgap(10);
223 grid.setPadding(new Insets(20, 150, 10, 10));
224
225 TextField userNameTextField = new TextField();
226 userNameTextField.setPromptText("Username");
227 PasswordField passwordField = new PasswordField();
228 passwordField.setPromptText("Password");
229
230 grid.add(new Label("Username:"), 0, 0);
231 grid.add(userNameTextField, 1, 0);
232 grid.add(new Label("Password:"), 0, 1);
233 grid.add(passwordField, 1, 1);
234
235
236
237 javafx.scene.Node loginButton = dialog.getDialogPane().lookupButton(login);
238 loginButton.setDisable(true);
239
240
241 userNameTextField.textProperty().addListener((observable, oldValue, newValue) -> loginButton.setDisable(newValue.trim().isEmpty()));
242
243 dialog.getDialogPane().setContent(grid);
244
245
246 Platform.runLater(() -> userNameTextField.requestFocus());
247
248
249 dialog.setResultConverter(dialogButton -> {
250 if (dialogButton == login) {
251 return new Pair<String, String>(userNameTextField.getText(), passwordField.getText());
252 }
253 return null;
254 });
255
256 Optional<Pair<String, String>> result = dialog.showAndWait();
257
258 result.ifPresent(userPass -> {
259 saveData();
260 if (DbInt.verifyLogin(userPass)) {
261 if (DbInt.testConnection()) {
262 Alert alert = new Alert(Alert.AlertType.INFORMATION, "DB OKAY", ButtonType.YES);
263 alert.setHeaderText("Databse is OK.");
264 alert.setContentText("Would you like to recreate the Database anyway?");
265 alert.showAndWait().ifPresent(buttonType -> {
266 if (buttonType == ButtonType.YES) {
267 promptCreateDb(true);
268 }
269 });
270 } else {
271 Alert alert = new Alert(Alert.AlertType.INFORMATION, "DB NOT OKAY", ButtonType.YES);
272 alert.setHeaderText("Databse is NOT OK.");
273 alert.setContentText("Would you like to create the Database?");
274 alert.showAndWait().ifPresent(buttonType -> {
275 if (buttonType == ButtonType.YES) {
276 promptCreateDb(false);
277 }
278 });
279 }
280 } else {
281 Alert alert = new Alert(Alert.AlertType.ERROR);
282 alert.setHeaderText("Connection unsuccessful");
283 alert.setContentText("Check address, username/password");
284 alert.showAndWait();
285 }
286
287
288 });
289 }
290
291 private void promptCreateDb(boolean dbValid) {
292 Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "Create DB?", ButtonType.YES);
293 alert.setTitle("Create DB");
294 alert.setHeaderText("Please enter DB admin credentials to verify Database existance.");
295 alert.setContentText("If you click create, the software will delete recreate the central database. \r\n THIS WILL DELETE ALL DATA!!!!");
296
297
298
299 alert.showAndWait().ifPresent(buttonType -> {
300 if (buttonType == ButtonType.YES) {
301 if (dbValid) {
302 DbInt.deleteAllDB();
303
304 }
305
306 DbInt.createSetAndTables();
307 }
308 });
309
310
311 }
312
313 @FXML
314 public void promptPDF(ActionEvent event) {
315
316 FileChooser chooser = new FileChooser();
317 FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Portable Document files", "*.pdf", "*.PDF");
318 chooser.getExtensionFilters().add(filter);
319 chooser.setSelectedExtensionFilter(filter);
320 File pdf = chooser.showSaveDialog(settings);
321 if (pdf != null) {
322 String path = pdf.getAbsolutePath();
323 if (!path.toLowerCase().endsWith(".pdf")) {
324 path += ".pdf";
325 }
326 pdfLoc.setText(path);
327 }
328 }
329
330 @FXML
331 public void submit(ActionEvent event) {
332 saveData();
333
334
335 Stage stage = (Stage) pdfLoc.getScene().getWindow();
336
337 stage.close();
338 }
339
340 @FXML
341 public void cancel(ActionEvent event) {
342 Stage stage = (Stage) pdfLoc.getScene().getWindow();
343
344 stage.close();
345 }
346
347
348 public void initUI(Settings settingsWindow) {
349 settings = settingsWindow;
350 if (!Config.doesConfExist()) {
351 Config.createConfigFile();
352 }
353
354
355
356 DbLoc.setText(Config.getDbLoc());
357
358 URL url = getClass().getResource("/LICENSE.html");
359
360 licenseWebView.getEngine().load(url.toExternalForm());
361
362 Name.setText(Config.getProp("CustomerName"));
363
364 Address.setText(Config.getProp("CustomerAddress"));
365
366 ZipCode.setText(Config.getProp("CustomerZipCode"));
367
368 ZipCode.setOnKeyTyped(keyEvent -> {
369 if (ZipCode.getCharacters().length() >= 4) {
370 String zip = ZipCode.getText() + keyEvent.getCharacter();
371
372 String cityAndState = "";
373 try {
374 cityAndState = Geolocation.getCityState(zip);
375 } catch (IOException e1) {
376 LogToFile.log(e1, Severity.WARNING, "Couldn't contact geolocation service. Please try again or enter the adress manually and contact suport.");
377 }
378 String[] StateTown = cityAndState.split("&");
379 String state = StateTown[1];
380 String town = StateTown[0];
381 Town.setText(town);
382 State.setText(state);
383 }
384 });
385 Town.setText(Config.getProp("CustomerTown"));
386
387 State.setText(Config.getProp("CustomerState"));
388
389
390 Phone.setText(Config.getProp("CustomerPhone"));
391
392 Email.setText(Config.getProp("CustomerEmail"));
393
394 Paid.setSelected(Boolean.valueOf(Config.getProp("CustomerPaid")));
395
396 Delivered.setSelected(Boolean.valueOf(Config.getProp("CustomerDelivered")));
397
398 DonationsT.setText(Config.getProp("CustomerDonations"));
399
400 if (Config.getProp("CustomerDonations").isEmpty()) {
401 DonationsT.setText("0.0");
402 }
403
404
405 scoutName.setText(Config.getProp("ScoutName"));
406 scoutStAddr.setText(Config.getProp("ScoutAddress"));
407 scoutZip.setText(Config.getProp("ScoutZip"));
408 scoutTown.setText(Config.getProp("ScoutTown"));
409 scoutState.setText(Config.getProp("ScoutState"));
410 scoutPhone.setText(Config.getProp("ScoutPhone"));
411
412 scoutRank.setText(Config.getProp("ScoutRank"));
413 logoLoc.setText(Config.getProp("logoLoc"));
414 scoutZip.setOnKeyTyped(keyEvent -> {
415 if (scoutZip.getCharacters().length() >= 4) {
416 String zip = scoutZip.getText() + keyEvent.getCharacter();
417
418 String cityAndState;
419 try {
420 cityAndState = Geolocation.getCityState(zip);
421 String[] StateTown = cityAndState.split("&");
422 String state = StateTown[1];
423 String town = StateTown[0];
424 scoutTown.setText(town);
425 scoutState.setText(state);
426 } catch (IOException e1) {
427 LogToFile.log(e1, Severity.WARNING, "Couldn't contact geolocation service. Please try again or enter the adress manually and contact suport.");
428 }
429
430 }
431 });
432 pdfLoc.setText(Config.getProp("pdfLoc"));
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476 }
477
478 private void saveData() {
479 String ssl = Config.getSSL();
480 String prefix = Config.getPrefix();
481
482
483 Properties prop = new Properties();
484 OutputStream output = null;
485
486 try {
487 output = new FileOutputStream("./ABOSConfig.properties");
488
489
490 if (Config.doesConfExist()) {
491
492 prop.put("databaseLocation", DbLoc.getText());
493 prop.put("SSL", ssl);
494 prop.setProperty("databasePrefix", prefix);
495 } else if (!Config.doesConfExist()) {
496 prop.put("databaseLocation", DbLoc.getText());
497 prop.put("SSL", "TRUE");
498 prop.setProperty("databasePrefix", "ABOS-Test-");
499
500 prop.store(output, null);
501 prop = new Properties();
502
503
504
505
506 }
507
508
509 {
510 prop.put("CustomerName", Name.getText());
511 prop.put("CustomerAddress", Address.getText());
512 prop.put("CustomerZipCode", ZipCode.getText());
513 prop.put("CustomerTown", Town.getText());
514 prop.put("CustomerState", State.getText());
515 prop.put("CustomerPhone", Phone.getText());
516 prop.put("CustomerEmail", Email.getText());
517 prop.put("CustomerPaid", Boolean.toString(Paid.isSelected()));
518 prop.put("CustomerDelivered", Boolean.toString(Delivered.isSelected()));
519 prop.put("CustomerDonation", DonationsT.getText());
520 }
521
522
523 {
524 prop.put("ReportType", cmbxReportType.getSelectionModel().getSelectedIndex() >= 0 ? cmbxReportType.getSelectionModel().getSelectedItem().toString() : "");
525 prop.put("ScoutName", scoutName.getText());
526 prop.put("ScoutAddress", scoutStAddr.getText());
527 prop.put("ScoutZip", scoutZip.getText());
528 prop.put("ScoutTown", scoutTown.getText());
529 prop.put("ScoutState", scoutState.getText());
530 prop.put("ScoutPhone", scoutPhone.getText());
531
532 prop.put("ScoutRank", scoutRank.getText());
533 prop.put("logoLoc", logoLoc.getText());
534 prop.put("pdfLoc", pdfLoc.getText());
535
536 }
537 prop.store(output, null);
538
539 } catch (IOException io) {
540 LogToFile.log(io, Severity.SEVERE, "Error writing settings file. Please try again.");
541 } finally {
542 if (output != null) {
543 try {
544 output.close();
545 } catch (IOException e) {
546 LogToFile.log(e, Severity.SEVERE, "Error closing settings file. Please try again.");
547 }
548 }
549
550 }
551
552 }
553
554
555
556 }