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
/
Copy pathNewsFeedDBManager.java
356 lines (325 loc) · 13.6 KB
/
NewsFeedDBManager.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
354
355
package no.ntnu.stud.minvakt.database;
import no.ntnu.stud.minvakt.data.NewsFeedItem;
import no.ntnu.stud.minvakt.data.user.User;
import java.sql.*;
import java.util.ArrayList;
import java.util.logging.Level;
/**
* Database layer class for news feed
*/
public class NewsFeedDBManager extends DBManager{
private final String sqlCreateNotification = "INSERT INTO newsfeed VALUES(DEFAULT,?,?,0,?,?,?,?,?)";
private final String sqlDeleteNotification = "DELETE FROM newsfeed WHERE feed_id = ?";
private final String sqlDeleteNotificationsForShift = "DELETE FROM newsfeed WHERE shift_id = ?";
private final String sqlGetLastID = "SELECT LAST_INSERT_ID();";
private final String sqlGetNewsFeedForUser = "SELECT * FROM newsfeed WHERE user_id = ? AND resolved = 0";
private final String sqlGetNewsFeedForAdmin = "SELECT feed_id, date_time,content,newsfeed.category, start_time, user.user_id, shift_id, shift_user_id " +
"FROM newsfeed JOIN user ON(user.user_id = newsfeed.user_id) WHERE user.category = ? AND resolved = 0;";
private final String sqlGetNewsFeedItem = "SELECT * FROM newsfeed WHERE feed_id = ? AND resolved = 0;";
private final String sqlSetNewsFeedItemResolved = "UPDATE newsfeed SET resolved = ? WHERE feed_id = ?;";
private final String sqlGetNewsFeedIdFromOvertime = "SELECT feed_id FROM overtime JOIN newsfeed ON overtime.shift_id = newsfeed.shift_id AND overtime.user_id = newsfeed.shift_user_id AND overtime.start_time = newsfeed.start_time WHERE overtime.user_id = ? AND overtime.shift_id = ? AND overtime.start_time = ?;";
private final String sqlGetShiftChangePendingCount = "SELECT COUNT(*) as pending FROM newsfeed WHERE category = 0 AND shift_id = ? AND shift_user_id = ? AND resolved = 0;";
private final String sqlResolveNotifications = "UPDATE newsfeed SET resolved = 1 WHERE shift_user_id = ? AND shift_id = ? AND category = ?";
Connection conn;
PreparedStatement prep;
public NewsFeedDBManager(){
super();
}
/**
* Creates a notification
* @param notification - NewsFeedItem object
* @return int: NewsFeed id
*/
public int createNotification(NewsFeedItem notification){
int id = 0;
if(setUp()) {
ResultSet res = null;
try {
conn = getConnection();
PreparedStatement prep = conn.prepareStatement(sqlCreateNotification);
prep.setTimestamp(1, notification.getDateTime());
prep.setString(2, notification.getContent());
prep.setInt(3,notification.getCategory().getValue());
prep.setInt(4, notification.getUserIdTo());
prep.setInt(5, notification.getShiftId());
prep.setInt(6, notification.getUserIdInvolving());
prep.setInt(7,notification.getStartTimeTimebank());
id = prep.executeUpdate();
if(id != 0){
prep = conn.prepareStatement(sqlGetLastID);
res = prep.executeQuery();
if(res.next()){
id = res.getInt(1);
}
}
} catch (SQLException e) {
log.log(Level.WARNING, "Not able to add notification to news feed", e);
} finally {
finallyStatement(res, prep);
}
}
return id;
}
/**
* Deletes notification for a given shift
* @param shiftId - ID for given shift
* @return True: If success
*/
public boolean deleteNotificationsForShift(int shiftId){
int status = 0;
if(setUp()) {
try {
conn = getConnection();
PreparedStatement prep = conn.prepareStatement(sqlDeleteNotificationsForShift);
prep.setInt(1,shiftId);
status = prep.executeUpdate();
} catch (SQLException e) {
log.log(Level.WARNING, "Not able to delete notifications for shift " + shiftId + " from news feed", e);
} finally {
finallyStatement(prep);
}
}
return status != 0;
}
/**
* Deletes notification from news feed
* @param notificationId - ID for notification
* @return True: If success
*/
public boolean deleteNotification(int notificationId){
int status = 0;
if(setUp()) {
try {
conn = getConnection();
PreparedStatement prep = conn.prepareStatement(sqlDeleteNotification);
prep.setInt(1,notificationId);
status = prep.executeUpdate();
} catch (SQLException sqle) {
log.log(Level.WARNING, "Not able to delete notification from news feed");
sqle.printStackTrace();
} finally {
finallyStatement(prep);
}
}
return status != 0;
}
/**
* Returns news feed for given user
* @param userId - ID for given user
* @return ArrayList<NewsFeedItem>
*/
public ArrayList<NewsFeedItem> getNewsFeed(int userId){
ArrayList<NewsFeedItem> out = new ArrayList<>();
if(setUp()) {
ResultSet res = null;
try {
conn = getConnection();
PreparedStatement prep = conn.prepareStatement(sqlGetNewsFeedForUser);
prep.setInt(1, userId);
res = prep.executeQuery();
while(res.next()){
out.add(
new NewsFeedItem(res.getInt("feed_id"),
res.getTimestamp("date_time"),
res.getString("content"),
res.getInt("user_id"),
res.getInt("shift_user_id"),
res.getInt("shift_id"),
NewsFeedItem.NewsFeedCategory.valueOf(res.getInt("category")))
);
}
} catch (SQLException e) {
log.log(Level.WARNING, "Not able to delete notification from news feed", e);
} finally {
finallyStatement(res, prep);
}
}
return out;
}
/**
* Returns NewsFeedItem object for given feed
* @param feedId - ID for given feed
* @return NewsFeedItem
*/
public NewsFeedItem getNewsFeedItem(int feedId){
NewsFeedItem notification = null;
if(setUp()) {
ResultSet res = null;
try {
conn = getConnection();
PreparedStatement prep = conn.prepareStatement(sqlGetNewsFeedItem);
prep.setInt(1, feedId);
res = prep.executeQuery();
if(res.next()){
notification = new NewsFeedItem(res.getInt("feed_id"),
res.getTimestamp("date_time"),
res.getString("content"),
res.getInt("user_id"),
res.getInt("shift_user_id"),
res.getInt("shift_id"),
NewsFeedItem.NewsFeedCategory.valueOf(res.getInt("category")),
res.getInt("start_time")
);
}
} catch (SQLException e) {
log.log(Level.WARNING, "Not able to get notification from news feed", e);
} finally {
finallyStatement(res, prep);
}
}
return notification;
}
/**
* Gets news feed for administrator
* @return ArrayList<NewsFeedItem>
*/
public ArrayList<NewsFeedItem> getNewsFeedAdmin(){
ArrayList<NewsFeedItem> out = new ArrayList<>();
if(setUp()) {
ResultSet res = null;
try {
conn = getConnection();
PreparedStatement prep = conn.prepareStatement(sqlGetNewsFeedForAdmin);
prep.setInt(1, User.UserCategory.ADMIN.getValue());
System.out.println(prep);
res = prep.executeQuery();
while(res.next()){
out.add(
new NewsFeedItem(res.getInt("feed_id"),
res.getTimestamp("date_time"),
res.getString("content"),
res.getInt("user_id"),
res.getInt("shift_user_id"),
res.getInt("shift_id"),
NewsFeedItem.NewsFeedCategory.valueOf(res.getInt("category")),
res.getInt("start_time")
)
);
}
} catch (SQLException e) {
log.log(Level.WARNING, "Not able to delete notification from news feed", e);
} finally {
finallyStatement(res,prep);
}
}
return out;
}
/**
* Sets NewsFeedItem resolved
* @param feedId - ID for given feed
* @param resolved - boolean resolved
* @return True: If success
*/
public boolean setNewsFeedItemResolved(int feedId, boolean resolved){
int status = 0;
if(setUp()) {
try {
conn = getConnection();
PreparedStatement prep = conn.prepareStatement(sqlSetNewsFeedItemResolved);
prep.setBoolean(1,resolved);
prep.setInt(2,feedId);
status = prep.executeUpdate();
} catch (SQLException e) {
log.log(Level.WARNING, "Not able to update notification resolve to "+resolved, e);
} finally {
finallyStatement(prep);
}
}
return status != 0;
}
/**
* @param userId - the ID of the employee who has worked overtime
* @param shiftId - the ID of the shift
* @param startTime - the time of day for given overtime
* @return int - the ID of the news feed item
*/
public int getNewsFeedIdThroughOvertime(int userId, int shiftId, int startTime){
int feedId = 0;
ResultSet res = null;
if(setUp()){
try{
conn = getConnection();
PreparedStatement prep = conn.prepareStatement(sqlGetNewsFeedIdFromOvertime);
prep.setInt(1, userId);
prep.setInt(2, shiftId);
prep.setInt(3, startTime);
res = prep.executeQuery();
res.next();
feedId = res.getInt("feed_id");
} catch (SQLException e) {
log.log(Level.WARNING, "Not able to find feedId for userID: "+userId+", shiftID: " +shiftId+ ", startTime: "+startTime, e);
} finally {
finallyStatement(res, prep);
}
}
return feedId;
}
/**
* @param shiftId the ID of the shift
* @param shiftUserId the ID of the employee who wants to change their shift
* @return int - count of the employees who have yet to accept or reject an offer to take a shift
*/
public int getShiftChangeCountPending(int shiftId, int shiftUserId){
int resolvedCount = 0;
ResultSet res=null;
//category = ? AND shift_id = ? AND shift_user_id = ? AND resolved = 0;";
if(setUp()){
try{
conn = getConnection();
PreparedStatement prep = conn.prepareStatement(sqlGetShiftChangePendingCount);
prep.setInt(1, shiftId);
prep.setInt(2, shiftUserId);
res = prep.executeQuery();
res.next();
resolvedCount = res.getInt("pending");
} catch (SQLException e) {
log.log(Level.WARNING, "Not able to find unresolved user count for shiftID: " +shiftId+ " and shift_user: "+shiftUserId, e);
} finally {
finallyStatement(res, prep);
}
}
return resolvedCount;
}
private static final String sqlUserHasFeed = "SELECT 1 FROM newsfeed WHERE user_id = ? AND feed_id = ?";
/**
* Checks if an user has access to a specified feed item
* @param userId The ID of the user
* @param feedId The ID of the feed item
* @return True if the user has access
*/
public boolean userHasFeed(int userId, int feedId) {
if(!setUp()) {
return false;
}
try {
conn = getConnection();
PreparedStatement prep = conn.prepareStatement(sqlUserHasFeed);
prep.setInt(1, userId);
prep.setInt(2, feedId);
return prep.executeQuery().next();
} catch (SQLException e) {
log.log(Level.WARNING, "Unable to check if user " + userId + " has feed " + feedId, e);
}
return false;
}
public boolean updateResolvedNotifications(int userIdInvolving, int shiftId, NewsFeedItem.NewsFeedCategory category){
int status = 0;
if(setUp()){
try{
conn = getConnection();
PreparedStatement prep = conn.prepareStatement(sqlResolveNotifications);
prep.setInt(1,userIdInvolving);
prep.setInt(2,shiftId);
prep.setInt(3,category.getValue());
status = prep.executeUpdate();
}
catch (SQLException sqle){
log.log(Level.WARNING, "Unable to update notifications for feed", sqle);
}
finally {
finallyStatement(prep);
}
}
return status != 0;
}
}