KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > containers > QueryTypeConstraintTag


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.containers;
11
12 import javax.servlet.jsp.JspTagException JavaDoc;
13
14 import org.mmbase.bridge.*;
15 import org.mmbase.bridge.jsp.taglib.CloudReferrerTag;
16 import org.mmbase.bridge.jsp.taglib.util.Attribute;
17 import org.mmbase.bridge.util.Queries;
18 import org.mmbase.storage.search.*;
19 import java.util.*;
20
21 /**
22  * Type as constraint.
23  *
24  * @author Michiel Meeuwissen
25  * @since MMBase-1.8
26  * @version $Id: QueryTypeConstraintTag.java,v 1.4 2005/05/02 11:54:31 michiel Exp $
27  */

28 public class QueryTypeConstraintTag extends CloudReferrerTag implements QueryContainerReferrer {
29
30     // private static final Logger log = Logging.getLoggerInstance(NodeListAliasConstraintTag.class);
31

32     protected Attribute container = Attribute.NULL;
33
34     protected Attribute element = Attribute.NULL;
35     protected Attribute name = Attribute.NULL;
36
37     protected Attribute inverse = Attribute.NULL;
38     protected Attribute descendants = Attribute.NULL;
39
40     public void setContainer(String JavaDoc c) throws JspTagException JavaDoc {
41         container = getAttribute(c);
42     }
43
44     public void setElement(String JavaDoc e) throws JspTagException JavaDoc {
45         element = getAttribute(e);
46     }
47
48     public void setName(String JavaDoc n) throws JspTagException JavaDoc {
49         name = getAttribute(n);
50     }
51
52
53     public void setInverse(String JavaDoc i) throws JspTagException JavaDoc {
54         inverse = getAttribute(i);
55     }
56
57     public void setDescendants(String JavaDoc d) throws JspTagException JavaDoc {
58         descendants = getAttribute(d);
59     }
60
61
62     protected SortedSet getOTypes(List names) throws JspTagException JavaDoc {
63         Cloud cloud = getCloudVar();
64         SortedSet set = new TreeSet();
65         Iterator i = names.iterator();
66         boolean desc = descendants.getBoolean(this, true);
67         while (i.hasNext()) {
68             NodeManager nm = cloud.getNodeManager((String JavaDoc) i.next());
69             set.add(new Integer JavaDoc(nm.getNumber()));
70             if (desc) {
71                 NodeManagerIterator j = nm.getDescendants().nodeManagerIterator();
72                 while (j.hasNext()) {
73                     set.add(new Integer JavaDoc(j.nextNodeManager().getNumber()));
74                 }
75             }
76         }
77         return set;
78     }
79
80
81
82     public int doStartTag() throws JspTagException JavaDoc {
83         QueryContainer c = (QueryContainer) findParentTag(QueryContainer.class, (String JavaDoc) container.getValue(this));
84         Query query = c.getQuery();
85         String JavaDoc elementString = element.getString(this);
86         Step step;
87         if (elementString.equals("")) {
88             if (query instanceof NodeQuery) {
89                 step = ((NodeQuery) query).getNodeStep();
90             } else {
91                 throw new JspTagException JavaDoc("Don't know on what path element the type constraint must be applied. Use the 'element' attribute");
92             }
93         } else {
94             step = query.getStep(elementString);
95         }
96         if (step == null) {
97             throw new JspTagException JavaDoc("No element '" + element.getString(this) + "' in path '" + query.getSteps() + "'");
98         }
99         StepField stepField = query.createStepField(step, "otype");
100
101         Constraint newConstraint = query.createConstraint(stepField, getOTypes(name.getList(this)));
102
103         if (newConstraint != null) {
104             if (inverse.getBoolean(this, false)) {
105                 query.setInverse(newConstraint, true);
106             }
107
108             // if there is a OR or an AND tag, add
109
// the constraint to that tag,
110
// otherwise add it direct to the query
111
QueryCompositeConstraintTag cons = (QueryCompositeConstraintTag) findParentTag(QueryCompositeConstraintTag.class, (String JavaDoc) container.getValue(this), false);
112             if (cons != null) {
113                 cons.addChildConstraint(newConstraint);
114             } else {
115                 Queries.addConstraint(query, newConstraint);
116             }
117         }
118
119         return SKIP_BODY;
120     }
121
122 }
123
Popular Tags