1 24 package org.ofbiz.widget.form; 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 import java.util.regex.PatternSyntaxException ; 34 35 import org.ofbiz.base.util.BshUtil; 36 import org.ofbiz.base.util.Debug; 37 import org.ofbiz.base.util.GeneralException; 38 import org.ofbiz.base.util.ObjectType; 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.EntityFinderUtil; 47 import org.ofbiz.entity.finder.PrimaryKeyFinder; 48 import org.ofbiz.entity.util.EntityListIterator; 49 import org.ofbiz.service.GenericServiceException; 50 import org.ofbiz.service.ModelService; 51 import org.w3c.dom.Element ; 52 53 54 61 public abstract class ModelFormAction { 62 public static final String module = ModelFormAction.class.getName(); 63 64 protected ModelForm modelForm; 65 66 public ModelFormAction(ModelForm modelForm, Element actionElement) { 67 this.modelForm = modelForm; 68 if (Debug.verboseOn()) Debug.logVerbose("Reading Screen action with name: " + actionElement.getNodeName(), module); 69 } 70 71 public abstract void runAction(Map context); 72 73 public static List readSubActions(ModelForm modelForm, Element parentElement) { 74 List actions = new LinkedList (); 75 76 List actionElementList = UtilXml.childElementList(parentElement); 77 Iterator actionElementIter = actionElementList.iterator(); 78 while (actionElementIter.hasNext()) { 79 Element actionElement = (Element ) actionElementIter.next(); 80 if ("set".equals(actionElement.getNodeName())) { 81 actions.add(new SetField(modelForm, actionElement)); 82 } else if ("property-map".equals(actionElement.getNodeName())) { 83 actions.add(new PropertyMap(modelForm, actionElement)); 84 } else if ("property-to-field".equals(actionElement.getNodeName())) { 85 actions.add(new PropertyToField(modelForm, actionElement)); 86 } else if ("script".equals(actionElement.getNodeName())) { 87 actions.add(new Script(modelForm, actionElement)); 88 } else if ("service".equals(actionElement.getNodeName())) { 89 actions.add(new Service(modelForm, actionElement)); 90 } else if ("entity-one".equals(actionElement.getNodeName())) { 91 actions.add(new EntityOne(modelForm, actionElement)); 92 } else if ("entity-and".equals(actionElement.getNodeName())) { 93 actions.add(new EntityAnd(modelForm, actionElement)); 94 } else if ("entity-condition".equals(actionElement.getNodeName())) { 95 actions.add(new EntityCondition(modelForm, actionElement)); 96 } else { 97 throw new IllegalArgumentException ("Action element not supported with name: " + actionElement.getNodeName()); 98 } 99 } 100 101 return actions; 102 } 103 104 public static void runSubActions(List actions, Map context) { 105 if (actions == null) return; 106 107 Iterator actionIter = actions.iterator(); 108 while (actionIter.hasNext()) { 109 ModelFormAction action = (ModelFormAction) actionIter.next(); 110 if (Debug.verboseOn()) Debug.logVerbose("Running screen action " + action.getClass().getName(), module); 111 action.runAction(context); 112 } 113 } 114 115 public static class SetField extends ModelFormAction { 116 protected FlexibleMapAccessor field; 117 protected FlexibleMapAccessor fromField; 118 protected FlexibleStringExpander valueExdr; 119 protected FlexibleStringExpander globalExdr; 120 protected String type; 121 122 public SetField(ModelForm modelForm, Element setElement) { 123 super (modelForm, setElement); 124 this.field = new FlexibleMapAccessor(setElement.getAttribute("field")); 125 this.fromField = UtilValidate.isNotEmpty(setElement.getAttribute("from-field")) ? new FlexibleMapAccessor(setElement.getAttribute("from-field")) : null; 126 this.valueExdr = UtilValidate.isNotEmpty(setElement.getAttribute("value")) ? new FlexibleStringExpander(setElement.getAttribute("value")) : null; 127 this.globalExdr = new FlexibleStringExpander(setElement.getAttribute("global")); 128 this.type = setElement.getAttribute("type"); 129 if (this.fromField != null && this.valueExdr != null) { 130 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"); 131 } 132 } 133 134 public void runAction(Map context) { 135 String globalStr = this.globalExdr.expandString(context); 136 boolean global = "true".equals(globalStr); 138 139 Object newValue = null; 140 if (this.fromField != null) { 141 newValue = this.fromField.get(context); 142 if (Debug.verboseOn()) Debug.logVerbose("In screen getting value for field from [" + this.fromField.getOriginalName() + "]: " + newValue, module); 143 } else if (this.valueExdr != null) { 144 newValue = this.valueExdr.expandString(context); 145 } 146 if (UtilValidate.isNotEmpty(this.type)) { 147 try { 148 newValue = ObjectType.simpleTypeConvert(newValue, this.type, null, null); 149 } catch (GeneralException e) { 150 String errMsg = "Could not convert field value for the field: [" + this.field.getOriginalName() + "] to the [" + this.type + "] type for the value [" + newValue + "]: " + e.toString(); 151 Debug.logError(e, errMsg, module); 152 throw new IllegalArgumentException (errMsg); 153 } 154 155 } 156 if (Debug.verboseOn()) Debug.logVerbose("In screen setting field [" + this.field.getOriginalName() + "] to value: " + newValue, module); 157 this.field.put(context, newValue); 158 159 if (global) { 160 Map globalCtx = (Map ) context.get("globalContext"); 161 if (globalCtx != null) { 162 this.field.put(globalCtx, newValue); 163 } 164 } 165 166 Map page = (Map ) context.get("page"); 168 if (page != null) { 169 this.field.put(page, newValue); 170 } 171 } 172 } 173 174 public static class PropertyMap extends ModelFormAction { 175 protected FlexibleStringExpander resourceExdr; 176 protected FlexibleMapAccessor mapNameAcsr; 177 protected FlexibleStringExpander globalExdr; 178 179 public PropertyMap(ModelForm modelForm, Element setElement) { 180 super (modelForm, setElement); 181 this.resourceExdr = new FlexibleStringExpander(setElement.getAttribute("resource")); 182 this.mapNameAcsr = new FlexibleMapAccessor(setElement.getAttribute("map-name")); 183 this.globalExdr = new FlexibleStringExpander(setElement.getAttribute("global")); 184 } 185 186 public void runAction(Map context) { 187 String globalStr = this.globalExdr.expandString(context); 188 boolean global = "true".equals(globalStr); 190 191 Locale locale = (Locale ) context.get("locale"); 192 String resource = this.resourceExdr.expandString(context, locale); 193 Map propertyMap = UtilProperties.getResourceBundleMap(resource, locale); 194 this.mapNameAcsr.put(context, propertyMap); 195 196 if (global) { 197 Map globalCtx = (Map ) context.get("globalContext"); 198 if (globalCtx != null) { 199 this.mapNameAcsr.put(globalCtx, propertyMap); 200 } 201 } 202 } 203 } 204 205 public static class PropertyToField extends ModelFormAction { 206 207 protected FlexibleStringExpander resourceExdr; 208 protected FlexibleStringExpander propertyExdr; 209 protected FlexibleMapAccessor fieldAcsr; 210 protected FlexibleStringExpander defaultExdr; 211 protected boolean noLocale; 212 protected FlexibleMapAccessor argListAcsr; 213 protected FlexibleStringExpander globalExdr; 214 215 public PropertyToField(ModelForm modelForm, Element setElement) { 216 super (modelForm, setElement); 217 this.resourceExdr = new FlexibleStringExpander(setElement.getAttribute("resource")); 218 this.propertyExdr = new FlexibleStringExpander(setElement.getAttribute("property")); 219 this.fieldAcsr = new FlexibleMapAccessor(setElement.getAttribute("field")); 220 this.defaultExdr = new FlexibleStringExpander(setElement.getAttribute("default")); 221 noLocale = "true".equals(setElement.getAttribute("no-locale")); 222 this.argListAcsr = new FlexibleMapAccessor(setElement.getAttribute("arg-list-name")); 223 this.globalExdr = new FlexibleStringExpander(setElement.getAttribute("global")); 224 } 225 226 public void runAction(Map context) { 227 231 Locale locale = (Locale ) context.get("locale"); 232 String resource = this.resourceExdr.expandString(context, locale); 233 String property = this.propertyExdr.expandString(context, locale); 234 235 String value = null; 236 if (noLocale) { 237 value = UtilProperties.getPropertyValue(resource, property); 238 } else { 239 value = UtilProperties.getMessage(resource, property, locale); 240 } 241 if (value == null || value.length() == 0) { 242 value = this.defaultExdr.expandString(context); 243 } 244 245 value = FlexibleStringExpander.expandString(value, context); 249 250 if (!argListAcsr.isEmpty()) { 251 List argList = (List ) argListAcsr.get(context); 252 if (argList != null && argList.size() > 0) { 253 value = MessageFormat.format(value, argList.toArray()); 254 } 255 } 256 257 fieldAcsr.put(context, value); 258 } 259 } 260 261 public static class Script extends ModelFormAction { 262 protected String location; 263 264 public Script(ModelForm modelForm, Element scriptElement) { 265 super (modelForm, scriptElement); 266 this.location = scriptElement.getAttribute("location"); 267 } 268 269 public void runAction(Map context) { 270 if (location.endsWith(".bsh")) { 271 try { 272 BshUtil.runBshAtLocation(location, context); 273 } catch (GeneralException e) { 274 String errMsg = "Error running BSH script at location [" + location + "]: " + e.toString(); 275 Debug.logError(e, errMsg, module); 276 throw new IllegalArgumentException (errMsg); 277 } 278 } else { 279 throw new IllegalArgumentException ("For screen script actions the script type is not yet support for location:" + location); 280 } 281 } 282 } 283 284 public static class Service extends ModelFormAction { 285 protected FlexibleStringExpander serviceNameExdr; 286 protected FlexibleMapAccessor resultMapNameAcsr; 287 protected FlexibleStringExpander autoFieldMapExdr; 288 protected FlexibleStringExpander resultMapListIteratorNameExdr; 289 protected FlexibleStringExpander resultMapListNameExdr; 290 protected Map fieldMap; 291 292 public Service(ModelForm modelForm, Element serviceElement) { 293 super (modelForm, serviceElement); 294 this.serviceNameExdr = new FlexibleStringExpander(serviceElement.getAttribute("service-name")); 295 this.resultMapNameAcsr = UtilValidate.isNotEmpty(serviceElement.getAttribute("result-map-name")) ? new FlexibleMapAccessor(serviceElement.getAttribute("result-map-name")) : null; 296 this.autoFieldMapExdr = new FlexibleStringExpander(serviceElement.getAttribute("auto-field-map")); 297 if (UtilValidate.isEmpty(serviceElement.getAttribute("result-map-list-name"))) { 298 String lstNm = modelForm.getListName(); 299 if (UtilValidate.isEmpty(lstNm)) { 300 lstNm = ModelForm.DEFAULT_FORM_RESULT_LIST_NAME; 301 } 302 this.resultMapListNameExdr = new FlexibleStringExpander(lstNm); 303 } else { 304 this.resultMapListNameExdr = new FlexibleStringExpander(serviceElement.getAttribute("result-map-list-name")); 305 } 306 307 if (UtilValidate.isEmpty(serviceElement.getAttribute("result-map-list-iterator-name"))) { 308 String lstNm = modelForm.getListIteratorName(); 309 if (UtilValidate.isEmpty(lstNm)) { 310 lstNm = ModelForm.DEFAULT_FORM_RESULT_LIST_NAME; 311 } 312 this.resultMapListIteratorNameExdr = new FlexibleStringExpander(lstNm); 313 } else { 314 this.resultMapListIteratorNameExdr = new FlexibleStringExpander(serviceElement.getAttribute("result-map-list-iterator-name")); 315 } 316 this.fieldMap = EntityFinderUtil.makeFieldMap(serviceElement); 317 } 318 319 public void runAction(Map context) { 320 String serviceNameExpanded = this.serviceNameExdr.expandString(context); 321 if (UtilValidate.isEmpty(serviceNameExpanded)) { 322 throw new IllegalArgumentException ("Service name was empty, expanded from: " + this.serviceNameExdr.getOriginal()); 323 } 324 325 String autoFieldMapString = this.autoFieldMapExdr.expandString(context); 326 boolean autoFieldMapBool = !"false".equals(autoFieldMapString); 327 328 try { 329 Map serviceContext = null; 330 if (autoFieldMapBool) { 331 serviceContext = this.modelForm.getDispatcher(context).getDispatchContext().makeValidContext(serviceNameExpanded, ModelService.IN_PARAM, context); 332 } else { 333 serviceContext = new HashMap (); 334 } 335 336 if (this.fieldMap != null) { 337 EntityFinderUtil.expandFieldMapToContext(this.fieldMap, context, serviceContext); 338 } 339 340 Map result = this.modelForm.getDispatcher(context).runSync(serviceNameExpanded, serviceContext); 341 342 if (this.resultMapNameAcsr != null) { 343 this.resultMapNameAcsr.put(context, result); 344 String queryString = (String )result.get("queryString"); 345 context.put("queryString", queryString); 346 context.put("queryStringMap", result.get("queryStringMap")); 347 if (UtilValidate.isNotEmpty(queryString)){ 348 try { 349 String queryStringEncoded = queryString.replaceAll("&", "%26"); 350 context.put("queryStringEncoded", queryStringEncoded); 351 } catch (PatternSyntaxException e) { 352 353 } 354 } 355 } else { 356 context.putAll(result); 357 } 358 String resultMapListIteratorName = resultMapListIteratorNameExdr.expandString(context); 359 Object obj = result.get(resultMapListIteratorName); 360 String formListIteratorName = modelForm.getListIteratorName(); 361 if (obj != null && obj instanceof EntityListIterator) { 362 context.put("listIteratorName", formListIteratorName); 363 context.put(formListIteratorName, obj); 364 } 365 String listName = resultMapListNameExdr.expandString(context); 366 Object listObj = result.get(listName); 367 if (listObj != null) { 368 if (!(listObj instanceof List )) { 369 throw new IllegalArgumentException ("Error in form [" + this.modelForm.getName() + "] calling service with name [" + serviceNameExpanded + "]: the result that is supposed to be a list is not a List. You may need to use list-iterator-name isntead of list-name, or something like that."); 370 } 371 List lst = (List ) listObj; 372 context.put("listName", listName); 373 context.put(listName, lst); 374 } 375 } catch (GenericServiceException e) { 376 String errMsg = "Error in form [" + this.modelForm.getName() + "] calling service with name [" + serviceNameExpanded + "]: " + e.toString(); 377 Debug.logError(e, errMsg, module); 378 throw new IllegalArgumentException (errMsg); 379 } 380 } 381 } 382 383 public static class EntityOne extends ModelFormAction { 384 protected PrimaryKeyFinder finder; 385 386 public EntityOne(ModelForm modelForm, Element entityOneElement) { 387 super (modelForm, entityOneElement); 388 finder = new PrimaryKeyFinder(entityOneElement); 389 } 390 391 public void runAction(Map context) { 392 try { 393 finder.runFind(context, this.modelForm.getDelegator(context)); 394 } catch (GeneralException e) { 395 String errMsg = "Error doing entity query by condition: " + e.toString(); 396 Debug.logError(e, errMsg, module); 397 throw new IllegalArgumentException (errMsg); 398 } 399 } 400 } 401 402 public static class EntityAnd extends ModelFormAction { 403 protected ByAndFinder finder; 404 String actualListName; 405 406 public EntityAnd(ModelForm modelForm, Element entityAndElement) { 407 super (modelForm, entityAndElement); 408 409 414 if (UtilValidate.isEmpty(entityAndElement.getAttribute("list-name"))) { 416 String lstNm = modelForm.getListName(); 417 if (UtilValidate.isEmpty(lstNm)) { 418 lstNm = modelForm.getListIteratorName(); 419 } 420 if (UtilValidate.isEmpty(lstNm)) { 421 lstNm = ModelForm.DEFAULT_FORM_RESULT_LIST_NAME; 422 } 423 entityAndElement.setAttribute("list-name", lstNm); 424 } 425 this.actualListName = entityAndElement.getAttribute("list-name"); 426 finder = new ByAndFinder(entityAndElement); 427 } 428 429 public void runAction(Map context) { 430 try { 431 finder.runFind(context, this.modelForm.getDelegator(context)); 433 Object obj = context.get(this.actualListName); 434 435 if (obj != null && (obj instanceof EntityListIterator)) { 436 String modelFormIteratorName = modelForm.getListIteratorName(); 437 context.put(modelFormIteratorName, obj); 438 } else if (obj != null && obj instanceof List ) { 439 String modelFormListName = modelForm.getListName(); 440 context.put(modelFormListName, obj); 441 } 442 } catch (GeneralException e) { 443 String errMsg = "Error doing entity query by condition: " + e.toString(); 444 Debug.logError(e, errMsg, module); 445 throw new IllegalArgumentException (errMsg); 446 } 447 } 448 449 } 450 451 public static class EntityCondition extends ModelFormAction { 452 ByConditionFinder finder; 453 String actualListName; 454 455 public EntityCondition(ModelForm modelForm, Element entityConditionElement) { 456 super (modelForm, entityConditionElement); 457 458 463 if (UtilValidate.isEmpty(entityConditionElement.getAttribute("list-name"))) { 465 String lstNm = modelForm.getListName(); 466 if (UtilValidate.isEmpty(lstNm)) { 467 lstNm = modelForm.getListIteratorName(); 468 } 469 if (UtilValidate.isEmpty(lstNm)) { 470 lstNm = ModelForm.DEFAULT_FORM_RESULT_LIST_NAME; 471 } 472 entityConditionElement.setAttribute("list-name", lstNm); 473 } 474 this.actualListName = entityConditionElement.getAttribute("list-name"); 475 finder = new ByConditionFinder(entityConditionElement); 476 } 477 478 public void runAction(Map context) { 479 try { 480 finder.runFind(context, this.modelForm.getDelegator(context)); 482 Object obj = context.get(this.actualListName); 483 if (obj != null && (obj instanceof EntityListIterator)) { 484 String modelFormIteratorName = modelForm.getListIteratorName(); 485 context.put(modelFormIteratorName, obj); 486 } else if (obj != null && obj instanceof List ) { 487 String modelFormListName = modelForm.getListName(); 488 context.put(modelFormListName, obj); 489 } 490 } catch (GeneralException e) { 491 String errMsg = "Error doing entity query by condition: " + e.toString(); 492 Debug.logError(e, errMsg, module); 493 throw new IllegalArgumentException (errMsg); 494 } 495 } 496 } 497 } 498 499 | Popular Tags |