1 17 18 19 20 package org.apache.lenya.cms.task; 21 22 import java.util.Map ; 23 24 import org.apache.lenya.ac.Identity; 25 import org.apache.lenya.ac.Machine; 26 import org.apache.lenya.ac.Role; 27 import org.apache.lenya.ac.User; 28 import org.apache.lenya.cms.publication.Document; 29 import org.apache.lenya.cms.publication.DocumentBuildException; 30 import org.apache.lenya.cms.publication.Publication; 31 import org.apache.lenya.cms.workflow.WorkflowFactory; 32 import org.apache.lenya.util.NamespaceMap; 33 import org.apache.lenya.workflow.Event; 34 import org.apache.lenya.workflow.Situation; 35 import org.apache.lenya.workflow.SynchronizedWorkflowInstances; 36 import org.apache.log4j.Category; 37 38 public class WorkflowInvoker extends ParameterWrapper { 39 40 private static Category log = Category.getInstance(WorkflowInvoker.class); 41 42 public static final String ROLES = "roles"; 43 public static final String USER_ID = "user-id"; 44 public static final String MACHINE = "machine"; 45 public static final String EVENT = "event"; 46 47 public static final String PREFIX = "workflow"; 48 49 public static final String EVENT_REQUEST_PARAMETER = "workflow.event"; 50 public static final String LENYA_EVENT_REQUEST_PARAMETER = "lenya.event"; 51 52 63 public static NamespaceMap extractParameters( 64 String eventName, 65 Identity identity, 66 Role[] roles) { 67 NamespaceMap parameters = new NamespaceMap(PREFIX); 68 log.debug("Extractign workflow invoker parameters."); 69 log.debug(" Event: [" + eventName + "]"); 70 parameters.put(EVENT, eventName); 71 setRoles(parameters, roles); 72 setIdentity(parameters, identity); 73 return parameters; 74 } 75 76 82 public WorkflowInvoker(Map parameters) { 83 super(parameters); 84 } 85 86 91 protected String [] getRoleIDs() { 92 String rolesString = get(ROLES); 93 String [] roleIDs = rolesString.split(","); 94 return roleIDs; 95 } 96 97 105 public static void setRoles(NamespaceMap parameters, Role[] roles) { 106 107 String roleString = ""; 108 for (int i = 0; i < roles.length; i++) { 109 if (i > 0) { 110 roleString += ","; 111 } 112 roleString += roles[i].getId(); 113 } 114 parameters.put(ROLES, roleString); 115 } 116 117 125 public static void setIdentity(NamespaceMap parameters, Identity identity) { 126 127 String userId = ""; 128 User user = identity.getUser(); 129 if (user != null) { 130 userId = user.getId(); 131 } 132 parameters.put(USER_ID, userId); 133 134 String machineIp = ""; 135 Machine machine = identity.getMachine(); 136 if (machine != null) { 137 machineIp = machine.getIp(); 138 } 139 parameters.put(MACHINE, machineIp); 140 } 141 142 147 public String getEventName() { 148 return get(EVENT); 149 } 150 151 156 public String getUserId() { 157 return get(USER_ID); 158 } 159 160 165 public String getMachineIp() { 166 return get(MACHINE); 167 } 168 169 private Document document; 170 private boolean doTransition = false; 171 172 182 public void setup(Publication publication, String webappUrl) throws ExecutionException { 183 String eventName = getEventName(); 184 if (eventName == null) { 185 log.debug("No workflow event."); 186 } else { 187 log.debug("Workflow event: [" + eventName + "]"); 188 WorkflowFactory factory = WorkflowFactory.newInstance(); 190 try { 191 document = publication.getDocumentBuilder().buildDocument(publication, webappUrl); 192 } catch (DocumentBuildException e) { 193 throw new ExecutionException(e); 194 } 195 doTransition = factory.hasWorkflow(document); 196 } 197 } 198 199 205 public void invokeTransition() throws ExecutionException { 206 if (doTransition) { 207 208 try { 209 WorkflowFactory factory = WorkflowFactory.newInstance(); 210 SynchronizedWorkflowInstances instance = 211 factory.buildSynchronizedInstance(document); 212 Situation situation = 213 factory.buildSituation(getRoleIDs(), getUserId(), getMachineIp()); 214 215 Event event = null; 216 Event[] events = instance.getExecutableEvents(situation); 217 218 log.debug("Resolved executable events."); 219 220 for (int i = 0; i < events.length; i++) { 221 if (events[i].getName().equals(getEventName())) { 222 event = events[i]; 223 } 224 } 225 226 assert event != null; 227 228 log.debug("Invoking transition."); 229 instance.invoke(situation, event); 230 log.debug("Invoking transition completed."); 231 232 } catch (Exception e) { 233 throw new ExecutionException(e); 234 } 235 } 236 237 } 238 239 242 public String getPrefix() { 243 return PREFIX; 244 } 245 246 249 protected String [] getRequiredKeys() { 250 String [] keys = { 251 }; 252 return keys; 253 } 254 255 } 256 | Popular Tags |