1 10 package org.mmbase.bridge.jsp.taglib.containers; 11 12 import javax.servlet.jsp.JspTagException ; 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 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 c) throws JspTagException { 42 container = getAttribute(c); 43 } 44 45 48 public void setField(String f) throws JspTagException { field = getAttribute(f); 50 } 51 52 public void setElement(String e) throws JspTagException { 53 element = getAttribute(e); 54 } 55 56 public void setMinage(String a) throws JspTagException { 57 minAge = getAttribute(a); 58 } 59 60 public void setMaxage(String a) throws JspTagException { 61 maxAge = getAttribute(a); 62 } 63 64 public void setInverse(String i) throws JspTagException { 65 inverse = getAttribute(i); 66 } 67 68 69 protected int getDayMark(int age) throws JspTagException { 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 day = new Integer (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 { 98 99 if (minAge == Attribute.NULL && maxAge == Attribute.NULL) { 100 throw new JspTagException ("Either 'minage' or 'maxage' (or both) attributes must be present"); 101 } 102 QueryContainer c = (QueryContainer) findParentTag(QueryContainer.class, (String ) container.getValue(this)); 103 Query query = c.getQuery(); 104 105 String 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 ("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 128 if (maxAgeInt != -1 && minAgeInt > 0) { 129 int maxMarker = getDayMark(maxAgeInt); 130 if (maxMarker > 0) { 131 newConstraint = query.createConstraint(stepField, new Integer (maxMarker + 1), new Integer (getDayMark(minAgeInt - 1))); 133 } else { 134 newConstraint = query.createConstraint(stepField, FieldCompareConstraint.LESS_EQUAL, new Integer (getDayMark(minAgeInt - 1))); 135 } 136 } else if (maxAgeInt != -1) { int maxMarker = getDayMark(maxAgeInt); 138 if (maxMarker > 0) { 139 newConstraint = query.createConstraint(stepField, FieldCompareConstraint.GREATER_EQUAL, new Integer (maxMarker + 1)); 140 } 141 } else if (minAgeInt > 0) { 142 newConstraint = query.createConstraint(stepField, FieldCompareConstraint.LESS_EQUAL, new Integer (getDayMark(minAgeInt - 1))); 143 } else { 144 } 146 147 if (newConstraint != null) { 148 if (inverse.getBoolean(this, false)) { 149 query.setInverse(newConstraint, true); 150 } 151 152 QueryCompositeConstraintTag cons = (QueryCompositeConstraintTag) findParentTag(QueryCompositeConstraintTag.class, (String ) 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 |