KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > widget > form > ModelFormAction


1 /*
2  * $Id: ModelFormAction.java 6789 2006-02-20 21:22:34Z 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.form;
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 import java.util.regex.PatternSyntaxException JavaDoc;
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 JavaDoc;
52
53
54 /**
55  * Widget Library - Screen model class
56  *
57  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
58  * @version $Revision: 6789 $
59  * @since 3.1
60  */

61 public abstract class ModelFormAction {
62     public static final String JavaDoc module = ModelFormAction.class.getName();
63
64     protected ModelForm modelForm;
65
66     public ModelFormAction(ModelForm modelForm, Element JavaDoc 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 JavaDoc context);
72     
73     public static List JavaDoc readSubActions(ModelForm modelForm, Element JavaDoc parentElement) {
74         List JavaDoc actions = new LinkedList JavaDoc();
75         
76         List JavaDoc actionElementList = UtilXml.childElementList(parentElement);
77         Iterator JavaDoc actionElementIter = actionElementList.iterator();
78         while (actionElementIter.hasNext()) {
79             Element JavaDoc actionElement = (Element JavaDoc) 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 JavaDoc("Action element not supported with name: " + actionElement.getNodeName());
98             }
99         }
100         
101         return actions;
102     }
103     
104     public static void runSubActions(List JavaDoc actions, Map JavaDoc context) {
105         if (actions == null) return;
106         
107         Iterator JavaDoc 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 JavaDoc type;
121         
122         public SetField(ModelForm modelForm, Element JavaDoc 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 JavaDoc("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 JavaDoc context) {
135             String JavaDoc globalStr = this.globalExdr.expandString(context);
136             // default to false
137
boolean global = "true".equals(globalStr);
138             
139             Object JavaDoc 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 JavaDoc 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 JavaDoc(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 JavaDoc globalCtx = (Map JavaDoc) context.get("globalContext");
161                 if (globalCtx != null) {
162                     this.field.put(globalCtx, newValue);
163                 }
164             }
165             
166             // this is a hack for backward compatibility with the JPublish page object
167
Map JavaDoc page = (Map JavaDoc) 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 JavaDoc 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 JavaDoc context) {
187             String JavaDoc globalStr = this.globalExdr.expandString(context);
188             // default to false
189
boolean global = "true".equals(globalStr);
190
191             Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
192             String JavaDoc resource = this.resourceExdr.expandString(context, locale);
193             Map JavaDoc propertyMap = UtilProperties.getResourceBundleMap(resource, locale);
194             this.mapNameAcsr.put(context, propertyMap);
195
196             if (global) {
197                 Map JavaDoc globalCtx = (Map JavaDoc) 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 JavaDoc 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 JavaDoc context) {
227             //String globalStr = this.globalExdr.expandString(context);
228
// default to false
229
//boolean global = "true".equals(globalStr);
230

231             Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
232             String JavaDoc resource = this.resourceExdr.expandString(context, locale);
233             String JavaDoc property = this.propertyExdr.expandString(context, locale);
234             
235             String JavaDoc 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             // note that expanding the value string here will handle defaultValue and the string from
246
// the properties file; if we decide later that we don't want the string from the properties
247
// file to be expanded we should just expand the defaultValue at the beginning of this method.
248
value = FlexibleStringExpander.expandString(value, context);
249
250             if (!argListAcsr.isEmpty()) {
251                 List JavaDoc argList = (List JavaDoc) 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 JavaDoc location;
263         
264         public Script(ModelForm modelForm, Element JavaDoc scriptElement) {
265             super (modelForm, scriptElement);
266             this.location = scriptElement.getAttribute("location");
267         }
268         
269         public void runAction(Map JavaDoc context) {
270             if (location.endsWith(".bsh")) {
271                 try {
272                     BshUtil.runBshAtLocation(location, context);
273                 } catch (GeneralException e) {
274                     String JavaDoc errMsg = "Error running BSH script at location [" + location + "]: " + e.toString();
275                     Debug.logError(e, errMsg, module);
276                     throw new IllegalArgumentException JavaDoc(errMsg);
277                 }
278             } else {
279                 throw new IllegalArgumentException JavaDoc("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 JavaDoc fieldMap;
291         
292         public Service(ModelForm modelForm, Element JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc context) {
320             String JavaDoc serviceNameExpanded = this.serviceNameExdr.expandString(context);
321             if (UtilValidate.isEmpty(serviceNameExpanded)) {
322                 throw new IllegalArgumentException JavaDoc("Service name was empty, expanded from: " + this.serviceNameExdr.getOriginal());
323             }
324             
325             String JavaDoc autoFieldMapString = this.autoFieldMapExdr.expandString(context);
326             boolean autoFieldMapBool = !"false".equals(autoFieldMapString);
327             
328             try {
329                 Map JavaDoc serviceContext = null;
330                 if (autoFieldMapBool) {
331                     serviceContext = this.modelForm.getDispatcher(context).getDispatchContext().makeValidContext(serviceNameExpanded, ModelService.IN_PARAM, context);
332                 } else {
333                     serviceContext = new HashMap JavaDoc();
334                 }
335                 
336                 if (this.fieldMap != null) {
337                     EntityFinderUtil.expandFieldMapToContext(this.fieldMap, context, serviceContext);
338                 }
339                 
340                 Map JavaDoc result = this.modelForm.getDispatcher(context).runSync(serviceNameExpanded, serviceContext);
341                 
342                 if (this.resultMapNameAcsr != null) {
343                     this.resultMapNameAcsr.put(context, result);
344                     String JavaDoc queryString = (String JavaDoc)result.get("queryString");
345                     context.put("queryString", queryString);
346                     context.put("queryStringMap", result.get("queryStringMap"));
347                     if (UtilValidate.isNotEmpty(queryString)){
348                         try {
349                             String JavaDoc queryStringEncoded = queryString.replaceAll("&", "%26");
350                             context.put("queryStringEncoded", queryStringEncoded);
351                         } catch (PatternSyntaxException JavaDoc e) {
352                             
353                         }
354                     }
355                 } else {
356                     context.putAll(result);
357                 }
358                 String JavaDoc resultMapListIteratorName = resultMapListIteratorNameExdr.expandString(context);
359                 Object JavaDoc obj = result.get(resultMapListIteratorName);
360                 String JavaDoc formListIteratorName = modelForm.getListIteratorName();
361                 if (obj != null && obj instanceof EntityListIterator) {
362                     context.put("listIteratorName", formListIteratorName);
363                     context.put(formListIteratorName, obj);
364                 }
365                 String JavaDoc listName = resultMapListNameExdr.expandString(context);
366                 Object JavaDoc listObj = result.get(listName);
367                 if (listObj != null) {
368                     if (!(listObj instanceof List JavaDoc)) {
369                         throw new IllegalArgumentException JavaDoc("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 JavaDoc lst = (List JavaDoc) listObj;
372                     context.put("listName", listName);
373                     context.put(listName, lst);
374                 }
375             } catch (GenericServiceException e) {
376                 String JavaDoc errMsg = "Error in form [" + this.modelForm.getName() + "] calling service with name [" + serviceNameExpanded + "]: " + e.toString();
377                 Debug.logError(e, errMsg, module);
378                 throw new IllegalArgumentException JavaDoc(errMsg);
379             }
380         }
381     }
382
383     public static class EntityOne extends ModelFormAction {
384         protected PrimaryKeyFinder finder;
385         
386         public EntityOne(ModelForm modelForm, Element JavaDoc entityOneElement) {
387             super (modelForm, entityOneElement);
388             finder = new PrimaryKeyFinder(entityOneElement);
389         }
390         
391         public void runAction(Map JavaDoc context) {
392             try {
393                 finder.runFind(context, this.modelForm.getDelegator(context));
394             } catch (GeneralException e) {
395                 String JavaDoc errMsg = "Error doing entity query by condition: " + e.toString();
396                 Debug.logError(e, errMsg, module);
397                 throw new IllegalArgumentException JavaDoc(errMsg);
398             }
399         }
400     }
401
402     public static class EntityAnd extends ModelFormAction {
403         protected ByAndFinder finder;
404         String JavaDoc actualListName;
405         
406         public EntityAnd(ModelForm modelForm, Element JavaDoc entityAndElement) {
407             super (modelForm, entityAndElement);
408
409             //don't want to default to the iterator, should be specified explicitly, not the default
410
// Document ownerDoc = entityAndElement.getOwnerDocument();
411
// boolean useCache = "true".equalsIgnoreCase(entityAndElement.getAttribute("use-cache"));
412
// if (!useCache) UtilXml.addChildElement(entityAndElement, "use-iterator", ownerDoc);
413

414             // make list-name optional
415
if (UtilValidate.isEmpty(entityAndElement.getAttribute("list-name"))) {
416                 String JavaDoc 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 JavaDoc context) {
430             try {
431                 // don't want to do this: context.put("defaultFormResultList", null);
432
finder.runFind(context, this.modelForm.getDelegator(context));
433                 Object JavaDoc obj = context.get(this.actualListName);
434
435                 if (obj != null && (obj instanceof EntityListIterator)) {
436                     String JavaDoc modelFormIteratorName = modelForm.getListIteratorName();
437                     context.put(modelFormIteratorName, obj);
438                 } else if (obj != null && obj instanceof List JavaDoc) {
439                     String JavaDoc modelFormListName = modelForm.getListName();
440                     context.put(modelFormListName, obj);
441                 }
442             } catch (GeneralException e) {
443                 String JavaDoc errMsg = "Error doing entity query by condition: " + e.toString();
444                 Debug.logError(e, errMsg, module);
445                 throw new IllegalArgumentException JavaDoc(errMsg);
446             }
447         }
448         
449     }
450
451     public static class EntityCondition extends ModelFormAction {
452         ByConditionFinder finder;
453         String JavaDoc actualListName;
454         
455         public EntityCondition(ModelForm modelForm, Element JavaDoc entityConditionElement) {
456             super (modelForm, entityConditionElement);
457             
458             //don't want to default to the iterator, should be specified explicitly, not the default
459
// Document ownerDoc = entityConditionElement.getOwnerDocument();
460
// boolean useCache = "true".equalsIgnoreCase(entityConditionElement.getAttribute("use-cache"));
461
// if (!useCache) UtilXml.addChildElement(entityConditionElement, "use-iterator", ownerDoc);
462

463             // make list-name optional
464
if (UtilValidate.isEmpty(entityConditionElement.getAttribute("list-name"))) {
465                 String JavaDoc 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 JavaDoc context) {
479             try {
480                 // don't want to do this: context.put("defaultFormResultList", null);
481
finder.runFind(context, this.modelForm.getDelegator(context));
482                 Object JavaDoc obj = context.get(this.actualListName);
483                 if (obj != null && (obj instanceof EntityListIterator)) {
484                     String JavaDoc modelFormIteratorName = modelForm.getListIteratorName();
485                     context.put(modelFormIteratorName, obj);
486                 } else if (obj != null && obj instanceof List JavaDoc) {
487                     String JavaDoc modelFormListName = modelForm.getListName();
488                     context.put(modelFormListName, obj);
489                 }
490             } catch (GeneralException e) {
491                 String JavaDoc errMsg = "Error doing entity query by condition: " + e.toString();
492                 Debug.logError(e, errMsg, module);
493                 throw new IllegalArgumentException JavaDoc(errMsg);
494             }
495         }
496     }
497 }
498
499
Popular Tags