3
3
import controller .MainMenuController ;
4
4
import javafx .collections .FXCollections ;
5
5
import javafx .collections .ObservableList ;
6
+ import javafx .scene .control .Alert ;
6
7
import model .Appointment ;
7
8
import utils .DBConnection ;
8
9
import utils .DBQuery ;
9
10
import utils .TimezoneUtil ;
11
+ import utils .Warning ;
10
12
11
13
import java .sql .*;
12
14
@@ -20,9 +22,8 @@ public AppointmentDao() {
20
22
public static ObservableList <Appointment > getAppointmentList (String selectedFilter ) {
21
23
Timestamp selectedDate = MainMenuController .getSelectedDate ();
22
24
String sqlStatement = "SELECT *, Start + INTERVAL ? HOUR as Local_Start, End + INTERVAL ? HOUR as Local_End FROM appointments" ;
23
-
24
25
try {
25
- switch (selectedFilter ) {
26
+ switch (selectedFilter ) {
26
27
case "View Month" :
27
28
sqlStatement += " WHERE MONTH(Start) = ? AND YEAR(Start) = ?" ;
28
29
DBQuery .setPreparedStatement (connection , sqlStatement );
@@ -31,13 +32,14 @@ public static ObservableList<Appointment> getAppointmentList(String selectedFilt
31
32
sqlStatement += " WHERE WEEK(Start) = WEEK(?) AND YEAR(Start) = ?" ;
32
33
break ;
33
34
}
35
+ sqlStatement += " ORDER BY Start ASC" ;
34
36
35
37
DBQuery .setPreparedStatement (connection , sqlStatement );
36
38
PreparedStatement preparedStatement = DBQuery .getPreparedStatement ();
37
39
preparedStatement .setInt (1 , TimezoneUtil .getOffsetToLocalTime ());
38
40
preparedStatement .setInt (2 , TimezoneUtil .getOffsetToLocalTime ());
39
41
40
- switch (selectedFilter ) {
42
+ switch (selectedFilter ) {
41
43
case "View Month" :
42
44
preparedStatement .setInt (3 , selectedDate .toLocalDateTime ().getMonthValue ());
43
45
preparedStatement .setInt (4 , selectedDate .toLocalDateTime ().getYear ());
@@ -94,8 +96,8 @@ public static Boolean addAppointment(Appointment appointment) {
94
96
preparedStatement .setString (3 , appointment .getLocation ());
95
97
preparedStatement .setInt (4 , appointment .getContactId ());
96
98
preparedStatement .setString (5 , appointment .getType ());
97
- preparedStatement .setTimestamp (6 , TimezoneUtil . timestampWithOffset ( appointment .getStart (), - TimezoneUtil . getOffsetToLocalTime () ));
98
- preparedStatement .setTimestamp (7 , TimezoneUtil . timestampWithOffset ( appointment .getEnd (), - TimezoneUtil . getOffsetToLocalTime () ));
99
+ preparedStatement .setTimestamp (6 , appointment .getStart ());
100
+ preparedStatement .setTimestamp (7 , appointment .getEnd ());
99
101
preparedStatement .setInt (8 , appointment .getCustomerId ());
100
102
101
103
preparedStatement .execute ();
@@ -149,4 +151,40 @@ public static void deleteAppointment(int id) {
149
151
public void setAppointmentList (ObservableList <Appointment > appointmentList ) {
150
152
this .appointmentList = appointmentList ;
151
153
}
152
- }
154
+
155
+ private static Boolean hasAppointmentConflicts (Appointment appointment ) {
156
+ String selectStatement = "SELECT Local_Start, Local_End " +
157
+ "FROM (SELECT Start + INTERVAL ? HOUR as Local_Start, End + INTERVAL ? HOUR as Local_End " +
158
+ "FROM appointments " +
159
+ "WHERE Customer_ID = ?) LocalTimeTable " +
160
+ "WHERE Local_Start < ? " +
161
+ "AND Local_End > ?" ;
162
+
163
+ try {
164
+ DBQuery .setPreparedStatement (connection , selectStatement );
165
+ PreparedStatement preparedStatement = DBQuery .getPreparedStatement ();
166
+
167
+ preparedStatement .setInt (1 , TimezoneUtil .getOffsetToLocalTime ());
168
+ preparedStatement .setInt (2 , TimezoneUtil .getOffsetToLocalTime ());
169
+ preparedStatement .setInt (3 , appointment .getCustomerId ());
170
+ preparedStatement .setTimestamp (4 , appointment .getEnd ());
171
+ preparedStatement .setTimestamp (5 , appointment .getStart ());
172
+
173
+ preparedStatement .execute ();
174
+ ResultSet resultSet = preparedStatement .getResultSet ();
175
+
176
+ if (resultSet .next ()) {
177
+ Warning .generateMessage ("Schedule Conflict with appointment(s)" , Alert .AlertType .ERROR );
178
+ return true ;
179
+ }
180
+
181
+ } catch (SQLException e ) {
182
+ e .printStackTrace ();
183
+ }
184
+ return false ;
185
+ }
186
+
187
+ public static Boolean isValidAppointment (Appointment appointment ) {
188
+ return (!hasAppointmentConflicts (appointment ) && TimezoneUtil .isOfficeHours (appointment ));
189
+ }
190
+ }
0 commit comments