KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > FieldListTag


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;
11
12 import org.mmbase.bridge.jsp.taglib.util.*;
13 import org.mmbase.bridge.jsp.taglib.containers.*;
14
15 import java.io.IOException JavaDoc;
16
17 import javax.servlet.jsp.JspTagException JavaDoc;
18 import javax.servlet.jsp.jstl.core.LoopTagStatus;
19
20 import java.util.*;
21 import org.mmbase.bridge.*;
22 /**
23  * This class makes a tag which can list the fields of a NodeManager.
24  *
25  * @author Michiel Meeuwissen
26  * @version $Id: FieldListTag.java,v 1.54 2006/07/17 15:38:47 johannes Exp $
27  */

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 JavaDoc 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 JavaDoc t) throws JspTagException JavaDoc {
73         nodeManagerAtt = getAttribute(t);
74     }
75     public void setContainer(String JavaDoc c) throws JspTagException JavaDoc {
76         container = getAttribute(c);
77     }
78
79     public void setType(String JavaDoc t) throws JspTagException JavaDoc {
80         type = getAttribute(t);
81     }
82     protected int getType() throws JspTagException JavaDoc {
83         if (type == Attribute.NULL) return NodeManager.ORDER_NONE;
84         String JavaDoc 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 JavaDoc("Unknown field order type " + t);
97         }
98     }
99
100     private String JavaDoc varType = null;
101     private String JavaDoc jspVar = null;
102     public void setVartype(String JavaDoc t) {
103         varType = t;
104     }
105     public void setJspvar(String JavaDoc j) {
106         jspVar = j;
107     }
108
109
110     private Attribute fields = Attribute.NULL;
111
112     public void setFields(String JavaDoc f) throws JspTagException JavaDoc {
113         fields = getAttribute(f);
114     }
115     protected List getFields() throws JspTagException JavaDoc {
116         return fields.getList(this);
117     }
118
119     public NodeProvider getNodeProvider() {
120         return nodeProvider;
121     }
122
123     public Node getNodeVar() throws JspTagException JavaDoc {
124         /*
125         if (nodeManagerString != null) {
126             return null;
127         }
128         */

129         nodeProvider = findNodeProvider(false);
130         if (nodeProvider == null) return null;
131         return nodeProvider.getNodeVar();
132     }
133
134
135     public void setAdd(String JavaDoc a) throws JspTagException JavaDoc {
136         add = getAttribute(a);
137     }
138
139     public void setRetain(String JavaDoc r) throws JspTagException JavaDoc {
140         retain = getAttribute(r);
141     }
142
143     public void setRemove(String JavaDoc r) throws JspTagException JavaDoc {
144         remove = getAttribute(r);
145     }
146
147
148     public void setComparator(String JavaDoc c) throws JspTagException JavaDoc {
149         comparator = getAttribute(c);
150     }
151
152
153     /**
154      * Lists do implement ContextProvider
155      */

156     private ContextCollector collector;
157
158
159
160     // ContextProvider implementation
161
public ContextContainer getContextContainer() {
162         return collector.getContextContainer();
163     }
164
165
166     /**
167      * @since MMBase-1.8.1
168      */

169     protected NodeManager getNodeManagerFromQuery(String JavaDoc id, boolean exception) throws JspTagException JavaDoc {
170         NodeQueryContainer qc = (NodeQueryContainer) findParentTag(NodeQueryContainer.class, container.getString(this), exception);
171         NodeQuery query = qc.getNodeQuery();
172         return query.getNodeManager();
173     }
174
175     /**
176      *
177      **/

178     public int doStartTag() throws JspTagException JavaDoc{
179         collector = new ContextCollector(getContextProvider());
180
181         if (getReferid() != null) {
182             if (nodeManagerAtt != Attribute.NULL || type != Attribute.NULL) {
183                 throw new JspTagException JavaDoc("Cannot specify referid attribute together with nodetype/type attributes");
184             }
185             Object JavaDoc o = getObject(getReferid());
186             if (! (o instanceof FieldList)) {
187                 throw new JspTagException JavaDoc("Context variable " + getReferid() + " is not a FieldList, but " + (o == null ? "NULL" : "a " + o.getClass().getName()));
188             }
189             if (getReferid().equals(getId())) { // in such a case, don't whine
190
getContextProvider().getContextContainer().unRegister(getId());
191             }
192             returnList = (FieldList) o;
193         } else {
194             NodeManager nodeManager;
195
196             if (nodeManagerAtt == Attribute.NULL) { // living as NodeReferrer, or Query-referrer
197
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 JavaDoc("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 JavaDoc) 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 JavaDoc fieldName = (String JavaDoc) 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc) comparator.getValue(this), this);
265         fieldIterator = returnList.fieldIterator();
266
267         //this is where we do the search
268
currentItemIndex= -1; // reset index
269

270         // if we get a result from the query
271
// evaluate the body , else skip the body
272
if (fieldIterator.hasNext()) {
273             return EVAL_BODY_BUFFERED;
274         } else {
275             return SKIP_BODY;
276         }
277     }
278
279     public int doAfterBody() throws JspTagException JavaDoc {
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 JavaDoc ioe){
294                     throw new TaglibException(ioe);
295                 }
296             }
297             return SKIP_BODY;
298         }
299     }
300
301
302     public int doEndTag() throws JspTagException JavaDoc {
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 JavaDoc {
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 JavaDoc("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