From 3743d182d99dbed9d2be84dc357f6839ffe4d2ec Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Thu, 18 Sep 2014 10:39:05 +0200 Subject: Hierarchical Roles. Fixes #199 --- src/main/java/com/c2kernel/lookup/Lookup.java | 2 + .../java/com/c2kernel/lookup/LookupManager.java | 10 +++- src/main/java/com/c2kernel/lookup/RolePath.java | 56 ++++++++++------------ 3 files changed, 35 insertions(+), 33 deletions(-) (limited to 'src/main/java/com/c2kernel/lookup') diff --git a/src/main/java/com/c2kernel/lookup/Lookup.java b/src/main/java/com/c2kernel/lookup/Lookup.java index 87dec1d..6627ea4 100644 --- a/src/main/java/com/c2kernel/lookup/Lookup.java +++ b/src/main/java/com/c2kernel/lookup/Lookup.java @@ -137,6 +137,8 @@ public interface Lookup { public RolePath[] getRoles(AgentPath agentPath); /** + * Returns all of the Agents in this centre who hold this role (including sub-roles) + * * @param agentPath * @param role * @return diff --git a/src/main/java/com/c2kernel/lookup/LookupManager.java b/src/main/java/com/c2kernel/lookup/LookupManager.java index ce1268e..a8ce41b 100644 --- a/src/main/java/com/c2kernel/lookup/LookupManager.java +++ b/src/main/java/com/c2kernel/lookup/LookupManager.java @@ -40,16 +40,20 @@ public interface LookupManager extends Lookup { // Role and agent management /** + * Creates a new Role in this centre + * * @param role * @param hasJobList * @return * @throws ObjectAlreadyExistsException * @throws ObjectCannotBeUpdated */ - public RolePath createRole(String role, boolean hasJobList) throws ObjectAlreadyExistsException, ObjectCannotBeUpdated; + public RolePath createRole(RolePath role) throws ObjectAlreadyExistsException, ObjectCannotBeUpdated; /** - * @param agent + * Adds the given Agent to the given Role, if they both exist. + * + * @param agent - the * @param rolePath * @throws ObjectCannotBeUpdated * @throws ObjectNotFoundException @@ -57,6 +61,8 @@ public interface LookupManager extends Lookup { public void addRole(AgentPath agent, RolePath rolePath) throws ObjectCannotBeUpdated, ObjectNotFoundException; /** + * Removes the given Agent from the given Role. Does not delete the Role. + * * @param agent * @param role * @throws ObjectCannotBeUpdated diff --git a/src/main/java/com/c2kernel/lookup/RolePath.java b/src/main/java/com/c2kernel/lookup/RolePath.java index 9f9c597..e82c7ad 100644 --- a/src/main/java/com/c2kernel/lookup/RolePath.java +++ b/src/main/java/com/c2kernel/lookup/RolePath.java @@ -10,14 +10,12 @@ package com.c2kernel.lookup; -import java.util.ArrayList; import java.util.Iterator; import com.c2kernel.common.CannotManageException; import com.c2kernel.common.ObjectCannotBeUpdated; import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.process.Gateway; -import com.c2kernel.utils.Logger; @@ -32,13 +30,31 @@ public class RolePath extends DomainPath */ private boolean hasJobList = false; + + public RolePath() { + super("agent"); + } - public RolePath(String roleName) { - super(new DomainPath("agent"), roleName); + @Override + public RolePath getParent() { + try { + if (mPath.length > 2) + return Gateway.getLookup().getRolePath(mPath[mPath.length-2]); + } catch (ObjectNotFoundException ex) { } + return null; + } + + public RolePath(RolePath parent, String roleName) { + super(parent, roleName); + } + + public RolePath(String[] path, boolean jobList) { + super(path); + hasJobList = jobList; } - public RolePath(String roleName, boolean jobList) { - this(roleName); + public RolePath(RolePath parent, String roleName, boolean jobList) { + this(parent, roleName); hasJobList = jobList; } @@ -56,7 +72,6 @@ public class RolePath extends DomainPath */ public void setHasJobList(boolean hasJobList) throws ObjectNotFoundException, ObjectCannotBeUpdated, CannotManageException { this.hasJobList = hasJobList; - Gateway.getLookupManager().setHasJobList(this, hasJobList); } @@ -64,30 +79,9 @@ public class RolePath extends DomainPath protected void checkType() { mType = CONTEXT; } - - public Iterator getChildren() { - AgentPath[] agents = getAgentsWithRole(); - ArrayList children = new ArrayList(agents.length); - for (int i = 0; i < agents.length; i++) - children.add(i, agents[i]); - return children.iterator(); - } - - public AgentPath[] getAgentsWithRole() { - try { - return Gateway.getLookup().getAgents(this); - } catch (ObjectNotFoundException ex) { - Logger.error("Cannot retrieve agent list. Role "+getName()+" does not exist in LDAP"); - return new AgentPath[0]; - } - } - - public void addAgent(AgentPath agent) throws ObjectCannotBeUpdated, CannotManageException, ObjectNotFoundException { - Gateway.getLookupManager().addRole(agent, this); - } - - public void removeAgent(AgentPath agent) throws ObjectCannotBeUpdated, CannotManageException, ObjectNotFoundException { - Gateway.getLookupManager().removeRole(agent, this); + + public Iterator getChildren() { + return Gateway.getLookup().getChildren(this); } @Override -- cgit v1.2.3