aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/database
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/traccar/database')
-rw-r--r--src/main/java/org/traccar/database/DeviceManager.java1
-rw-r--r--src/main/java/org/traccar/database/ExtendedObjectManager.java30
-rw-r--r--src/main/java/org/traccar/database/NotificationManager.java126
-rw-r--r--src/main/java/org/traccar/database/PermissionsManager.java51
4 files changed, 48 insertions, 160 deletions
diff --git a/src/main/java/org/traccar/database/DeviceManager.java b/src/main/java/org/traccar/database/DeviceManager.java
index 973cf3f68..e1d6ad1dd 100644
--- a/src/main/java/org/traccar/database/DeviceManager.java
+++ b/src/main/java/org/traccar/database/DeviceManager.java
@@ -91,7 +91,6 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity
if (defaultGroupId != 0) {
Context.getPermissionsManager().refreshDeviceAndGroupPermissions();
- Context.getPermissionsManager().refreshAllExtendedPermissions();
}
return device;
diff --git a/src/main/java/org/traccar/database/ExtendedObjectManager.java b/src/main/java/org/traccar/database/ExtendedObjectManager.java
index 006ed47b2..fe0ebb96e 100644
--- a/src/main/java/org/traccar/database/ExtendedObjectManager.java
+++ b/src/main/java/org/traccar/database/ExtendedObjectManager.java
@@ -44,34 +44,6 @@ public abstract class ExtendedObjectManager<T extends BaseModel> extends SimpleO
refreshExtendedPermissions();
}
- public final Set<Long> getGroupItems(long groupId) {
- try {
- readLock();
- Set<Long> result = groupItems.get(groupId);
- if (result != null) {
- return new HashSet<>(result);
- } else {
- return new HashSet<>();
- }
- } finally {
- readUnlock();
- }
- }
-
- public final Set<Long> getDeviceItems(long deviceId) {
- try {
- readLock();
- Set<Long> result = deviceItems.get(deviceId);
- if (result != null) {
- return new HashSet<>(result);
- } else {
- return new HashSet<>();
- }
- } finally {
- readUnlock();
- }
- }
-
public Set<Long> getAllDeviceItems(long deviceId) {
try {
readLock();
@@ -122,7 +94,7 @@ public abstract class ExtendedObjectManager<T extends BaseModel> extends SimpleO
.add(devicePermission.getPropertyId());
}
- for (Device device : Context.getDeviceManager().getAllDevices()) {
+ for (Device device : getDataManager().getObjects(Device.class)) {
long groupId = device.getGroupId();
while (groupId > 0) {
deviceItemsWithGroups
diff --git a/src/main/java/org/traccar/database/NotificationManager.java b/src/main/java/org/traccar/database/NotificationManager.java
index 3c87aacb2..46b58dd75 100644
--- a/src/main/java/org/traccar/database/NotificationManager.java
+++ b/src/main/java/org/traccar/database/NotificationManager.java
@@ -16,39 +16,36 @@
*/
package org.traccar.database;
-import java.lang.reflect.Field;
-import java.lang.reflect.Modifier;
-import java.util.Arrays;
-import java.util.Date;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Set;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.traccar.Context;
+import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.geocoder.Geocoder;
import org.traccar.model.Calendar;
import org.traccar.model.Event;
import org.traccar.model.Notification;
import org.traccar.model.Position;
-import org.traccar.model.Typed;
-import org.traccar.model.User;
import org.traccar.notification.EventForwarder;
import org.traccar.notification.MessageException;
import org.traccar.notification.NotificatorManager;
import org.traccar.session.cache.CacheManager;
+import org.traccar.storage.Storage;
import org.traccar.storage.StorageException;
+import org.traccar.storage.query.Columns;
+import org.traccar.storage.query.Request;
import javax.annotation.Nullable;
+import javax.inject.Inject;
+import java.util.Arrays;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.stream.Collectors;
-public class NotificationManager extends ExtendedObjectManager<Notification> {
+public class NotificationManager {
private static final Logger LOGGER = LoggerFactory.getLogger(NotificationManager.class);
+ private final Storage storage;
private final CacheManager cacheManager;
private final EventForwarder eventForwarder;
private final NotificatorManager notificatorManager;
@@ -56,81 +53,67 @@ public class NotificationManager extends ExtendedObjectManager<Notification> {
private final boolean geocodeOnRequest;
+ @Inject
public NotificationManager(
- DataManager dataManager, CacheManager cacheManager, @Nullable EventForwarder eventForwarder,
+ Config config, Storage storage, CacheManager cacheManager, @Nullable EventForwarder eventForwarder,
NotificatorManager notificatorManager, @Nullable Geocoder geocoder) {
- super(dataManager, Notification.class);
+ this.storage = storage;
this.cacheManager = cacheManager;
this.eventForwarder = eventForwarder;
this.notificatorManager = notificatorManager;
this.geocoder = geocoder;
- geocodeOnRequest = Context.getConfig().getBoolean(Keys.GEOCODER_ON_REQUEST);
- }
-
- private Set<Long> getEffectiveNotifications(long userId, long deviceId, Date time) {
- Set<Long> result = new HashSet<>();
- Set<Long> deviceNotifications = getAllDeviceItems(deviceId);
- for (long itemId : getUserItems(userId)) {
- if (getById(itemId).getAlways() || deviceNotifications.contains(itemId)) {
- long calendarId = getById(itemId).getCalendarId();
- Calendar calendar = calendarId != 0 ? cacheManager.getObject(Calendar.class, calendarId) : null;
- if (calendar == null || calendar.checkMoment(time)) {
- result.add(itemId);
- }
- }
- }
- return result;
+ geocodeOnRequest = config.getBoolean(Keys.GEOCODER_ON_REQUEST);
}
public void updateEvent(Event event, Position position) {
try {
- getDataManager().addObject(event);
+ event.setId(storage.addObject(event, new Request(new Columns.Exclude("id"))));
} catch (StorageException error) {
LOGGER.warn("Event save error", error);
}
- long deviceId = event.getDeviceId();
- Set<Long> users = Context.getPermissionsManager().getDeviceUsers(deviceId);
- Set<Long> usersToForward = new HashSet<>();
- for (long userId : users) {
- usersToForward.add(userId);
- final Set<String> notificators = new HashSet<>();
- for (long notificationId : getEffectiveNotifications(userId, deviceId, event.getEventTime())) {
- Notification notification = getById(notificationId);
- if (getById(notificationId).getType().equals(event.getType())) {
- boolean filter = false;
+ var notifications = cacheManager.getDeviceObjects(event.getDeviceId(), Notification.class).stream()
+ .filter(notification -> notification.getType().equals(event.getType()))
+ .filter(notification -> {
if (event.getType().equals(Event.TYPE_ALARM)) {
String alarmsAttribute = notification.getString("alarms");
- if (alarmsAttribute == null) {
- filter = true;
- } else {
- List<String> alarms = Arrays.asList(alarmsAttribute.split(","));
- filter = !alarms.contains(event.getString(Position.KEY_ALARM));
+ if (alarmsAttribute != null) {
+ return Arrays.asList(alarmsAttribute.split(","))
+ .contains(event.getString(Position.KEY_ALARM));
}
+ return false;
}
- if (!filter) {
- notificators.addAll(notification.getNotificatorsTypes());
- }
- }
- }
+ return true;
+ })
+ .filter(notification -> {
+ long calendarId = notification.getCalendarId();
+ Calendar calendar = calendarId != 0 ? cacheManager.getObject(Calendar.class, calendarId) : null;
+ return calendar == null || calendar.checkMoment(event.getEventTime());
+ })
+ .collect(Collectors.toUnmodifiableList());
+ if (!notifications.isEmpty()) {
if (position != null && position.getAddress() == null && geocodeOnRequest && geocoder != null) {
position.setAddress(geocoder.getAddress(position.getLatitude(), position.getLongitude(), null));
}
- User user = Context.getUsersManager().getById(userId);
- new Thread(() -> {
- for (String notificator : notificators) {
- try {
- notificatorManager.getNotificator(notificator).send(user, event, position);
- } catch (MessageException | InterruptedException exception) {
- LOGGER.warn("Notification failed", exception);
- }
- }
- }).start();
+ notifications.forEach(notification -> {
+ cacheManager.getNotificationUsers(notification.getId()).forEach(user -> {
+ new Thread(() -> {
+ for (String notificator : notification.getNotificatorsTypes()) {
+ try {
+ notificatorManager.getNotificator(notificator).send(user, event, position);
+ } catch (MessageException | InterruptedException exception) {
+ LOGGER.warn("Notification failed", exception);
+ }
+ }
+ }).start();
+ });
+ });
}
+
if (eventForwarder != null) {
- eventForwarder.forwardEvent(event, position, usersToForward);
+ eventForwarder.forwardEvent(event, position);
}
}
@@ -139,19 +122,4 @@ public class NotificationManager extends ExtendedObjectManager<Notification> {
updateEvent(event.getKey(), event.getValue());
}
}
-
- public Set<Typed> getAllNotificationTypes() {
- Set<Typed> types = new HashSet<>();
- Field[] fields = Event.class.getDeclaredFields();
- for (Field field : fields) {
- if (Modifier.isStatic(field.getModifiers()) && field.getName().startsWith("TYPE_")) {
- try {
- types.add(new Typed(field.get(null).toString()));
- } catch (IllegalArgumentException | IllegalAccessException error) {
- LOGGER.warn("Get event types error", error);
- }
- }
- }
- return types;
- }
}
diff --git a/src/main/java/org/traccar/database/PermissionsManager.java b/src/main/java/org/traccar/database/PermissionsManager.java
index 3c74c0049..f34810439 100644
--- a/src/main/java/org/traccar/database/PermissionsManager.java
+++ b/src/main/java/org/traccar/database/PermissionsManager.java
@@ -22,7 +22,6 @@ import org.traccar.model.BaseModel;
import org.traccar.model.Device;
import org.traccar.model.Group;
import org.traccar.model.ManagedUser;
-import org.traccar.model.Notification;
import org.traccar.model.Permission;
import org.traccar.model.Server;
import org.traccar.model.User;
@@ -237,31 +236,11 @@ public class PermissionsManager {
}
}
- public void checkDeviceLimit(long userId) throws SecurityException {
- int deviceLimit = getUser(userId).getDeviceLimit();
- if (deviceLimit != -1) {
- int deviceCount;
- if (getUserManager(userId)) {
- deviceCount = Context.getDeviceManager().getAllManagedItems(userId).size();
- } else {
- deviceCount = Context.getDeviceManager().getAllUserItems(userId).size();
- }
- if (deviceCount >= deviceLimit) {
- throw new SecurityException("User device limit reached");
- }
- }
- }
-
public boolean getUserReadonly(long userId) {
User user = getUser(userId);
return user != null && user.getReadonly();
}
- public boolean getUserLimitCommands(long userId) {
- User user = getUser(userId);
- return user != null && user.getLimitCommands();
- }
-
public void checkReadonly(long userId) throws SecurityException {
if (!getUserAdmin(userId) && (server.getReadonly() || getUserReadonly(userId))) {
throw new SecurityException("Account is readonly");
@@ -313,18 +292,6 @@ public class PermissionsManager {
}
}
- public void checkGroup(long userId, long groupId) throws SecurityException {
- if (!getGroupPermissions(userId).contains(groupId) && !getUserAdmin(userId)) {
- checkManager(userId);
- for (long managedUserId : usersManager.getUserItems(userId)) {
- if (getGroupPermissions(managedUserId).contains(groupId)) {
- return;
- }
- }
- throw new SecurityException("Group access denied");
- }
- }
-
public void checkDevice(long userId, long deviceId) throws SecurityException {
if (!Context.getDeviceManager().getUserItems(userId).contains(deviceId) && !getUserAdmin(userId)) {
checkManager(userId);
@@ -354,31 +321,13 @@ public class PermissionsManager {
}
}
- public void refreshAllUsersPermissions() {
- if (Context.getNotificationManager() != null) {
- Context.getNotificationManager().refreshUserItems();
- }
- }
-
- public void refreshAllExtendedPermissions() {
- }
-
public void refreshPermissions(Permission permission) {
if (permission.getOwnerClass().equals(User.class)) {
if (permission.getPropertyClass().equals(Device.class)
|| permission.getPropertyClass().equals(Group.class)) {
refreshDeviceAndGroupPermissions();
- refreshAllExtendedPermissions();
} else if (permission.getPropertyClass().equals(ManagedUser.class)) {
usersManager.refreshUserItems();
- } else if (permission.getPropertyClass().equals(Notification.class)
- && Context.getNotificationManager() != null) {
- Context.getNotificationManager().refreshUserItems();
- }
- } else if (permission.getOwnerClass().equals(Device.class) || permission.getOwnerClass().equals(Group.class)) {
- if (permission.getPropertyClass().equals(Notification.class)
- && Context.getNotificationManager() != null) {
- Context.getNotificationManager().refreshExtendedPermissions();
}
}
}