KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > RelatedNodesTag


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.bridge.jsp.taglib;
11
12 import javax.servlet.jsp.JspTagException JavaDoc;
13
14 import org.mmbase.bridge.*;
15 import org.mmbase.bridge.jsp.taglib.containers.RelatedNodesContainerTag;
16 import org.mmbase.bridge.jsp.taglib.util.Attribute;
17 import org.mmbase.bridge.util.Queries;
18 import org.mmbase.storage.search.*;
19
20 /**
21  * RelatedNodesTag, provides functionality for listing single related nodes in MMBase
22  *
23  * @author Kees Jongenburger
24  * @author Michiel Meeuwissen
25  * @author Pierre van Rooden
26  * @author Jaco de Groot
27  * @version $Id: RelatedNodesTag.java,v 1.39 2006/07/08 12:51:56 michiel Exp $
28  */

29 public class RelatedNodesTag extends AbstractNodeListTag {
30
31     protected Attribute type = Attribute.NULL;
32     protected Attribute path = Attribute.NULL;
33     protected Attribute element = Attribute.NULL;
34     protected Attribute role = Attribute.NULL;
35     protected Attribute searchDir = Attribute.NULL; // for use with 'role' and 'type'
36
protected Attribute searchDirs = Attribute.NULL; // for use with 'path' and 'element'
37

38     protected Attribute container = Attribute.NULL;
39
40     public void setContainer(String JavaDoc c) throws JspTagException JavaDoc {
41         container = getAttribute(c);
42     }
43
44     /**
45      * @param type a nodeManager
46      */

47     public void setType(String JavaDoc type) throws JspTagException JavaDoc {
48         this.type = getAttribute(type);
49     }
50     /**
51      * @param role a role
52      */

53     public void setRole(String JavaDoc role) throws JspTagException JavaDoc {
54         this.role = getAttribute(role);
55     }
56
57     /**
58      * The search parameter, determines how directionality affects the search.
59      * Possible values are <code>both</code>, <code>destination</code>,
60      * <code>source</code>, and <code>all</code>
61      * @param search the swerach value
62      */

63     public void setSearchdir(String JavaDoc search) throws JspTagException JavaDoc {
64         searchDir = getAttribute(search);
65     }
66
67     /**
68      * @since MMBase-1.7.1
69      */

70     public void setPath(String JavaDoc p) throws JspTagException JavaDoc {
71         path = getAttribute(p);
72     }
73     /**
74      * @since MMBase-1.7.1
75      */

76     public void setElement(String JavaDoc e) throws JspTagException JavaDoc {
77         element = getAttribute(e);
78     }
79     /**
80      * @since MMBase-1.7.1
81      */

82     public void setSearchdirs(String JavaDoc s) throws JspTagException JavaDoc {
83         searchDirs = getAttribute(s);
84     }
85
86     /**
87      * Performs the search
88      */

89     public int doStartTag() throws JspTagException JavaDoc {
90         int superresult = doStartTagHelper(); // the super-tag handles the use of referid...
91
if (superresult != NOT_HANDLED) {
92             return superresult;
93         }
94         RelatedNodesContainerTag c = (RelatedNodesContainerTag) findParentTag(RelatedNodesContainerTag.class, (String JavaDoc) container.getValue(this), false);
95
96         NodeQuery query;
97         if (type != Attribute.NULL || path != Attribute.NULL || c == null || parentNodeId != Attribute.NULL) {
98
99             // obtain a reference to the node through a parent tag
100
Node parentNode = getNode();
101             if (parentNode == null) {
102                 throw new TaglibException("Could not find parent node!!");
103             }
104             Cloud cloud;
105             {
106                 // prefer cloud of current page, otherwise of node
107
CloudProvider cloudProvider = findCloudProvider(false);
108                 cloud = cloudProvider != null ? cloudProvider.getCloudVar() : parentNode.getCloud();
109             }
110             query = cloud.createNodeQuery();
111             Step step1 = query.addStep(parentNode.getNodeManager());
112             query.setAlias(step1, parentNode.getNodeManager().getName() + "0");
113             query.addNode(step1, parentNode);
114
115
116             String JavaDoc searchDirections;
117             if (searchDir == Attribute.NULL) { // searchdir is a bit deprecated.
118
searchDirections = (String JavaDoc) searchDirs.getValue(this);
119             } else {
120                 if (searchDirs != Attribute.NULL) {
121                     throw new TaglibException("Cannot specify both 'searchdir' and 'searchdirs' attributes. ");
122                 }
123                 searchDirections = (String JavaDoc) searchDir.getValue(this);
124             }
125
126             NodeManager otherManager;
127             if (path == Attribute.NULL) {
128                 if (type == Attribute.NULL) {
129                     otherManager = cloud.getNodeManager("object");
130                 } else {
131                     if (element != Attribute.NULL) {
132                         throw new TaglibException("Cannot specify both 'element' and 'type' attributes");
133                     }
134                     otherManager = cloud.getNodeManager(type.getString(this));
135                 }
136                 RelationStep step2 = query.addRelationStep(otherManager, (String JavaDoc) role.getValue(this), searchDirections);
137                 Step step3 = step2.getNext();
138                 query.setNodeStep(step3); // makes it ready for use as NodeQuery
139
} else {
140                 if (role != Attribute.NULL) {
141                     throw new TaglibException("Cannot specify both 'path' and 'role' attributes");
142                 }
143                 Queries.addPath(query, (String JavaDoc) path.getValue(this), searchDirections);
144                 if (element != Attribute.NULL) {
145                     String JavaDoc alias = element.getString(this);
146                     Step nodeStep = query.getStep(alias);
147                     if (nodeStep == null) {
148                         throw new JspTagException JavaDoc("Could not set element to '" + alias + "' (no such step)");
149                     }
150                     query.setNodeStep(nodeStep);
151                 } else {
152                     // default to third step (first step is the related node, second is the relation)
153
query.setNodeStep((Step) query.getSteps().get(2));
154                 }
155             }
156
157
158
159         } else {
160             query = (NodeQuery) c.getQuery();
161         }
162         if (constraints != Attribute.NULL) {
163             Queries.addConstraints(query, (String JavaDoc) constraints.getValue(this));
164         }
165         if (orderby != Attribute.NULL) {
166             Queries.addSortOrders(query, (String JavaDoc) orderby.getValue(this), (String JavaDoc) directions.getValue(this));
167         }
168
169         NodesAndTrim result = getNodesAndTrim(query);
170         return setReturnValues(result.nodeList, result.needsTrim);
171     }
172 }
173
Popular Tags