KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > widget > tree > ModelTreeAction


1 /*
2  * $Id: ModelTreeAction.java 5660 2005-09-07 19:53:13Z 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.tree;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.ListIterator JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.regex.PatternSyntaxException JavaDoc;
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 JavaDoc;
51 import org.w3c.dom.Element JavaDoc;
52
53 /**
54  * Widget Library - Tree model class
55  *
56  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
57  * @version $Rev: 5660 $
58  * @since 3.1
59  */

60 public abstract class ModelTreeAction {
61     public static final String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc context);
81     
82 /*
83     public static List readSubActions(ModelTree.ModelNode modelNode, Element parentElement) {
84         List actions = new LinkedList();
85         
86         List actionElementList = UtilXml.childElementList(parentElement);
87         Iterator actionElementIter = actionElementList.iterator();
88         while (actionElementIter.hasNext()) {
89             Element actionElement = (Element) actionElementIter.next();
90             if ("set".equals(actionElement.getNodeName())) {
91                 actions.add(new SetField(modelTree, actionElement));
92             } else if ("script".equals(actionElement.getNodeName())) {
93                 actions.add(new Script(modelTree, actionElement));
94             } else if ("service".equals(actionElement.getNodeName())) {
95                 actions.add(new Service(modelTree, actionElement));
96             } else if ("entity-one".equals(actionElement.getNodeName())) {
97                 actions.add(new EntityOne(modelTree, actionElement));
98             } else if ("entity-and".equals(actionElement.getNodeName())) {
99                 actions.add(new EntityAnd(modelTree, actionElement));
100             } else if ("entity-condition".equals(actionElement.getNodeName())) {
101                 actions.add(new EntityCondition(modelTree, actionElement));
102             } else {
103                 throw new IllegalArgumentException("Action element not supported with name: " + actionElement.getNodeName());
104             }
105         }
106         
107         return actions;
108     }
109     */

110     
111     public static void runSubActions(List JavaDoc actions, Map JavaDoc context) {
112         Iterator JavaDoc 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 JavaDoc type;
126         
127         public SetField(ModelTree.ModelNode modelNode, Element JavaDoc 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 JavaDoc("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 JavaDoc context) {
140             String JavaDoc globalStr = this.globalExdr.expandString(context);
141             // default to false
142
boolean global = "true".equals(globalStr);
143             
144             Object JavaDoc 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 JavaDoc 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 JavaDoc(errMsg);
157                 }
158          
159             }
160             this.field.put(context, newValue);
161             
162             if (global) {
163                 Map JavaDoc globalCtx = (Map JavaDoc) context.get("globalContext");
164                 if (globalCtx != null) {
165                     this.field.put(globalCtx, newValue);
166                 }
167             }
168             
169             // this is a hack for backward compatibility with the JPublish page object
170
Map JavaDoc page = (Map JavaDoc) 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 JavaDoc location;
179         
180         public Script(ModelTree.ModelNode modelNode, Element JavaDoc scriptElement) {
181             super (modelNode, scriptElement);
182             this.location = scriptElement.getAttribute("location");
183         }
184         
185         public Script(ModelTree.ModelNode.ModelSubNode modelSubNode, Element JavaDoc scriptElement) {
186             super (modelSubNode, scriptElement);
187             this.location = scriptElement.getAttribute("location");
188         }
189         
190         public void runAction(Map JavaDoc context) {
191             if (location.endsWith(".bsh")) {
192                 try {
193                     context.put("_LIST_ITERATOR_", null);
194                     BshUtil.runBshAtLocation(location, context);
195                     Object JavaDoc obj = context.get("_LIST_ITERATOR_");
196                     if (this.modelSubNode != null) {
197                         if (obj != null && (obj instanceof EntityListIterator || obj instanceof ListIterator JavaDoc)) {
198                             this.modelSubNode.setListIterator((ListIterator JavaDoc)obj);
199                         } else {
200                             if (obj instanceof List JavaDoc)
201                                 this.modelSubNode.setListIterator(((List JavaDoc)obj).listIterator());
202                         }
203                     }
204                 } catch (GeneralException e) {
205                     String JavaDoc errMsg = "Error running BSH script at location [" + location + "]: " + e.toString();
206                     Debug.logError(e, errMsg, module);
207                     throw new IllegalArgumentException JavaDoc(errMsg);
208                 }
209             } else {
210                 throw new IllegalArgumentException JavaDoc("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 JavaDoc fieldMap;
224         
225         public Service(ModelTree.ModelNode modelNode, Element JavaDoc serviceElement) {
226             super (modelNode, serviceElement);
227             initService(serviceElement);
228         }
229         
230         public Service(ModelTree.ModelNode.ModelSubNode modelSubNode, Element JavaDoc serviceElement) {
231             super (modelSubNode, serviceElement);
232             initService(serviceElement);
233         }
234         
235         public void initService( Element JavaDoc 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 JavaDoc context) {
248             String JavaDoc serviceNameExpanded = this.serviceNameExdr.expandString(context);
249             if (UtilValidate.isEmpty(serviceNameExpanded)) {
250                 throw new IllegalArgumentException JavaDoc("Service name was empty, expanded from: " + this.serviceNameExdr.getOriginal());
251             }
252             
253             String JavaDoc autoFieldMapString = this.autoFieldMapExdr.expandString(context);
254             boolean autoFieldMapBool = !"false".equals(autoFieldMapString);
255             
256             try {
257                 Map JavaDoc serviceContext = null;
258                 if (autoFieldMapBool) {
259                     serviceContext = this.modelTree.getDispatcher().getDispatchContext().makeValidContext(serviceNameExpanded, ModelService.IN_PARAM, context);
260                 } else {
261                     serviceContext = new HashMap JavaDoc();
262                 }
263                 
264                 if (this.fieldMap != null) {
265                     EntityFinderUtil.expandFieldMapToContext(this.fieldMap, context, serviceContext);
266                 }
267                 
268                 Map JavaDoc result = this.modelTree.getDispatcher().runSync(serviceNameExpanded, serviceContext);
269                 
270                 if (this.resultMapNameAcsr != null) {
271                     this.resultMapNameAcsr.put(context, result);
272                     String JavaDoc queryString = (String JavaDoc)result.get("queryString");
273                     context.put("queryString", queryString);
274                     context.put("queryStringMap", result.get("queryStringMap"));
275                     if (UtilValidate.isNotEmpty(queryString)){
276                         try {
277                             String JavaDoc queryStringEncoded = queryString.replaceAll("&", "%26");
278                             context.put("queryStringEncoded", queryStringEncoded);
279                         } catch (PatternSyntaxException JavaDoc e) {
280                             
281                         }
282                     }
283                 } else {
284                     context.putAll(result);
285                 }
286                 String JavaDoc resultMapListName = resultMapListNameExdr.expandString(context);
287                 String JavaDoc resultMapListIteratorName = resultMapListIteratorNameExdr.expandString(context);
288                 String JavaDoc resultMapValueName = resultMapValueNameExdr.expandString(context);
289                 String JavaDoc valueName = valueNameExdr.expandString(context);
290                 
291                 if (this.modelSubNode != null) {
292                     //ListIterator iter = null;
293
if (UtilValidate.isNotEmpty(resultMapListIteratorName)) {
294                         this.modelSubNode.setListIterator((ListIterator JavaDoc)result.get(resultMapListIteratorName));
295                     } else if (UtilValidate.isNotEmpty(resultMapListName)) {
296                         List JavaDoc lst = (List JavaDoc)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 JavaDoc)result.get(resultMapValueName));
307                         }
308                     }
309                 }
310             } catch (GenericServiceException e) {
311                 String JavaDoc errMsg = "Error calling service with name " + serviceNameExpanded + ": " + e.toString();
312                 Debug.logError(e, errMsg, module);
313                 throw new IllegalArgumentException JavaDoc(errMsg);
314             }
315         }
316     }
317
318     public static class EntityOne extends ModelTreeAction {
319         protected PrimaryKeyFinder finder;
320         String JavaDoc valueName;
321         
322         public EntityOne(ModelTree.ModelNode modelNode, Element JavaDoc 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 JavaDoc context) {
330             try {
331                 finder.runFind(context, this.modelTree.getDelegator());
332             } catch (GeneralException e) {
333                 String JavaDoc errMsg = "Error doing entity query by condition: " + e.toString();
334                 Debug.logError(e, errMsg, module);
335                 throw new IllegalArgumentException JavaDoc(errMsg);
336             }
337         }
338     }
339
340     public static class EntityAnd extends ModelTreeAction {
341         protected ByAndFinder finder;
342         String JavaDoc listName;
343         
344         public EntityAnd(ModelTree.ModelNode.ModelSubNode modelSubNode, Element JavaDoc entityAndElement) {
345             super (modelSubNode, entityAndElement);
346             boolean useCache = "true".equalsIgnoreCase(entityAndElement.getAttribute("use-cache"));
347             Document JavaDoc 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 JavaDoc context) {
356             try {
357                 context.put(this.listName, null);
358                 finder.runFind(context, this.modelTree.getDelegator());
359                 Object JavaDoc obj = context.get(this.listName);
360                 if (obj != null && (obj instanceof EntityListIterator || obj instanceof ListIterator JavaDoc)) {
361                     this.modelSubNode.setListIterator((ListIterator JavaDoc)obj);
362                 } else {
363                     if (obj instanceof List JavaDoc) this.modelSubNode.setListIterator(((List JavaDoc)obj).listIterator());
364                 }
365             } catch (GeneralException e) {
366                 String JavaDoc errMsg = "Error doing entity query by condition: " + e.toString();
367                 Debug.logError(e, errMsg, module);
368                 throw new IllegalArgumentException JavaDoc(errMsg);
369             }
370         }
371     }
372
373     public static class EntityCondition extends ModelTreeAction {
374         ByConditionFinder finder;
375         String JavaDoc listName;
376         
377         public EntityCondition(ModelTree.ModelNode.ModelSubNode modelSubNode, Element JavaDoc entityConditionElement) {
378             super (modelSubNode, entityConditionElement);
379             Document JavaDoc 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 JavaDoc context) {
390             try {
391                 context.put(this.listName, null);
392                 finder.runFind(context, this.modelTree.getDelegator());
393                 Object JavaDoc obj = context.get(this.listName);
394                 if (obj != null && (obj instanceof EntityListIterator || obj instanceof ListIterator JavaDoc)) {
395                     this.modelSubNode.setListIterator((ListIterator JavaDoc)obj);
396                 } else {
397                     if (obj instanceof List JavaDoc) this.modelSubNode.setListIterator(((List JavaDoc)obj).listIterator());
398                 }
399             } catch (GeneralException e) {
400                 String JavaDoc errMsg = "Error doing entity query by condition: " + e.toString();
401                 Debug.logError(e, errMsg, module);
402                 throw new IllegalArgumentException JavaDoc(errMsg);
403             }
404         }
405     }
406 }
407
Popular Tags