KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
16 import org.mmbase.bridge.jsp.taglib.util.Attribute;
17 import org.mmbase.bridge.util.Queries;
18 import org.mmbase.util.logging.*;
19
20 /**
21  * ListTag, provides functionality for listing cluster nodes
22  * ('multilevel' search) in MMBase
23  *
24  * @author Kees Jongenburger
25  * @author Michiel Meeuwissen
26  * @author Pierre van Rooden
27  * @version $Id: ListTag.java,v 1.51 2005/01/30 16:46:35 nico Exp $
28  */

29
30 public class ListTag extends AbstractNodeListTag implements ClusterNodeProvider {
31     private static final Logger log = Logging.getLoggerInstance(ListTag.class);
32
33     protected Attribute nodes = Attribute.NULL;
34     protected Attribute path = Attribute.NULL;
35     protected Attribute distinct = Attribute.NULL;
36     protected Attribute search = Attribute.NULL;
37     protected Attribute fields = Attribute.NULL;
38     protected Attribute container = Attribute.NULL;
39
40     /**
41      * @param fields a comma separated list of fields of the nodes.
42      **/

43     public void setFields(String JavaDoc fields) throws JspTagException JavaDoc {
44         this.fields = getAttribute(fields);
45     }
46
47     public void setContainer(String JavaDoc c) throws JspTagException JavaDoc {
48         container = getAttribute(c);
49     }
50
51     /**
52      * Sets the nodes to start the search with.
53      * The value '-1' interpreted as <code>null</code>, whcih indicates no
54      * predefined startnodes are sued (a more general search is conducted
55      * instead).
56      * @param nodes a node or a comma separated list of nodes.
57      */

58     public void setNodes(String JavaDoc nodes) throws JspTagException JavaDoc {
59         // parse/map the nodes they can be params, sessions or aliases
60
// instead of just numbers
61
this.nodes = getAttribute(nodes);
62     }
63
64     /**
65      * @param path a comma separated list of nodeManagers
66      */

67     public void setPath(String JavaDoc path) throws JspTagException JavaDoc {
68         this.path = getAttribute(path);
69     }
70
71     /**
72      * The search parameter, determines how directionality affects the search.
73      * Possible values are <code>both</code>, <code>destination</code>,
74      * <code>source</code>, <code>either</code> and <code>all</code>
75      * @param search the search value
76      */

77     public void setSearchdir(String JavaDoc search) throws JspTagException JavaDoc {
78         this.search = getAttribute(search);
79         if (log.isDebugEnabled())
80             log.debug("Setting search dir to " + this.search);
81     }
82
83     /**
84      * @param distinct the selection query for the object we are looking for
85      */

86     public void setDistinct(String JavaDoc distinct) throws JspTagException JavaDoc {
87         this.distinct = getAttribute(distinct);
88     }
89
90     /**
91      * To be overrided by related-tag
92      */

93     protected String JavaDoc getSearchNodes() throws JspTagException JavaDoc {
94         if (nodes != Attribute.NULL) {
95             return nodes.getString(this);
96         } else {
97             return "";
98         }
99     }
100
101     /**
102      * To be overrided by related-tag
103      */

104     protected String JavaDoc getPath() throws JspTagException JavaDoc {
105         return path.getString(this);
106     }
107
108     protected QueryContainer getListContainer() throws JspTagException JavaDoc {
109         return (QueryContainer) findParentTag(ListContainerTag.class, (String JavaDoc) container.getValue(this), false);
110     }
111
112     /**
113      * Performs the search
114      */

115     public int doStartTag() throws JspTagException JavaDoc {
116         int superresult = doStartTagHelper();
117         if (superresult != NOT_HANDLED) {
118             return superresult;
119         }
120         QueryContainer c = getListContainer();
121
122
123         Query query;
124         if (c == null || path != Attribute.NULL) {
125             // old-style, container-less working
126
if (path == Attribute.NULL) {
127                 throw new JspTagException JavaDoc("Path attribute is mandatory if referid not speficied");
128             }
129
130             String JavaDoc distinctString = distinct.getString(this).toLowerCase();
131             boolean searchDistinct = false;
132             if ("true".equals(distinctString) || "yes".equals(distinctString)) {
133                 searchDistinct = true;
134             }
135
136
137             String JavaDoc searchString = search.getString(this).toUpperCase();
138             if (searchString.equals("")) {
139                 searchString = "BOTH";
140             } else if (
141                 !searchString.equals("BOTH")
142                     && !searchString.equals("EITHER")
143                     && !searchString.equals("SOURCE")
144                     && !searchString.equals("DESTINATION")
145                     && !searchString.equals("ALL")) {
146                 throw new JspTagException JavaDoc("Search should be one of BOTH, SOURCE, " + "DESTINATION, EITHER, or ALL (value found was " + searchString + ")");
147             }
148
149             if (log.isDebugEnabled()) {
150                 log.debug("pathstring " + path.getString(this));
151                 log.debug("directions " + directions);
152                 log.debug("searchString " + searchString);
153             }
154
155             query = Queries.createQuery(getCloudVar(),
156                     getSearchNodes(),
157                     getPath(),
158                     fields.getString(this),
159                     (String JavaDoc) constraints.getValue(this),
160                     (String JavaDoc) orderby.getValue(this),
161                     (String JavaDoc) directions.getValue(this),
162                     searchString,
163                     searchDistinct);
164         } else { // container found!
165
if (path != Attribute.NULL || search != Attribute.NULL) {
166                 throw new JspTagException JavaDoc("search and path attributes not supported within a container.");
167             }
168
169             query = c.getQuery();
170             if (constraints != Attribute.NULL) {
171                 Queries.addConstraints(query, (String JavaDoc) constraints.getValue(this));
172             }
173             if (orderby != Attribute.NULL) {
174                 Queries.addSortOrders(query, (String JavaDoc) orderby.getValue(this), (String JavaDoc) directions.getValue(this));
175             }
176
177             if (fields != Attribute.NULL) {
178                 query.removeFields();
179                 Queries.addFields(query, fields.getString(this));
180             }
181             if (distinct != Attribute.NULL) {
182                 query.setDistinct(distinct.getBoolean(this, false));
183             }
184             if (nodes != Attribute.NULL) {
185                 Queries.addStartNodes(query, nodes.getString(this));
186             }
187
188         }
189
190         NodesAndTrim result = getNodesAndTrim(query);
191         return setReturnValues(result.nodeList, result.needsTrim);
192
193     }
194
195 }
196
Popular Tags