1 25 package org.ofbiz.workeffort.workeffort; 26 27 import java.io.IOException ; 28 import java.util.Collection ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import javax.servlet.jsp.PageContext ; 35 36 import org.ofbiz.base.util.BshUtil; 37 import org.ofbiz.base.util.Debug; 38 import org.ofbiz.base.util.StringUtil; 39 import org.ofbiz.base.util.UtilMisc; 40 import org.ofbiz.entity.GenericDelegator; 41 import org.ofbiz.entity.GenericEntityException; 42 import org.ofbiz.entity.GenericValue; 43 import org.ofbiz.entity.serialize.SerializeException; 44 import org.ofbiz.entity.serialize.XmlSerializer; 45 import org.ofbiz.workflow.WfException; 46 import org.ofbiz.workflow.WfUtil; 47 48 import bsh.EvalError; 49 50 57 public final class WorkEffortApplication { 58 59 public static final String module = WorkEffortApplication.class.getName(); 60 61 67 public static boolean isApplication(GenericValue workEffort) { 68 69 if (!workEffort.get("workEffortTypeId").equals("ACTIVITY")) 70 return false; 71 try { 73 if (getApplications(workEffort).isEmpty()) 74 return false; 75 } catch (GenericEntityException ee) { 76 Debug.logWarning(ee, module); 77 return false; 78 } 79 80 return true; 81 82 } 83 84 89 public static void isApplication( 90 PageContext pageContext, 91 String workEffortAttrName, 92 String isApplicationAttrName) { 93 94 pageContext.setAttribute( 95 isApplicationAttrName, 96 new Boolean (isApplication((GenericValue) pageContext.getAttribute(workEffortAttrName)))); 97 98 } 99 100 public static String getApplicationId(PageContext pageContext, String partyAssignsAttrName) { 101 Collection partyAssigns = (Collection ) pageContext.getAttribute(partyAssignsAttrName); 102 if (!partyAssigns.isEmpty()) { 103 GenericValue partyAssignAcepted = null; 105 for (Iterator partyAssignIt = partyAssigns.iterator(); partyAssignIt.hasNext();) { 106 GenericValue partyAssign = (GenericValue) partyAssignIt.next(); 107 if (((String ) partyAssign.get("statusId")).equals("CAL_ACCEPTED")) { 108 partyAssignAcepted = partyAssign; 109 break; 110 } 111 } 112 if (partyAssignAcepted != null) 113 try { 114 final Collection applicationSandboxs = partyAssignAcepted.getRelated("ApplicationSandbox"); 115 GenericValue applicationSandbox = (GenericValue) applicationSandboxs.toArray()[0]; 116 return (String ) applicationSandbox.get("applicationId"); 117 } catch (GenericEntityException ee) { 118 Debug.logWarning(ee, module); 119 } 120 } 121 Debug.logWarning("Can't find applicationId", module); 122 return null; 123 } 124 125 135 public static void getApplication(PageContext pageContext, String workEffortAttrName, String applicationAttrName) 136 throws WfException { 137 getApplication(pageContext, workEffortAttrName, applicationAttrName, null, null, null); 138 } 139 140 152 public static void getApplication(PageContext pageContext, String workEffortAttrName, String applicationAttrName, 153 String applicationContextSignatureName, String applicationResultSignatureName, String applicationContextName) 154 throws WfException { 155 GenericValue workEffort = (GenericValue) pageContext.getAttribute(workEffortAttrName); 156 GenericDelegator delegator = (GenericDelegator) pageContext.getRequest().getAttribute("delegator"); 157 try { 158 Collection applications = getApplications(workEffort); 159 160 if (applications.iterator().hasNext()) { 161 GenericValue workflowActivityTool = (GenericValue) applications.iterator().next(); 162 if (applicationAttrName != null && applicationAttrName.length() != 0) 163 pageContext.setAttribute(applicationAttrName, workflowActivityTool); 164 165 Map contextSignature = new HashMap (); 166 Map resultSignature = new HashMap (); 167 getApplicationSignatures(delegator, workflowActivityTool, contextSignature, resultSignature); 168 169 Map context = 170 getApplicationContext( 171 delegator, 172 contextSignature, 173 (String ) workflowActivityTool.get("actualParameters"), 174 (String ) workflowActivityTool.get("extendedAttributes"), 175 getContext(workEffort), 176 (String ) workEffort.get("workEffortId")); 177 178 if (applicationContextSignatureName != null 179 && applicationContextSignatureName.length() != 0 180 && contextSignature != null) 181 pageContext.setAttribute(applicationContextSignatureName, contextSignature); 182 183 if (applicationResultSignatureName != null 184 && applicationResultSignatureName.length() != 0 185 && resultSignature != null) 186 pageContext.setAttribute(applicationResultSignatureName, resultSignature); 187 188 if (applicationContextName != null && applicationContextName.length() != 0) 189 pageContext.setAttribute(applicationContextName, context); 190 } 191 192 } catch (GenericEntityException gex) { 193 throw new WfException( 194 "Can't find Applications in Workeffort [" + workEffort.get("workEffortId") + "]", 195 gex); 196 } 197 198 } 199 200 private static Collection getApplications(GenericValue workEffort) throws GenericEntityException { 201 202 final String packageId = (String ) workEffort.get("workflowPackageId"); 203 final String packageVersion = (String ) workEffort.get("workflowPackageVersion"); 204 final String processId = (String ) workEffort.get("workflowProcessId"); 205 final String processVersion = (String ) workEffort.get("workflowProcessVersion"); 206 final String activityId = (String ) workEffort.get("workflowActivityId"); 207 final GenericDelegator delegator = workEffort.getDelegator(); 208 209 Map expresions = new HashMap (); 210 expresions.putAll(UtilMisc.toMap("packageId", packageId)); 211 expresions.putAll(UtilMisc.toMap("packageVersion", packageVersion)); 212 expresions.putAll(UtilMisc.toMap("processId", processId)); 213 expresions.putAll(UtilMisc.toMap("processVersion", processVersion)); 214 expresions.putAll(UtilMisc.toMap("activityId", activityId)); 215 expresions.putAll(UtilMisc.toMap("toolTypeEnumId", "WTT_APPLICATION")); 216 217 return delegator.findByAnd("WorkflowActivityTool", expresions); 218 219 } 220 221 private static void getApplicationSignatures( 222 GenericDelegator delegator, 223 GenericValue application, 224 Map contextSignature, 225 Map resultSignature) 226 throws GenericEntityException { 227 final String packageId = (String ) application.get("packageId"); 228 final String packageVersion = (String ) application.get("packageVersion"); 229 final String processId = (String ) application.get("processId"); 230 final String processVersion = (String ) application.get("processVersion"); 231 final String applicationId = (String ) application.get("toolId"); 232 233 Map expresions = new HashMap (); 234 expresions.putAll(UtilMisc.toMap("packageId", packageId)); 235 expresions.putAll(UtilMisc.toMap("packageVersion", packageVersion)); 236 expresions.putAll(UtilMisc.toMap("processId", processId)); 237 expresions.putAll(UtilMisc.toMap("processVersion", processVersion)); 238 expresions.putAll(UtilMisc.toMap("applicationId", applicationId)); 239 240 final Collection params = delegator.findByAnd("WorkflowFormalParam", expresions); 241 242 Iterator i = params.iterator(); 243 while (i.hasNext()) { 244 GenericValue param = (GenericValue) i.next(); 245 String name = param.getString("formalParamId"); 246 String mode = param.getString("modeEnumId"); 247 String type = param.getString("dataTypeEnumId"); 248 if (mode.equals("WPM_IN") || mode.equals("WPM_INOUT")) 249 contextSignature.put(name, WfUtil.getJavaType(type)); 250 else if (mode.equals("WPM_OUT") || mode.equals("WPM_INOUT")) 251 resultSignature.put(name, WfUtil.getJavaType(type)); 252 } 253 } 254 255 private static Map getApplicationContext( 256 GenericDelegator delegator, 257 Map contextSignature, 258 String actualParameters, 259 String extendedAttr, 260 Map context, 261 String workEffortId) 262 throws WfException { 263 List params = StringUtil.split(actualParameters, ","); 264 Map actualContext = new HashMap (); 265 266 Map extendedAttributes = StringUtil.strToMap(extendedAttr); 267 if (extendedAttributes != null && extendedAttributes.size() > 0) 268 actualContext.putAll(extendedAttributes); 269 270 GenericValue userLogin = null; 273 if (context.containsKey("runAsUser")) { 274 userLogin = getUserLogin(delegator, (String ) context.get("runAsUser")); 275 actualContext.put("userLogin", userLogin); 276 } else if (context.containsKey("workflowOwnerId")) { 277 userLogin = getUserLogin(delegator, (String ) context.get("workflowOwnerId")); 278 } 279 280 context.put("userLogin", userLogin); 281 context.put("workEffortId", workEffortId); 282 284 Iterator i = params.iterator(); 285 while (i.hasNext()) { 286 287 Object keyExpr = i.next(); 288 String keyExprStr = (String ) keyExpr; 289 if (keyExprStr != null && keyExprStr.trim().toLowerCase().startsWith("expr:")) 290 try { 291 BshUtil.eval(keyExprStr.trim().substring(5).trim(), context); 292 } catch (EvalError e) { 293 throw new WfException("Error evaluating actual parameter: " + keyExprStr, e); 294 } else if (keyExprStr != null && keyExprStr.trim().toLowerCase().startsWith("name:")) { 295 List couple = StringUtil.split(keyExprStr.trim().substring(5).trim(), "="); 296 if (contextSignature.containsKey(((String ) couple.get(0)).trim())) 297 actualContext.put(((String ) couple.get(0)).trim(), context.get(couple.get(1))); 298 } else if (context.containsKey(keyExprStr)) { 299 if (contextSignature.containsKey(keyExprStr)) 300 actualContext.put(keyExprStr, context.get(keyExprStr)); 301 } else if (!actualContext.containsKey(keyExprStr)) 302 throw new WfException("Context does not contain the key: '" + keyExprStr + "'"); 303 } 304 305 return actualContext; 306 307 } 308 309 private static Map getContext(GenericValue dataObject) throws WfException { 310 String contextXML = null; 311 Map context = null; 312 if (dataObject.get("runtimeDataId") == null) 313 return context; 314 try { 315 GenericValue runtimeData = dataObject.getRelatedOne("RuntimeData"); 316 contextXML = runtimeData.getString("runtimeInfo"); 317 } catch (GenericEntityException e) { 318 throw new WfException(e.getMessage(), e); 319 } 320 if (contextXML != null) { 322 try { 323 context = (Map ) XmlSerializer.deserialize(contextXML, dataObject.getDelegator()); 324 } catch (SerializeException e) { 325 throw new WfException(e.getMessage(), e); 326 } catch (IOException e) { 327 throw new WfException(e.getMessage(), e); 328 } catch (Exception e) { 329 throw new WfException(e.getMessage(), e); 330 } 331 } 332 return context; 333 } 334 private static GenericValue getUserLogin(GenericDelegator delegator, String userId) throws WfException { 337 GenericValue userLogin = null; 338 try { 339 userLogin = delegator.findByPrimaryKey("UserLogin", UtilMisc.toMap("userLoginId", userId)); 340 } catch (GenericEntityException e) { 341 throw new WfException(e.getMessage(), e); 342 } 343 return userLogin; 344 } 345 346 } 347 | Popular Tags |