1 package org.jbpm.identity.assignment; 2 3 import java.util.Set ; 4 5 import org.apache.commons.logging.Log; 6 import org.apache.commons.logging.LogFactory; 7 import org.jbpm.db.JbpmSession; 8 import org.jbpm.graph.exe.ExecutionContext; 9 import org.jbpm.graph.exe.Token; 10 import org.jbpm.identity.Entity; 11 import org.jbpm.identity.Group; 12 import org.jbpm.identity.User; 13 import org.jbpm.identity.hibernate.IdentitySession; 14 import org.jbpm.security.Authentication; 15 import org.jbpm.taskmgmt.def.AssignmentHandler; 16 import org.jbpm.taskmgmt.exe.Assignable; 17 import org.jbpm.taskmgmt.exe.SwimlaneInstance; 18 19 35 public class ExpressionAssignmentHandler implements AssignmentHandler { 36 37 private static final long serialVersionUID = 1L; 38 39 protected String expression; 40 protected ExecutionContext executionContext = null; 41 protected ExpressionSession expressionSession = null; 42 protected TermTokenizer tokenizer; 43 protected Entity entity = null; 44 45 public void assign(Assignable assignable, ExecutionContext executionContext) { 46 47 try { 48 expressionSession = getExpressionSession(); 49 if (expressionSession==null) { 50 throw new NullPointerException ("getIdentitySession returned null"); 51 } 52 this.tokenizer = new TermTokenizer(expression); 53 this.executionContext = executionContext; 54 entity = resolveFirstTerm(tokenizer.nextTerm()); 55 while( tokenizer.hasMoreTerms() 56 && (entity!=null) ) { 57 entity = resolveNextTerm(tokenizer.nextTerm()); 58 } 59 if (entity==null) { 61 throw new RuntimeException ("couldn't resolve assignment expression '"+expression+"'"); 63 64 } else if (entity instanceof User) { 66 assignable.setActorId(entity.getName()); 68 69 } else if (entity instanceof Group) { 71 assignable.setPooledActors(new String []{entity.getName()}); 73 } 74 75 } catch (RuntimeException e) { 76 throw new ExpressionAssignmentException("couldn't resolve assignment expression '"+expression+"'", e); 77 } 78 } 79 80 85 protected ExpressionSession getExpressionSession() { 86 JbpmSession currentJbpmSession = JbpmSession.getCurrentJbpmSession(); 87 if ( (currentJbpmSession==null) 88 || (currentJbpmSession.getSession()==null) 89 || (! currentJbpmSession.getSession().isOpen()) 90 ) { 91 throw new RuntimeException ("no active JbpmSession for resolving assignment expression'"+expression+"'"); 92 } 93 return new IdentitySession(currentJbpmSession.getSession()); 94 } 95 96 protected Entity resolveFirstTerm(String term) { 97 Entity entity = null; 98 99 log.debug("resolving first term '"+term+"'"); 100 101 if (term.equalsIgnoreCase("previous")) { 102 String userName = Authentication.getAuthenticatedActorId(); 103 entity = getUserByName(userName); 104 105 } else if ( (term.startsWith("swimlane(")) 106 && (term.endsWith(")")) ) { 107 String swimlaneName = term.substring(9,term.length()-1).trim(); 108 String userName = getSwimlaneActorId(swimlaneName); 109 entity = getUserByName(userName); 110 111 } else if ( (term.startsWith("variable(")) 112 && (term.endsWith(")")) ) { 113 String variableName = term.substring(9,term.length()-1).trim(); 114 Object value = getVariable(variableName); 115 116 if (value==null) { 117 throw new ExpressionAssignmentException("variable '"+variableName+"' is null"); 118 119 } else if (value instanceof String ) { 120 entity = getUserByName((String ) value); 121 122 } else if (value instanceof Entity) { 123 entity = (Entity) value; 124 } 125 126 } else if ( (term.startsWith("user(")) 127 && (term.endsWith(")")) ) { 128 String userName = term.substring(5,term.length()-1).trim(); 129 entity = getUserByName(userName); 130 131 } else if ( (term.startsWith("group(")) 132 && (term.endsWith(")")) ) { 133 String groupName = term.substring(6,term.length()-1).trim(); 134 entity = getGroupByName(groupName); 135 136 } else { 137 throw new ExpressionAssignmentException("couldn't interpret first term in expression '"+expression+"'"); 138 } 139 140 return entity; 141 } 142 143 protected Entity resolveNextTerm(String term) { 144 145 log.debug("resolving next term '"+term+"'"); 146 147 if ( (term.startsWith("group(")) 148 && (term.endsWith(")")) ) { 149 String groupType = term.substring(6,term.length()-1).trim(); 150 User user = (User) entity; 151 Set groups = user.getGroupsForGroupType(groupType); 152 if (groups.size()==0) { 153 throw new ExpressionAssignmentException("no groups for group-type '"+groupType+"'"); 154 } 155 entity = (Entity) groups.iterator().next(); 156 157 } else if ( (term.startsWith("member(")) 158 && (term.endsWith(")")) ) { 159 String roleName = term.substring(7,term.length()-1).trim(); 160 Group group = (Group) entity; 161 Set users = group.getUsersForMembershipName(roleName); 162 if (users.size()==0) { 163 throw new ExpressionAssignmentException("no users in role '"+roleName+"'"); 164 } 165 entity = (Entity) users.iterator().next(); 166 167 } else { 168 throw new ExpressionAssignmentException("couldn't interpret term '"+term+"' in expression '"+expression+"'"); 169 } 170 171 return entity; 172 } 173 174 protected Object getVariable(String variableName) { 175 Token token = executionContext.getToken(); 176 return executionContext.getContextInstance().getVariable(variableName, token); 177 } 178 179 protected Entity getGroupByName(String groupName) { 180 Group group = null; 181 group = expressionSession.getGroupByName(groupName); 182 if (group==null) { 183 throw new ExpressionAssignmentException("group '"+groupName+"' couldn't be fetched from the user db"); 184 } 185 return group; 186 } 187 188 protected Entity getUserByName(String userName) { 189 User user = null; 190 user = expressionSession.getUserByName(userName); 191 if (user==null) { 192 throw new ExpressionAssignmentException("user '"+userName+"' couldn't be fetched from the user db"); 193 } 194 return user; 195 } 196 197 protected String getSwimlaneActorId(String swimlaneName) { 198 SwimlaneInstance swimlaneInstance = executionContext 199 .getTaskMgmtInstance() 200 .getSwimlaneInstance(swimlaneName); 201 if (swimlaneInstance==null) { 202 throw new ExpressionAssignmentException("no swimlane instance '"+swimlaneName+"'"); 203 } 204 return swimlaneInstance.getActorId(); 205 } 206 207 private static final Log log = LogFactory.getLog(ExpressionAssignmentHandler.class); 208 } 209 | Popular Tags |