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 21 28 public class QueryConstraintTag extends CloudReferrerTag implements QueryContainerReferrer { 29 30 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; protected Attribute referid2 = Attribute.NULL; 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; 51 public void setContainer(String c) throws JspTagException { 52 container = getAttribute(c); 53 } 54 55 public void setField(String f) throws JspTagException { 56 field = getAttribute(f); 57 } 58 59 public void setField2(String f) throws JspTagException { 60 field2 = getAttribute(f); 61 } 62 63 public void setValue(String v) throws JspTagException { 64 value = getAttribute(v); 65 } 66 67 public void setValue2(String v) throws JspTagException { 68 value2 = getAttribute(v); 69 } 70 71 public void setReferid(String r) throws JspTagException { 72 referid = getAttribute(r); 73 } 74 75 public void setReferid2(String r) throws JspTagException { 76 referid2 = getAttribute(r); 77 } 78 79 public void setOperator(String o) throws JspTagException { 80 operator = getAttribute(o); 81 } 82 83 public void setInverse(String i) throws JspTagException { 84 inverse = getAttribute(i); 85 } 86 87 public void setCasesensitive(String c) throws JspTagException { 88 caseSensitive = getAttribute(c); 89 } 90 91 public void setPart(String p) throws JspTagException { 92 part = getAttribute(p); 93 } 94 95 public boolean getCaseSensitive() throws JspTagException { 96 String 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 ("Unknown value '" + cs + "' for casesensitive attribute"); 103 } 104 } 105 106 private Constraint addConstraint(Query query) throws JspTagException { 107 int op = Queries.getOperator(operator.getString(this)); 108 109 Object compareValue; 110 if (value != Attribute.NULL) { 111 if (referid != Attribute.NULL || field2 != Attribute.NULL) throw new JspTagException ("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 ("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 ("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 compareValue2 = null; 131 if (op == Queries.OPERATOR_BETWEEN) { 132 if (value2 != Attribute.NULL) { 133 if (referid != Attribute.NULL) throw new JspTagException ("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 ("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 147 QueryCompositeConstraintTag cons = (QueryCompositeConstraintTag) findParentTag(QueryCompositeConstraintTag.class, (String ) 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 { 160 QueryContainer c = (QueryContainer) findParentTag(QueryContainer.class, (String ) 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); return SKIP_BODY; 170 } 171 172 } 173 | Popular Tags |