KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > widget > menu > ModelMenuAction


1 /*
2  * $Id: ModelMenuAction.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 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.menu;
25
26 import java.text.MessageFormat JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Locale JavaDoc;
32 import java.util.Map JavaDoc;
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 JavaDoc;
51 import javax.servlet.*;
52 import javax.servlet.http.*;
53
54
55 /**
56  * Widget Library - Screen model class
57  *
58  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
59  * @version $Rev: 5462 $
60  * @since 3.1
61  */

62 public abstract class ModelMenuAction {
63     public static final String JavaDoc module = ModelMenuAction.class.getName();
64
65     protected ModelMenu modelMenu;
66     protected ModelMenuItem modelMenuItem;
67
68     public ModelMenuAction(ModelMenu modelMenu, Element JavaDoc 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 JavaDoc 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 JavaDoc context);
80     
81     public static List JavaDoc readSubActions(ModelMenuItem modelMenuItem, Element JavaDoc parentElement) {
82         return readSubActions(modelMenuItem.getModelMenu(), parentElement);
83     }
84     
85     public static List JavaDoc readSubActions(ModelMenu modelMenu, Element JavaDoc parentElement) {
86         List JavaDoc actions = new LinkedList JavaDoc();
87         
88         List JavaDoc actionElementList = UtilXml.childElementList(parentElement);
89         Iterator JavaDoc actionElementIter = actionElementList.iterator();
90         while (actionElementIter.hasNext()) {
91             Element JavaDoc actionElement = (Element JavaDoc) 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 JavaDoc("Action element not supported with name: " + actionElement.getNodeName());
110             }
111         }
112         
113         return actions;
114     }
115     
116     public static void runSubActions(List JavaDoc actions, Map JavaDoc context) {
117         if (actions == null) return;
118         
119         Iterator JavaDoc 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 JavaDoc type;
134         protected String JavaDoc toScope;
135         protected String JavaDoc fromScope;
136         
137         public SetField(ModelMenu modelMenu, Element JavaDoc 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 JavaDoc("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 JavaDoc context) {
153             String JavaDoc globalStr = this.globalExdr.expandString(context);
154             // default to false
155
boolean global = "true".equals(globalStr);
156             
157             Object JavaDoc newValue = null;
158             if (this.fromScope != null && this.fromScope.equals("user")) {
159                 if (this.fromField != null) {
160                     String JavaDoc originalName = this.fromField.getOriginalName();
161                     String JavaDoc currentWidgetTrail = (String JavaDoc)context.get("_WIDGETTRAIL_");
162                     String JavaDoc 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 JavaDoc originalName = this.fromField.getOriginalName();
173                     String JavaDoc currentWidgetTrail = (String JavaDoc)context.get("_WIDGETTRAIL_");
174                     String JavaDoc 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 newValue is still empty, use the default value
192
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 JavaDoc 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 JavaDoc(errMsg);
205                 }
206          
207             }
208             if (this.toScope != null && this.toScope.equals("user")) {
209                     String JavaDoc originalName = this.field.getOriginalName();
210                     String JavaDoc currentWidgetTrail = (String JavaDoc)context.get("_WIDGETTRAIL_");
211                     String JavaDoc 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 JavaDoc originalName = this.field.getOriginalName();
218                     String JavaDoc currentWidgetTrail = (String JavaDoc)context.get("_WIDGETTRAIL_");
219                     String JavaDoc 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 JavaDoc globalCtx = (Map JavaDoc) context.get("globalContext");
231                 if (globalCtx != null) {
232                     this.field.put(globalCtx, newValue);
233                 }
234             }
235             
236             // this is a hack for backward compatibility with the JPublish page object
237
Map JavaDoc page = (Map JavaDoc) 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 JavaDoc 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 JavaDoc context) {
257             String JavaDoc globalStr = this.globalExdr.expandString(context);
258             // default to false
259
boolean global = "true".equals(globalStr);
260
261             Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
262             String JavaDoc resource = this.resourceExdr.expandString(context, locale);
263             Map JavaDoc propertyMap = UtilProperties.getResourceBundleMap(resource, locale);
264             this.mapNameAcsr.put(context, propertyMap);
265
266             if (global) {
267                 Map JavaDoc globalCtx = (Map JavaDoc) 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 JavaDoc 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 JavaDoc context) {
297             String JavaDoc globalStr = this.globalExdr.expandString(context);
298             // default to false
299
boolean global = "true".equals(globalStr);
300
301             Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
302             String JavaDoc resource = this.resourceExdr.expandString(context, locale);
303             String JavaDoc property = this.propertyExdr.expandString(context, locale);
304             
305             String JavaDoc 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             // note that expanding the value string here will handle defaultValue and the string from
316
// the properties file; if we decide later that we don't want the string from the properties
317
// file to be expanded we should just expand the defaultValue at the beginning of this method.
318
value = FlexibleStringExpander.expandString(value, context);
319
320             if (!argListAcsr.isEmpty()) {
321                 List JavaDoc argList = (List JavaDoc) 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 JavaDoc location;
333         
334         public Script(ModelMenu modelMenu, Element JavaDoc scriptElement) {
335             super (modelMenu, scriptElement);
336             this.location = scriptElement.getAttribute("location");
337         }
338         
339         public void runAction(Map JavaDoc context) {
340             if (location.endsWith(".bsh")) {
341                 try {
342                     BshUtil.runBshAtLocation(location, context);
343                 } catch (GeneralException e) {
344                     String JavaDoc errMsg = "Error running BSH script at location [" + location + "]: " + e.toString();
345                     Debug.logError(e, errMsg, module);
346                     throw new IllegalArgumentException JavaDoc(errMsg);
347                 }
348             } else {
349                 throw new IllegalArgumentException JavaDoc("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 JavaDoc fieldMap;
359         
360         public Service(ModelMenu modelMenu, Element JavaDoc 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 JavaDoc fieldMapElementList = UtilXml.childElementList(serviceElement, "field-map");
367             if (fieldMapElementList.size() > 0) {
368                 this.fieldMap = new HashMap JavaDoc();
369                 Iterator JavaDoc fieldMapElementIter = fieldMapElementList.iterator();
370                 while (fieldMapElementIter.hasNext()) {
371                     Element JavaDoc fieldMapElement = (Element JavaDoc) fieldMapElementIter.next();
372                     // set the env-name for each field-name, noting that if no field-name is specified it defaults to the env-name
373
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 JavaDoc context) {
381             String JavaDoc serviceNameExpanded = this.serviceNameExdr.expandString(context);
382             if (UtilValidate.isEmpty(serviceNameExpanded)) {
383                 throw new IllegalArgumentException JavaDoc("Service name was empty, expanded from: " + this.serviceNameExdr.getOriginal());
384             }
385             
386             String JavaDoc autoFieldMapString = this.autoFieldMapExdr.expandString(context);
387             boolean autoFieldMapBool = !"false".equals(autoFieldMapString);
388             
389             try {
390                 Map JavaDoc serviceContext = null;
391                 if (autoFieldMapBool) {
392                     serviceContext = this.modelMenu.getDispacher().getDispatchContext().makeValidContext(serviceNameExpanded, ModelService.IN_PARAM, context);
393                 } else {
394                     serviceContext = new HashMap JavaDoc();
395                 }
396                 
397                 if (this.fieldMap != null) {
398                     Iterator JavaDoc fieldMapEntryIter = this.fieldMap.entrySet().iterator();
399                     while (fieldMapEntryIter.hasNext()) {
400                         Map.Entry JavaDoc entry = (Map.Entry JavaDoc) 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 JavaDoc 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 JavaDoc errMsg = "Error calling service with name " + serviceNameExpanded + ": " + e.toString();
416                 Debug.logError(e, errMsg, module);
417                 throw new IllegalArgumentException JavaDoc(errMsg);
418             }
419         }
420     }
421
422     public static class EntityOne extends ModelMenuAction {
423         protected PrimaryKeyFinder finder;
424         
425         public EntityOne(ModelMenu modelMenu, Element JavaDoc entityOneElement) {
426             super (modelMenu, entityOneElement);
427             finder = new PrimaryKeyFinder(entityOneElement);
428         }
429         
430         public void runAction(Map JavaDoc context) {
431             try {
432                 finder.runFind(context, this.modelMenu.getDelegator());
433             } catch (GeneralException e) {
434                 String JavaDoc errMsg = "Error doing entity query by condition: " + e.toString();
435                 Debug.logError(e, errMsg, module);
436                 throw new IllegalArgumentException JavaDoc(errMsg);
437             }
438         }
439     }
440
441     public static class EntityAnd extends ModelMenuAction {
442         protected ByAndFinder finder;
443         
444         public EntityAnd(ModelMenu modelMenu, Element JavaDoc entityAndElement) {
445             super (modelMenu, entityAndElement);
446             finder = new ByAndFinder(entityAndElement);
447         }
448         
449         public void runAction(Map JavaDoc context) {
450             try {
451                 finder.runFind(context, this.modelMenu.getDelegator());
452             } catch (GeneralException e) {
453                 String JavaDoc errMsg = "Error doing entity query by condition: " + e.toString();
454                 Debug.logError(e, errMsg, module);
455                 throw new IllegalArgumentException JavaDoc(errMsg);
456             }
457         }
458     }
459
460     public static class EntityCondition extends ModelMenuAction {
461         ByConditionFinder finder;
462         
463         public EntityCondition(ModelMenu modelMenu, Element JavaDoc entityConditionElement) {
464             super (modelMenu, entityConditionElement);
465             finder = new ByConditionFinder(entityConditionElement);
466         }
467         
468         public void runAction(Map JavaDoc context) {
469             try {
470                 finder.runFind(context, this.modelMenu.getDelegator());
471             } catch (GeneralException e) {
472                 String JavaDoc errMsg = "Error doing entity query by condition: " + e.toString();
473                 Debug.logError(e, errMsg, module);
474                 throw new IllegalArgumentException JavaDoc(errMsg);
475             }
476         }
477     }
478 }
479
480
Popular Tags