View Javadoc
1   /*
2    * Copyright (c) Patrick Magauran 2018.
3    *   Licensed under the AGPLv3. All conditions of said license apply.
4    *       This file is part of ABOS.
5    *
6    *       ABOS is free software: you can redistribute it and/or modify
7    *       it under the terms of the GNU Affero General Public License as published by
8    *       the Free Software Foundation, either version 3 of the License, or
9    *       (at your option) any later version.
10   *
11   *       ABOS is distributed in the hope that it will be useful,
12   *       but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   *       GNU Affero General Public License for more details.
15   *
16   *       You should have received a copy of the GNU Affero General Public License
17   *       along with ABOS.  If not, see <http://www.gnu.org/licenses/>.
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  //import javax.swing.border.EmptyBorder;
44  //import java.awt.*;
45  
46  /**
47   * Created by patrick on 12/24/15.
48   */
49  @SuppressWarnings("WeakerAccess")
50  
51  public class SettingsController {
52      //private final JPanel contentPanel = new JPanel();
53      //private JTabbedPane north;
54      //General
55      @FXML
56      private TextField DbLoc;
57  
58      //Add Utilities.Customer
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      //Report
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     public static void main(String... args) {
110         try {
111             new Launchers.Settings();
112 
113         } catch (RuntimeException e) {
114             e.printStackTrace();
115         }
116     }
117 */
118 
119 
120 
121     @FXML
122     public void promptLogo(ActionEvent event) {
123         //Creates a JFileChooser to select a directory to store the Databases
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 //        logoLoc.setText(chooser.showOpenDialog(settings).getAbsolutePath());
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 // Set the button types.
143         ButtonType login = new ButtonType("Login", ButtonBar.ButtonData.OK_DONE);
144         dialog.getDialogPane().getButtonTypes().addAll(login, ButtonType.CANCEL);
145 
146 // Create the username and password labels and fields.
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 // Enable/Disable login button depending on whether a username was entered.
164         javafx.scene.Node loginButton = dialog.getDialogPane().lookupButton(login);
165         loginButton.setDisable(true);
166 
167 // Do some validation (using the Java 8 lambda syntax).
168         userNameTextField.textProperty().addListener((observable, oldValue, newValue) -> loginButton.setDisable(newValue.trim().isEmpty()));
169 
170         dialog.getDialogPane().setContent(grid);
171 
172 // Request focus on the username field by default.
173         Platform.runLater(() -> userNameTextField.requestFocus());
174 
175 // Convert the result to a username-password-pair when the login button is clicked.
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 // Set the button types.
216         ButtonType login = new ButtonType("Verify", ButtonBar.ButtonData.OK_DONE);
217         dialog.getDialogPane().getButtonTypes().addAll(login, ButtonType.CANCEL);
218 
219 // Create the username and password labels and fields.
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 // Enable/Disable login button depending on whether a username was entered.
237         javafx.scene.Node loginButton = dialog.getDialogPane().lookupButton(login);
238         loginButton.setDisable(true);
239 
240 // Do some validation (using the Java 8 lambda syntax).
241         userNameTextField.textProperty().addListener((observable, oldValue, newValue) -> loginButton.setDisable(newValue.trim().isEmpty()));
242 
243         dialog.getDialogPane().setContent(grid);
244 
245 // Request focus on the username field by default.
246         Platform.runLater(() -> userNameTextField.requestFocus());
247 
248 // Convert the result to a username-password-pair when the login button is clicked.
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 // Set the button types.
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         //Creates a JFileChooser to select save location of XML file
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         // get a handle to the stage
334 
335         Stage stage = (Stage) pdfLoc.getScene().getWindow();
336         // do what you have to do
337         stage.close();
338     }
339 
340     @FXML
341     public void cancel(ActionEvent event) {
342         Stage stage = (Stage) pdfLoc.getScene().getWindow();
343         // do what you have to do
344         stage.close();
345     }
346 
347     //SetBounds(X,Y,Width,Height)
348     public void initUI(Settings settingsWindow) {
349         settings = settingsWindow;
350         if (!Config.doesConfExist()) {
351             Config.createConfigFile();
352         }
353         //Launchers.Main Content
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 /*                final URI uri;
437                 try {
438                     uri = new URI("https://www.gnu.org/licenses/agpl.html");
439 
440                     class OpenUrlAction implements ActionListener {
441                         @Override
442                         public void actionPerformed(ActionEvent e) {
443                             open(uri);
444                         }
445                     }
446                     JButton button = new JButton();
447                     button.setText("<HTML><h2>This software is released under the AGPLv3 license.</h2> Click <FONT size=14px color=\"#000099\"><U>Here</U></FONT>"
448                             + " to access the AGPLv3 license.</HTML>");
449                     button.setHorizontalAlignment(SwingConstants.LEFT);
450                     button.setBorderPainted(false);
451                     button.setOpaque(false);
452                     button.setBackground(Color.WHITE);
453                     button.setToolTipText(uri.toString());
454                     button.addActionListener(new OpenUrlAction());
455                     License.add(button);
456 
457                 } catch (URISyntaxException ignored) {
458 
459                 }
460                 JLabel libs = new JLabel("<HTML><h2>Included Libraries:</h2>" +
461                         "<ul>" +
462                         "<li>jDatePicker Utilities.Version 1.3.4</li>" +
463                         "<li>Apache Derby Utilities.Version 10.11</li>" +
464                         "<li>iText Utilities.Version 5.5.10</li>" +
465                         "<li>JMapViewer Utilities.Version 1.0.0</li>" +
466                         "<li>JTidy Utilities.Version 938</li>" +
467                         "<li>Saxon Utilities.Version 9</li>" +
468                         "</ul></HTML>");
469                 License.add(libs);
470             }
471             north.addTab("License", License);
472             contentPanel.add(north, BorderLayout.CENTER);
473         */
474 
475 
476     }
477 
478     private void saveData() {
479         String ssl = Config.getSSL();
480         String prefix = Config.getPrefix();
481         //General
482         //If firstRun Create DB, if not, update Db Location
483         Properties prop = new Properties();
484         OutputStream output = null;
485 
486         try {
487             output = new FileOutputStream("./ABOSConfig.properties");
488 
489             //Add DB setting
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                 //Utilities.DbInt.createSetAndTables();
504 
505 
506             }
507 
508             //AddCustomer
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             //Maps
522             //Launchers.Reports
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 }