1 24 package org.ofbiz.widget.menu; 25 26 import java.text.MessageFormat ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import java.util.Locale ; 32 import java.util.Map ; 33 34 import org.ofbiz.base.util.BshUtil; 35 import org.ofbiz.base.util.Debug; 36 import org.ofbiz.base.util.GeneralException; 37 import org.ofbiz.base.util.ObjectType; 38 import org.ofbiz.base.util.UtilFormatOut; 39 import org.ofbiz.base.util.UtilProperties; 40 import org.ofbiz.base.util.UtilValidate; 41 import org.ofbiz.base.util.UtilXml; 42 import org.ofbiz.base.util.collections.FlexibleMapAccessor; 43 import org.ofbiz.base.util.string.FlexibleStringExpander; 44 import org.ofbiz.entity.finder.ByAndFinder; 45 import org.ofbiz.entity.finder.ByConditionFinder; 46 import org.ofbiz.entity.finder.PrimaryKeyFinder; 47 import org.ofbiz.service.GenericServiceException; 48 import org.ofbiz.service.ModelService; 49 50 import org.w3c.dom.Element ; 51 import javax.servlet.*; 52 import javax.servlet.http.*; 53 54 55 62 public abstract class ModelMenuAction { 63 public static final String module = ModelMenuAction.class.getName(); 64 65 protected ModelMenu modelMenu; 66 protected ModelMenuItem modelMenuItem; 67 68 public ModelMenuAction(ModelMenu modelMenu, Element actionElement) { 69 this.modelMenu = modelMenu; 70 if (Debug.verboseOn()) Debug.logVerbose("Reading Screen action with name: " + actionElement.getNodeName(), module); 71 } 72 73 public ModelMenuAction(ModelMenuItem modelMenuItem, Element actionElement) { 74 this.modelMenuItem = modelMenuItem; 75 this.modelMenu = modelMenuItem.getModelMenu(); 76 if (Debug.verboseOn()) Debug.logVerbose("Reading Screen action with name: " + actionElement.getNodeName(), module); 77 } 78 79 public abstract void runAction(Map context); 80 81 public static List readSubActions(ModelMenuItem modelMenuItem, Element parentElement) { 82 return readSubActions(modelMenuItem.getModelMenu(), parentElement); 83 } 84 85 public static List readSubActions(ModelMenu modelMenu, Element parentElement) { 86 List actions = new LinkedList (); 87 88 List actionElementList = UtilXml.childElementList(parentElement); 89 Iterator actionElementIter = actionElementList.iterator(); 90 while (actionElementIter.hasNext()) { 91 Element actionElement = (Element ) actionElementIter.next(); 92 if ("set".equals(actionElement.getNodeName())) { 93 actions.add(new SetField(modelMenu, actionElement)); 94 } else if ("property-map".equals(actionElement.getNodeName())) { 95 actions.add(new PropertyMap(modelMenu, actionElement)); 96 } else if ("property-to-field".equals(actionElement.getNodeName())) { 97 actions.add(new PropertyToField(modelMenu, actionElement)); 98 } else if ("script".equals(actionElement.getNodeName())) { 99 actions.add(new Script(modelMenu, actionElement)); 100 } else if ("service".equals(actionElement.getNodeName())) { 101 actions.add(new Service(modelMenu, actionElement)); 102 } else if ("entity-one".equals(actionElement.getNodeName())) { 103 actions.add(new EntityOne(modelMenu, actionElement)); 104 } else if ("entity-and".equals(actionElement.getNodeName())) { 105 actions.add(new EntityAnd(modelMenu, actionElement)); 106 } else if ("entity-condition".equals(actionElement.getNodeName())) { 107 actions.add(new EntityCondition(modelMenu, actionElement)); 108 } else { 109 throw new IllegalArgumentException ("Action element not supported with name: " + actionElement.getNodeName()); 110 } 111 } 112 113 return actions; 114 } 115 116 public static void runSubActions(List actions, Map context) { 117 if (actions == null) return; 118 119 Iterator actionIter = actions.iterator(); 120 while (actionIter.hasNext()) { 121 ModelMenuAction action = (ModelMenuAction) actionIter.next(); 122 if (Debug.verboseOn()) Debug.logVerbose("Running screen action " + action.getClass().getName(), module); 123 action.runAction(context); 124 } 125 } 126 127 public static class SetField extends ModelMenuAction { 128 protected FlexibleMapAccessor field; 129 protected FlexibleMapAccessor fromField; 130 protected FlexibleStringExpander valueExdr; 131 protected FlexibleStringExpander defaultExdr; 132 protected FlexibleStringExpander globalExdr; 133 protected String type; 134 protected String toScope; 135 protected String fromScope; 136 137 public SetField(ModelMenu modelMenu, Element setElement) { 138 super (modelMenu, setElement); 139 this.field = new FlexibleMapAccessor(setElement.getAttribute("field")); 140 this.fromField = UtilValidate.isNotEmpty(setElement.getAttribute("from-field")) ? new FlexibleMapAccessor(setElement.getAttribute("from-field")) : null; 141 this.valueExdr = UtilValidate.isNotEmpty(setElement.getAttribute("value")) ? new FlexibleStringExpander(setElement.getAttribute("value")) : null; 142 this.defaultExdr = UtilValidate.isNotEmpty(setElement.getAttribute("default-value")) ? new FlexibleStringExpander(setElement.getAttribute("default-value")) : null; 143 this.globalExdr = new FlexibleStringExpander(setElement.getAttribute("global")); 144 this.type = setElement.getAttribute("type"); 145 this.toScope = setElement.getAttribute("to-scope"); 146 this.fromScope = setElement.getAttribute("from-scope"); 147 if (this.fromField != null && this.valueExdr != null) { 148 throw new IllegalArgumentException ("Cannot specify a from-field [" + setElement.getAttribute("from-field") + "] and a value [" + setElement.getAttribute("value") + "] on the set action in a screen widget"); 149 } 150 } 151 152 public void runAction(Map context) { 153 String globalStr = this.globalExdr.expandString(context); 154 boolean global = "true".equals(globalStr); 156 157 Object newValue = null; 158 if (this.fromScope != null && this.fromScope.equals("user")) { 159 if (this.fromField != null) { 160 String originalName = this.fromField.getOriginalName(); 161 String currentWidgetTrail = (String )context.get("_WIDGETTRAIL_"); 162 String newKey = currentWidgetTrail + "|" + originalName; 163 HttpSession session = (HttpSession)context.get("session"); 164 newValue = session.getAttribute(newKey); 165 if (Debug.verboseOn()) Debug.logVerbose("In user getting value for field from [" + this.fromField.getOriginalName() + "]: " + newValue, module); 166 } else if (this.valueExdr != null) { 167 newValue = this.valueExdr.expandString(context); 168 } 169 170 } else if (this.fromScope != null && this.fromScope.equals("application")) { 171 if (this.fromField != null) { 172 String originalName = this.fromField.getOriginalName(); 173 String currentWidgetTrail = (String )context.get("_WIDGETTRAIL_"); 174 String newKey = currentWidgetTrail + "|" + originalName; 175 ServletContext servletContext = (ServletContext)context.get("application"); 176 newValue = servletContext.getAttribute(newKey); 177 if (Debug.verboseOn()) Debug.logVerbose("In application getting value for field from [" + this.fromField.getOriginalName() + "]: " + newValue, module); 178 } else if (this.valueExdr != null) { 179 newValue = this.valueExdr.expandString(context); 180 } 181 182 } else { 183 if (this.fromField != null) { 184 newValue = this.fromField.get(context); 185 if (Debug.verboseOn()) Debug.logVerbose("In screen getting value for field from [" + this.fromField.getOriginalName() + "]: " + newValue, module); 186 } else if (this.valueExdr != null) { 187 newValue = this.valueExdr.expandString(context); 188 } 189 } 190 191 if (this.defaultExdr != null) { 193 if (ObjectType.isEmpty(newValue)) { 194 newValue = this.defaultExdr.expandString(context); 195 } 196 } 197 198 if (UtilValidate.isNotEmpty(this.type)) { 199 try { 200 newValue = ObjectType.simpleTypeConvert(newValue, this.type, null, null); 201 } catch (GeneralException e) { 202 String errMsg = "Could not convert field value for the field: [" + this.field.getOriginalName() + "] to the [" + this.type + "] type for the value [" + newValue + "]: " + e.toString(); 203 Debug.logError(e, errMsg, module); 204 throw new IllegalArgumentException (errMsg); 205 } 206 207 } 208 if (this.toScope != null && this.toScope.equals("user")) { 209 String originalName = this.field.getOriginalName(); 210 String currentWidgetTrail = (String )context.get("_WIDGETTRAIL_"); 211 String newKey = currentWidgetTrail + "|" + originalName; 212 HttpSession session = (HttpSession)context.get("session"); 213 session.setAttribute(newKey, newValue); 214 if (Debug.verboseOn()) Debug.logVerbose("In user setting value for field from [" + this.field.getOriginalName() + "]: " + newValue, module); 215 216 } else if (this.toScope != null && this.toScope.equals("application")) { 217 String originalName = this.field.getOriginalName(); 218 String currentWidgetTrail = (String )context.get("_WIDGETTRAIL_"); 219 String newKey = currentWidgetTrail + "|" + originalName; 220 ServletContext servletContext = (ServletContext)context.get("application"); 221 servletContext.setAttribute(newKey, newValue); 222 if (Debug.verboseOn()) Debug.logVerbose("In application setting value for field from [" + this.field.getOriginalName() + "]: " + newValue, module); 223 224 } else { 225 if (Debug.verboseOn()) Debug.logVerbose("In screen setting field [" + this.field.getOriginalName() + "] to value: " + newValue, module); 226 this.field.put(context, newValue); 227 } 228 229 if (global) { 230 Map globalCtx = (Map ) context.get("globalContext"); 231 if (globalCtx != null) { 232 this.field.put(globalCtx, newValue); 233 } 234 } 235 236 Map page = (Map ) context.get("page"); 238 if (page != null) { 239 this.field.put(page, newValue); 240 } 241 } 242 } 243 244 public static class PropertyMap extends ModelMenuAction { 245 protected FlexibleStringExpander resourceExdr; 246 protected FlexibleMapAccessor mapNameAcsr; 247 protected FlexibleStringExpander globalExdr; 248 249 public PropertyMap(ModelMenu modelMenu, Element setElement) { 250 super (modelMenu, setElement); 251 this.resourceExdr = new FlexibleStringExpander(setElement.getAttribute("resource")); 252 this.mapNameAcsr = new FlexibleMapAccessor(setElement.getAttribute("map-name")); 253 this.globalExdr = new FlexibleStringExpander(setElement.getAttribute("global")); 254 } 255 256 public void runAction(Map context) { 257 String globalStr = this.globalExdr.expandString(context); 258 boolean global = "true".equals(globalStr); 260 261 Locale locale = (Locale ) context.get("locale"); 262 String resource = this.resourceExdr.expandString(context, locale); 263 Map propertyMap = UtilProperties.getResourceBundleMap(resource, locale); 264 this.mapNameAcsr.put(context, propertyMap); 265 266 if (global) { 267 Map globalCtx = (Map ) context.get("globalContext"); 268 if (globalCtx != null) { 269 this.mapNameAcsr.put(globalCtx, propertyMap); 270 } 271 } 272 } 273 } 274 275 public static class PropertyToField extends ModelMenuAction { 276 277 protected FlexibleStringExpander resourceExdr; 278 protected FlexibleStringExpander propertyExdr; 279 protected FlexibleMapAccessor fieldAcsr; 280 protected FlexibleStringExpander defaultExdr; 281 protected boolean noLocale; 282 protected FlexibleMapAccessor argListAcsr; 283 protected FlexibleStringExpander globalExdr; 284 285 public PropertyToField(ModelMenu modelMenu, Element setElement) { 286 super (modelMenu, setElement); 287 this.resourceExdr = new FlexibleStringExpander(setElement.getAttribute("resource")); 288 this.propertyExdr = new FlexibleStringExpander(setElement.getAttribute("property")); 289 this.fieldAcsr = new FlexibleMapAccessor(setElement.getAttribute("field")); 290 this.defaultExdr = new FlexibleStringExpander(setElement.getAttribute("default")); 291 noLocale = "true".equals(setElement.getAttribute("no-locale")); 292 this.argListAcsr = new FlexibleMapAccessor(setElement.getAttribute("arg-list-name")); 293 this.globalExdr = new FlexibleStringExpander(setElement.getAttribute("global")); 294 } 295 296 public void runAction(Map context) { 297 String globalStr = this.globalExdr.expandString(context); 298 boolean global = "true".equals(globalStr); 300 301 Locale locale = (Locale ) context.get("locale"); 302 String resource = this.resourceExdr.expandString(context, locale); 303 String property = this.propertyExdr.expandString(context, locale); 304 305 String value = null; 306 if (noLocale) { 307 value = UtilProperties.getPropertyValue(resource, property); 308 } else { 309 value = UtilProperties.getMessage(resource, property, locale); 310 } 311 if (value == null || value.length() == 0) { 312 value = this.defaultExdr.expandString(context); 313 } 314 315 value = FlexibleStringExpander.expandString(value, context); 319 320 if (!argListAcsr.isEmpty()) { 321 List argList = (List ) argListAcsr.get(context); 322 if (argList != null && argList.size() > 0) { 323 value = MessageFormat.format(value, argList.toArray()); 324 } 325 } 326 327 fieldAcsr.put(context, value); 328 } 329 } 330 331 public static class Script extends ModelMenuAction { 332 protected String location; 333 334 public Script(ModelMenu modelMenu, Element scriptElement) { 335 super (modelMenu, scriptElement); 336 this.location = scriptElement.getAttribute("location"); 337 } 338 339 public void runAction(Map context) { 340 if (location.endsWith(".bsh")) { 341 try { 342 BshUtil.runBshAtLocation(location, context); 343 } catch (GeneralException e) { 344 String errMsg = "Error running BSH script at location [" + location + "]: " + e.toString(); 345 Debug.logError(e, errMsg, module); 346 throw new IllegalArgumentException (errMsg); 347 } 348 } else { 349 throw new IllegalArgumentException ("For screen script actions the script type is not yet support for location:" + location); 350 } 351 } 352 } 353 354 public static class Service extends ModelMenuAction { 355 protected FlexibleStringExpander serviceNameExdr; 356 protected FlexibleMapAccessor resultMapNameAcsr; 357 protected FlexibleStringExpander autoFieldMapExdr; 358 protected Map fieldMap; 359 360 public Service(ModelMenu modelMenu, Element serviceElement) { 361 super (modelMenu, serviceElement); 362 this.serviceNameExdr = new FlexibleStringExpander(serviceElement.getAttribute("service-name")); 363 this.resultMapNameAcsr = UtilValidate.isNotEmpty(serviceElement.getAttribute("result-map-name")) ? new FlexibleMapAccessor(serviceElement.getAttribute("result-map-name")) : null; 364 this.autoFieldMapExdr = new FlexibleStringExpander(serviceElement.getAttribute("auto-field-map")); 365 366 List fieldMapElementList = UtilXml.childElementList(serviceElement, "field-map"); 367 if (fieldMapElementList.size() > 0) { 368 this.fieldMap = new HashMap (); 369 Iterator fieldMapElementIter = fieldMapElementList.iterator(); 370 while (fieldMapElementIter.hasNext()) { 371 Element fieldMapElement = (Element ) fieldMapElementIter.next(); 372 this.fieldMap.put( 374 new FlexibleMapAccessor(UtilFormatOut.checkEmpty(fieldMapElement.getAttribute("field-name"), fieldMapElement.getAttribute("env-name"))), 375 new FlexibleMapAccessor(fieldMapElement.getAttribute("env-name"))); 376 } 377 } 378 } 379 380 public void runAction(Map context) { 381 String serviceNameExpanded = this.serviceNameExdr.expandString(context); 382 if (UtilValidate.isEmpty(serviceNameExpanded)) { 383 throw new IllegalArgumentException ("Service name was empty, expanded from: " + this.serviceNameExdr.getOriginal()); 384 } 385 386 String autoFieldMapString = this.autoFieldMapExdr.expandString(context); 387 boolean autoFieldMapBool = !"false".equals(autoFieldMapString); 388 389 try { 390 Map serviceContext = null; 391 if (autoFieldMapBool) { 392 serviceContext = this.modelMenu.getDispacher().getDispatchContext().makeValidContext(serviceNameExpanded, ModelService.IN_PARAM, context); 393 } else { 394 serviceContext = new HashMap (); 395 } 396 397 if (this.fieldMap != null) { 398 Iterator fieldMapEntryIter = this.fieldMap.entrySet().iterator(); 399 while (fieldMapEntryIter.hasNext()) { 400 Map.Entry entry = (Map.Entry ) fieldMapEntryIter.next(); 401 FlexibleMapAccessor serviceContextFieldAcsr = (FlexibleMapAccessor) entry.getKey(); 402 FlexibleMapAccessor contextEnvAcsr = (FlexibleMapAccessor) entry.getValue(); 403 serviceContextFieldAcsr.put(serviceContext, contextEnvAcsr.get(context)); 404 } 405 } 406 407 Map result = this.modelMenu.getDispacher().runSync(serviceNameExpanded, serviceContext); 408 409 if (this.resultMapNameAcsr != null) { 410 this.resultMapNameAcsr.put(context, result); 411 } else { 412 context.putAll(result); 413 } 414 } catch (GenericServiceException e) { 415 String errMsg = "Error calling service with name " + serviceNameExpanded + ": " + e.toString(); 416 Debug.logError(e, errMsg, module); 417 throw new IllegalArgumentException (errMsg); 418 } 419 } 420 } 421 422 public static class EntityOne extends ModelMenuAction { 423 protected PrimaryKeyFinder finder; 424 425 public EntityOne(ModelMenu modelMenu, Element entityOneElement) { 426 super (modelMenu, entityOneElement); 427 finder = new PrimaryKeyFinder(entityOneElement); 428 } 429 430 public void runAction(Map context) { 431 try { 432 finder.runFind(context, this.modelMenu.getDelegator()); 433 } catch (GeneralException e) { 434 String errMsg = "Error doing entity query by condition: " + e.toString(); 435 Debug.logError(e, errMsg, module); 436 throw new IllegalArgumentException (errMsg); 437 } 438 } 439 } 440 441 public static class EntityAnd extends ModelMenuAction { 442 protected ByAndFinder finder; 443 444 public EntityAnd(ModelMenu modelMenu, Element entityAndElement) { 445 super (modelMenu, entityAndElement); 446 finder = new ByAndFinder(entityAndElement); 447 } 448 449 public void runAction(Map context) { 450 try { 451 finder.runFind(context, this.modelMenu.getDelegator()); 452 } catch (GeneralException e) { 453 String errMsg = "Error doing entity query by condition: " + e.toString(); 454 Debug.logError(e, errMsg, module); 455 throw new IllegalArgumentException (errMsg); 456 } 457 } 458 } 459 460 public static class EntityCondition extends ModelMenuAction { 461 ByConditionFinder finder; 462 463 public EntityCondition(ModelMenu modelMenu, Element entityConditionElement) { 464 super (modelMenu, entityConditionElement); 465 finder = new ByConditionFinder(entityConditionElement); 466 } 467 468 public void runAction(Map context) { 469 try { 470 finder.runFind(context, this.modelMenu.getDelegator()); 471 } catch (GeneralException e) { 472 String errMsg = "Error doing entity query by condition: " + e.toString(); 473 Debug.logError(e, errMsg, module); 474 throw new IllegalArgumentException (errMsg); 475 } 476 } 477 } 478 } 479 480 | Popular Tags |