From 6bfda86daaef7ce7b591f2fe1b33ab3b1d5ca2cd Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Sun, 4 Dec 2016 17:33:17 +0700 Subject: Use model instead of attributes to store Notification options --- .../traccar/api/resource/NotificationResource.java | 2 +- src/org/traccar/database/NotificationManager.java | 27 ++++++++++++++++++---- src/org/traccar/model/Notification.java | 21 +++++++++++++++++ 3 files changed, 44 insertions(+), 6 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/api/resource/NotificationResource.java b/src/org/traccar/api/resource/NotificationResource.java index 6c9725f37..03f7e4ba0 100644 --- a/src/org/traccar/api/resource/NotificationResource.java +++ b/src/org/traccar/api/resource/NotificationResource.java @@ -49,7 +49,7 @@ public class NotificationResource extends BaseResource { userId = getUserId(); } Context.getPermissionsManager().checkUser(getUserId(), userId); - return Context.getNotificationManager().getUserNotifications(userId); + return Context.getNotificationManager().getAllUserNotifications(userId); } @POST diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java index 7e79e289f..caf1091cc 100644 --- a/src/org/traccar/database/NotificationManager.java +++ b/src/org/traccar/database/NotificationManager.java @@ -59,10 +59,10 @@ public class NotificationManager { && Context.getGeofenceManager().checkGeofence(userId, event.getGeofenceId())) { Notification notification = getUserNotificationByType(userId, event.getType()); if (notification != null) { - if (notification.getAttributes().containsKey("web")) { + if (notification.getWeb()) { Context.getConnectionManager().updateEvent(userId, event, position); } - if (notification.getAttributes().containsKey("mail")) { + if (notification.getMail()) { NotificationMail.sendMailAsync(userId, event, position); } } @@ -130,8 +130,11 @@ public class NotificationManager { public void updateNotification(Notification notification) { Notification cachedNotification = getUserNotificationByType(notification.getUserId(), notification.getType()); if (cachedNotification != null) { - if (!cachedNotification.getAttributes().equals(notification.getAttributes())) { - if (notification.getAttributes().isEmpty()) { + if (cachedNotification.getWeb() != notification.getWeb() + || cachedNotification.getMail() != notification.getMail() + || !cachedNotification.getAttributes().equals(notification.getAttributes())) { + if (!notification.getWeb() && !notification.getMail() + && notification.getAttributes().isEmpty()) { try { dataManager.removeNotification(cachedNotification); } catch (SQLException error) { @@ -146,6 +149,8 @@ public class NotificationManager { } else { notificationsLock.writeLock().lock(); try { + cachedNotification.setWeb(notification.getWeb()); + cachedNotification.setMail(notification.getMail()); cachedNotification.setAttributes(notification.getAttributes()); } finally { notificationsLock.writeLock().unlock(); @@ -159,7 +164,7 @@ public class NotificationManager { } else { notification.setId(cachedNotification.getId()); } - } else if (!notification.getAttributes().isEmpty()) { + } else if (notification.getWeb() || notification.getMail() || !notification.getAttributes().isEmpty()) { try { dataManager.addNotification(notification); } catch (SQLException error) { @@ -193,4 +198,16 @@ public class NotificationManager { return notifications; } + public Collection getAllUserNotifications(long userId) { + Map notifications = new HashMap<>(); + for (Notification notification : getAllNotifications()) { + notification.setUserId(userId); + notifications.put(notification.getType(), notification); + } + for (Notification notification : getUserNotifications(userId)) { + notifications.put(notification.getType(), notification); + } + return notifications.values(); + } + } diff --git a/src/org/traccar/model/Notification.java b/src/org/traccar/model/Notification.java index 64e1ac60c..3f08c0a39 100644 --- a/src/org/traccar/model/Notification.java +++ b/src/org/traccar/model/Notification.java @@ -37,4 +37,25 @@ public class Notification extends Extensible { this.type = type; } + private boolean web; + + public boolean getWeb() { + return web; + } + + public void setWeb(boolean web) { + this.web = web; + } + + private boolean mail; + + public boolean getMail() { + return mail; + } + + public void setMail(boolean mail) { + this.mail = mail; + } + + } -- cgit v1.2.3 From effcde41e6f77df3829a9809f48fcc3aed9caade Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Mon, 5 Dec 2016 11:53:35 +0700 Subject: - Ignore attributes in notifications - Remove extra spaces --- src/org/traccar/database/NotificationManager.java | 8 +++----- src/org/traccar/model/Notification.java | 2 -- 2 files changed, 3 insertions(+), 7 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java index caf1091cc..ee804f5cd 100644 --- a/src/org/traccar/database/NotificationManager.java +++ b/src/org/traccar/database/NotificationManager.java @@ -131,10 +131,8 @@ public class NotificationManager { Notification cachedNotification = getUserNotificationByType(notification.getUserId(), notification.getType()); if (cachedNotification != null) { if (cachedNotification.getWeb() != notification.getWeb() - || cachedNotification.getMail() != notification.getMail() - || !cachedNotification.getAttributes().equals(notification.getAttributes())) { - if (!notification.getWeb() && !notification.getMail() - && notification.getAttributes().isEmpty()) { + || cachedNotification.getMail() != notification.getMail()) { + if (!notification.getWeb() && !notification.getMail()) { try { dataManager.removeNotification(cachedNotification); } catch (SQLException error) { @@ -164,7 +162,7 @@ public class NotificationManager { } else { notification.setId(cachedNotification.getId()); } - } else if (notification.getWeb() || notification.getMail() || !notification.getAttributes().isEmpty()) { + } else if (notification.getWeb() || notification.getMail()) { try { dataManager.addNotification(notification); } catch (SQLException error) { diff --git a/src/org/traccar/model/Notification.java b/src/org/traccar/model/Notification.java index 3f08c0a39..dd5f66f15 100644 --- a/src/org/traccar/model/Notification.java +++ b/src/org/traccar/model/Notification.java @@ -56,6 +56,4 @@ public class Notification extends Extensible { public void setMail(boolean mail) { this.mail = mail; } - - } -- cgit v1.2.3