1 10 package org.mmbase.bridge.jsp.taglib; 11 12 import org.mmbase.bridge.jsp.taglib.util.*; 13 import org.mmbase.bridge.jsp.taglib.containers.*; 14 15 import java.io.IOException ; 16 17 import javax.servlet.jsp.JspTagException ; 18 import javax.servlet.jsp.jstl.core.LoopTagStatus; 19 20 import java.util.*; 21 import org.mmbase.bridge.*; 22 28 public class FieldListTag extends FieldReferrerTag implements ListProvider, FieldProvider, QueryContainerReferrer { 29 30 private FieldList returnList; 31 private FieldIterator fieldIterator; 32 private Field currentField; 33 private int currentItemIndex= -1; 34 35 private Attribute nodeManagerAtt = Attribute.NULL; 36 private Attribute container = Attribute.NULL; 37 private NodeProvider nodeProvider = null; 38 39 private Attribute type = Attribute.NULL; 40 41 protected Attribute add= Attribute.NULL; 42 protected Attribute retain = Attribute.NULL; 43 protected Attribute remove = Attribute.NULL; 44 private Attribute comparator = Attribute.NULL; 45 46 public int size(){ 47 return returnList.size(); 48 } 49 public int getIndex() { 50 return currentItemIndex; 51 } 52 53 public int getIndexOffset() { 54 return 1; 55 } 56 57 public Object getCurrent() { 58 return currentField; 59 } 60 61 public boolean isChanged() { 62 return true; 63 } 64 public void remove() { 65 fieldIterator.remove(); 66 } 67 68 public Field getFieldVar() { 69 return currentField; 70 } 71 72 public void setNodetype(String t) throws JspTagException { 73 nodeManagerAtt = getAttribute(t); 74 } 75 public void setContainer(String c) throws JspTagException { 76 container = getAttribute(c); 77 } 78 79 public void setType(String t) throws JspTagException { 80 type = getAttribute(t); 81 } 82 protected int getType() throws JspTagException { 83 if (type == Attribute.NULL) return NodeManager.ORDER_NONE; 84 String t = type.getString(this).toLowerCase(); 85 if("create".equals(t)) { 86 return NodeManager.ORDER_CREATE; 87 } else if ("edit".equals(t)) { 88 return NodeManager.ORDER_EDIT; 89 } else if ("list".equals(t)) { 90 return NodeManager.ORDER_LIST; 91 } else if ("search".equals(t)) { 92 return NodeManager.ORDER_SEARCH; 93 } else if ("all".equals(t)) { 94 return NodeManager.ORDER_NONE; 95 } else { 96 throw new JspTagException ("Unknown field order type " + t); 97 } 98 } 99 100 private String varType = null; 101 private String jspVar = null; 102 public void setVartype(String t) { 103 varType = t; 104 } 105 public void setJspvar(String j) { 106 jspVar = j; 107 } 108 109 110 private Attribute fields = Attribute.NULL; 111 112 public void setFields(String f) throws JspTagException { 113 fields = getAttribute(f); 114 } 115 protected List getFields() throws JspTagException { 116 return fields.getList(this); 117 } 118 119 public NodeProvider getNodeProvider() { 120 return nodeProvider; 121 } 122 123 public Node getNodeVar() throws JspTagException { 124 129 nodeProvider = findNodeProvider(false); 130 if (nodeProvider == null) return null; 131 return nodeProvider.getNodeVar(); 132 } 133 134 135 public void setAdd(String a) throws JspTagException { 136 add = getAttribute(a); 137 } 138 139 public void setRetain(String r) throws JspTagException { 140 retain = getAttribute(r); 141 } 142 143 public void setRemove(String r) throws JspTagException { 144 remove = getAttribute(r); 145 } 146 147 148 public void setComparator(String c) throws JspTagException { 149 comparator = getAttribute(c); 150 } 151 152 153 156 private ContextCollector collector; 157 158 159 160 public ContextContainer getContextContainer() { 162 return collector.getContextContainer(); 163 } 164 165 166 169 protected NodeManager getNodeManagerFromQuery(String id, boolean exception) throws JspTagException { 170 NodeQueryContainer qc = (NodeQueryContainer) findParentTag(NodeQueryContainer.class, container.getString(this), exception); 171 NodeQuery query = qc.getNodeQuery(); 172 return query.getNodeManager(); 173 } 174 175 178 public int doStartTag() throws JspTagException { 179 collector = new ContextCollector(getContextProvider()); 180 181 if (getReferid() != null) { 182 if (nodeManagerAtt != Attribute.NULL || type != Attribute.NULL) { 183 throw new JspTagException ("Cannot specify referid attribute together with nodetype/type attributes"); 184 } 185 Object o = getObject(getReferid()); 186 if (! (o instanceof FieldList)) { 187 throw new JspTagException ("Context variable " + getReferid() + " is not a FieldList, but " + (o == null ? "NULL" : "a " + o.getClass().getName())); 188 } 189 if (getReferid().equals(getId())) { getContextProvider().getContextContainer().unRegister(getId()); 191 } 192 returnList = (FieldList) o; 193 } else { 194 NodeManager nodeManager; 195 196 if (nodeManagerAtt == Attribute.NULL) { if (container != Attribute.NULL) { 198 nodeManager = getNodeManagerFromQuery(container.getString(this), true); 199 } else { 200 Node n = getNodeVar(); 201 if (n == null) { 202 nodeManager = getNodeManagerFromQuery(null, false); 203 if (nodeManager == null) { 204 throw new JspTagException ("Fieldlist tag must be used either as node-referrer, or use the nodetype attribute, or 'container' attribute."); 205 } 206 } else { 207 nodeManager = n.getNodeManager(); 208 } 209 } 210 } else { 211 nodeManager = getCloudVar().getNodeManager(nodeManagerAtt.getString(this)); 212 } 213 214 if (type != Attribute.NULL) { 215 returnList = nodeManager.getFields(getType()); 216 if (fields != Attribute.NULL) { 217 Iterator i = getFields().iterator(); 218 while (i.hasNext()) { 219 returnList.add(nodeManager.getField((String ) i.next())); 220 } 221 } 222 223 } else { 224 returnList = nodeManager.getFields(); 225 if (fields != Attribute.NULL) { 226 returnList.clear(); 227 Iterator i = getFields().iterator(); 228 while (i.hasNext()) { 229 String fieldName = (String ) i.next(); 230 if (fieldName.endsWith("?")) { 231 fieldName = fieldName.substring(0,fieldName.length()-1); 232 if (!nodeManager.hasField(fieldName)) continue; 233 } 234 returnList.add(nodeManager.getField(fieldName)); 235 } 236 } 237 } 238 } 239 if (add != Attribute.NULL) { 240 Object addObject = getObject(add.getString(this)); 241 if (addObject instanceof Collection) { 242 returnList.addAll((Collection) addObject); 243 } else { 244 returnList.add(addObject); 245 } 246 } 247 if (retain != Attribute.NULL) { 248 Object retainObject = getObject(retain.getString(this)); 249 if (retainObject instanceof Collection) { 250 returnList.retainAll((Collection) retainObject); 251 } else { 252 returnList.retainAll(Collections.singletonList(retainObject)); 253 } 254 } 255 if (remove != Attribute.NULL) { 256 Object removeObject = getObject(remove.getString(this)); 257 if (removeObject instanceof Collection) { 258 returnList.removeAll((Collection) removeObject); 259 } else { 260 returnList.remove(removeObject); 261 } 262 } 263 264 ListSorter.sort(returnList, (String ) comparator.getValue(this), this); 265 fieldIterator = returnList.fieldIterator(); 266 267 currentItemIndex= -1; 270 if (fieldIterator.hasNext()) { 273 return EVAL_BODY_BUFFERED; 274 } else { 275 return SKIP_BODY; 276 } 277 } 278 279 public int doAfterBody() throws JspTagException { 280 if (getId() != null) { 281 getContextProvider().getContextContainer().unRegister(getId()); 282 } 283 284 collector.doAfterBody(); 285 286 if (fieldIterator.hasNext()){ 287 doInitBody(); 288 return EVAL_BODY_AGAIN; 289 } else { 290 if (bodyContent != null) { 291 try { 292 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 293 } catch (IOException ioe){ 294 throw new TaglibException(ioe); 295 } 296 } 297 return SKIP_BODY; 298 } 299 } 300 301 302 public int doEndTag() throws JspTagException { 303 if (getId() != null) { 304 getContextProvider().getContextContainer().register(getId(), returnList, false); 305 } 306 super.doEndTag(); 307 return EVAL_PAGE; 308 } 309 310 public void doFinally() { 311 collector = null; 312 returnList = null; 313 fieldIterator = null; 314 currentField = null; 315 nodeProvider = null; 316 super.doFinally(); 317 } 318 319 public void doInitBody() throws JspTagException { 320 if (fieldIterator.hasNext()){ 321 currentItemIndex ++; 322 currentField = fieldIterator.nextField(); 323 if (jspVar != null) { 324 switch (WriterHelper.stringToType(varType == null ? "field" : varType)) { 325 case WriterHelper.TYPE_FIELD: pageContext.setAttribute(jspVar, currentField); break; 326 case WriterHelper.TYPE_FIELDVALUE: pageContext.setAttribute(jspVar, getNodeVar().getFieldValue(currentField)); break; 327 default: throw new JspTagException ("Unsupported value for vartype attribute '" + varType + "'"); 328 } 329 } 330 if (getId() != null) { 331 getContextProvider().getContextContainer().register(getId(), currentField); 332 } 333 } 334 } 335 public LoopTagStatus getLoopStatus() { 336 return new ListProviderLoopTagStatus(this); 337 } 338 } 339 340 | Popular Tags |