From 5e2ad583f62db56fe81ec4c41756fb3fc28747a5 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 11 Mar 2016 16:22:40 +1300 Subject: Add devices to group tree --- src/org/traccar/database/GroupTree.java | 99 +++++++++++++++++------- src/org/traccar/database/PermissionsManager.java | 13 ++-- 2 files changed, 79 insertions(+), 33 deletions(-) (limited to 'src/org/traccar/database') diff --git a/src/org/traccar/database/GroupTree.java b/src/org/traccar/database/GroupTree.java index 7263a6331..b383b1501 100644 --- a/src/org/traccar/database/GroupTree.java +++ b/src/org/traccar/database/GroupTree.java @@ -15,6 +15,7 @@ */ package org.traccar.database; +import org.traccar.model.Device; import org.traccar.model.Group; import java.util.ArrayList; @@ -26,80 +27,126 @@ import java.util.Set; public class GroupTree { - private static class GroupNode { + private static class TreeNode { private Group group; - private GroupNode parent; - private Collection children = new HashSet<>(); + private Device device; + private Collection children = new HashSet<>(); - public GroupNode(Group group) { + public TreeNode(Group group) { this.group = group; } + public TreeNode(Device device) { + this.device = device; + } + @Override public int hashCode() { - return (int) group.getId(); + if (group != null) { + return (int) group.getId(); + } else { + return (int) device.getId(); + } } @Override public boolean equals(Object obj) { - if (!(obj instanceof GroupNode)) { + if (!(obj instanceof TreeNode)) { return false; } - return obj == this || group.getId() == ((GroupNode) obj).group.getId(); + TreeNode other = (TreeNode) obj; + if (other == this) { + return true; + } + if (group != null) { + if (other.group != null) { + return group.getId() == other.group.getId(); + } + } else if (device != null) { + if (other.device != null) { + return device.getId() == other.device.getId(); + } + } + return false; } public Group getGroup() { return group; } - public void setParent(GroupNode parent) { - this.parent = parent; + public Device getDevice() { + return device; + } + + public void setParent(TreeNode parent) { if (parent != null) { parent.children.add(this); } } - public GroupNode getParent() { - return parent; - } - - public Collection getChildren() { + public Collection getChildren() { return children; } } - private final Map groupMap = new HashMap<>(); + private final Map groupMap = new HashMap<>(); - public GroupTree(Collection groups) { + public GroupTree(Collection groups, Collection devices) { for (Group group : groups) { - groupMap.put(group.getId(), new GroupNode(group)); + groupMap.put(group.getId(), new TreeNode(group)); } - for (GroupNode node : groupMap.values()) { + for (TreeNode node : groupMap.values()) { if (node.getGroup().getGroupId() != 0) { node.setParent(groupMap.get(node.getGroup().getGroupId())); } } + Map deviceMap = new HashMap<>(); + + for (Device device : devices) { + deviceMap.put(device.getId(), new TreeNode(device)); + } + + for (TreeNode node : deviceMap.values()) { + if (node.getDevice().getGroupId() != 0) { + node.setParent(groupMap.get(node.getDevice().getGroupId())); + } + } + } - public Collection getDescendants(long groupId) { - Set results = new HashSet<>(); - getDescendants(results, groupMap.get(groupId)); + public Collection getGroups(long groupId) { + Set results = new HashSet<>(); + getNodes(results, groupMap.get(groupId)); Collection groups = new ArrayList<>(); - for (GroupNode node : results) { - groups.add(node.getGroup()); + for (TreeNode node : results) { + if (node.getGroup() != null) { + groups.add(node.getGroup()); + } } return groups; } - private void getDescendants(Set results, GroupNode node) { - for (GroupNode child : node.getChildren()) { + public Collection getDevices(long groupId) { + Set results = new HashSet<>(); + getNodes(results, groupMap.get(groupId)); + Collection devices = new ArrayList<>(); + for (TreeNode node : results) { + if (node.getDevice() != null) { + devices.add(node.getDevice()); + } + } + return devices; + } + + private void getNodes(Set results, TreeNode node) { + for (TreeNode child : node.getChildren()) { results.add(child); - getDescendants(results, child); + getNodes(results, child); } } diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 0aefb9605..dc37bbc84 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -16,7 +16,6 @@ package org.traccar.database; import java.sql.SQLException; -import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -69,23 +68,23 @@ public class PermissionsManager { users.put(user.getId(), user); } - GroupTree groupTree = new GroupTree(dataManager.getAllGroups()); + GroupTree groupTree = new GroupTree(dataManager.getAllGroups(), dataManager.getAllDevices()); for (GroupPermission permission : dataManager.getGroupPermissions()) { Set userGroupPermissions = getGroupPermissions(permission.getUserId()); + Set userDevicePermissions = getDevicePermissions(permission.getUserId()); userGroupPermissions.add(permission.getGroupId()); - for (Group group : groupTree.getDescendants(permission.getGroupId())) { + for (Group group : groupTree.getGroups(permission.getGroupId())) { userGroupPermissions.add(group.getId()); } + for (Device device : groupTree.getDevices(permission.getGroupId())) { + userDevicePermissions.add(device.getId()); + } } for (DevicePermission permission : dataManager.getDevicePermissions()) { getDevicePermissions(permission.getUserId()).add(permission.getDeviceId()); } - for (Device device : dataManager.getAllDevices()) { - // TODO - } - } catch (SQLException error) { Log.warning(error); } -- cgit v1.2.3