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 Launchers;
21  
22  import Utilities.*;
23  import javafx.application.Platform;
24  import javafx.geometry.Insets;
25  import javafx.scene.control.*;
26  import javafx.scene.layout.GridPane;
27  
28  import java.sql.Connection;
29  import java.sql.PreparedStatement;
30  import java.sql.ResultSet;
31  import java.sql.SQLException;
32  import java.util.Optional;
33  
34  public class AddGroup {
35      public static Group addGroup(String year, callBack successCallback) {
36          Optional<Group> returnGroup = Optional.empty();
37          Dialog<String> dialog = new Dialog<>();
38          dialog.setTitle("Add new group");
39  
40  // Set the button types.
41          ButtonType addGrp = new ButtonType("Add", ButtonBar.ButtonData.OK_DONE);
42          dialog.getDialogPane().getButtonTypes().addAll(addGrp, ButtonType.CANCEL);
43  
44  // Create the username and password labels and fields.
45          GridPane grid = new GridPane();
46          grid.setHgap(10);
47          grid.setVgap(10);
48          grid.setPadding(new Insets(20, 150, 10, 10));
49  
50          TextField groupName = new TextField();
51          groupName.setPromptText("Group Name");
52  
53          grid.add(new Label("Group Name:"), 0, 0);
54          grid.add(groupName, 1, 0);
55  
56  
57  // Enable/Disable login button depending on whether a username was entered.
58          javafx.scene.Node addGroupButton = dialog.getDialogPane().lookupButton(addGrp);
59          addGroupButton.setDisable(true);
60  
61  // Do some validation (using the Java 8 lambda syntax).
62          groupName.textProperty().addListener((observable, oldValue, newValue) -> addGroupButton.setDisable(newValue.trim().isEmpty()));
63  
64          dialog.getDialogPane().setContent(grid);
65  
66  // Request focus on the username field by default.
67          Platform.runLater(() -> groupName.requestFocus());
68  
69  // Convert the result to a username-password-pair when the login button is clicked.
70          dialog.setResultConverter(dialogButton -> {
71              if (dialogButton == addGrp) {
72                  return groupName.getText();
73              }
74              return null;
75          });
76  
77          Optional<String> result = dialog.showAndWait();
78          if (result.isPresent()) {
79              try (Connection con = DbInt.getConnection(year);
80                   PreparedStatement prep = con.prepareStatement("INSERT INTO groups(Name) VALUES(?)", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) {
81  
82                  prep.setString(1, result.get());
83  
84                  prep.execute();
85              } catch (SQLException e) {
86                  LogToFile.log(e, Severity.SEVERE, CommonErrors.returnSqlMessage(e));
87              }
88              returnGroup = Optional.of(new Group(result.get(), year));
89              successCallback.doAction(returnGroup.get());
90  
91          }
92          return returnGroup.orElse(null);
93      }
94  
95      public static Group addGroup(String year, String groupName, callBack successCallback) {
96          Optional<Group> returnGroup = Optional.empty();
97          Dialog<String> dialog = new Dialog<>();
98          dialog.setTitle("Edit group " + groupName);
99  
100 // Set the button types.
101         ButtonType addGrp = new ButtonType("Save", ButtonBar.ButtonData.OK_DONE);
102         dialog.getDialogPane().getButtonTypes().addAll(addGrp, ButtonType.CANCEL);
103 
104 // Create the username and password labels and fields.
105         GridPane grid = new GridPane();
106         grid.setHgap(10);
107         grid.setVgap(10);
108         grid.setPadding(new Insets(20, 150, 10, 10));
109 
110         TextField groupNameTextField = new TextField();
111         groupNameTextField.setText(groupName);
112 
113         grid.add(new Label("Group Name:"), 0, 0);
114         grid.add(groupNameTextField, 1, 0);
115 
116 
117 // Enable/Disable login button depending on whether a username was entered.
118         javafx.scene.Node addGroupButton = dialog.getDialogPane().lookupButton(addGrp);
119         addGroupButton.setDisable(true);
120 
121 // Do some validation (using the Java 8 lambda syntax).
122         groupNameTextField.textProperty().addListener((observable, oldValue, newValue) -> addGroupButton.setDisable(newValue.trim().isEmpty()));
123 
124         dialog.getDialogPane().setContent(grid);
125 
126 // Request focus on the username field by default.
127         Platform.runLater(() -> groupNameTextField.requestFocus());
128 
129 // Convert the result to a username-password-pair when the login button is clicked.
130         dialog.setResultConverter(dialogButton -> {
131             if (dialogButton == addGrp) {
132                 return groupNameTextField.getText();
133             }
134             return null;
135         });
136 
137         Optional<String> result = dialog.showAndWait();
138         if (result.isPresent()) {
139             try (Connection con = DbInt.getConnection(year);
140                  PreparedStatement prep = con.prepareStatement("UPDATE groups SET Name=? WHERE Name=?", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) {
141                 prep.setString(1, result.get());
142                 prep.setString(2, groupName);
143 
144                 prep.execute();
145             } catch (SQLException e) {
146                 LogToFile.log(e, Severity.SEVERE, CommonErrors.returnSqlMessage(e));
147             }
148             returnGroup = Optional.of(new Group(result.get(), year));
149             successCallback.doAction(returnGroup.get());
150         }
151         return returnGroup.orElse(null);
152     }
153 
154     public interface callBack {
155 
156         void doAction(Group grp);
157     }
158 }