KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Alias as constraint.
23  *
24  * @author Michiel Meeuwissen
25  * @since MMBase-1.7
26  * @version $Id: QueryAliasConstraintTag.java,v 1.7 2005/01/30 16:46:34 nico Exp $
27  */

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