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 --- .../c2kernel/entity/agent/AgentImplementation.java | 4 ++-- .../com/c2kernel/entity/imports/ImportAgent.java | 4 ++-- .../com/c2kernel/entity/imports/ImportRole.java | 28 ++++++++++++++++++++-- 3 files changed, 30 insertions(+), 6 deletions(-) (limited to 'src/main/java/com/c2kernel/entity') diff --git a/src/main/java/com/c2kernel/entity/agent/AgentImplementation.java b/src/main/java/com/c2kernel/entity/agent/AgentImplementation.java index 0406387..c0f0dd0 100644 --- a/src/main/java/com/c2kernel/entity/agent/AgentImplementation.java +++ b/src/main/java/com/c2kernel/entity/agent/AgentImplementation.java @@ -60,7 +60,7 @@ public class AgentImplementation extends ItemImplementation implements public void addRole(String roleName) throws CannotManageException, ObjectNotFoundException { RolePath newRole = Gateway.getLookup().getRolePath(roleName); try { - newRole.addAgent(mAgentPath); + Gateway.getLookupManager().addRole(mAgentPath, newRole); } catch (ObjectCannotBeUpdated ex) { throw new CannotManageException("Could not update role"); } @@ -70,7 +70,7 @@ public class AgentImplementation extends ItemImplementation implements public void removeRole(String roleName) throws CannotManageException, ObjectNotFoundException { RolePath rolePath = Gateway.getLookup().getRolePath(roleName); try { - rolePath.removeAgent(mAgentPath); + Gateway.getLookupManager().removeRole(mAgentPath, rolePath); } catch (ObjectCannotBeUpdated ex) { throw new CannotManageException("Could not update role"); } diff --git a/src/main/java/com/c2kernel/entity/imports/ImportAgent.java b/src/main/java/com/c2kernel/entity/imports/ImportAgent.java index ac9911e..2bbd307 100644 --- a/src/main/java/com/c2kernel/entity/imports/ImportAgent.java +++ b/src/main/java/com/c2kernel/entity/imports/ImportAgent.java @@ -50,9 +50,9 @@ public class ImportAgent extends ModuleImport implements java.io.Serializable { try { thisRole = Gateway.getLookup().getRolePath(role); } catch (ObjectNotFoundException ex) { - throw new ObjectNotFoundException("Role "+role+" does not exist."); + throw new ObjectNotFoundException("Role "+role+" does not exist.", ""); } - thisRole.addAgent(newAgent); + Gateway.getLookupManager().addRole(newAgent, thisRole); } } diff --git a/src/main/java/com/c2kernel/entity/imports/ImportRole.java b/src/main/java/com/c2kernel/entity/imports/ImportRole.java index dc8f351..975c18b 100644 --- a/src/main/java/com/c2kernel/entity/imports/ImportRole.java +++ b/src/main/java/com/c2kernel/entity/imports/ImportRole.java @@ -1,9 +1,14 @@ package com.c2kernel.entity.imports; +import java.util.Iterator; + import com.c2kernel.common.CannotManageException; import com.c2kernel.common.ObjectAlreadyExistsException; import com.c2kernel.common.ObjectCannotBeUpdated; +import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.lookup.AgentPath; +import com.c2kernel.lookup.Path; +import com.c2kernel.lookup.RolePath; import com.c2kernel.process.Gateway; import com.c2kernel.process.module.ModuleImport; @@ -15,8 +20,27 @@ public class ImportRole extends ModuleImport { } @Override - public void create(AgentPath agentPath, boolean reset) throws ObjectAlreadyExistsException, ObjectCannotBeUpdated, CannotManageException { - Gateway.getLookupManager().createRole(name, jobList); + public void create(AgentPath agentPath, boolean reset) throws ObjectAlreadyExistsException, ObjectCannotBeUpdated, CannotManageException, ObjectNotFoundException { + RolePath parent = new RolePath(); + if (name.indexOf('/') > -1) { + String[] roleComp = name.split("/"); + for (int i=0; i childIter = parent.getChildren(); + boolean found = false; + while (childIter.hasNext()) { + RolePath childRole = (RolePath)childIter.next(); + if (childRole.getName().equals(roleComp[i])) { + parent = childRole; + found = true; + break; + } + } + if (!found) throw new ObjectNotFoundException("Parent role "+roleComp[i]+" was not found", ""); + } + name = roleComp[roleComp.length-1]; + } + RolePath newRole = new RolePath(parent, name, jobList); + if (!newRole.exists()) Gateway.getLookupManager().createRole(newRole); } public boolean hasJobList() { -- cgit v1.2.3