-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModifyAppointmentController.java
315 lines (263 loc) · 11.2 KB
/
ModifyAppointmentController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package Controller;
import DOA.AppointmentDao;
import DOA.AppointmentDaoImpl;
import DOA.DoaLists;
import Model.Appointment;
import Model.Contact;
import Model.Customer;
import Model.User;
import Utils.AlertMessage;
import Utils.SwitchView;
import Utils.TimeZoneClass;
import Utils.Validation;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.ListView;
import javafx.scene.control.Slider;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.text.Text;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.sql.Time;
import java.time.*;
import java.util.ResourceBundle;
/**
* This class is the controller for the modify Appointment view, which updates and deletes appointments from the program/database
*/
public class ModifyAppointmentController implements Initializable {
private int endHour;
private int endMinute;
private String EndTimeString;
private int startHour;
private int startMinute;
private String StartTimeString;
@FXML
private Text modifyAppointmentTitleTxt;
@FXML
private TextField appointmentIDTxtField;
@FXML
private Text appointmentIDTxt;
@FXML
private TextField titleTxtField;
@FXML
private Text titleTxt;
@FXML
private TextArea descriptionTxtArea;
@FXML
private Text descriptionTxt;
@FXML
private TextField locationTxtField;
@FXML
private Text locationTxt;
@FXML
private TextField typeTxtFIeld;
@FXML
private Text typeTxt;
@FXML
private ComboBox<Contact> contactComboBox;
@FXML
private Text contactTxt;
@FXML
private ComboBox<Customer> customerCombox;
@FXML
private Text customerTxt;
@FXML
private DatePicker datePicker;
@FXML
private Text dateLabel;
@FXML
private Text startTimeLabel;
@FXML
private Text endTimeLabel;
@FXML
private Slider startHourSlider;
@FXML
private Slider startMinSlider;
@FXML
private Slider endHourSlider;
@FXML
private Slider endMinSlider;
@FXML
private Text startHourTxt;
@FXML
private Text startMinTxt;
@FXML
private Text endHourTxt;
@FXML
private Text endMinTxt;
@FXML
private Text startTimeTxt;
@FXML
private Text endTimeTxt;
@FXML
private Text startTimeZoneLabel;
@FXML
private Text endTimeZoneLabel;
@FXML
private ListView<Appointment> appointmentList;
@FXML
private Button deleteBtn;
@FXML
private Button saveBtn;
@FXML
private Button cancelBtn;
@FXML
private ComboBox<User> userComboBox;
/** switches view back to main view
* @param event on cancel button clicked
* @throws IOException
*/
@FXML
void onActionCancel(ActionEvent event) throws IOException {
SwitchView.mainPage(event);
}
/** delete appointment from the db and program
* @param event on delete button clicked
* @throws SQLException
*/
@FXML
void onActionDelete(ActionEvent event) throws SQLException {
Appointment appointment = appointmentList.getSelectionModel().getSelectedItem();
AppointmentDao appointmentDao = new AppointmentDaoImpl();
AlertMessage.appointmentCanceled(appointment);
DoaLists.getAppointmentsList().remove(appointment);
appointmentDao.deleteAppointment(appointment);
}
/** sets the hour of the end time of the appointment via a slider from 00 to 23
* @param event dragging the end hour slider
*/
@FXML
void onDragEndHour(MouseEvent event) {
endHour = (int) endHourSlider.getValue();
EndTimeString = String.format("%02d:%02d:00", endHour, endMinute);
endTimeTxt.setText(EndTimeString);
}
/** sets the minute of the end time of the appointment via a slider from 00 to 59
* @param event dragging the end minute slider
*/
@FXML
void onDragEndMin(MouseEvent event) {
endMinute = (int) endMinSlider.getValue();
EndTimeString = String.format("%02d:%02d:00", endHour, endMinute);
endTimeTxt.setText(EndTimeString);
}
/** sets the hour of the start time of the appointment via a slider from 00 to 23
* @param event dragging the start hour slider
*/
@FXML
void onDragStartHour(MouseEvent event) {
startHour = (int) startHourSlider.getValue();
StartTimeString = String.format("%02d:%02d:00", startHour, startMinute);
startTimeTxt.setText(StartTimeString);
}
/** sets the minute of the start time of the appointment via a slider from 00 to 59
* @param event dragging the start minute slider
*/
@FXML
void onDragStartMin(MouseEvent event) {
startMinute = (int) startMinSlider.getValue();
StartTimeString = String.format("%02d:%02d:00", startHour, startMinute);
startTimeTxt.setText(StartTimeString);
}
/** sets all the values in the text fields, combo boxes, sliders, texts, and text areas to the proper data of the appointment selected in the list view
* <p>the <b>LAMBDA</b> in the stream for the contact object makes so that I don't have to write and enhanced for loop to match the contact name string in the
* appointment object selected to the contactName string in an object in the appointment Doalist.getContactsList</p>
* <p> the <b>LAMBDA</b> in the stream for the customer object makes it so that I don't have to write and enhanced for loop to match the customerInt in the
* appointment object selected to the customerInt in the DoaList.getCustomersList()</p>
* <p>The <b>LAMBDA</b> in the stream for the user object makes it so that I don't have to write a for loop to match the userID integer in the appointment
* object with the correct user object in the DoaList.getAllUsersList</p>
* @param event on select appointment
*/
@FXML
void onSelectAppointment(MouseEvent event) {
Appointment a = appointmentList.getSelectionModel().getSelectedItem();
String contactString = a.getContact();
Contact contact = DoaLists.getContactsList().stream().filter(c -> c.getContactName().equals(contactString)).findFirst().get();
int customerInt = a.getCustomerID();
Customer customer = DoaLists.getCustomersList().stream().filter(i -> i.getID() == customerInt).findFirst().get();
appointmentIDTxtField.setText(String.valueOf(a.getAppointmentID()));
titleTxtField.setText(a.getTitle());
descriptionTxtArea.setText(a.getDescription());
locationTxtField.setText(a.getLocation());
contactComboBox.setValue(contact);
typeTxtFIeld.setText(a.getType());
datePicker.setValue(a.getDate());
startHourSlider.setValue(a.getStartTime().getHour());
startMinSlider.setValue(a.getStartTime().getMinute());
startTimeTxt.setText(a.getStartTime() +":00");
StartTimeString = a.getStartTime() + ":00";
customerCombox.setValue(customer);
//endDatePicker.setValue(a.getEndDate());
endHourSlider.setValue(a.getEndTime().getHour());
endMinSlider.setValue(a.getEndTime().getMinute());
endTimeTxt.setText(a.getEndTime() +":00");
EndTimeString = a.getEndTime() + ":00";
User user = DoaLists.getAllUsersList().stream().filter(u -> u.getUserID() == a.getUserID()).findAny().get();
userComboBox.setValue(user);
}
/** creates a new appointment object and sends the object to the database via update command in the appointmentDao
* <p> clears appointments List in DoaList and refreshes it with the new data from the db</p>
* @param event clicking the save button
* @throws SQLException
* @throws IOException
*/
@FXML
void onActionSave(ActionEvent event) throws SQLException, IOException{
LocalDate date = datePicker.getValue();
LocalTime startTime = LocalTime.parse(StartTimeString);
LocalTime endTime = LocalTime.parse(EndTimeString);
ZoneId localZoneID = ZoneId.systemDefault();
ZoneId nycZoneID = ZoneId.of("America/New_York");
ZonedDateTime localStartZDT = ZonedDateTime.of(date, startTime, localZoneID);
ZonedDateTime localEndZDT = ZonedDateTime.of(date, endTime, localZoneID);
Instant localStartToUTC = localStartZDT.toInstant();
Instant localEndToUTC = localEndZDT.toInstant();
LocalDateTime startESTDateTime = localStartToUTC.atZone(nycZoneID).toLocalDateTime();
LocalDateTime endESTDateTime = localEndToUTC.atZone(nycZoneID).toLocalDateTime();
try {
int ID = Integer.parseInt(appointmentIDTxtField.getText());
String title = titleTxtField.getText();
String description = descriptionTxtArea.getText();
String location = locationTxtField.getText();
String type = typeTxtFIeld.getText();
String contact = contactComboBox.getSelectionModel().getSelectedItem().toString();
int customer = customerCombox.getSelectionModel().getSelectedItem().getID();
int userID = userComboBox.getSelectionModel().getSelectedItem().getUserID();
Appointment appointment = new Appointment(ID, title, description, location, type, startTime, date, endTime, contact, customer, userID);
if(Validation.overLappingAppointment(appointment) && Validation.datesCompatible(startESTDateTime, endESTDateTime) && Validation.timeIntervalCheck(startESTDateTime, endESTDateTime)){
DoaLists.getAppointmentsList().clear();
AppointmentDao ad = new AppointmentDaoImpl();
if (ad.updateAppointment(appointment)) {
DoaLists.getAllUsersList().clear();
DoaLists.getContactsList().clear();
ad.getAllAppointments();
SwitchView.mainPage(event);
}
}
}
catch (NullPointerException e) {
AlertMessage.fillTextFields();
}
}
/** populates appointmentList, contactComboBox, customerCombo box, and sets the time zone labels to the local time zone
* @param url
* @param resourceBundle
*/
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
appointmentList.setItems(DoaLists.getAppointmentsList());
contactComboBox.setItems(DoaLists.getContactsList());
customerCombox.setItems(DoaLists.getCustomersList());
startTimeZoneLabel.setText(String.valueOf(TimeZoneClass.getLocalZoneID()));
endTimeZoneLabel.setText(String.valueOf(TimeZoneClass.getLocalZoneID()));
userComboBox.setItems(DoaLists.getAllUsersList());
}
}