1 24 package org.ofbiz.widget.tree; 25 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.ListIterator ; 30 import java.util.Map ; 31 import java.util.regex.PatternSyntaxException ; 32 33 import org.ofbiz.base.util.BshUtil; 34 import org.ofbiz.base.util.Debug; 35 import org.ofbiz.base.util.GeneralException; 36 import org.ofbiz.base.util.ObjectType; 37 import org.ofbiz.base.util.UtilFormatOut; 38 import org.ofbiz.base.util.UtilValidate; 39 import org.ofbiz.base.util.UtilXml; 40 import org.ofbiz.base.util.collections.FlexibleMapAccessor; 41 import org.ofbiz.base.util.string.FlexibleStringExpander; 42 import org.ofbiz.entity.finder.ByAndFinder; 43 import org.ofbiz.entity.finder.ByConditionFinder; 44 import org.ofbiz.entity.finder.EntityFinderUtil; 45 import org.ofbiz.entity.finder.PrimaryKeyFinder; 46 import org.ofbiz.entity.util.EntityListIterator; 47 import org.ofbiz.service.GenericServiceException; 48 import org.ofbiz.service.ModelService; 49 50 import org.w3c.dom.Document ; 51 import org.w3c.dom.Element ; 52 53 60 public abstract class ModelTreeAction { 61 public static final String module = ModelTreeAction.class.getName(); 62 63 protected ModelTree modelTree; 64 protected ModelTree.ModelNode modelNode; 65 protected ModelTree.ModelNode.ModelSubNode modelSubNode; 66 67 public ModelTreeAction(ModelTree.ModelNode modelNode, Element actionElement) { 68 if (Debug.verboseOn()) Debug.logVerbose("Reading Tree action with name: " + actionElement.getNodeName(), module); 69 this.modelNode = modelNode; 70 this.modelTree = modelNode.getModelTree(); 71 } 72 73 public ModelTreeAction(ModelTree.ModelNode.ModelSubNode modelSubNode, Element actionElement) { 74 if (Debug.verboseOn()) Debug.logVerbose("Reading Tree action with name: " + actionElement.getNodeName(), module); 75 this.modelSubNode = modelSubNode; 76 this.modelNode = this.modelSubNode.getNode(); 77 this.modelTree = this.modelNode.getModelTree(); 78 } 79 80 public abstract void runAction(Map context); 81 82 110 111 public static void runSubActions(List actions, Map context) { 112 Iterator actionIter = actions.iterator(); 113 while (actionIter.hasNext()) { 114 ModelTreeAction action = (ModelTreeAction) actionIter.next(); 115 if (Debug.verboseOn()) Debug.logVerbose("Running tree action " + action.getClass().getName(), module); 116 action.runAction(context); 117 } 118 } 119 120 public static class SetField extends ModelTreeAction { 121 protected FlexibleMapAccessor field; 122 protected FlexibleMapAccessor fromField; 123 protected FlexibleStringExpander valueExdr; 124 protected FlexibleStringExpander globalExdr; 125 protected String type; 126 127 public SetField(ModelTree.ModelNode modelNode, Element setElement) { 128 super (modelNode, setElement); 129 this.field = new FlexibleMapAccessor(setElement.getAttribute("field")); 130 this.fromField = UtilValidate.isNotEmpty(setElement.getAttribute("from-field")) ? new FlexibleMapAccessor(setElement.getAttribute("from-field")) : null; 131 this.valueExdr = UtilValidate.isNotEmpty(setElement.getAttribute("value")) ? new FlexibleStringExpander(setElement.getAttribute("value")) : null; 132 this.globalExdr = new FlexibleStringExpander(setElement.getAttribute("global")); 133 this.type = setElement.getAttribute("type"); 134 if (this.fromField != null && this.valueExdr != null) { 135 throw new IllegalArgumentException ("Cannot specify a from-field [" + setElement.getAttribute("from-field") + "] and a value [" + setElement.getAttribute("value") + "] on the set action in a tree widget"); 136 } 137 } 138 139 public void runAction(Map context) { 140 String globalStr = this.globalExdr.expandString(context); 141 boolean global = "true".equals(globalStr); 143 144 Object newValue = null; 145 if (this.fromField != null) { 146 newValue = this.fromField.get(context); 147 } else if (this.valueExdr != null) { 148 newValue = this.valueExdr.expandString(context); 149 } 150 if (UtilValidate.isNotEmpty(this.type)) { 151 try { 152 newValue = ObjectType.simpleTypeConvert(newValue, this.type, null, null); 153 } catch (GeneralException e) { 154 String errMsg = "Could not convert field value for the field: [" + this.field.getOriginalName() + "] to the [" + this.type + "] type for the value [" + newValue + "]: " + e.toString(); 155 Debug.logError(e, errMsg, module); 156 throw new IllegalArgumentException (errMsg); 157 } 158 159 } 160 this.field.put(context, newValue); 161 162 if (global) { 163 Map globalCtx = (Map ) context.get("globalContext"); 164 if (globalCtx != null) { 165 this.field.put(globalCtx, newValue); 166 } 167 } 168 169 Map page = (Map ) context.get("page"); 171 if (page != null) { 172 this.field.put(page, newValue); 173 } 174 } 175 } 176 177 public static class Script extends ModelTreeAction { 178 protected String location; 179 180 public Script(ModelTree.ModelNode modelNode, Element scriptElement) { 181 super (modelNode, scriptElement); 182 this.location = scriptElement.getAttribute("location"); 183 } 184 185 public Script(ModelTree.ModelNode.ModelSubNode modelSubNode, Element scriptElement) { 186 super (modelSubNode, scriptElement); 187 this.location = scriptElement.getAttribute("location"); 188 } 189 190 public void runAction(Map context) { 191 if (location.endsWith(".bsh")) { 192 try { 193 context.put("_LIST_ITERATOR_", null); 194 BshUtil.runBshAtLocation(location, context); 195 Object obj = context.get("_LIST_ITERATOR_"); 196 if (this.modelSubNode != null) { 197 if (obj != null && (obj instanceof EntityListIterator || obj instanceof ListIterator )) { 198 this.modelSubNode.setListIterator((ListIterator )obj); 199 } else { 200 if (obj instanceof List ) 201 this.modelSubNode.setListIterator(((List )obj).listIterator()); 202 } 203 } 204 } catch (GeneralException e) { 205 String errMsg = "Error running BSH script at location [" + location + "]: " + e.toString(); 206 Debug.logError(e, errMsg, module); 207 throw new IllegalArgumentException (errMsg); 208 } 209 } else { 210 throw new IllegalArgumentException ("For tree script actions the script type is not yet support for location:" + location); 211 } 212 } 213 } 214 215 public static class Service extends ModelTreeAction { 216 protected FlexibleStringExpander serviceNameExdr; 217 protected FlexibleMapAccessor resultMapNameAcsr; 218 protected FlexibleStringExpander autoFieldMapExdr; 219 protected FlexibleStringExpander resultMapListNameExdr; 220 protected FlexibleStringExpander resultMapListIteratorNameExdr; 221 protected FlexibleStringExpander resultMapValueNameExdr; 222 protected FlexibleStringExpander valueNameExdr; 223 protected Map fieldMap; 224 225 public Service(ModelTree.ModelNode modelNode, Element serviceElement) { 226 super (modelNode, serviceElement); 227 initService(serviceElement); 228 } 229 230 public Service(ModelTree.ModelNode.ModelSubNode modelSubNode, Element serviceElement) { 231 super (modelSubNode, serviceElement); 232 initService(serviceElement); 233 } 234 235 public void initService( Element serviceElement ) { 236 237 this.serviceNameExdr = new FlexibleStringExpander(serviceElement.getAttribute("service-name")); 238 this.resultMapNameAcsr = UtilValidate.isNotEmpty(serviceElement.getAttribute("result-map-name")) ? new FlexibleMapAccessor(serviceElement.getAttribute("result-map-name")) : null; 239 this.autoFieldMapExdr = new FlexibleStringExpander(serviceElement.getAttribute("auto-field-map")); 240 this.resultMapListNameExdr = new FlexibleStringExpander(serviceElement.getAttribute("result-map-list-name")); 241 this.resultMapListIteratorNameExdr = new FlexibleStringExpander(serviceElement.getAttribute("result-map-list-iterator-name")); 242 this.resultMapValueNameExdr = new FlexibleStringExpander(serviceElement.getAttribute("result-map-value-name")); 243 this.valueNameExdr = new FlexibleStringExpander(serviceElement.getAttribute("value-name")); 244 this.fieldMap = EntityFinderUtil.makeFieldMap(serviceElement); 245 } 246 247 public void runAction(Map context) { 248 String serviceNameExpanded = this.serviceNameExdr.expandString(context); 249 if (UtilValidate.isEmpty(serviceNameExpanded)) { 250 throw new IllegalArgumentException ("Service name was empty, expanded from: " + this.serviceNameExdr.getOriginal()); 251 } 252 253 String autoFieldMapString = this.autoFieldMapExdr.expandString(context); 254 boolean autoFieldMapBool = !"false".equals(autoFieldMapString); 255 256 try { 257 Map serviceContext = null; 258 if (autoFieldMapBool) { 259 serviceContext = this.modelTree.getDispatcher().getDispatchContext().makeValidContext(serviceNameExpanded, ModelService.IN_PARAM, context); 260 } else { 261 serviceContext = new HashMap (); 262 } 263 264 if (this.fieldMap != null) { 265 EntityFinderUtil.expandFieldMapToContext(this.fieldMap, context, serviceContext); 266 } 267 268 Map result = this.modelTree.getDispatcher().runSync(serviceNameExpanded, serviceContext); 269 270 if (this.resultMapNameAcsr != null) { 271 this.resultMapNameAcsr.put(context, result); 272 String queryString = (String )result.get("queryString"); 273 context.put("queryString", queryString); 274 context.put("queryStringMap", result.get("queryStringMap")); 275 if (UtilValidate.isNotEmpty(queryString)){ 276 try { 277 String queryStringEncoded = queryString.replaceAll("&", "%26"); 278 context.put("queryStringEncoded", queryStringEncoded); 279 } catch (PatternSyntaxException e) { 280 281 } 282 } 283 } else { 284 context.putAll(result); 285 } 286 String resultMapListName = resultMapListNameExdr.expandString(context); 287 String resultMapListIteratorName = resultMapListIteratorNameExdr.expandString(context); 288 String resultMapValueName = resultMapValueNameExdr.expandString(context); 289 String valueName = valueNameExdr.expandString(context); 290 291 if (this.modelSubNode != null) { 292 if (UtilValidate.isNotEmpty(resultMapListIteratorName)) { 294 this.modelSubNode.setListIterator((ListIterator )result.get(resultMapListIteratorName)); 295 } else if (UtilValidate.isNotEmpty(resultMapListName)) { 296 List lst = (List )result.get(resultMapListName); 297 if (lst != null ) { 298 this.modelSubNode.setListIterator(lst.listIterator()); 299 } 300 } 301 } else { 302 if (UtilValidate.isNotEmpty(resultMapValueName)) { 303 if (UtilValidate.isNotEmpty(valueName)) { 304 context.put(valueName, result.get(resultMapValueName)); 305 } else { 306 context.putAll((Map )result.get(resultMapValueName)); 307 } 308 } 309 } 310 } catch (GenericServiceException e) { 311 String errMsg = "Error calling service with name " + serviceNameExpanded + ": " + e.toString(); 312 Debug.logError(e, errMsg, module); 313 throw new IllegalArgumentException (errMsg); 314 } 315 } 316 } 317 318 public static class EntityOne extends ModelTreeAction { 319 protected PrimaryKeyFinder finder; 320 String valueName; 321 322 public EntityOne(ModelTree.ModelNode modelNode, Element entityOneElement) { 323 super (modelNode, entityOneElement); 324 this.valueName = UtilFormatOut.checkEmpty(entityOneElement.getAttribute("value-name"), null); 325 entityOneElement.setAttribute( "value-name", this.valueName); 326 finder = new PrimaryKeyFinder(entityOneElement); 327 } 328 329 public void runAction(Map context) { 330 try { 331 finder.runFind(context, this.modelTree.getDelegator()); 332 } catch (GeneralException e) { 333 String errMsg = "Error doing entity query by condition: " + e.toString(); 334 Debug.logError(e, errMsg, module); 335 throw new IllegalArgumentException (errMsg); 336 } 337 } 338 } 339 340 public static class EntityAnd extends ModelTreeAction { 341 protected ByAndFinder finder; 342 String listName; 343 344 public EntityAnd(ModelTree.ModelNode.ModelSubNode modelSubNode, Element entityAndElement) { 345 super (modelSubNode, entityAndElement); 346 boolean useCache = "true".equalsIgnoreCase(entityAndElement.getAttribute("use-cache")); 347 Document ownerDoc = entityAndElement.getOwnerDocument(); 348 if (!useCache) 349 UtilXml.addChildElement(entityAndElement, "use-iterator", ownerDoc); 350 this.listName = UtilFormatOut.checkEmpty(entityAndElement.getAttribute("list-name"), "_LIST_ITERATOR_"); 351 entityAndElement.setAttribute( "list-name", this.listName); 352 finder = new ByAndFinder(entityAndElement); 353 } 354 355 public void runAction(Map context) { 356 try { 357 context.put(this.listName, null); 358 finder.runFind(context, this.modelTree.getDelegator()); 359 Object obj = context.get(this.listName); 360 if (obj != null && (obj instanceof EntityListIterator || obj instanceof ListIterator )) { 361 this.modelSubNode.setListIterator((ListIterator )obj); 362 } else { 363 if (obj instanceof List ) this.modelSubNode.setListIterator(((List )obj).listIterator()); 364 } 365 } catch (GeneralException e) { 366 String errMsg = "Error doing entity query by condition: " + e.toString(); 367 Debug.logError(e, errMsg, module); 368 throw new IllegalArgumentException (errMsg); 369 } 370 } 371 } 372 373 public static class EntityCondition extends ModelTreeAction { 374 ByConditionFinder finder; 375 String listName; 376 377 public EntityCondition(ModelTree.ModelNode.ModelSubNode modelSubNode, Element entityConditionElement) { 378 super (modelSubNode, entityConditionElement); 379 Document ownerDoc = entityConditionElement.getOwnerDocument(); 380 boolean useCache = "true".equalsIgnoreCase(entityConditionElement.getAttribute("use-cache")); 381 if (!useCache) 382 UtilXml.addChildElement(entityConditionElement, "use-iterator", ownerDoc); 383 384 this.listName = UtilFormatOut.checkEmpty(entityConditionElement.getAttribute("list-name"), "_LIST_ITERATOR_"); 385 entityConditionElement.setAttribute( "list-name", this.listName); 386 finder = new ByConditionFinder(entityConditionElement); 387 } 388 389 public void runAction(Map context) { 390 try { 391 context.put(this.listName, null); 392 finder.runFind(context, this.modelTree.getDelegator()); 393 Object obj = context.get(this.listName); 394 if (obj != null && (obj instanceof EntityListIterator || obj instanceof ListIterator )) { 395 this.modelSubNode.setListIterator((ListIterator )obj); 396 } else { 397 if (obj instanceof List ) this.modelSubNode.setListIterator(((List )obj).listIterator()); 398 } 399 } catch (GeneralException e) { 400 String errMsg = "Error doing entity query by condition: " + e.toString(); 401 Debug.logError(e, errMsg, module); 402 throw new IllegalArgumentException (errMsg); 403 } 404 } 405 } 406 } 407 | Popular Tags |