1 10 package org.mmbase.bridge.jsp.taglib; 11 12 13 import java.util.*; 14 import org.mmbase.util.Casting; 15 16 import javax.servlet.jsp.JspTagException ; 17 import javax.servlet.jsp.tagext.*; 18 import javax.servlet.jsp.jstl.core.*; 19 import javax.servlet.http.HttpServletRequest ; 20 21 import org.mmbase.bridge.*; 22 import org.mmbase.bridge.jsp.taglib.util.*; 23 import org.mmbase.storage.search.*; 24 import org.mmbase.util.logging.*; 25 26 35 36 abstract public class AbstractNodeListTag extends AbstractNodeProviderTag implements BodyTag, ListProvider { 37 private static final Logger log = Logging.getLoggerInstance(AbstractNodeListTag.class); 38 39 private static final int QUERY_WARN_SIZE = 1000; 40 41 45 protected Attribute orderby = Attribute.NULL; 46 47 51 protected Attribute directions = Attribute.NULL; 52 53 60 protected Attribute constraints = Attribute.NULL; 61 62 final protected NodeListHelper listHelper = new NodeListHelper(this, nodeHelper); 63 64 private Query generatingQuery; 65 66 protected NodeList getReturnList() { 67 return listHelper.getReturnList(); 68 } 69 70 public Object getCurrent() { 71 return listHelper.getCurrent(); 72 } 73 74 public int getIndex() { 75 return listHelper.getIndex(); 76 } 77 public int getIndexOffset() { 78 return listHelper.getIndexOffset(); 79 } 80 81 public void remove() { 82 listHelper.remove(); 83 } 84 85 public Query getGeneratingQuery() { 86 return generatingQuery; 87 } 88 89 94 public void setOrderby(String orderby) throws JspTagException { 95 this.orderby = getAttribute(orderby); 96 } 97 98 103 public void setDirections(String directions) throws JspTagException { 104 this.directions = getAttribute(directions); 105 } 106 107 111 public void setMax(String max) throws JspTagException { 112 listHelper.setMax(max); 113 } 114 115 119 public void setOffset(String o) throws JspTagException { 120 listHelper.setOffset(o); 121 } 122 123 public void setComparator(String c) throws JspTagException { 124 listHelper.setComparator(c); 125 } 126 public void setAdd(String c) throws JspTagException { 127 listHelper.setAdd(c); 128 } 129 public void setRetain(String c) throws JspTagException { 130 listHelper.setRetain(c); 131 } 132 public void setRemove(String c) throws JspTagException { 133 listHelper.setRemove(c); 134 } 135 136 137 141 public void setConstraints(String where) throws JspTagException { 142 constraints = getAttribute(where); 143 } 144 145 protected static class NodesAndTrim { 146 boolean needsTrim; 147 NodeList nodeList; 148 } 149 150 protected final NodesAndTrim getNodesAndTrim(Query query) throws JspTagException { 151 return getNodesAndTrim(query, 0); 152 } 153 154 160 protected NodesAndTrim getNodesAndTrim(Query query, int more) throws JspTagException { 161 generatingQuery = query; 162 NodesAndTrim result = new NodesAndTrim(); 163 if (listHelper.getComparator().equals("")) { 164 if (listHelper.getMax() != Attribute.NULL) { 165 int m = listHelper.getMax().getInt(this, -1); 166 if (m != -1) { 167 query.setMaxNumber(m + more); 168 } 169 } 170 if (listHelper.getOffset() != Attribute.NULL) { 171 query.setOffset(listHelper.getOffset().getInt(this, 0)); 172 } 173 174 if (query instanceof NodeQuery) { 175 NodeQuery nq = (NodeQuery) query; 176 result.nodeList = nq.getNodeManager().getList(nq); 177 } else { 178 result.nodeList = query.getCloud().getList(query); 179 } 180 result.needsTrim = more > 0; 181 } else { 182 if (query instanceof NodeQuery) { 184 NodeQuery nq = (NodeQuery) query; 185 result.nodeList = nq.getNodeManager().getList(nq); 186 } else { 187 result.nodeList = query.getCloud().getList(query); 188 } 189 190 if(result.nodeList.size() > QUERY_WARN_SIZE) { 192 log.warn("Trying to use compare on a query with result size " + result.nodeList.size() + " > " + QUERY_WARN_SIZE + " in page " + 193 ((HttpServletRequest )pageContext.getRequest()).getRequestURI() + "." + 194 " Note that the attribute 'max' will in combination with the 'comparator' attribute not set a limit on the query" + 195 " (but the result will be limited afterwards). You might want to limit the query in another way (use a container?)"); 196 } 197 result.needsTrim = true; 198 } 199 return result; 200 } 201 202 public ContextContainer getContextContainer() throws JspTagException { 204 return listHelper.getContextContainer(); 205 } 206 207 protected static final int NOT_HANDLED = -100; 208 209 protected int doStartTagHelper() throws JspTagException { 210 log.debug("doStartTaghelper"); 211 212 listHelper.doStartTagHelper(); 213 214 if (getReferid() != null) { 215 Object o = getObject(getReferid()); 216 if (! (o instanceof NodeList)) { 217 if (o instanceof Collection) { 218 NodeList list = getCloudVar().createNodeList(); 219 list.addAll((Collection) o); 220 o = list; 221 } else { 222 throw new JspTagException ("Context variable " + getReferid() + " is not a NodeList (or some other Collection of Nodes), but" + (o == null ? "NULL" : "a " + o.getClass())); 223 } 224 } 225 if (orderby != Attribute.NULL) { 226 throw new JspTagException ("'orderby' attribute does not make sense with 'referid' attribute"); 227 } 228 229 if (directions != Attribute.NULL) { 230 throw new JspTagException ("'directions' attribute does not make sense with 'referid' attribute"); 231 } 232 if (constraints != Attribute.NULL) { 233 throw new JspTagException ("'contraints' attribute does not make sense with 'referid' attribute"); 234 } 235 if (getReferid().equals(getId())) { getContextProvider().getContextContainer().unRegister(getId()); 237 } 238 return setReturnValues((NodeList) o, true); 239 } 240 return NOT_HANDLED; 241 } 242 243 252 protected int setReturnValues(NodeList nodes) throws JspTagException { 253 return setReturnValues(nodes, false); 254 } 255 256 267 protected int setReturnValues(NodeList nodes, boolean trim) throws JspTagException { 268 Query query = (Query) nodes.getProperty(NodeList.QUERY_PROPERTY); 269 270 if (query != null) { 271 List ls = query.getSortOrders(); 273 if (ls.size() > 0) { 274 StepField sf= ((SortOrder)ls.get(0)).getField(); 275 if (query instanceof NodeQuery) { 276 nodes.setProperty("orderby", sf.getFieldName()); 277 } else { 278 nodes.setProperty("orderby", sf.getStep().getAlias() + '.' + sf.getFieldName()); 279 } 280 } 281 } else { 282 if (orderby != Attribute.NULL) nodes.setProperty("orderby", orderby.getString(this)); 283 } 284 285 return listHelper.setReturnValues(nodes, trim); 286 } 287 288 289 public int doAfterBody() throws JspTagException { 290 super.doAfterBody(); 291 return listHelper.doAfterBody(); 292 } 293 294 public int doEndTag() throws JspTagException { 295 listHelper.doEndTag(); 296 return super.doEndTag(); 297 } 298 299 public void doFinally() { 300 generatingQuery = null; 301 listHelper.doFinally(); 302 super.doFinally(); 303 } 304 305 309 public boolean isChanged() { 310 return listHelper.isChanged(); 311 } 312 313 public int size() { 314 return listHelper.size(); 315 } 316 317 public LoopTagStatus getLoopStatus() { 318 return listHelper.getLoopStatus(); 319 } 320 321 } 322 323 | Popular Tags |