1 25 package org.ofbiz.workflow; 26 27 import java.io.IOException ; 28 import java.sql.Timestamp ; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.Date ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.Map ; 36 37 import javax.xml.parsers.ParserConfigurationException ; 38 39 import org.ofbiz.base.util.Debug; 40 import org.ofbiz.base.util.GeneralException; 41 import org.ofbiz.base.util.ObjectType; 42 import org.ofbiz.base.util.UtilMisc; 43 import org.ofbiz.entity.GenericDelegator; 44 import org.ofbiz.entity.GenericEntityException; 45 import org.ofbiz.entity.GenericValue; 46 import org.ofbiz.entity.serialize.SerializeException; 47 import org.ofbiz.entity.serialize.XmlSerializer; 48 import org.ofbiz.service.DispatchContext; 49 import org.ofbiz.service.GenericServiceException; 50 import org.ofbiz.service.ModelService; 51 import org.xml.sax.SAXException ; 52 53 60 public class WfApplicationServices { 61 62 66 public static final String module = WfApplicationServices.class.getName(); 67 68 80 public static Map activateApplication(DispatchContext ctx, Map context) { 81 final String workEffortId = (String ) context.get("workEffortId"); 82 final Map result = new HashMap (); 83 84 try { 85 final GenericValue weAssigment = getWorkEffortPartyAssigment(ctx.getDelegator(), workEffortId); 86 87 final String partyId = (String ) weAssigment.get("partyId"); 88 final String roleTypeId = (String ) weAssigment.get("roleTypeId"); 89 final Timestamp fromDate = (Timestamp ) weAssigment.get("fromDate"); 90 result.put("applicationId", insertAppSandbox(ctx.getDelegator(), workEffortId, partyId, 91 roleTypeId, fromDate, context)); 92 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 93 } catch (GenericServiceException we) { 94 we.printStackTrace(); 95 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 96 result.put(ModelService.ERROR_MESSAGE, we.getMessage()); 97 } 98 99 return result; 100 } 101 102 public static Map getApplicationContext(DispatchContext ctx, Map context) throws GenericServiceException { 103 final GenericDelegator delegator = ctx.getDelegator(); 104 final Map result = new HashMap (); 105 try { 106 result.put("applicationContext", 107 getRunTimeContext(delegator, getRuntimeData(delegator, (String ) context.get("applicationId")))); 108 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 109 } catch (GenericServiceException we) { 110 we.printStackTrace(); 111 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 112 result.put(ModelService.ERROR_MESSAGE, we.getMessage()); 113 } 114 return result; 115 } 116 117 public static Map completeApplication(DispatchContext ctx, Map context) throws GenericServiceException { 118 final GenericDelegator delegator = ctx.getDelegator(); 119 final String applicationId = (String ) context.get("applicationId"); 120 final Map result = new HashMap (); 121 122 GenericValue application = getApplicationSandbox(delegator, applicationId); 123 GenericValue runTimeData = getRuntimeData(delegator, applicationId); 124 Map runTimeContext = getRunTimeContext(delegator, runTimeData); 125 Map contextSignature = new HashMap (); 126 Map resultSignature = new HashMap (); 127 Map resultContext = new HashMap (); 128 Map runContext = new HashMap (context); 129 130 try { 131 getApplicationSignatures(delegator, application, contextSignature, resultSignature); 133 for (Iterator names = resultSignature.keySet().iterator(); names.hasNext();) { 134 String name = (String ) names.next(); 135 Object value = null; 136 if (runTimeContext.containsKey(name) 137 && contextSignature.containsKey(name) 138 && resultSignature.containsKey(name)) 139 value = runTimeContext.get(name); 140 if (((Map ) context.get("result")).containsKey(name)) 141 value = ((Map ) context.get("result")).get(name); 142 if (value != null) 143 resultContext.put(name, 144 ObjectType.simpleTypeConvert(value, (String ) resultSignature.get(name), null, null)); 145 } 146 runTimeContext.putAll(resultContext); 147 if (Debug.verboseOn()) { 149 Debug.logVerbose("Completing Application: " + applicationId, module); 150 Debug.logVerbose(" Result Signature: " + resultSignature.toString(), module); 151 Debug.logVerbose(" Result Values: " + resultContext.toString(), module); 152 153 } 154 155 setRunTimeContext(runTimeData, runTimeContext); 156 158 runContext.remove("applicationId"); 159 160 final String workEffortId = (String ) runTimeContext.get("workEffortId"); 161 final Timestamp fromDate = (Timestamp ) application.get("fromDate"); 162 final String partyId = (String ) application.get("partyId"); 163 final String roleTypeId = (String ) application.get("roleTypeId"); 164 165 runContext.put("workEffortId", workEffortId); 166 runContext.put("fromDate", fromDate); 167 runContext.put("partyId", partyId); 168 runContext.put("roleTypeId", roleTypeId); 169 runContext.put("result", resultContext); 170 171 result.putAll(ctx.getDispatcher().runSync("wfCompleteAssignment", runContext)); 172 } catch (GenericEntityException we) { 173 we.printStackTrace(); 174 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 175 result.put(ModelService.ERROR_MESSAGE, we.getMessage()); 176 } catch (GenericServiceException we) { 177 we.printStackTrace(); 178 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 179 result.put(ModelService.ERROR_MESSAGE, we.getMessage()); 180 } catch (GeneralException ge) { 181 ge.printStackTrace(); 182 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 183 result.put(ModelService.ERROR_MESSAGE, ge.getMessage()); 184 } 185 return result; 186 } 187 188 private static String insertAppSandbox(GenericDelegator delegator, String workEffortId, String partyId, 189 String roleTypeId, Timestamp fromDate, Map context) throws GenericServiceException { 190 String dataId = null; 191 String infoId = null; 192 String applicationId = new String (new Long ((new Date ().getTime())).toString()); 193 194 try { 195 dataId = delegator.getNextSeqId("RuntimeData").toString(); 196 GenericValue runtimeData = delegator.makeValue("RuntimeData", UtilMisc.toMap("runtimeDataId", dataId)); 197 runtimeData.set("runtimeInfo", XmlSerializer.serialize(context)); 198 delegator.create(runtimeData); 199 } catch (GenericEntityException ee) { 200 throw new GenericServiceException(ee.getMessage(), ee); 201 } catch (SerializeException se) { 202 throw new GenericServiceException(se.getMessage(), se); 203 } catch (IOException ioe) { 204 throw new GenericServiceException(ioe.getMessage(), ioe); 205 } 206 Map aFields = UtilMisc.toMap("applicationId", applicationId, "workEffortId", workEffortId, 207 "partyId", partyId, "roleTypeId", roleTypeId, "fromDate", fromDate, "runtimeDataId", dataId); 208 209 GenericValue appV = null; 210 try { 211 appV = delegator.makeValue("ApplicationSandbox", aFields); 212 delegator.create(appV); 213 } catch (GenericEntityException e) { 214 throw new GenericServiceException(e.getMessage(), e); 215 } 216 return applicationId; 217 } 218 219 private static GenericValue getApplicationSandbox(GenericDelegator delegator, String applicationId) 220 throws GenericServiceException { 221 try { 222 GenericValue application = 223 delegator.findByPrimaryKey("ApplicationSandbox", UtilMisc.toMap("applicationId", applicationId)); 224 return application; 225 } catch (GenericEntityException ee) { 226 throw new GenericServiceException(ee.getMessage(), ee); 227 } 228 } 229 230 private static Map getRunTimeContext(GenericDelegator delegator, GenericValue runTimeData) 231 throws GenericServiceException { 232 try { 233 return (Map ) XmlSerializer.deserialize((String ) runTimeData.get("runtimeInfo"), delegator); 234 } catch (SerializeException se) { 235 throw new GenericServiceException(se.getMessage(), se); 236 } catch (ParserConfigurationException pe) { 237 throw new GenericServiceException(pe.getMessage(), pe); 238 } catch (SAXException se) { 239 throw new GenericServiceException(se.getMessage(), se); 240 } catch (IOException ioe) { 241 throw new GenericServiceException(ioe.getMessage(), ioe); 242 } 243 } 244 245 private static void setRunTimeContext(GenericValue runTimeData, Map context) throws GenericServiceException { 246 try { 247 runTimeData.set("runtimeInfo", XmlSerializer.serialize(context)); 248 runTimeData.store(); 249 } catch (GenericEntityException ee) { 250 throw new GenericServiceException(ee.getMessage(), ee); 251 } catch (SerializeException se) { 252 throw new GenericServiceException(se.getMessage(), se); 253 } catch (IOException ioe) { 254 throw new GenericServiceException(ioe.getMessage(), ioe); 255 } 256 } 257 258 private static GenericValue getRuntimeData(GenericDelegator delegator, String applicationId) 259 throws GenericServiceException { 260 try { 261 GenericValue application = 262 delegator.findByPrimaryKey("ApplicationSandbox", UtilMisc.toMap("applicationId", applicationId)); 263 return application.getRelatedOne("RuntimeData"); 264 } catch (GenericEntityException ee) { 265 throw new GenericServiceException(ee.getMessage(), ee); 266 } 267 } 268 269 private static void getApplicationSignatures(GenericDelegator delegator, GenericValue application, 270 Map contextSignature, Map resultSignature) throws GenericEntityException { 271 Map expresions = null; 272 final GenericValue workEffort = 274 delegator.findByPrimaryKey("WorkEffort", UtilMisc.toMap("workEffortId", application.get("workEffortId"))); 275 String packageId = (String ) workEffort.get("workflowPackageId"); 276 String packageVersion = (String ) workEffort.get("workflowPackageVersion"); 277 String processId = (String ) workEffort.get("workflowProcessId"); 278 String processVersion = (String ) workEffort.get("workflowProcessVersion"); 279 String activityId = (String ) workEffort.get("workflowActivityId"); 280 281 expresions = new HashMap (); 282 expresions.putAll(UtilMisc.toMap("packageId", packageId)); 283 expresions.putAll(UtilMisc.toMap("packageVersion", packageVersion)); 284 expresions.putAll(UtilMisc.toMap("processId", processId)); 285 expresions.putAll(UtilMisc.toMap("processVersion", processVersion)); 286 expresions.putAll(UtilMisc.toMap("activityId", activityId)); 287 288 final Collection wfActivityTools = delegator.findByAnd("WorkflowActivityTool", expresions); 289 final GenericValue wfActivityTool = (GenericValue) wfActivityTools.toArray()[0]; 290 291 packageId = (String ) wfActivityTool.get("packageId"); 292 packageVersion = (String ) wfActivityTool.get("packageVersion"); 293 processId = (String ) wfActivityTool.get("processId"); 294 processVersion = (String ) wfActivityTool.get("processVersion"); 295 final String applicationId = (String ) wfActivityTool.get("toolId"); 296 297 expresions = new HashMap (); 298 expresions.putAll(UtilMisc.toMap("packageId", packageId)); 299 expresions.putAll(UtilMisc.toMap("packageVersion", packageVersion)); 300 expresions.putAll(UtilMisc.toMap("processId", processId)); 301 expresions.putAll(UtilMisc.toMap("processVersion", processVersion)); 302 expresions.putAll(UtilMisc.toMap("applicationId", applicationId)); 303 304 final Collection params = delegator.findByAnd("WorkflowFormalParam", expresions); 305 306 Iterator i = params.iterator(); 307 while (i.hasNext()) { 308 GenericValue param = (GenericValue) i.next(); 309 String name = param.getString("formalParamId"); 310 String mode = param.getString("modeEnumId"); 311 String type = param.getString("dataTypeEnumId"); 312 if (mode.equals("WPM_IN") || mode.equals("WPM_INOUT")) 313 contextSignature.put(name, WfUtil.getJavaType(type)); 314 else if (mode.equals("WPM_OUT") || mode.equals("WPM_INOUT")) 315 resultSignature.put(name, WfUtil.getJavaType(type)); 316 } 317 } 318 319 private static GenericValue getWorkEffortPartyAssigment(GenericDelegator delegator, String workEffortId) 320 throws GenericServiceException { 321 Map expresions = new HashMap (); 322 expresions.putAll(UtilMisc.toMap("workEffortId", workEffortId)); 323 expresions.putAll(UtilMisc.toMap("statusId", "CAL_ACCEPTED")); 324 List orderBy = new ArrayList (); 325 orderBy.add("-fromDate"); 326 327 try { 328 final Collection assigments = delegator.findByAnd("WorkEffortPartyAssignment", expresions, orderBy); 329 GenericValue assigment; 330 if (assigments.isEmpty()) { 331 Debug.logError("No accepted activities found for the workEffortId=" + workEffortId, module); 332 throw new GenericServiceException("Can not find WorkEffortPartyAssignment for the Workflow service. WorkEffortId=" + workEffortId); 333 } 334 if (assigments.size() != 1) 335 Debug.logWarning("More than one accepted activities found for the workEffortId=" + workEffortId, module); 336 return (GenericValue) assigments.iterator().next(); 337 } catch (GenericEntityException ee) { 338 throw new GenericServiceException(ee.getMessage(), ee); 339 } 340 } 341 } 342 | Popular Tags |