KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > widget > screen > ModelScreenAction


1 /*
2  * $Id: ModelScreenAction.java 6082 2005-11-06 06:32:16Z jonesde $
3  *
4  * Copyright (c) 2004-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.widget.screen;
25
26 import java.io.Serializable JavaDoc;
27 import java.text.MessageFormat JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Locale JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.regex.PatternSyntaxException JavaDoc;
34
35 import javax.servlet.ServletContext JavaDoc;
36 import javax.servlet.http.HttpSession JavaDoc;
37
38 import javolution.util.FastList;
39 import javolution.util.FastMap;
40
41 import org.ofbiz.base.util.BshUtil;
42 import org.ofbiz.base.util.Debug;
43 import org.ofbiz.base.util.GeneralException;
44 import org.ofbiz.base.util.ObjectType;
45 import org.ofbiz.base.util.StringUtil;
46 import org.ofbiz.base.util.UtilProperties;
47 import org.ofbiz.base.util.UtilValidate;
48 import org.ofbiz.base.util.UtilXml;
49 import org.ofbiz.base.util.collections.FlexibleMapAccessor;
50 import org.ofbiz.base.util.collections.ResourceBundleMapWrapper;
51 import org.ofbiz.base.util.string.FlexibleStringExpander;
52 import org.ofbiz.entity.GenericValue;
53 import org.ofbiz.entity.GenericEntityException;
54 import org.ofbiz.entity.finder.ByAndFinder;
55 import org.ofbiz.entity.finder.ByConditionFinder;
56 import org.ofbiz.entity.finder.EntityFinderUtil;
57 import org.ofbiz.entity.finder.PrimaryKeyFinder;
58 import org.ofbiz.service.GenericServiceException;
59 import org.ofbiz.service.ModelService;
60 import org.w3c.dom.Element JavaDoc;
61
62
63 /**
64  * Widget Library - Screen model class
65  *
66  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
67  * @version $Rev: 6082 $
68  * @since 3.1
69  */

70 public abstract class ModelScreenAction implements Serializable JavaDoc {
71     public static final String JavaDoc module = ModelScreenAction.class.getName();
72
73     protected ModelScreen modelScreen;
74
75     public ModelScreenAction(ModelScreen modelScreen, Element JavaDoc actionElement) {
76         this.modelScreen = modelScreen;
77         if (Debug.verboseOn()) Debug.logVerbose("Reading Screen action with name: " + actionElement.getNodeName(), module);
78     }
79     
80     public abstract void runAction(Map JavaDoc context) throws GeneralException;
81     
82     public static List JavaDoc readSubActions(ModelScreen modelScreen, Element JavaDoc parentElement) {
83         List JavaDoc actions = FastList.newInstance();
84         
85         List JavaDoc actionElementList = UtilXml.childElementList(parentElement);
86         Iterator JavaDoc actionElementIter = actionElementList.iterator();
87         while (actionElementIter.hasNext()) {
88             Element JavaDoc actionElement = (Element JavaDoc) actionElementIter.next();
89             if ("set".equals(actionElement.getNodeName())) {
90                 actions.add(new SetField(modelScreen, actionElement));
91             } else if ("property-map".equals(actionElement.getNodeName())) {
92                 actions.add(new PropertyMap(modelScreen, actionElement));
93             } else if ("property-to-field".equals(actionElement.getNodeName())) {
94                 actions.add(new PropertyToField(modelScreen, actionElement));
95             } else if ("script".equals(actionElement.getNodeName())) {
96                 actions.add(new Script(modelScreen, actionElement));
97             } else if ("service".equals(actionElement.getNodeName())) {
98                 actions.add(new Service(modelScreen, actionElement));
99             } else if ("entity-one".equals(actionElement.getNodeName())) {
100                 actions.add(new EntityOne(modelScreen, actionElement));
101             } else if ("entity-and".equals(actionElement.getNodeName())) {
102                 actions.add(new EntityAnd(modelScreen, actionElement));
103             } else if ("entity-condition".equals(actionElement.getNodeName())) {
104                 actions.add(new EntityCondition(modelScreen, actionElement));
105             } else if ("get-related-one".equals(actionElement.getNodeName())) {
106                 actions.add(new GetRelatedOne(modelScreen, actionElement));
107             } else if ("get-related".equals(actionElement.getNodeName())) {
108                 actions.add(new GetRelated(modelScreen, actionElement));
109             } else {
110                 throw new IllegalArgumentException JavaDoc("Action element not supported with name: " + actionElement.getNodeName());
111             }
112         }
113         
114         return actions;
115     }
116     
117     public static void runSubActions(List JavaDoc actions, Map JavaDoc context) throws GeneralException {
118         if (actions == null) return;
119         
120         Iterator JavaDoc actionIter = actions.iterator();
121         while (actionIter.hasNext()) {
122             ModelScreenAction action = (ModelScreenAction) actionIter.next();
123             if (Debug.verboseOn()) Debug.logVerbose("Running screen action " + action.getClass().getName(), module);
124             action.runAction(context);
125         }
126     }
127     
128     public static class SetField extends ModelScreenAction {
129         protected FlexibleMapAccessor field;
130         protected FlexibleMapAccessor fromField;
131         protected FlexibleStringExpander valueExdr;
132         protected FlexibleStringExpander defaultExdr;
133         protected FlexibleStringExpander globalExdr;
134         protected String JavaDoc type;
135         protected String JavaDoc toScope;
136         protected String JavaDoc fromScope;
137         
138         public SetField(ModelScreen modelScreen, Element JavaDoc setElement) {
139             super (modelScreen, setElement);
140             this.field = new FlexibleMapAccessor(setElement.getAttribute("field"));
141             this.fromField = new FlexibleMapAccessor(setElement.getAttribute("from-field"));
142             this.valueExdr = new FlexibleStringExpander(setElement.getAttribute("value"));
143             this.defaultExdr = new FlexibleStringExpander(setElement.getAttribute("default-value"));
144             this.globalExdr = new FlexibleStringExpander(setElement.getAttribute("global"));
145             this.type = setElement.getAttribute("type");
146             this.toScope = setElement.getAttribute("to-scope");
147             this.fromScope = setElement.getAttribute("from-scope");
148             if (!this.fromField.isEmpty() && !this.valueExdr.isEmpty()) {
149                 throw new IllegalArgumentException JavaDoc("Cannot specify a from-field [" + setElement.getAttribute("from-field") + "] and a value [" + setElement.getAttribute("value") + "] on the set action in a screen widget");
150             }
151         }
152         
153         public void runAction(Map JavaDoc context) {
154             String JavaDoc globalStr = this.globalExdr.expandString(context);
155             // default to false
156
boolean global = "true".equals(globalStr);
157             
158             Object JavaDoc newValue = null;
159             if (this.fromScope != null && this.fromScope.equals("user")) {
160                 if (!this.fromField.isEmpty()) {
161                     HttpSession JavaDoc session = (HttpSession JavaDoc) context.get("session");
162                     newValue = getInMemoryPersistedFromField(session, context);
163                     if (Debug.verboseOn()) Debug.logVerbose("In user getting value for field from [" + this.fromField.getOriginalName() + "]: " + newValue, module);
164                 } else if (!this.valueExdr.isEmpty()) {
165                     newValue = this.valueExdr.expandString(context);
166                 }
167             } else if (this.fromScope != null && this.fromScope.equals("application")) {
168                 if (!this.fromField.isEmpty()) {
169                     ServletContext JavaDoc servletContext = (ServletContext JavaDoc) context.get("application");
170                     newValue = getInMemoryPersistedFromField(servletContext, context);
171                     if (Debug.verboseOn()) Debug.logVerbose("In application getting value for field from [" + this.fromField.getOriginalName() + "]: " + newValue, module);
172                 } else if (!this.valueExdr.isEmpty()) {
173                     newValue = this.valueExdr.expandString(context);
174                 }
175             } else {
176                 if (!this.fromField.isEmpty()) {
177                     newValue = this.fromField.get(context);
178                     if (Debug.verboseOn()) Debug.logVerbose("In screen getting value for field from [" + this.fromField.getOriginalName() + "]: " + newValue, module);
179                 } else if (!this.valueExdr.isEmpty()) {
180                     newValue = this.valueExdr.expandString(context);
181                 }
182             }
183
184             // If newValue is still empty, use the default value
185
if (ObjectType.isEmpty(newValue) && !this.defaultExdr.isEmpty()) {
186                 newValue = this.defaultExdr.expandString(context);
187             }
188             
189             if (UtilValidate.isNotEmpty(this.type)) {
190                 try {
191                     newValue = ObjectType.simpleTypeConvert(newValue, this.type, null, null);
192                 } catch (GeneralException e) {
193                     String JavaDoc errMsg = "Could not convert field value for the field: [" + this.field.getOriginalName() + "] to the [" + this.type + "] type for the value [" + newValue + "]: " + e.toString();
194                     Debug.logError(e, errMsg, module);
195                     throw new IllegalArgumentException JavaDoc(errMsg);
196                 }
197             }
198             
199             if (this.toScope != null && this.toScope.equals("user")) {
200                     String JavaDoc originalName = this.field.getOriginalName();
201                     List JavaDoc currentWidgetTrail = (List JavaDoc)context.get("_WIDGETTRAIL_");
202                     String JavaDoc newKey = "";
203                     if (currentWidgetTrail != null) {
204                         newKey = StringUtil.join(currentWidgetTrail, "|");
205                     }
206                     if (UtilValidate.isNotEmpty(newKey)) {
207                         newKey += "|";
208                     }
209                     newKey += originalName;
210                     HttpSession JavaDoc session = (HttpSession JavaDoc)context.get("session");
211                     session.setAttribute(newKey, newValue);
212                     if (Debug.verboseOn()) Debug.logVerbose("In user setting value for field from [" + this.field.getOriginalName() + "]: " + newValue, module);
213             } else if (this.toScope != null && this.toScope.equals("application")) {
214                     String JavaDoc originalName = this.field.getOriginalName();
215                     List JavaDoc currentWidgetTrail = (List JavaDoc)context.get("_WIDGETTRAIL_");
216                     String JavaDoc newKey = "";
217                     if (currentWidgetTrail != null) {
218                         newKey = StringUtil.join(currentWidgetTrail, "|");
219                     }
220                     if (UtilValidate.isNotEmpty(newKey)) {
221                         newKey += "|";
222                     }
223                     newKey += originalName;
224                     ServletContext JavaDoc servletContext = (ServletContext JavaDoc)context.get("application");
225                     servletContext.setAttribute(newKey, newValue);
226                     if (Debug.verboseOn()) Debug.logVerbose("In application setting value for field from [" + this.field.getOriginalName() + "]: " + newValue, module);
227             } else {
228                 // only do this if it is not global, if global ONLY put it in the global context
229
if (!global) {
230                     if (Debug.verboseOn()) Debug.logVerbose("In screen setting field [" + this.field.getOriginalName() + "] to value: " + newValue, module);
231                     this.field.put(context, newValue);
232                 }
233             }
234             
235             if (global) {
236                 Map JavaDoc globalCtx = (Map JavaDoc) context.get("globalContext");
237                 if (globalCtx != null) {
238                     this.field.put(globalCtx, newValue);
239                 } else {
240                     this.field.put(context, newValue);
241                 }
242             }
243             
244             // this is a hack for backward compatibility with the JPublish page object
245
Map JavaDoc page = (Map JavaDoc) context.get("page");
246             if (page != null) {
247                 this.field.put(page, newValue);
248             }
249         }
250         
251         public Object JavaDoc getInMemoryPersistedFromField(Object JavaDoc storeAgent, Map JavaDoc context) {
252             Object JavaDoc newValue = null;
253             String JavaDoc originalName = this.fromField.getOriginalName();
254             List JavaDoc currentWidgetTrail = (List JavaDoc)context.get("_WIDGETTRAIL_");
255             List JavaDoc trailList = new ArrayList JavaDoc();
256             if (currentWidgetTrail != null) {
257                 trailList.addAll(currentWidgetTrail);
258             }
259             
260             for (int i=trailList.size(); i >= 0; i--) {
261                 List JavaDoc subTrail = trailList.subList(0,i);
262                 String JavaDoc newKey = null;
263                 if (subTrail.size() > 0)
264                     newKey = StringUtil.join(subTrail, "|") + "|" + originalName;
265                 else
266                     newKey = originalName;
267                 
268                 if (storeAgent instanceof ServletContext JavaDoc) {
269                     newValue = ((ServletContext JavaDoc)storeAgent).getAttribute(newKey);
270                 } else if (storeAgent instanceof HttpSession JavaDoc) {
271                     newValue = ((HttpSession JavaDoc)storeAgent).getAttribute(newKey);
272                 }
273                 if (newValue != null) {
274                     break;
275                 }
276             }
277             return newValue;
278         }
279     }
280     
281     public static class PropertyMap extends ModelScreenAction {
282         protected FlexibleStringExpander resourceExdr;
283         protected FlexibleMapAccessor mapNameAcsr;
284         protected FlexibleStringExpander globalExdr;
285         
286         public PropertyMap(ModelScreen modelScreen, Element JavaDoc setElement) {
287             super (modelScreen, setElement);
288             this.resourceExdr = new FlexibleStringExpander(setElement.getAttribute("resource"));
289             this.mapNameAcsr = new FlexibleMapAccessor(setElement.getAttribute("map-name"));
290             this.globalExdr = new FlexibleStringExpander(setElement.getAttribute("global"));
291         }
292         
293         public void runAction(Map JavaDoc context) {
294             String JavaDoc globalStr = this.globalExdr.expandString(context);
295             // default to false
296
boolean global = "true".equals(globalStr);
297
298             Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
299             String JavaDoc resource = this.resourceExdr.expandString(context, locale);
300             
301             ResourceBundleMapWrapper existingPropMap = (ResourceBundleMapWrapper) this.mapNameAcsr.get(context);
302             if (existingPropMap == null) {
303                 this.mapNameAcsr.put(context, UtilProperties.getResourceBundleMap(resource, locale));
304             } else {
305                 existingPropMap.addBottomResourceBundle(resource);
306             }
307
308             if (global) {
309                 Map JavaDoc globalCtx = (Map JavaDoc) context.get("globalContext");
310                 if (globalCtx != null) {
311                     ResourceBundleMapWrapper globalExistingPropMap = (ResourceBundleMapWrapper) this.mapNameAcsr.get(globalCtx);
312                     if (globalExistingPropMap == null) {
313                         this.mapNameAcsr.put(globalCtx, UtilProperties.getResourceBundleMap(resource, locale));
314                     } else {
315                         // is it the same object? if not add it in here too...
316
if (existingPropMap != globalExistingPropMap) {
317                             globalExistingPropMap.addBottomResourceBundle(resource);
318                         }
319                     }
320                 }
321             }
322         }
323     }
324     
325     public static class PropertyToField extends ModelScreenAction {
326         
327         protected FlexibleStringExpander resourceExdr;
328         protected FlexibleStringExpander propertyExdr;
329         protected FlexibleMapAccessor fieldAcsr;
330         protected FlexibleStringExpander defaultExdr;
331         protected boolean noLocale;
332         protected FlexibleMapAccessor argListAcsr;
333         protected FlexibleStringExpander globalExdr;
334
335         public PropertyToField(ModelScreen modelScreen, Element JavaDoc setElement) {
336             super (modelScreen, setElement);
337             this.resourceExdr = new FlexibleStringExpander(setElement.getAttribute("resource"));
338             this.propertyExdr = new FlexibleStringExpander(setElement.getAttribute("property"));
339             this.fieldAcsr = new FlexibleMapAccessor(setElement.getAttribute("field"));
340             this.defaultExdr = new FlexibleStringExpander(setElement.getAttribute("default"));
341             noLocale = "true".equals(setElement.getAttribute("no-locale"));
342             this.argListAcsr = new FlexibleMapAccessor(setElement.getAttribute("arg-list-name"));
343             this.globalExdr = new FlexibleStringExpander(setElement.getAttribute("global"));
344         }
345         
346         public void runAction(Map JavaDoc context) {
347             //String globalStr = this.globalExdr.expandString(context);
348
// default to false
349
//boolean global = "true".equals(globalStr);
350

351             Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
352             String JavaDoc resource = this.resourceExdr.expandString(context, locale);
353             String JavaDoc property = this.propertyExdr.expandString(context, locale);
354             
355             String JavaDoc value = null;
356             if (noLocale) {
357                 value = UtilProperties.getPropertyValue(resource, property);
358             } else {
359                 value = UtilProperties.getMessage(resource, property, locale);
360             }
361             if (value == null || value.length() == 0) {
362                 value = this.defaultExdr.expandString(context);
363             }
364             
365             // note that expanding the value string here will handle defaultValue and the string from
366
// the properties file; if we decide later that we don't want the string from the properties
367
// file to be expanded we should just expand the defaultValue at the beginning of this method.
368
value = FlexibleStringExpander.expandString(value, context);
369
370             if (!argListAcsr.isEmpty()) {
371                 List JavaDoc argList = (List JavaDoc) argListAcsr.get(context);
372                 if (argList != null && argList.size() > 0) {
373                     value = MessageFormat.format(value, argList.toArray());
374                 }
375             }
376
377             fieldAcsr.put(context, value);
378         }
379     }
380     
381     public static class Script extends ModelScreenAction {
382         protected String JavaDoc location;
383         
384         public Script(ModelScreen modelScreen, Element JavaDoc scriptElement) {
385             super (modelScreen, scriptElement);
386             this.location = scriptElement.getAttribute("location");
387         }
388         
389         public void runAction(Map JavaDoc context) throws GeneralException {
390             if (location.endsWith(".bsh")) {
391                 try {
392                     BshUtil.runBshAtLocation(location, context);
393                 } catch (GeneralException e) {
394                     String JavaDoc errMsg = "Error running BSH script at location [" + location + "]: " + e.toString();
395                     // throwing nested exception instead of logging full detail: Debug.logError(e, errMsg, module);
396
throw new GeneralException(errMsg, e);
397                 }
398             } else {
399                 throw new GeneralException("For screen script actions the script type is not yet support for location:" + location);
400             }
401         }
402     }
403
404     public static class Service extends ModelScreenAction {
405         protected FlexibleStringExpander serviceNameExdr;
406         protected FlexibleMapAccessor resultMapNameAcsr;
407         protected FlexibleStringExpander autoFieldMapExdr;
408         protected Map JavaDoc fieldMap;
409         
410         public Service(ModelScreen modelScreen, Element JavaDoc serviceElement) {
411             super (modelScreen, serviceElement);
412             this.serviceNameExdr = new FlexibleStringExpander(serviceElement.getAttribute("service-name"));
413             this.resultMapNameAcsr = UtilValidate.isNotEmpty(serviceElement.getAttribute("result-map-name")) ? new FlexibleMapAccessor(serviceElement.getAttribute("result-map-name")) : null;
414             this.autoFieldMapExdr = new FlexibleStringExpander(serviceElement.getAttribute("auto-field-map"));
415             this.fieldMap = EntityFinderUtil.makeFieldMap(serviceElement);
416         }
417         
418         public void runAction(Map JavaDoc context) {
419             String JavaDoc serviceNameExpanded = this.serviceNameExdr.expandString(context);
420             if (UtilValidate.isEmpty(serviceNameExpanded)) {
421                 throw new IllegalArgumentException JavaDoc("Service name was empty, expanded from: " + this.serviceNameExdr.getOriginal());
422             }
423             
424             String JavaDoc autoFieldMapString = this.autoFieldMapExdr.expandString(context);
425             
426             try {
427                 Map JavaDoc serviceContext = null;
428                 if ("true".equals(autoFieldMapString)) {
429                     serviceContext = this.modelScreen.getDispatcher(context).getDispatchContext().makeValidContext(serviceNameExpanded, ModelService.IN_PARAM, context);
430                 } else if (UtilValidate.isNotEmpty(autoFieldMapString) && !"false".equals(autoFieldMapString)) {
431                     FlexibleMapAccessor fieldFma = new FlexibleMapAccessor(autoFieldMapString);
432                     Map JavaDoc autoFieldMap = (Map JavaDoc) fieldFma.get(context);
433                     if (autoFieldMap != null) {
434                         serviceContext = this.modelScreen.getDispatcher(context).getDispatchContext().makeValidContext(serviceNameExpanded, ModelService.IN_PARAM, autoFieldMap);
435                     }
436                 }
437                 if (serviceContext == null) {
438                     serviceContext = FastMap.newInstance();
439                 }
440                 
441                 if (this.fieldMap != null) {
442                     EntityFinderUtil.expandFieldMapToContext(this.fieldMap, context, serviceContext);
443                 }
444                 
445                 Map JavaDoc result = this.modelScreen.getDispatcher(context).runSync(serviceNameExpanded, serviceContext);
446                 
447                 if (this.resultMapNameAcsr != null) {
448                     this.resultMapNameAcsr.put(context, result);
449                     String JavaDoc queryString = (String JavaDoc)result.get("queryString");
450                     context.put("queryString", queryString);
451                     context.put("queryStringMap", result.get("queryStringMap"));
452                     if (UtilValidate.isNotEmpty(queryString)){
453                         try {
454                             String JavaDoc queryStringEncoded = queryString.replaceAll("&", "%26");
455                             context.put("queryStringEncoded", queryStringEncoded);
456                         } catch (PatternSyntaxException JavaDoc e) {
457                             
458                         }
459                     }
460                 } else {
461                     context.putAll(result);
462                 }
463             } catch (GenericServiceException e) {
464                 String JavaDoc errMsg = "Error calling service with name " + serviceNameExpanded + ": " + e.toString();
465                 Debug.logError(e, errMsg, module);
466                 throw new IllegalArgumentException JavaDoc(errMsg);
467             }
468         }
469     }
470
471     public static class EntityOne extends ModelScreenAction {
472         protected PrimaryKeyFinder finder;
473         
474         public EntityOne(ModelScreen modelScreen, Element JavaDoc entityOneElement) {
475             super (modelScreen, entityOneElement);
476             finder = new PrimaryKeyFinder(entityOneElement);
477         }
478         
479         public void runAction(Map JavaDoc context) {
480             try {
481                 finder.runFind(context, this.modelScreen.getDelegator(context));
482             } catch (GeneralException e) {
483                 String JavaDoc errMsg = "Error doing entity query by condition: " + e.toString();
484                 Debug.logError(e, errMsg, module);
485                 throw new IllegalArgumentException JavaDoc(errMsg);
486             }
487         }
488     }
489
490     public static class EntityAnd extends ModelScreenAction {
491         protected ByAndFinder finder;
492         
493         public EntityAnd(ModelScreen modelScreen, Element JavaDoc entityAndElement) {
494             super (modelScreen, entityAndElement);
495             finder = new ByAndFinder(entityAndElement);
496         }
497         
498         public void runAction(Map JavaDoc context) {
499             try {
500                 finder.runFind(context, this.modelScreen.getDelegator(context));
501             } catch (GeneralException e) {
502                 String JavaDoc errMsg = "Error doing entity query by condition: " + e.toString();
503                 Debug.logError(e, errMsg, module);
504                 throw new IllegalArgumentException JavaDoc(errMsg);
505             }
506         }
507     }
508
509     public static class EntityCondition extends ModelScreenAction {
510         ByConditionFinder finder;
511         
512         public EntityCondition(ModelScreen modelScreen, Element JavaDoc entityConditionElement) {
513             super (modelScreen, entityConditionElement);
514             finder = new ByConditionFinder(entityConditionElement);
515         }
516         
517         public void runAction(Map JavaDoc context) {
518             try {
519                 finder.runFind(context, this.modelScreen.getDelegator(context));
520             } catch (GeneralException e) {
521                 String JavaDoc errMsg = "Error doing entity query by condition: " + e.toString();
522                 Debug.logError(e, errMsg, module);
523                 throw new IllegalArgumentException JavaDoc(errMsg);
524             }
525         }
526     }
527
528     public static class GetRelatedOne extends ModelScreenAction {
529         protected FlexibleMapAccessor valueNameAcsr;
530         protected FlexibleMapAccessor toValueNameAcsr;
531         protected String JavaDoc relationName;
532         protected boolean useCache;
533         
534         public GetRelatedOne(ModelScreen modelScreen, Element JavaDoc getRelatedOneElement) {
535             super (modelScreen, getRelatedOneElement);
536             this.valueNameAcsr = new FlexibleMapAccessor(getRelatedOneElement.getAttribute("value-name"));
537             this.toValueNameAcsr = new FlexibleMapAccessor(getRelatedOneElement.getAttribute("to-value-name"));
538             this.relationName = getRelatedOneElement.getAttribute("relation-name");
539             this.useCache = "true".equals(getRelatedOneElement.getAttribute("use-cache"));
540         }
541
542         public void runAction(Map JavaDoc context) {
543             Object JavaDoc valueObject = valueNameAcsr.get(context);
544             if (valueObject == null) {
545                 Debug.logVerbose("Value not found with name: " + valueNameAcsr + ", not getting related...", module);
546                 return;
547             }
548             if (!(valueObject instanceof GenericValue)) {
549                 String JavaDoc errMsg = "Env variable for value-name " + valueNameAcsr.toString() + " is not a GenericValue object; for the relation-name: " + relationName + "]";
550                 Debug.logError(errMsg, module);
551                 throw new IllegalArgumentException JavaDoc(errMsg);
552             }
553             GenericValue value = (GenericValue) valueObject;
554             try {
555                 if (useCache) {
556                     toValueNameAcsr.put(context, value.getRelatedOneCache(relationName));
557                 } else {
558                     toValueNameAcsr.put(context, value.getRelatedOne(relationName));
559                 }
560             } catch (GenericEntityException e) {
561                 String JavaDoc errMsg = "Problem getting related one from entity with name " + value.getEntityName() + " for the relation-name: " + relationName + ": " + e.getMessage();
562                 Debug.logError(e, errMsg, module);
563                 throw new IllegalArgumentException JavaDoc(errMsg);
564             }
565         }
566         
567     }
568
569     public static class GetRelated extends ModelScreenAction {
570         protected FlexibleMapAccessor valueNameAcsr;
571         protected FlexibleMapAccessor listNameAcsr;
572         protected FlexibleMapAccessor mapAcsr;
573         protected FlexibleMapAccessor orderByListAcsr;
574         protected String JavaDoc relationName;
575         protected boolean useCache;
576         
577         public GetRelated(ModelScreen modelScreen, Element JavaDoc getRelatedElement) {
578             super (modelScreen, getRelatedElement);
579             this.valueNameAcsr = new FlexibleMapAccessor(getRelatedElement.getAttribute("value-name"));
580             this.listNameAcsr = new FlexibleMapAccessor(getRelatedElement.getAttribute("list-name"));
581             this.relationName = getRelatedElement.getAttribute("relation-name");
582             this.mapAcsr = new FlexibleMapAccessor(getRelatedElement.getAttribute("map-name"));
583             this.orderByListAcsr = new FlexibleMapAccessor(getRelatedElement.getAttribute("order-by-list-name"));
584             this.useCache = "true".equals(getRelatedElement.getAttribute("use-cache"));
585         }
586
587         public void runAction(Map JavaDoc context) {
588             Object JavaDoc valueObject = valueNameAcsr.get(context);
589             if (valueObject == null) {
590                 Debug.logVerbose("Value not found with name: " + valueNameAcsr + ", not getting related...", module);
591                 return;
592             }
593             if (!(valueObject instanceof GenericValue)) {
594                 String JavaDoc errMsg = "Env variable for value-name " + valueNameAcsr.toString() + " is not a GenericValue object; for the relation-name: " + relationName + "]";
595                 Debug.logError(errMsg, module);
596                 throw new IllegalArgumentException JavaDoc(errMsg);
597             }
598             GenericValue value = (GenericValue) valueObject;
599             List JavaDoc orderByNames = null;
600             if (!orderByListAcsr.isEmpty()) {
601                 orderByNames = (List JavaDoc) orderByListAcsr.get(context);
602             }
603             Map JavaDoc constraintMap = null;
604             if (!mapAcsr.isEmpty()) {
605                 constraintMap = (Map JavaDoc) mapAcsr.get(context);
606             }
607             try {
608                 if (useCache) {
609                     listNameAcsr.put(context, value.getRelatedCache(relationName, constraintMap, orderByNames));
610                 } else {
611                     listNameAcsr.put(context, value.getRelated(relationName, constraintMap, orderByNames));
612                 }
613             } catch (GenericEntityException e) {
614                 String JavaDoc errMsg = "Problem getting related from entity with name " + value.getEntityName() + " for the relation-name: " + relationName + ": " + e.getMessage();
615                 Debug.logError(e, errMsg, module);
616                 throw new IllegalArgumentException JavaDoc(errMsg);
617             }
618         }
619     }
620
621 }
622
Popular Tags