KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ModelTree.java 6605 2006-01-29 04:17:04Z 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.io.IOException JavaDoc;
27 import java.io.StringWriter JavaDoc;
28 import java.io.Writer JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.ListIterator JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.xml.parsers.ParserConfigurationException JavaDoc;
38
39 import org.ofbiz.base.util.Debug;
40 import org.ofbiz.base.util.GeneralException;
41 import org.ofbiz.base.util.StringUtil;
42 import org.ofbiz.base.util.UtilFormatOut;
43 import org.ofbiz.base.util.UtilMisc;
44 import org.ofbiz.base.util.UtilValidate;
45 import org.ofbiz.base.util.UtilXml;
46 import org.ofbiz.base.util.collections.MapStack;
47 import org.ofbiz.base.util.string.FlexibleStringExpander;
48 import org.ofbiz.widget.screen.ModelScreen;
49 import org.ofbiz.widget.screen.ScreenFactory;
50 import org.ofbiz.widget.screen.ScreenStringRenderer;
51 import org.ofbiz.entity.GenericDelegator;
52 import org.ofbiz.entity.GenericEntityException;
53 import org.ofbiz.entity.GenericValue;
54 import org.ofbiz.entity.model.ModelEntity;
55 import org.ofbiz.entity.model.ModelField;
56 import org.ofbiz.entity.util.EntityListIterator;
57 import org.ofbiz.service.LocalDispatcher;
58 import org.w3c.dom.Element JavaDoc;
59 import org.xml.sax.SAXException JavaDoc;
60
61 //import com.clarkware.profiler.Profiler;
62

63 /**
64  * Widget Library - Tree model class
65  *
66  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
67  * @version $Rev: 6605 $
68  * @since 3.1
69  */

70 public class ModelTree {
71
72     public static final String JavaDoc module = ModelTree.class.getName();
73
74     protected String JavaDoc name;
75     protected String JavaDoc rootNodeName;
76     protected String JavaDoc defaultRenderStyle;
77     protected FlexibleStringExpander defaultWrapStyleExdr;
78     protected List JavaDoc nodeList = new ArrayList JavaDoc();
79     protected Map JavaDoc nodeMap = new HashMap JavaDoc();
80     protected GenericDelegator delegator;
81     protected LocalDispatcher dispatcher;
82     protected FlexibleStringExpander expandCollapseRequestExdr;
83     protected FlexibleStringExpander trailNameExdr;
84     protected List JavaDoc trail = new ArrayList JavaDoc();
85     protected List JavaDoc currentNodeTrail;
86     protected int openDepth;
87     protected int postTrailOpenDepth;
88     protected int [] nodeIndices = new int[20];
89     protected String JavaDoc defaultEntityName;
90     protected String JavaDoc defaultPkName;
91     protected boolean forceChildCheck;
92     
93 // ===== CONSTRUCTORS =====
94
/** Default Constructor */
95
96     /** XML Constructor */
97     public ModelTree() {}
98     
99     public ModelTree(Element JavaDoc treeElement, GenericDelegator delegator, LocalDispatcher dispatcher) {
100
101         this.name = treeElement.getAttribute("name");
102         this.rootNodeName = treeElement.getAttribute("root-node-name");
103         this.defaultRenderStyle = UtilFormatOut.checkEmpty(treeElement.getAttribute("default-render-style"), "simple");
104         // A temporary hack to accommodate those who might still be using "render-style" instead of "default-render-style"
105
if (UtilValidate.isEmpty(this.defaultRenderStyle) || this.defaultRenderStyle.equals("simple")) {
106             String JavaDoc rStyle = treeElement.getAttribute("render-style");
107             if (UtilValidate.isNotEmpty(rStyle))
108                 this.defaultRenderStyle = rStyle;
109         }
110         this.defaultWrapStyleExdr = new FlexibleStringExpander(treeElement.getAttribute("default-wrap-style"));
111         this.expandCollapseRequestExdr = new FlexibleStringExpander(treeElement.getAttribute("expand-collapse-request"));
112         this.trailNameExdr = new FlexibleStringExpander(UtilFormatOut.checkEmpty(treeElement.getAttribute("trail-name"), "trail"));
113         this.delegator = delegator;
114         this.dispatcher = dispatcher;
115         this.forceChildCheck = !"false".equals(treeElement.getAttribute("force-child-check"));
116         setDefaultEntityName(treeElement.getAttribute("entity-name"));
117         try {
118             openDepth = Integer.parseInt(treeElement.getAttribute("open-depth"));
119         } catch(NumberFormatException JavaDoc e) {
120             openDepth = 0;
121         }
122
123         try {
124             postTrailOpenDepth = Integer.parseInt(treeElement.getAttribute("post-trail-open-depth"));
125         } catch(NumberFormatException JavaDoc e) {
126             postTrailOpenDepth = 999;
127         }
128
129         List JavaDoc nodeElements = UtilXml.childElementList(treeElement, "node");
130         Iterator JavaDoc nodeElementIter = nodeElements.iterator();
131         while (nodeElementIter.hasNext()) {
132             Element JavaDoc nodeElementEntry = (Element JavaDoc) nodeElementIter.next();
133             ModelNode node = new ModelNode(nodeElementEntry, this);
134             String JavaDoc nodeName = node.getName();
135             nodeList.add(node);
136             nodeMap.put(nodeName,node);
137         }
138     
139         if (nodeList.size() == 0) {
140             throw new IllegalArgumentException JavaDoc("No node elements found for the tree definition with name: " + this.name);
141         }
142
143     }
144     
145     public String JavaDoc getName() {
146         return name;
147     }
148
149     public void setDefaultEntityName(String JavaDoc name) {
150         String JavaDoc nm = name;
151         if (UtilValidate.isEmpty(nm)) {
152             nm = "Content";
153         }
154         this.defaultEntityName = nm;
155         ModelEntity modelEntity = delegator.getModelEntity(this.defaultEntityName);
156         if (modelEntity.getPksSize() == 1) {
157             ModelField modelField = modelEntity.getOnlyPk();
158             this.defaultPkName = modelField.getName();
159         }
160     }
161     
162     public String JavaDoc getDefaultEntityName() {
163         return this.defaultEntityName;
164     }
165     
166     public String JavaDoc getDefaultPkName() {
167         return this.defaultPkName;
168     }
169     
170     public String JavaDoc getRootNodeName() {
171         return rootNodeName;
172     }
173
174     public String JavaDoc getWrapStyle(Map JavaDoc context) {
175         return this.defaultWrapStyleExdr.expandString(context);
176     }
177     
178     public int getOpenDepth() {
179         return openDepth;
180     }
181     
182     public int getPostTrailOpenDepth() {
183         return postTrailOpenDepth;
184     }
185     
186     public int getNodeIndexAtDepth(int i) {
187         return nodeIndices[i];
188     }
189     
190     public void setNodeIndexAtDepth(int i, int val) {
191         nodeIndices[i] = val;
192     }
193     
194     public String JavaDoc getExpandCollapseRequest(Map JavaDoc context) {
195         String JavaDoc expColReq = this.expandCollapseRequestExdr.expandString(context);
196         if (UtilValidate.isEmpty(expColReq)) {
197             HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)context.get("request");
198             String JavaDoc s1 = request.getRequestURI();
199             int pos = s1.lastIndexOf("/");
200             if (pos >= 0)
201                 expColReq = s1.substring(pos + 1);
202             else
203                 expColReq = s1;
204         }
205         return expColReq;
206     }
207     
208     public String JavaDoc getTrailName(Map JavaDoc context) {
209         return this.trailNameExdr.expandString(context);
210     }
211     
212     public List JavaDoc getTrailList() {
213         return trail;
214     }
215     
216     public void setTrailList(List JavaDoc trailList) {
217         this.trail = trailList;
218     }
219     
220     public List JavaDoc getCurrentNodeTrail() {
221         return currentNodeTrail;
222     }
223     
224
225     /**
226      * Renders this tree to a String, i.e. in a text format, as defined with the
227      * TreeStringRenderer implementation.
228      *
229      * @param writer The Writer that the tree text will be written to
230      * @param context Map containing the tree context; the following are
231      * reserved words in this context: parameters (Map), isError (Boolean),
232      * itemIndex (Integer, for lists only, otherwise null), bshInterpreter,
233      * treeName (String, optional alternate name for tree, defaults to the
234      * value of the name attribute)
235      * @param treeStringRenderer An implementation of the TreeStringRenderer
236      * interface that is responsible for the actual text generation for
237      * different tree elements; implementing your own makes it possible to
238      * use the same tree definitions for many types of tree UIs
239      */

240     public void renderTreeString(StringBuffer JavaDoc buf, Map JavaDoc context, TreeStringRenderer treeStringRenderer) throws GeneralException {
241         ModelNode node = (ModelNode)nodeMap.get(rootNodeName);
242         /*
243         List parentNodeTrail = (List)context.get("currentNodeTrail");
244         if (parentNodeTrail != null)
245             currentNodeTrail = new ArrayList(parentNodeTrail);
246         else
247         */

248             currentNodeTrail = new ArrayList JavaDoc();
249             
250         //Map requestParameters = (Map)context.get("requestParameters");
251
//String treeString = (String)requestParameters.get("trail");
252
String JavaDoc trailName = trailNameExdr.expandString(context);
253         String JavaDoc treeString = (String JavaDoc)context.get(trailName);
254         if (UtilValidate.isEmpty(treeString)) {
255             Map JavaDoc parameters = (Map JavaDoc)context.get("parameters");
256             treeString = (String JavaDoc)parameters.get(trailName);
257         }
258         if (UtilValidate.isNotEmpty(treeString)) {
259             trail = StringUtil.split(treeString, "|");
260             if (trail == null || trail.size() == 0)
261                 throw new RuntimeException JavaDoc("Tree 'trail' value is empty.");
262             
263             context.put("rootEntityId", trail.get(0));
264             context.put(defaultPkName, trail.get(0));
265             context.put("targetNodeTrail", trail);
266         } else {
267                 Debug.logError("Trail value is empty.", module);
268         }
269         StringWriter JavaDoc writer = new StringWriter JavaDoc();
270         try {
271             node.renderNodeString(writer, context, treeStringRenderer, 0, true);
272             buf.append(writer.toString());
273         } catch (IOException JavaDoc e2) {
274                 String JavaDoc errMsg = "Error rendering included label with name [" + name + "] : " + e2.toString();
275                 Debug.logError(e2, errMsg, module);
276                 throw new RuntimeException JavaDoc(errMsg);
277         }
278 // try {
279
// FileOutputStream fw = new FileOutputStream(new File("/usr/local/agi/ofbiz/hot-deploy/ofbizdoc/misc/profile.data"));
280
// Profiler.print(fw);
281
// fw.close();
282
// } catch (IOException e) {
283
// Debug.logError("[PROFILER] " + e.getMessage(),"");
284
// }
285

286
287     }
288
289     public LocalDispatcher getDispatcher() {
290         return this.dispatcher;
291     }
292
293     public GenericDelegator getDelegator() {
294         return this.delegator;
295     }
296
297     public String JavaDoc getRenderStyle() {
298         return this.defaultRenderStyle;
299     }
300     
301
302     public static class ModelNode {
303
304         protected FlexibleStringExpander screenNameExdr;
305         protected FlexibleStringExpander screenLocationExdr;
306         protected String JavaDoc shareScope;
307         protected Label label;
308         protected Link link;
309         protected Image image;
310         protected List JavaDoc subNodeList = new ArrayList JavaDoc();
311         protected List JavaDoc actions = new ArrayList JavaDoc();
312         protected String JavaDoc name;
313         protected ModelTree modelTree;
314         protected List JavaDoc subNodeValues;
315         protected String JavaDoc expandCollapseStyle;
316         protected FlexibleStringExpander wrapStyleExdr;
317         protected ModelTreeCondition condition;
318         protected String JavaDoc renderStyle;
319         protected String JavaDoc entryName;
320         protected String JavaDoc entityName;
321         protected String JavaDoc pkName;
322
323         public ModelNode() {}
324
325         public ModelNode(Element JavaDoc nodeElement, ModelTree modelTree) {
326
327             this.modelTree = modelTree;
328             this.name = nodeElement.getAttribute("name");
329             this.expandCollapseStyle = nodeElement.getAttribute("expand-collapse-style");
330             this.wrapStyleExdr = new FlexibleStringExpander(nodeElement.getAttribute("wrap-style"));
331             this.renderStyle = nodeElement.getAttribute("render-style");
332             this.entryName = UtilFormatOut.checkEmpty(nodeElement.getAttribute("entry-name"), null);
333             setEntityName(nodeElement.getAttribute("entity-name"));
334             if (this.pkName == null || nodeElement.hasAttribute("join-field-name"))
335                 this.pkName = nodeElement.getAttribute("join-field-name");
336     
337             Element JavaDoc actionElement = UtilXml.firstChildElement(nodeElement, "entity-one");
338             if (actionElement != null) {
339                actions.add(new ModelTreeAction.EntityOne(this, actionElement));
340             }
341             
342             actionElement = UtilXml.firstChildElement(nodeElement, "service");
343             if (actionElement != null) {
344                 actions.add(new ModelTreeAction.Service(this, actionElement));
345             }
346             
347             actionElement = UtilXml.firstChildElement(nodeElement, "script");
348             if (actionElement != null) {
349                 actions.add(new ModelTreeAction.Script(this, actionElement));
350             }
351         
352             Element JavaDoc screenElement = UtilXml.firstChildElement(nodeElement, "include-screen");
353             if (screenElement != null) {
354                 this.screenNameExdr = new FlexibleStringExpander(screenElement.getAttribute("name"));
355                 this.screenLocationExdr = new FlexibleStringExpander(screenElement.getAttribute("location"));
356                 this.shareScope = screenElement.getAttribute("share-scope");
357             }
358             
359             Element JavaDoc labelElement = UtilXml.firstChildElement(nodeElement, "label");
360             if (labelElement != null) {
361                 this.label = new Label(labelElement);
362             }
363     
364             Element JavaDoc linkElement = UtilXml.firstChildElement(nodeElement, "link");
365             if (linkElement != null) {
366                 this.link = new Link(linkElement);
367             }
368             
369             Element JavaDoc imageElement = UtilXml.firstChildElement(nodeElement, "image");
370             if (imageElement != null) {
371                 this.image = new Image(imageElement);
372             }
373     
374             /* there are situations in which nothing should be displayed
375             if (screenElement == null && labelElement == null && linkElement == null) {
376                 throw new IllegalArgumentException("Neither 'screen' nor 'label' nor 'link' found for the node definition with name: " + this.name);
377             }
378             */

379         Element JavaDoc conditionElement = UtilXml.firstChildElement(nodeElement, "condition");
380         if (conditionElement != null) {
381             this.condition = new ModelTreeCondition(modelTree, conditionElement);
382         }
383
384             List JavaDoc subNodeElements = UtilXml.childElementList(nodeElement, "sub-node");
385             Iterator JavaDoc subNodeElementIter = subNodeElements.iterator();
386             while (subNodeElementIter.hasNext()) {
387                 Element JavaDoc subNodeElementEntry = (Element JavaDoc) subNodeElementIter.next();
388                 ModelSubNode subNode = new ModelSubNode(subNodeElementEntry, this);
389                 subNodeList.add(subNode);
390             }
391     
392             
393         }
394     
395         public void renderNodeString(Writer JavaDoc writer, Map JavaDoc context,
396                 TreeStringRenderer treeStringRenderer, int depth, boolean isLast)
397                 throws IOException JavaDoc, GeneralException {
398             boolean passed = true;
399             if (this.condition != null) {
400                 if (!this.condition.eval(context)) {
401                     passed = false;
402                 }
403             }
404             //Debug.logInfo("in ModelMenu, name:" + this.getName(), module);
405
if (passed) {
406                 //this.subNodeValues = new ArrayList();
407
//context.put("subNodeValues", new ArrayList());
408
//if (Debug.infoOn()) Debug .logInfo(" renderNodeString, " + modelTree.getdefaultPkName() + " :" + context.get(modelTree.getdefaultPkName()), module);
409
context.put("processChildren", new Boolean JavaDoc(true));
410                 // this action will usually obtain the "current" entity
411
ModelTreeAction.runSubActions(this.actions, context);
412                 String JavaDoc pkName = getPkName();
413                 String JavaDoc id = null;
414                 if (UtilValidate.isNotEmpty(this.entryName)) {
415                     Map JavaDoc map = (Map JavaDoc)context.get(this.entryName);
416                     id = (String JavaDoc)map.get(pkName);
417                 } else {
418                     id = (String JavaDoc) context.get(pkName);
419                 }
420                 if (id != null) {
421                     modelTree.currentNodeTrail.add(id);
422                 }
423                 context.put("currentNodeTrail", modelTree.currentNodeTrail);
424                 String JavaDoc currentNodeTrailPiped = StringUtil.join(modelTree.currentNodeTrail, "|");
425                 context.put("currentNodeTrailPiped", currentNodeTrailPiped);
426                 treeStringRenderer.renderNodeBegin(writer, context, this, depth, isLast);
427                 //if (Debug.infoOn()) Debug.logInfo(" context:" +
428
// context.entrySet(), module);
429
try {
430                     String JavaDoc screenName = null;
431                     if (screenNameExdr != null)
432                         screenName = screenNameExdr.expandString(context);
433                     String JavaDoc screenLocation = null;
434                     if (screenLocationExdr != null)
435                         screenLocation = screenLocationExdr.expandString(context);
436                     if (screenName != null && screenLocation != null) {
437                         ScreenStringRenderer screenStringRenderer = treeStringRenderer .getScreenStringRenderer(context);
438                         ModelScreen modelScreen = ScreenFactory .getScreenFromLocation(screenLocation, screenName);
439                         modelScreen.renderScreenString(writer, context, screenStringRenderer);
440                     }
441                     if (label != null) {
442                         label.renderLabelString(writer, context, treeStringRenderer);
443                     }
444                     if (link != null) {
445                         link.renderLinkString(writer, context, treeStringRenderer);
446                     }
447                     Boolean JavaDoc processChildren = (Boolean JavaDoc) context .get("processChildren");
448                     //if (Debug.infoOn()) Debug.logInfo(" processChildren:" + processChildren, module);
449
if (processChildren.booleanValue()) {
450                         getChildren(context);
451                         Iterator JavaDoc nodeIter = this.subNodeValues.iterator();
452                         int nodeIndex = -1;
453                         int newDepth = depth + 1;
454                         while (nodeIter.hasNext()) {
455                             nodeIndex++;
456                             modelTree.setNodeIndexAtDepth(newDepth, nodeIndex);
457                             Object JavaDoc[] arr = (Object JavaDoc[]) nodeIter.next();
458                             ModelNode node = (ModelNode) arr[0];
459                             Map JavaDoc val = (Map JavaDoc) arr[1];
460                             //GenericPK pk = val.getPrimaryKey();
461
//if (Debug.infoOn()) Debug.logInfo(" pk:" + pk,
462
// module);
463
String JavaDoc thisPkName = node.getPkName();
464                             String JavaDoc thisEntityId = (String JavaDoc) val.get(thisPkName);
465                             Map JavaDoc newContext = ((MapStack) context) .standAloneChildStack();
466                             String JavaDoc nodeEntryName = node.getEntryName();
467                             if (UtilValidate.isNotEmpty(nodeEntryName)) {
468                                 newContext.put(nodeEntryName, val);
469                             } else {
470                                 newContext.putAll(val);
471                             }
472                             newContext.put("currentNodeIndex", new Integer JavaDoc(nodeIndex));
473                             String JavaDoc targetEntityId = null;
474                             List JavaDoc targetNodeTrail = this.modelTree .getTrailList();
475                             if (newDepth < targetNodeTrail.size()) {
476                                 targetEntityId = (String JavaDoc) targetNodeTrail .get(newDepth);
477                             }
478                             if ((targetEntityId != null && targetEntityId .equals(thisEntityId)) || this.showPeers(newDepth)) {
479                                 boolean lastNode = !nodeIter.hasNext();
480                                 newContext.put("lastNode", new Boolean JavaDoc(lastNode));
481                                 node.renderNodeString(writer, newContext, treeStringRenderer, newDepth, lastNode);
482                             }
483                         }
484                     }
485                 } catch (SAXException JavaDoc e) {
486                     String JavaDoc errMsg = "Error rendering included label with name ["
487                             + name + "] : " + e.toString();
488                     Debug.logError(e, errMsg, module);
489                     throw new RuntimeException JavaDoc(errMsg);
490                 } catch (ParserConfigurationException JavaDoc e3) {
491                     String JavaDoc errMsg = "Error rendering included label with name ["
492                             + name + "] : " + e3.toString();
493                     Debug.logError(e3, errMsg, module);
494                     throw new RuntimeException JavaDoc(errMsg);
495                 } catch (IOException JavaDoc e2) {
496                     String JavaDoc errMsg = "Error rendering included label with name ["
497                             + name + "] : " + e2.toString();
498                     Debug.logError(e2, errMsg, module);
499                     throw new RuntimeException JavaDoc(errMsg);
500                 }
501                 treeStringRenderer.renderNodeEnd(writer, context, this);
502                 int removeIdx = modelTree.currentNodeTrail.size() - 1;
503                 if (removeIdx >= 0) modelTree.currentNodeTrail.remove(removeIdx);
504             }
505         }
506
507         public boolean hasChildren(Map JavaDoc context) {
508
509              boolean hasChildren = false;
510              Long JavaDoc nodeCount = null;
511              String JavaDoc countFieldName = "childBranchCount";
512                Object JavaDoc obj = null;
513              if (UtilValidate.isNotEmpty(this.entryName)) {
514                     Map JavaDoc map = (Map JavaDoc)context.get(this.entryName);
515                     if (map instanceof GenericValue) {
516                         ModelEntity modelEntity = ((GenericValue)map).getModelEntity();
517                         if (modelEntity.isField(countFieldName)) {
518                             obj = map.get(countFieldName);
519                         }
520                     }
521              } else {
522                    obj = context.get(countFieldName);
523              }
524              if (obj != null) {
525                 nodeCount = (Long JavaDoc)obj;
526              }
527              String JavaDoc entName = this.getEntityName();
528              GenericDelegator delegator = modelTree.getDelegator();
529              ModelEntity modelEntity = delegator.getModelEntity(entName);
530              ModelField modelField = null;
531              if (modelEntity.isField(countFieldName)) {
532                  modelField = modelEntity.getField(countFieldName);
533              }
534              if (nodeCount == null && modelField != null || this.modelTree.forceChildCheck) {
535                  getChildren(context);
536                  /*
537                  String id = (String)context.get(modelTree.getPkName());
538                  if (UtilValidate.isNotEmpty(id)) {
539                      try {
540                          int leafCount = ContentManagementWorker.updateStatsTopDown(delegator, id, UtilMisc.toList("SUB_CONTENT", "PUBLISH_LINK"));
541                          GenericValue entity = delegator.findByPrimaryKeyCache(entName, UtilMisc.toMap(modelTree.getPkName(), id));
542                          obj = entity.get("childBranchCount");
543                         if (obj != null)
544                             nodeCount = (Long)obj;
545                      } catch(GenericEntityException e) {
546                          Debug.logError(e, module);
547                         throw new RuntimeException(e.getMessage());
548                      }
549                  }
550                  */

551                  nodeCount = new Long JavaDoc(this.subNodeValues.size());
552                 String JavaDoc pkName = this.getPkName();
553                 String JavaDoc id = null;
554                 if (UtilValidate.isNotEmpty(this.entryName)) {
555                     Map JavaDoc map = (Map JavaDoc)context.get(this.entryName);
556                     id = (String JavaDoc)map.get(pkName);
557                 } else {
558                     id = (String JavaDoc) context.get(pkName);
559                 }
560                  try {
561                      if (id != null && modelEntity.getPksSize() == 1) {
562                          GenericValue entity = delegator.findByPrimaryKey(entName, UtilMisc.toMap(pkName, id));
563                          if (modelEntity.isField("childBranchCount")) {
564                              entity.put("childBranchCount", nodeCount);
565                          }
566                          entity.store();
567                      }
568                  } catch(GenericEntityException e) {
569                      Debug.logError(e, module);
570                     throw new RuntimeException JavaDoc(e.getMessage());
571                  }
572              } else if (nodeCount == null) {
573                  getChildren(context);
574                 if (subNodeValues != null)
575                     nodeCount = new Long JavaDoc(subNodeValues.size());
576              }
577              
578              if (nodeCount != null && nodeCount.intValue() > 0)
579                  hasChildren = true;
580                 
581              
582              return hasChildren;
583         }
584
585         public void getChildren(Map JavaDoc context) {
586              this.subNodeValues = new ArrayList JavaDoc();
587              Iterator JavaDoc nodeIter = subNodeList.iterator();
588              while (nodeIter.hasNext()) {
589                  ModelSubNode subNode = (ModelSubNode)nodeIter.next();
590                  String JavaDoc nodeName = subNode.getNodeName(context);
591                  ModelNode node = (ModelNode)modelTree.nodeMap.get(nodeName);
592                  List JavaDoc subNodeActions = subNode.getActions();
593                  //if (Debug.infoOn()) Debug.logInfo(" context.currentValue:" + context.get("currentValue"), module);
594
ModelTreeAction.runSubActions(subNodeActions, context);
595                  // List dataFound = (List)context.get("dataFound");
596
ListIterator JavaDoc dataIter = subNode.getListIterator();
597                  if (dataIter instanceof EntityListIterator) {
598                      EntityListIterator eli = (EntityListIterator) dataIter;
599                      Map JavaDoc val = null;
600                      while ((val = (Map JavaDoc) eli.next()) != null) {
601                          Object JavaDoc [] arr = {node, val};
602                          this.subNodeValues.add(arr);
603                      }
604                      try {
605                          eli.close();
606                      } catch(GenericEntityException e) {
607                          Debug.logError(e, module);
608                          throw new RuntimeException JavaDoc(e.getMessage());
609                      }
610                  } else if (dataIter != null) {
611                      while (dataIter.hasNext()) {
612                          Map JavaDoc val = (Map JavaDoc) dataIter.next();
613                          Object JavaDoc [] arr = {node, val};
614                          this.subNodeValues.add(arr);
615                      }
616                  }
617              }
618              return;
619         }
620
621         public String JavaDoc getName() {
622             return name;
623         }
624
625         public String JavaDoc getEntryName() {
626             return this.entryName;
627         }
628
629         public String JavaDoc getRenderStyle() {
630             String JavaDoc rStyle = this.renderStyle;
631             if (UtilValidate.isEmpty(rStyle))
632                 rStyle = modelTree.getRenderStyle();
633             return rStyle;
634         }
635     
636         public boolean isExpandCollapse() {
637             boolean isExpCollapse = false;
638             String JavaDoc rStyle = getRenderStyle();
639             if (rStyle != null && rStyle.equals("expand-collapse"))
640                 isExpCollapse = true;
641             
642             return isExpCollapse;
643         }
644         
645         public boolean isFollowTrail() {
646             boolean isFollowTrail = false;
647             String JavaDoc rStyle = getRenderStyle();
648             if (rStyle != null && (rStyle.equals("follow-trail") || rStyle.equals("show-peers") || rStyle.equals("follow-trail"))) {
649                 isFollowTrail = true;
650             }
651             
652             return isFollowTrail;
653         }
654     
655         public boolean showPeers(int currentDepth) {
656             int trailSize = 0;
657             List JavaDoc trail = modelTree.getTrailList();
658             int openDepth = modelTree.getOpenDepth();
659             int postTrailOpenDepth = modelTree.getPostTrailOpenDepth();
660             if (trail != null) trailSize = trail.size();
661                 
662             boolean showPeers = false;
663             String JavaDoc rStyle = getRenderStyle();
664             if (rStyle == null) {
665                 showPeers = true;
666             } else if (!isFollowTrail()) {
667                 showPeers = true;
668             } else if ((currentDepth < trailSize) && (rStyle != null) && (rStyle.equals("show-peers") || rStyle.equals("expand-collapse"))) {
669                 showPeers = true;
670             } else if (openDepth >= currentDepth) {
671                 showPeers = true;
672             } else {
673                 int depthAfterTrail = currentDepth - trailSize;
674                 if (depthAfterTrail >= 0 && depthAfterTrail <= postTrailOpenDepth) showPeers = true;
675             }
676             
677             return showPeers;
678         }
679     
680         public String JavaDoc getExpandCollapseStyle() {
681             return expandCollapseStyle;
682         }
683
684         public String JavaDoc getWrapStyle(Map JavaDoc context) {
685             String JavaDoc val = this.wrapStyleExdr.expandString(context);
686             if (UtilValidate.isEmpty(val)) {
687                 val = this.modelTree.getWrapStyle(context);
688             }
689             return val;
690         }
691     
692         public ModelTree getModelTree() {
693             return this.modelTree;
694         }
695         
696         public void setEntityName(String JavaDoc name) {
697             this.entityName = name;
698             if (UtilValidate.isNotEmpty(this.entityName)) {
699                 ModelEntity modelEntity = modelTree.delegator.getModelEntity(this.entityName);
700                 if (modelEntity.getPksSize() == 1) {
701                     ModelField modelField = modelEntity.getOnlyPk();
702                     this.pkName = modelField.getName();
703                 } else {
704                     // TODO: what to do here?
705
}
706             }
707         }
708         
709         public String JavaDoc getEntityName() {
710             if (UtilValidate.isNotEmpty(this.entityName)) {
711                 return this.entityName;
712             } else {
713                 return this.modelTree.getDefaultEntityName();
714             }
715         }
716     
717         public String JavaDoc getPkName() {
718             if (UtilValidate.isNotEmpty(this.pkName)) {
719                 return this.pkName;
720             } else {
721                 return this.modelTree.getDefaultPkName();
722             }
723         }
724     
725         public void setPkName(String JavaDoc pkName) {
726             this.pkName = pkName;
727         }
728         
729         public static class ModelSubNode {
730     
731             protected ModelNode rootNode;
732             protected FlexibleStringExpander nodeNameExdr;
733             protected List JavaDoc actions = new ArrayList JavaDoc();
734             protected List JavaDoc outFieldMaps;
735             protected ListIterator JavaDoc listIterator;
736     
737             public ModelSubNode() {}
738     
739             public ModelSubNode(Element JavaDoc nodeElement, ModelNode modelNode) {
740     
741                 this.rootNode = modelNode;
742                 this.nodeNameExdr = new FlexibleStringExpander(nodeElement.getAttribute("node-name"));
743         
744                 Element JavaDoc actionElement = UtilXml.firstChildElement(nodeElement, "entity-and");
745                 if (actionElement != null) {
746                    actions.add(new ModelTreeAction.EntityAnd(this, actionElement));
747                 }
748                 
749                 actionElement = UtilXml.firstChildElement(nodeElement, "service");
750                 if (actionElement != null) {
751                     actions.add(new ModelTreeAction.Service(this, actionElement));
752                 }
753                 
754                 actionElement = UtilXml.firstChildElement(nodeElement, "entity-condition");
755                 if (actionElement != null) {
756                     actions.add(new ModelTreeAction.EntityCondition(this, actionElement));
757                 }
758                 
759                 actionElement = UtilXml.firstChildElement(nodeElement, "script");
760                 if (actionElement != null) {
761                     actions.add(new ModelTreeAction.Script(this, actionElement));
762                 }
763         
764             }
765             
766             public ModelTree.ModelNode getNode() {
767                 return this.rootNode;
768             }
769         
770             public String JavaDoc getNodeName(Map JavaDoc context) {
771                 return this.nodeNameExdr.expandString(context);
772             }
773     
774             public List JavaDoc getActions() {
775                 return actions;
776             }
777             
778             public void setListIterator(ListIterator JavaDoc iter) {
779                 listIterator = iter;
780             }
781         
782             public ListIterator JavaDoc getListIterator() {
783                 return listIterator;
784             }
785         }
786     
787         public static class Label {
788             protected FlexibleStringExpander textExdr;
789             
790             protected FlexibleStringExpander idExdr;
791             protected FlexibleStringExpander styleExdr;
792             
793             public Label(Element JavaDoc labelElement) {
794     
795                 // put the text attribute first, then the pcdata under the element, if both are there of course
796
String JavaDoc textAttr = UtilFormatOut.checkNull(labelElement.getAttribute("text"));
797                 String JavaDoc pcdata = UtilFormatOut.checkNull(UtilXml.elementValue(labelElement));
798                 this.textExdr = new FlexibleStringExpander(textAttr + pcdata);
799     
800                 this.idExdr = new FlexibleStringExpander(labelElement.getAttribute("id"));
801                 this.styleExdr = new FlexibleStringExpander(labelElement.getAttribute("style"));
802             }
803     
804             public void renderLabelString(Writer JavaDoc writer, Map JavaDoc context, TreeStringRenderer treeStringRenderer) {
805                 try {
806                     treeStringRenderer.renderLabel(writer, context, this);
807                 } catch (IOException JavaDoc e) {
808                     String JavaDoc errMsg = "Error rendering label with id [" + getId(context) + "]: " + e.toString();
809                     Debug.logError(e, errMsg, module);
810                     throw new RuntimeException JavaDoc(errMsg);
811                 }
812             }
813             
814             public String JavaDoc getText(Map JavaDoc context) {
815                 return this.textExdr.expandString(context);
816             }
817             
818             public String JavaDoc getId(Map JavaDoc context) {
819                 return this.idExdr.expandString(context);
820             }
821             
822             public String JavaDoc getStyle(Map JavaDoc context) {
823                 return this.styleExdr.expandString(context);
824             }
825         }
826
827     
828         public static class Link {
829             protected FlexibleStringExpander textExdr;
830             protected FlexibleStringExpander idExdr;
831             protected FlexibleStringExpander styleExdr;
832             protected FlexibleStringExpander targetExdr;
833             protected FlexibleStringExpander targetWindowExdr;
834             protected FlexibleStringExpander prefixExdr;
835             protected FlexibleStringExpander nameExdr;
836             protected FlexibleStringExpander titleExdr;
837             protected Image image;
838             protected String JavaDoc urlMode = "intra-app";
839             protected boolean fullPath = false;
840             protected boolean secure = false;
841             protected boolean encode = false;
842             
843             public Link() {
844                 setText(null);
845                 setId(null);
846                 setStyle(null);
847                 setTarget(null);
848                 setTargetWindow(null);
849                 setPrefix(null);
850                 setUrlMode(null);
851                 setFullPath(null);
852                 setSecure(null);
853                 setEncode(null);
854                 setName(null);
855                 setTitle(null);
856             }
857
858             public Link(Element JavaDoc linkElement) {
859     
860                 setText(linkElement.getAttribute("text"));
861                 setId(linkElement.getAttribute("id"));
862                 setStyle(linkElement.getAttribute("style"));
863                 setTarget(linkElement.getAttribute("target"));
864                 setTargetWindow(linkElement.getAttribute("target-window"));
865                 setPrefix(linkElement.getAttribute("prefix"));
866                 setUrlMode(linkElement.getAttribute("url-mode"));
867                 setFullPath(linkElement.getAttribute("full-path"));
868                 setSecure(linkElement.getAttribute("secure"));
869                 setEncode(linkElement.getAttribute("encode"));
870                 setName(linkElement.getAttribute("name"));
871                 setTitle(linkElement.getAttribute("title"));
872                 Element JavaDoc imageElement = UtilXml.firstChildElement(linkElement, "image");
873                 if (imageElement != null) {
874                     this.image = new Image(imageElement);
875                 }
876     
877             }
878     
879             public void renderLinkString(Writer JavaDoc writer, Map JavaDoc context, TreeStringRenderer treeStringRenderer) {
880                 try {
881                     treeStringRenderer.renderLink(writer, context, this);
882                 } catch (IOException JavaDoc e) {
883                     String JavaDoc errMsg = "Error rendering link with id [" + getId(context) + "]: " + e.toString();
884                     Debug.logError(e, errMsg, module);
885                     throw new RuntimeException JavaDoc(errMsg);
886                 }
887             }
888             
889             public String JavaDoc getText(Map JavaDoc context) {
890                 return this.textExdr.expandString(context);
891             }
892             
893             public String JavaDoc getId(Map JavaDoc context) {
894                 return this.idExdr.expandString(context);
895             }
896             
897             public String JavaDoc getStyle(Map JavaDoc context) {
898                 return this.styleExdr.expandString(context);
899             }
900             
901             public String JavaDoc getName(Map JavaDoc context) {
902                 return this.nameExdr.expandString(context);
903             }
904             public String JavaDoc getTitle(Map JavaDoc context) {
905                 return this.titleExdr.expandString(context);
906             }
907         
908             public String JavaDoc getTarget(Map JavaDoc context) {
909                 return this.targetExdr.expandString(context);
910             }
911             
912             public String JavaDoc getTargetWindow(Map JavaDoc context) {
913                 return this.targetWindowExdr.expandString(context);
914             }
915             
916             public String JavaDoc getUrlMode() {
917                 return this.urlMode;
918             }
919             
920             public String JavaDoc getPrefix(Map JavaDoc context) {
921                 return this.prefixExdr.expandString(context);
922             }
923             
924             public boolean getFullPath() {
925                 return this.fullPath;
926             }
927             
928             public boolean getSecure() {
929                 return this.secure;
930             }
931             
932             public boolean getEncode() {
933                 return this.encode;
934             }
935             
936             public Image getImage() {
937                 return this.image;
938             }
939
940             public void setText(String JavaDoc val) {
941                 String JavaDoc textAttr = UtilFormatOut.checkNull(val);
942                 this.textExdr = new FlexibleStringExpander(textAttr);
943             }
944             public void setId(String JavaDoc val) {
945                 this.idExdr = new FlexibleStringExpander(val);
946             }
947             public void setStyle(String JavaDoc val) {
948                 this.styleExdr = new FlexibleStringExpander(val);
949             }
950             public void setName(String JavaDoc val) {
951                 this.nameExdr = new FlexibleStringExpander(val);
952             }
953             public void setTitle(String JavaDoc val) {
954                 this.titleExdr = new FlexibleStringExpander(val);
955             }
956             public void setTarget(String JavaDoc val) {
957                 this.targetExdr = new FlexibleStringExpander(val);
958             }
959             public void setTargetWindow(String JavaDoc val) {
960                 this.targetWindowExdr = new FlexibleStringExpander(val);
961             }
962             public void setPrefix(String JavaDoc val) {
963                 this.prefixExdr = new FlexibleStringExpander(val);
964             }
965             public void setUrlMode(String JavaDoc val) {
966                 if (UtilValidate.isNotEmpty(val))
967                     this.urlMode = val;
968             }
969             public void setFullPath(String JavaDoc val) {
970                 String JavaDoc sFullPath = val;
971                 if (sFullPath != null && sFullPath.equalsIgnoreCase("true"))
972                     this.fullPath = true;
973                 else
974                     this.fullPath = false;
975             }
976
977             public void setSecure(String JavaDoc val) {
978                 String JavaDoc sSecure = val;
979                 if (sSecure != null && sSecure.equalsIgnoreCase("true"))
980                     this.secure = true;
981                 else
982                     this.secure = false;
983             }
984
985             public void setEncode(String JavaDoc val) {
986                 String JavaDoc sEncode = val;
987                 if (sEncode != null && sEncode.equalsIgnoreCase("true"))
988                     this.encode = true;
989                 else
990                     this.encode = false;
991             }
992             public void setImage(Image img) {
993                 this.image = img;
994             }
995                 
996         }
997
998         public static class Image {
999
1000            protected FlexibleStringExpander srcExdr;
1001            protected FlexibleStringExpander idExdr;
1002            protected FlexibleStringExpander styleExdr;
1003            protected FlexibleStringExpander widthExdr;
1004            protected FlexibleStringExpander heightExdr;
1005            protected FlexibleStringExpander borderExdr;
1006            protected String JavaDoc urlMode;
1007            
1008            public Image() {
1009
1010                setSrc(null);
1011                setId(null);
1012                setStyle(null);
1013                setWidth(null);
1014                setHeight(null);
1015                setBorder("0");
1016                setUrlMode(null);
1017            }
1018
1019            public Image(Element JavaDoc imageElement) {
1020    
1021                setSrc(imageElement.getAttribute("src"));
1022                setId(imageElement.getAttribute("id"));
1023                setStyle(imageElement.getAttribute("style"));
1024                setWidth(imageElement.getAttribute("width"));
1025                setHeight(imageElement.getAttribute("height"));
1026                setBorder(UtilFormatOut.checkEmpty(imageElement.getAttribute("border"), "0"));
1027                setUrlMode(UtilFormatOut.checkEmpty(imageElement.getAttribute("url-mode"), "content"));
1028    
1029            }
1030    
1031            public void renderImageString(Writer JavaDoc writer, Map JavaDoc context, TreeStringRenderer treeStringRenderer) {
1032                try {
1033                    treeStringRenderer.renderImage(writer, context, this);
1034                } catch (IOException JavaDoc e) {
1035                    String JavaDoc errMsg = "Error rendering image with id [" + getId(context) + "]: " + e.toString();
1036                    Debug.logError(e, errMsg, module);
1037                    throw new RuntimeException JavaDoc(errMsg);
1038                }
1039            }
1040            
1041            public String JavaDoc getSrc(Map JavaDoc context) {
1042                return this.srcExdr.expandString(context);
1043            }
1044            
1045            public String JavaDoc getId(Map JavaDoc context) {
1046                return this.idExdr.expandString(context);
1047            }
1048            
1049            public String JavaDoc getStyle(Map JavaDoc context) {
1050                return this.styleExdr.expandString(context);
1051            }
1052
1053            public String JavaDoc getWidth(Map JavaDoc context) {
1054                return this.widthExdr.expandString(context);
1055            }
1056
1057            public String JavaDoc getHeight(Map JavaDoc context) {
1058                return this.heightExdr.expandString(context);
1059            }
1060
1061            public String JavaDoc getBorder(Map JavaDoc context) {
1062                return this.borderExdr.expandString(context);
1063            }
1064            
1065            public String JavaDoc getUrlMode() {
1066                return this.urlMode;
1067            }
1068            
1069            public void setSrc(String JavaDoc val) {
1070                String JavaDoc textAttr = UtilFormatOut.checkNull(val);
1071                this.srcExdr = new FlexibleStringExpander(textAttr);
1072            }
1073            public void setId(String JavaDoc val) {
1074                this.idExdr = new FlexibleStringExpander(val);
1075            }
1076            public void setStyle(String JavaDoc val) {
1077                this.styleExdr = new FlexibleStringExpander(val);
1078            }
1079            public void setWidth(String JavaDoc val) {
1080                this.widthExdr = new FlexibleStringExpander(val);
1081            }
1082            public void setHeight(String JavaDoc val) {
1083                this.heightExdr = new FlexibleStringExpander(val);
1084            }
1085            public void setBorder(String JavaDoc val) {
1086                this.borderExdr = new FlexibleStringExpander(val);
1087            }
1088            public void setUrlMode(String JavaDoc val) {
1089                if (UtilValidate.isEmpty(val))
1090                    this.urlMode = "content";
1091                else
1092                    this.urlMode = val;
1093            }
1094                
1095        }
1096
1097    }
1098}
1099
1100
Popular Tags