aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/database/SimpleObjectManager.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/database/SimpleObjectManager.java')
-rw-r--r--src/org/traccar/database/SimpleObjectManager.java134
1 files changed, 23 insertions, 111 deletions
diff --git a/src/org/traccar/database/SimpleObjectManager.java b/src/org/traccar/database/SimpleObjectManager.java
index 124178a05..fa41f30b6 100644
--- a/src/org/traccar/database/SimpleObjectManager.java
+++ b/src/org/traccar/database/SimpleObjectManager.java
@@ -17,9 +17,7 @@
package org.traccar.database;
import java.sql.SQLException;
-import java.util.Collection;
import java.util.HashSet;
-import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@@ -30,43 +28,15 @@ import org.traccar.model.BaseModel;
import org.traccar.model.Permission;
import org.traccar.model.User;
-public abstract class SimpleObjectManager {
+public abstract class SimpleObjectManager extends BaseObjectManager implements ManagableObjects {
- private final DataManager dataManager;
-
- private Map<Long, BaseModel> items;
- private final Map<Long, Set<Long>> userItems = new ConcurrentHashMap<>();
-
- private Class<? extends BaseModel> baseClass;
- private String baseClassIdName;
+ private Map<Long, Set<Long>> userItems;
protected SimpleObjectManager(DataManager dataManager, Class<? extends BaseModel> baseClass) {
- this.dataManager = dataManager;
- this.baseClass = baseClass;
- baseClassIdName = DataManager.makeNameId(baseClass);
- refreshItems();
- }
-
- protected final DataManager getDataManager() {
- return dataManager;
- }
-
- protected final Class<? extends BaseModel> getBaseClass() {
- return baseClass;
- }
-
- protected final String getBaseClassIdName() {
- return baseClassIdName;
- }
-
- public final BaseModel getById(long itemId) {
- return items.get(itemId);
- }
-
- protected final void clearItems() {
- items.clear();
+ super(dataManager, baseClass);
}
+ @Override
public final Set<Long> getUserItems(long userId) {
if (!userItems.containsKey(userId)) {
userItems.put(userId, new HashSet<Long>());
@@ -74,47 +44,35 @@ public abstract class SimpleObjectManager {
return userItems.get(userId);
}
- protected final void clearUserItems() {
- userItems.clear();
+ @Override
+ public Set<Long> getManagedItems(long userId) {
+ Set<Long> result = new HashSet<>();
+ result.addAll(getUserItems(userId));
+ for (long managedUserId : Context.getUsersManager().getUserItems(userId)) {
+ result.addAll(getUserItems(managedUserId));
+ }
+ return result;
}
public final boolean checkItemPermission(long userId, long itemId) {
return getUserItems(userId).contains(itemId);
}
+ @Override
public void refreshItems() {
- if (dataManager != null) {
- try {
- Collection<? extends BaseModel> databaseItems = dataManager.getObjects(baseClass);
- if (items == null) {
- items = new ConcurrentHashMap<>(databaseItems.size());
- }
- Set<Long> databaseItemIds = new HashSet<>();
- for (BaseModel item : databaseItems) {
- databaseItemIds.add(item.getId());
- if (items.containsKey(item.getId())) {
- updateCachedItem(item);
- } else {
- addNewItem(item);
- }
- }
- for (Long cachedItemId : items.keySet()) {
- if (!databaseItemIds.contains(cachedItemId)) {
- removeCachedItem(cachedItemId);
- }
- }
- } catch (SQLException error) {
- Log.warning(error);
- }
- }
+ super.refreshItems();
refreshUserItems();
}
public final void refreshUserItems() {
- if (dataManager != null) {
+ if (getDataManager() != null) {
try {
- clearUserItems();
- for (Permission permission : dataManager.getPermissions(User.class, baseClass)) {
+ if (userItems != null) {
+ userItems.clear();
+ } else {
+ userItems = new ConcurrentHashMap<>();
+ }
+ for (Permission permission : getDataManager().getPermissions(User.class, getBaseClass())) {
getUserItems(permission.getOwnerId()).add(permission.getPropertyId());
}
} catch (SQLException | ClassNotFoundException error) {
@@ -123,56 +81,10 @@ public abstract class SimpleObjectManager {
}
}
- protected void addNewItem(BaseModel item) {
- items.put(item.getId(), item);
- }
-
- public void addItem(BaseModel item) throws SQLException {
- dataManager.addObject(item);
- addNewItem(item);
- }
-
- protected void updateCachedItem(BaseModel item) {
- items.put(item.getId(), item);
- }
-
- public void updateItem(BaseModel item) throws SQLException {
- dataManager.updateObject(item);
- updateCachedItem(item);
- }
-
- protected void removeCachedItem(long itemId) {
- items.remove(itemId);
- }
-
+ @Override
public void removeItem(long itemId) throws SQLException {
- BaseModel item = getById(itemId);
- if (item != null) {
- dataManager.removeObject(baseClass, itemId);
- removeCachedItem(itemId);
- }
+ super.removeItem(itemId);
refreshUserItems();
}
- public final <T> Collection<T> getItems(Class<T> clazz, Set<Long> itemIds) {
- Collection<T> result = new LinkedList<>();
- for (long itemId : itemIds) {
- result.add((T) getById(itemId));
- }
- return result;
- }
-
- public final Set<Long> getAllItems() {
- return items.keySet();
- }
-
- public Set<Long> getManagedItems(long userId) {
- Set<Long> result = new HashSet<>();
- result.addAll(getUserItems(userId));
- for (long managedUserId : Context.getUsersManager().getManagedItems(userId)) {
- result.addAll(getUserItems(managedUserId));
- }
- return result;
- }
-
}