KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Cognate of daymarkers.
23  *
24  * @author Michiel Meeuwissen
25  * @since MMBase-1.7
26  * @version $Id: QueryAgeConstraintTag.java,v 1.6 2005/05/28 09:10:15 michiel Exp $
27  * @see org.mmbase.module.builders.DayMarkers
28  */

29 public class QueryAgeConstraintTag extends CloudReferrerTag implements QueryContainerReferrer {
30
31     private static final Logger log = Logging.getLoggerInstance(QueryAgeConstraintTag.class);
32
33     protected Attribute container = Attribute.NULL;
34
35     protected Attribute field = Attribute.NULL;
36     protected Attribute element = Attribute.NULL;
37     protected Attribute minAge = Attribute.NULL;
38     protected Attribute maxAge = Attribute.NULL;
39     protected Attribute inverse = Attribute.NULL;
40
41     public void setContainer(String JavaDoc c) throws JspTagException JavaDoc {
42         container = getAttribute(c);
43     }
44
45     /**
46      * @deprecated Use {@link #setElement}
47      */

48     public void setField(String JavaDoc f) throws JspTagException JavaDoc { // default to 'number'
49
field = getAttribute(f);
50     }
51
52     public void setElement(String JavaDoc e) throws JspTagException JavaDoc {
53         element = getAttribute(e);
54     }
55
56     public void setMinage(String JavaDoc a) throws JspTagException JavaDoc {
57         minAge = getAttribute(a);
58     }
59
60     public void setMaxage(String JavaDoc a) throws JspTagException JavaDoc {
61         maxAge = getAttribute(a);
62     }
63
64     public void setInverse(String JavaDoc i) throws JspTagException JavaDoc {
65         inverse = getAttribute(i);
66     }
67
68
69     protected int getDayMark(int age) throws JspTagException JavaDoc {
70         log.debug("finding day mark for " + age + " days ago");
71         Cloud cloud = getCloudVar();
72         NodeManager dayMarks = cloud.getNodeManager("daymarks");
73         NodeQuery query = dayMarks.createQuery();
74         StepField step = query.createStepField("daycount");
75         int currentDay = (int) (System.currentTimeMillis()/(1000*60*60*24));
76         Integer JavaDoc day = new Integer JavaDoc(currentDay - age);
77         if (log.isDebugEnabled()) {
78             log.debug("today : " + currentDay + " requested " + day);
79         }
80         Constraint constraint = query.createConstraint(step, FieldCompareConstraint.LESS_EQUAL, day);
81         query.setConstraint(constraint);
82         query.addSortOrder(query.createStepField("daycount"), SortOrder.ORDER_DESCENDING);
83         query.setMaxNumber(1);
84
85         NodeList result = dayMarks.getList(query);
86         if (result.size() == 0) {
87             return -1;
88         } else {
89             return result.getNode(0).getIntValue("mark");
90         }
91
92
93     }
94
95
96
97     public int doStartTag() throws JspTagException JavaDoc {
98
99         if (minAge == Attribute.NULL && maxAge == Attribute.NULL) {
100             throw new JspTagException JavaDoc("Either 'minage' or 'maxage' (or both) attributes must be present");
101         }
102         QueryContainer c = (QueryContainer) findParentTag(QueryContainer.class, (String JavaDoc) container.getValue(this));
103         Query query = c.getQuery();
104
105         String JavaDoc fieldName;
106         if (field == Attribute.NULL && element == Attribute.NULL) {
107             fieldName = "number";
108         } else if (field != Attribute.NULL) {
109             if(element != Attribute.NULL) {
110                 throw new JspTagException JavaDoc("Could not specify both 'field' and 'element' attributes on ageconstraint");
111             }
112             fieldName = field.getString(this);
113         } else {
114             fieldName = element.getString(this) + ".number";
115         }
116
117         StepField stepField = query.createStepField(fieldName);
118
119         Constraint newConstraint = null;
120
121         int minAgeInt = minAge.getInt(this, -1);
122         int maxAgeInt = maxAge.getInt(this, -1);
123         // if minimal age given:
124
// you need the day marker of the day after that (hence -1 in code below inside the getDayMark), node have to have this number or lower
125
// if maximal age given:
126
// daymarker object of that age must be included, but last object of previous day not, hece the +1 outside the getDayMark
127

128         if (maxAgeInt != -1 && minAgeInt > 0) {
129             int maxMarker = getDayMark(maxAgeInt);
130             if (maxMarker > 0) {
131                 // BETWEEN constraint
132
newConstraint = query.createConstraint(stepField, new Integer JavaDoc(maxMarker + 1), new Integer JavaDoc(getDayMark(minAgeInt - 1)));
133             } else {
134                 newConstraint = query.createConstraint(stepField, FieldCompareConstraint.LESS_EQUAL, new Integer JavaDoc(getDayMark(minAgeInt - 1)));
135             }
136         } else if (maxAgeInt != -1) { // only on max
137
int maxMarker = getDayMark(maxAgeInt);
138             if (maxMarker > 0) {
139                 newConstraint = query.createConstraint(stepField, FieldCompareConstraint.GREATER_EQUAL, new Integer JavaDoc(maxMarker + 1));
140             }
141         } else if (minAgeInt > 0) {
142             newConstraint = query.createConstraint(stepField, FieldCompareConstraint.LESS_EQUAL, new Integer JavaDoc(getDayMark(minAgeInt - 1)));
143         } else {
144             // both unspecified
145
}
146
147         if (newConstraint != null) {
148             if (inverse.getBoolean(this, false)) {
149                 query.setInverse(newConstraint, true);
150             }
151
152             // if there is a OR or an AND tag, add
153
// the constraint to that tag,
154
// otherwise add it direct to the query
155
QueryCompositeConstraintTag cons = (QueryCompositeConstraintTag) findParentTag(QueryCompositeConstraintTag.class, (String JavaDoc) container.getValue(this), false);
156             if (cons!=null) {
157                 cons.addChildConstraint(newConstraint);
158             } else {
159                 Queries.addConstraint(query, newConstraint);
160             }
161         }
162
163         return SKIP_BODY;
164     }
165
166 }
167
Popular Tags