This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OvertimeDBManager.java
353 lines (295 loc) · 11.6 KB
/
OvertimeDBManager.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package no.ntnu.stud.minvakt.database;
import no.ntnu.stud.minvakt.data.Overtime;
import java.sql.*;
import java.util.ArrayList;
import java.util.logging.Level;
/**
* Database layer class for overtime
*/
public class OvertimeDBManager extends DBManager{
private final String sqlSetOvertime = "REPLACE INTO overtime VALUES(?,?,?,?,?)";
private final String sqlApproveOvertime = "UPDATE overtime SET approved=1 WHERE user_id = ? AND shift_id = ?";
private final String sqlDeleteOvertime = "DELETE FROM overtime WHERE user_id = ? AND shift_id = ? AND start_time = ?";
private final String sqlGetUnapprovedOvertime = "SELECT * FROM overtime WHERE approved = 0";
private final String sqlCountUnapproved = "SELECT COUNT(*) FROM overtime WHERE approved = 0";
private final String getSqlGetOvertimeByUserId = "SELECT overtime.*, shift.date, shift.time FROM overtime NATURAL JOIN employee_shift JOIN shift ON (shift.shift_id = employee_shift.shift_id) WHERE overtime.user_id =?;";
private final String sqlGetMinutesByDate = "SELECT sum(minutes) AS minute_sum FROM overtime NATURAL JOIN employee_shift JOIN shift ON employee_shift.shift_id = shift.shift_id WHERE overtime.user_id = ? AND date BETWEEN ? AND ? AND shift.approved = TRUE";
private final String sqlGetMinutes = "SELECT minutes FROM overtime WHERE user_id = ? AND shift_id = ? AND start_time = ?";
private final String sqlGetOvertimeShift = "SELECT * FROM overtime WHERE user_id = ? AND shift_id = ?";
Connection conn;
PreparedStatement prep;
public OvertimeDBManager(){
super();
}
/**
* Registers overtime/absence on a given user. (not an entire shift)
* @param userId - employee ID
* @param shiftId - shift ID
* @param startTime - time of day
* @param minutes - the amount of minutes worked extra/been absent
* @return True if success
*/
public boolean setOvertime(int userId, int shiftId, int startTime, int minutes){
int out = 0;
if(setUp()){
try{
startTransaction();
conn = getConnection();
prep = conn.prepareStatement(sqlSetOvertime);
prep.setInt(1,userId);
prep.setInt(2,shiftId);
prep.setInt(3, startTime);
prep.setInt(4, minutes);
prep.setBoolean(5, false);
out = prep.executeUpdate();
} catch (SQLException sqlE){
log.log(Level.WARNING,"Error register overtime on user with ID = " + userId, sqlE);
} finally {
endTransaction();
finallyStatement(prep);
}
}
return out != 0;
}
/**
* Deletes overtime from database
* @param userId - employee ID
* @param shiftId - shift ID
* @param startTime
* @return True if success
*/
public boolean deleteOvertime(int userId, int shiftId, int startTime){
int out = 0;
if(setUp()){
try{
startTransaction();
conn = getConnection();
prep = conn.prepareStatement(sqlDeleteOvertime);
prep.setInt(1, userId);
prep.setInt(2, shiftId);
prep.setInt(3,startTime);
out = prep.executeUpdate();
} catch(SQLException sqlE){
log.log(Level.WARNING, "Error deleting overtime for user with id = " + userId + " and shift id = " + shiftId, sqlE);
} finally {
endTransaction();
finallyStatement(prep);
}
}
return out != 0;
}
/**
* For admin to approve overtime, sets approved to true in DB for given user on given shift
* @param userId - employee ID
* @param shiftId - shift ID
* @return True if success
*/
public boolean approveOvertime(int userId, int shiftId){
int out = 0;
if(setUp()){
try {
startTransaction();
conn = getConnection();
prep = conn.prepareStatement(sqlApproveOvertime);
prep.setInt(1, userId);
prep.setInt(2, shiftId);
out = prep.executeUpdate();
} catch (SQLException sqlE) {
log.log(Level.WARNING, "Error approving overtime for user with id = " + userId + " on shift with id = " + shiftId, sqlE);
} finally {
endTransaction();
finallyStatement(prep);
}
}
return out != 0;
}
/**
* @param userId - employee ID
* @return ArrayList<Overtime> - Overtime objects for user given by userId
*/
public ArrayList<Overtime> getOvertimeListByUserId(int userId){
Overtime overtimeObj;
ArrayList<Overtime> timeList = new ArrayList<>();
ResultSet res = null;
if(setUp()){
try{
conn = getConnection();
prep = conn.prepareStatement(getSqlGetOvertimeByUserId);
prep.setInt(1,userId);
res = prep.executeQuery();
while(res.next()){
overtimeObj = new Overtime(
res.getInt("user_id"),
res.getInt("shift_id"),
res.getInt("start_time"),
res.getInt("minutes"),
res.getBoolean("approved"),
res.getDate("date"),
res.getInt("time"));
timeList.add(overtimeObj);
}
} catch (SQLException sqlE){
log.log(Level.WARNING, "Error returning overtime for user with ID = " + userId, sqlE);
} finally {
finallyStatement(res, prep);
}
}
return timeList;
}
/**
* Fetched all unapproved overtime registered by employees
* @return Overtime[] - array of Overtime objects with the overtimes not yet approved by admin
*/
public Overtime[] getUnapprovedOvertime(){
Overtime overtimeObj;
int rowCount = getRowCount();
Overtime[] timeList = new Overtime[rowCount];
ResultSet res = null;
if(setUp()){
try{
startTransaction();
conn = getConnection();
prep = conn.prepareStatement(sqlGetUnapprovedOvertime);
res = prep.executeQuery();
int index = 0;
while(res.next()){
overtimeObj = new Overtime(
res.getInt("user_id"),
res.getInt("shift_id"),
res.getInt("start_time"),
res.getInt("minutes"),
res.getBoolean("approved"));
timeList[index] = overtimeObj;
index++;
}
} catch (SQLException sqlE){
log.log(Level.WARNING, "Error returning unapproved overtime", sqlE);
} finally {
endTransaction();
finallyStatement(res, prep);
}
}
return timeList;
}
/**
* @param userId - employee ID
* @param shiftId - shift ID
* @param startTime - time of day for worked overtime
* @return int - minutes worked for a given user, shift and time of day
*/
public int getMinutes(int userId, int shiftId, int startTime){
int minutes = 0;
ResultSet res = null;
if(setUp()){
try {
System.out.println("User: "+userId+" Shift: "+shiftId+ " Starttime: "+startTime);
startTransaction();
conn = getConnection();
prep = conn.prepareStatement(sqlGetMinutes);
prep.setInt(1, userId);
prep.setInt(2, shiftId);
prep.setInt(3, startTime);
System.out.println(prep);
res = prep.executeQuery();
if(res.next()) {
minutes = res.getInt("minutes");
}
} catch (SQLException sqlE){
log.log(Level.WARNING,"Error getting minutes for user with id = " + userId, sqlE);
} finally {
endTransaction();
finallyStatement(res, prep);
}
}
return minutes;
}
/**
* @param userId - employee ID
* @param shiftId - shift ID
* @return ArrayList<Overtime> - List of Overtime objects for the given shift
*/
public ArrayList<Overtime> getOvertimeByShift(int userId, int shiftId){
ArrayList<Overtime> overtimeList = new ArrayList<>();
Overtime overtime = null;
ResultSet res = null;
if(setUp()){
try {
startTransaction();
conn = getConnection();
prep = conn.prepareStatement(sqlGetOvertimeShift);
prep.setInt(1, userId);
prep.setInt(2, shiftId);
res = prep.executeQuery();
while(res.next()){
int startTime = res.getInt("start_time");
int minutes =res.getInt("minutes");
boolean approved = res.getBoolean("approved");
overtime = new Overtime(userId, shiftId,startTime,minutes, approved);
overtimeList.add(overtime);
}
} catch (SQLException sqlE){
log.log(Level.WARNING,"Error getting minutes for user with id = " + userId, sqlE);
} finally {
endTransaction();
finallyStatement(res, prep);
}
}
return overtimeList;
}
/**
* Calculates total minutes registered (aside from shifts) for a given period
* @param userId - employee ID
* @param fromDate - date start of period
* @param toDate - date end of period
* @return int - total minutes
*/
public int getMinutesByDate(int userId, Date fromDate, Date toDate){
int minutes = 0;
ResultSet res = null;
if(setUp()){
try {
startTransaction();
conn = getConnection();
prep = conn.prepareStatement(sqlGetMinutesByDate);
prep.setInt(1, userId);
prep.setDate(2, fromDate);
prep.setDate(3, toDate);
res = prep.executeQuery();
res.next();
minutes = res.getInt("minute_sum");
} catch (SQLException sqlE){
log.log(Level.WARNING,"Error getting minutes for user with id = " + userId, sqlE);
} finally {
endTransaction();
finallyStatement(res, prep);
}
}
return minutes;
}
/**
* Is used as a helping method for getting unapproved overtime by counting rows in the database
* @return int - counted rows
*/
//helping method
private int getRowCount() {
int count = 0;
ResultSet res = null;
if(setUp()){
try{
startTransaction();
conn = getConnection();
prep = conn.prepareStatement(sqlCountUnapproved);
res = prep.executeQuery();
while(res.next()){
count += res.getInt("COUNT(*)");
}
} catch (SQLException sqlE){
log.log(Level.WARNING, "Error getting row count", sqlE);
} finally{
endTransaction();
finallyStatement(res, prep);
}
}
return count;
}
}