KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Queries;
16 import org.mmbase.bridge.jsp.taglib.CloudReferrerTag;
17 import org.mmbase.bridge.jsp.taglib.util.Attribute;
18 import org.mmbase.storage.search.*;
19 //import org.mmbase.util.logging.*;
20

21 /**
22  * The most general 'constraint' tag. Constraints are constructed with a bunch of attributes.
23  *
24  * @author Michiel Meeuwissen
25  * @since MMBase-1.7
26  * @version $Id: QueryConstraintTag.java,v 1.6 2005/12/27 22:17:14 michiel Exp $
27  */

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

32     protected Attribute container = Attribute.NULL;
33
34     protected Attribute operator = Attribute.NULL;
35
36     protected Attribute field = Attribute.NULL;
37     protected Attribute value = Attribute.NULL;
38     protected Attribute referid = Attribute.NULL;
39
40     protected Attribute value2 = Attribute.NULL; // needed for BETWEEN
41
protected Attribute referid2 = Attribute.NULL; // needed for BETWEEN
42

43     protected Attribute inverse = Attribute.NULL;
44
45     protected Attribute field2 = Attribute.NULL;
46
47     protected Attribute caseSensitive = Attribute.NULL;
48
49     protected Attribute part = Attribute.NULL; // for dates
50

51     public void setContainer(String JavaDoc c) throws JspTagException JavaDoc {
52         container = getAttribute(c);
53     }
54
55     public void setField(String JavaDoc f) throws JspTagException JavaDoc {
56         field = getAttribute(f);
57     }
58
59     public void setField2(String JavaDoc f) throws JspTagException JavaDoc {
60         field2 = getAttribute(f);
61     }
62
63     public void setValue(String JavaDoc v) throws JspTagException JavaDoc {
64         value = getAttribute(v);
65     }
66
67     public void setValue2(String JavaDoc v) throws JspTagException JavaDoc {
68         value2 = getAttribute(v);
69     }
70
71     public void setReferid(String JavaDoc r) throws JspTagException JavaDoc {
72         referid = getAttribute(r);
73     }
74
75     public void setReferid2(String JavaDoc r) throws JspTagException JavaDoc {
76         referid2 = getAttribute(r);
77     }
78
79     public void setOperator(String JavaDoc o) throws JspTagException JavaDoc {
80         operator = getAttribute(o);
81     }
82
83     public void setInverse(String JavaDoc i) throws JspTagException JavaDoc {
84         inverse = getAttribute(i);
85     }
86
87     public void setCasesensitive(String JavaDoc c) throws JspTagException JavaDoc {
88         caseSensitive = getAttribute(c);
89     }
90
91     public void setPart(String JavaDoc p) throws JspTagException JavaDoc {
92         part = getAttribute(p);
93     }
94
95     public boolean getCaseSensitive() throws JspTagException JavaDoc {
96         String JavaDoc cs = caseSensitive.getString(this).toUpperCase();
97         if (cs.equals("") || cs.equals("FALSE")) {
98             return false;
99         } else if (cs.equals("TRUE")) {
100             return true;
101         } else {
102             throw new JspTagException JavaDoc("Unknown value '" + cs + "' for casesensitive attribute");
103         }
104     }
105
106     private Constraint addConstraint(Query query) throws JspTagException JavaDoc {
107         int op = Queries.getOperator(operator.getString(this));
108
109         Object JavaDoc compareValue;
110         if (value != Attribute.NULL) {
111             if (referid != Attribute.NULL || field2 != Attribute.NULL) throw new JspTagException JavaDoc("Can specify only one of value, referid and field2 attributes on constraint tag");
112             if (op == Queries.OPERATOR_IN) {
113                 compareValue = value.getList(this);
114             } else {
115                 compareValue = value.getString(this);
116             }
117         } else if (referid != Attribute.NULL) {
118             if (field2 != Attribute.NULL) throw new JspTagException JavaDoc("Can specify only one of value, referid and field2 attributes on constraint tag");
119             compareValue = getObject(referid.getString(this));
120         } else if (field2 != Attribute.NULL) {
121             compareValue = query.createStepField(field2.getString(this));
122         } else {
123             if (op != Queries.OPERATOR_NULL) {
124                 throw new JspTagException JavaDoc("Should specify one of value, referid and field2 attributes on constraint tag (unless operator is NULL)");
125             } else {
126                 compareValue = null;
127             }
128         }
129
130         Object JavaDoc compareValue2 = null;
131         if (op == Queries.OPERATOR_BETWEEN) {
132             if (value2 != Attribute.NULL) {
133                 if (referid != Attribute.NULL) throw new JspTagException JavaDoc("Can specify only one of value2, referid2 attributes on constraint tag");
134                 compareValue2 = value2.getString(this);
135             } else if (referid2 != Attribute.NULL) {
136                 compareValue2 = getObject(referid2.getString(this));
137             } else {
138                 throw new JspTagException JavaDoc("Should specify one of value2, referid2 attributes on constraint tag if operator is 'BETWEEN'");
139             }
140         }
141
142         Constraint newConstraint = Queries.createConstraint(query, field.getString(this), Queries.getOperator(operator.getString(this)),
143                                                             compareValue, compareValue2, getCaseSensitive(), Queries.getDateTimePart(part.getString(this)));
144
145         //buildConstraint(query, field.getString(this), field2.getString(this), getOperator(), value.getString(this), value2.getString(this), getCaseSensitive());
146

147         // if there is a OR or an AND tag, add
148
// the constraint to that tag,
149
// otherwise add it direct to the query
150
QueryCompositeConstraintTag cons = (QueryCompositeConstraintTag) findParentTag(QueryCompositeConstraintTag.class, (String JavaDoc) container.getValue(this), false);
151         if (cons!=null) {
152             cons.addChildConstraint(newConstraint);
153         } else {
154             newConstraint = Queries.addConstraint(query, newConstraint);
155         }
156         return newConstraint;
157     }
158
159     public int doStartTag() throws JspTagException JavaDoc {
160         QueryContainer c = (QueryContainer) findParentTag(QueryContainer.class, (String JavaDoc) container.getValue(this));
161
162         Query query = c.getQuery();
163         Constraint cons = addConstraint(query);
164         if (inverse.getBoolean(this, false)) {
165             query.setInverse(cons, true);
166         }
167         findWriter(false); // just to call haveBody.., because constraint is not officially a
168
// writerreferer (but e.g. _ can be used in attributes)
169
return SKIP_BODY;
170     }
171
172 }
173
Popular Tags