diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2016-03-11 16:22:40 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2016-03-11 16:22:40 +1300 |
commit | 5e2ad583f62db56fe81ec4c41756fb3fc28747a5 (patch) | |
tree | aa920439e487ba8f28d1bea116534a350985c0d6 /src/org/traccar | |
parent | 8a449b2b2f8a1c250543ad5c6134dedf302c75c5 (diff) | |
download | trackermap-server-5e2ad583f62db56fe81ec4c41756fb3fc28747a5.tar.gz trackermap-server-5e2ad583f62db56fe81ec4c41756fb3fc28747a5.tar.bz2 trackermap-server-5e2ad583f62db56fe81ec4c41756fb3fc28747a5.zip |
Add devices to group tree
Diffstat (limited to 'src/org/traccar')
-rw-r--r-- | src/org/traccar/database/GroupTree.java | 99 | ||||
-rw-r--r-- | src/org/traccar/database/PermissionsManager.java | 13 |
2 files changed, 79 insertions, 33 deletions
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<GroupNode> children = new HashSet<>(); + private Device device; + private Collection<TreeNode> 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<GroupNode> getChildren() { + public Collection<TreeNode> getChildren() { return children; } } - private final Map<Long, GroupNode> groupMap = new HashMap<>(); + private final Map<Long, TreeNode> groupMap = new HashMap<>(); - public GroupTree(Collection<Group> groups) { + public GroupTree(Collection<Group> groups, Collection<Device> 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<Long, TreeNode> 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<Group> getDescendants(long groupId) { - Set<GroupNode> results = new HashSet<>(); - getDescendants(results, groupMap.get(groupId)); + public Collection<Group> getGroups(long groupId) { + Set<TreeNode> results = new HashSet<>(); + getNodes(results, groupMap.get(groupId)); Collection<Group> 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<GroupNode> results, GroupNode node) { - for (GroupNode child : node.getChildren()) { + public Collection<Device> getDevices(long groupId) { + Set<TreeNode> results = new HashSet<>(); + getNodes(results, groupMap.get(groupId)); + Collection<Device> devices = new ArrayList<>(); + for (TreeNode node : results) { + if (node.getDevice() != null) { + devices.add(node.getDevice()); + } + } + return devices; + } + + private void getNodes(Set<TreeNode> 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<Long> userGroupPermissions = getGroupPermissions(permission.getUserId()); + Set<Long> 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); } |