KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > deprecated > taglibs > container > ContainerListTag


1 package org.jahia.deprecated.taglibs.container;
2
3 import java.io.IOException JavaDoc;
4 import java.util.Vector JavaDoc;
5
6 import javax.servlet.ServletRequest JavaDoc;
7 import javax.servlet.jsp.JspException JavaDoc;
8 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
9 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
10
11 import org.jahia.data.JahiaData;
12 import org.jahia.data.containers.JahiaContainer;
13 import org.jahia.data.containers.JahiaContainerList;
14 import org.jahia.deprecated.taglibs.util.Utils;
15 import org.jahia.exceptions.JahiaException;
16 import org.jahia.utils.JahiaConsole;
17
18
19
20 /**
21  * Class ContainerListTag : initializes Jahia in order to display a container list.
22  *
23  * A container list is a list of containers; container lists can be imbricated
24  * The name of the container list must be changed in this case, so that all the
25  * container lists of this page have an unique name
26  *
27  * @author Jerome Tamiotti
28  */

29 public class ContainerListTag extends BodyTagSupport JavaDoc {
30
31     private String JavaDoc listName = "";
32     private String JavaDoc title = "";
33     private Vector JavaDoc fields = new Vector JavaDoc();
34     private JahiaContainerList containerList = null;
35
36     // we have two versions of these properties because we have to respect the
37
// JavaBeans design pattern.
38
private int intWindowSize = -1; // -1 means functionality is deactivated by default
39
private int intWindowOffset = -1;
40
41     private String JavaDoc windowSize;
42     private String JavaDoc windowOffset;
43
44     private JahiaData jData = null;
45
46     private boolean display = true;
47     private boolean declarationPass = true;
48     private boolean hasParent = false;
49     private String JavaDoc parentListName = "";
50
51
52     public boolean displayBody() {
53         return display;
54     }
55
56     public void setName(String JavaDoc name) {
57         this.listName = name;
58     }
59
60     public String JavaDoc getName() {
61         return this.listName;
62     }
63
64     public boolean isDeclarationPass() {
65         return declarationPass;
66     }
67
68     public void setTitle(String JavaDoc title) {
69         this.title = title;
70     }
71
72     public String JavaDoc getTitle() {
73         return this.title;
74     }
75
76     public int getSize() {
77         if ( this.getContainerList() != null )
78         {
79             return this.getContainerList().size();
80         }
81         return 0;
82     }
83
84     public void setWindowSize(String JavaDoc windowSize) {
85         try {
86             this.windowSize = windowSize;
87             this.intWindowSize = Integer.parseInt(windowSize);
88         } catch (NumberFormatException JavaDoc nfe) {
89             this.windowSize="";
90             this.intWindowSize = -1;
91         }
92     }
93
94     public String JavaDoc getWindowSize() {
95         return this.windowSize;
96     }
97
98     public void setWindowOffset(String JavaDoc windowOffset) {
99         try {
100             this.windowOffset = windowOffset;
101             this.intWindowOffset = Integer.parseInt(windowOffset);
102         } catch (NumberFormatException JavaDoc nfe) {
103             this.intWindowOffset = -1;
104         }
105     }
106
107     public String JavaDoc getWindowOffset() {
108         return this.windowOffset;
109     }
110
111     public Vector JavaDoc getFields() {
112         return this.fields;
113     }
114
115     public void addField(String JavaDoc fieldName) {
116         if (isDeclarationPass()) {
117             // JahiaConsole.println("ContainerListTag.addField",
118
// "Adding field <"+ fieldName + ">");
119
this.fields.add(fieldName);
120         } else {
121             JahiaConsole.println("ContainerListTag.addField",
122                                  "Not allowed to add field <" + fieldName +
123                                  "> when out of declaration pass, aborting...");
124         }
125     }
126
127     public JahiaContainerList getContainerList() {
128         if (isDeclarationPass()) {
129             JahiaConsole.println("ContainerListTag.getContainerList",
130                                  "ERROR : Can't call this in declaration phase, returning null...");
131             return null;
132         }
133         return this.containerList;
134     }
135
136     public int doStartTag() {
137
138         // default behavior is to activate declaration pass
139
declarationPass = true;
140
141         ServletRequest JavaDoc request = pageContext.getRequest();
142         jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
143
144         // but we must also test the case where this containerList is enclosed
145
// in a parent containerList in which case we just trickle down the
146
// state of the parent list...
147
ContainerListTag parentContainerListTag = (ContainerListTag) findAncestorWithClass(this,ContainerListTag.class);
148         if (parentContainerListTag != null) {
149             parentListName = parentContainerListTag.getName();
150             // we are in an embedded containerList tag, let's check in which
151
// pass we are...
152
declarationPass = parentContainerListTag.isDeclarationPass();
153             hasParent = true;
154
155             if (!declarationPass) {
156                 JahiaConsole.println("ContainerListTag.doStartTag",
157                                      "Getting pass status from parent container list...IN DISPLAY MODE");
158                 // let's do remaining initialisation necessary to do our work...
159
this.listName = Utils.buildUniqueName(parentListName, this.listName);
160                 ContainerTag parentContainerTag = (ContainerTag) findAncestorWithClass(this,ContainerTag.class);
161                 try {
162                     // this is not right we must attach the right instance of the container to the parent container...
163
if (parentContainerTag != null) {
164                         JahiaContainer parentContainer = parentContainerTag.getContainer();
165                         if (parentContainer != null) {
166                             this.containerList = parentContainer.getContainerList(this.listName);
167                         } else {
168                             JahiaConsole.println("ContainerListTag.doStartTag",
169                                                  "Parent container not found...");
170                         }
171                     } else {
172                         JahiaConsole.println("ContainerListTag.doStartTag",
173                                              "Parent container tag not found...");
174                     }
175                 } catch (JahiaException je) {
176                     JahiaConsole.println("ContainerListTag.doStartTag",je.toString());
177                 }
178             } else {
179                 JahiaConsole.println("ContainerListTag.doStartTag",
180                                      "Getting pass status from parent container list...IN DECLARATION MODE");
181             }
182         } else {
183         }
184
185         JahiaConsole.println("ContainerListTag.doStartTag",
186                              "Beginning declaration pass for container list '" +
187                              this.listName + "'");
188
189
190         return EVAL_BODY_BUFFERED;
191     }
192
193
194     // Body is evaluated one time, so just writes it on standard output
195
public int doAfterBody() {
196
197         if (declarationPass) {
198             declarationPass = false;
199             /* let's do all declarative pass stuff here */
200
201             if ((intWindowSize >= 1) && (intWindowOffset == -1)) {
202                 // we allow a windowSize alone.
203
intWindowOffset = 0;
204             }
205             if ((intWindowSize >= 1) && (intWindowOffset >= 0) && (intWindowOffset < intWindowSize)) {
206                 // let's set the parameter to be retrieved by the display
207
// system. This is why we do this at declaration time.
208
if (jData.params().getParameter("ctnscroll_" + this.listName) == null) {
209                     JahiaConsole.println("ContainerListTag.doAfterBody",
210                                          "Setting window parameters to window size " +
211                                          Integer.toString(this.intWindowSize) +
212                                          " and offset " +
213                                          Integer.toString(this.intWindowOffset));
214                     jData.params().setParameter("ctnscroll_" + this.listName,
215                                                 Integer.toString(this.intWindowSize) +
216                                                 "_" +
217                                                 Integer.toString(this.intWindowOffset));
218                 }
219             }
220
221             // searches the current container list inside a container or at root
222
ContainerListTag parentContainerListTag = (ContainerListTag) findAncestorWithClass(this,ContainerListTag.class);
223             ContainerTag containerTag = (ContainerTag) findAncestorWithClass(this,ContainerTag.class);
224             if (parentContainerListTag != null) {
225                 // we are in an embedded containerList tag, let's check in which
226
// pass we are...
227
if (parentContainerListTag.isDeclarationPass()) {
228                     JahiaConsole.println("ContainerListTag.doAfterBody",
229                                          "ContainerList has a parent container list in declaration pass");
230                     // builds the complete list name
231
this.listName = Utils.buildUniqueName(parentContainerListTag.getName(), this.listName);
232                     // checks if the container list has been declared in the current container
233
try {
234                         JahiaConsole.println("ContainerListTag.doAfterBody",
235                                              "Declaring embedded container list with name " +
236                                              this.listName + " title=" + this.title +
237                                              " size=" + this.fields.size());
238                         jData.containers().declareContainer(this.listName, this.title, this.fields, intWindowSize, intWindowOffset);
239                         this.display = false;
240                         return SKIP_BODY; // exit the parsing of this containerList while still in parent declaration pass...
241
// update attribute
242
} catch (JahiaException je) {
243                         JahiaConsole.println("ContainerListTag.doAfterBody",je.toString());
244                     }
245                 } else {
246                     JahiaConsole.println("ContainerListTag.doAfterBody",
247                                          "ContainerList has a parent container list in display pass");
248                     // normally we should never reach this state...
249
}
250             } else {
251                 // at root: WARNING : the container list can be absolute !
252
// that explains the usage of the methods "checkAttributes()" and "checkDeclaration()"
253
// whose code differs in case of absolute container list
254
try {
255                     // first, check if the list has been declared
256
checkAttributes(jData);
257                     try {
258                         checkDeclaration(jData);
259                     } catch (JahiaException je) {
260                         JahiaConsole.println("ContainerListTag.doAfterBody",
261                                              "Container has already been defined, ignoring re-definition and loading content...");
262                     }
263                     // update attribute
264
this.containerList = getContainerList(jData, this.listName);
265                 } catch (JahiaException je) {
266                     JahiaConsole.println("ContainerListTag.doAfterBody",je.toString());
267                 }
268             }
269
270             // JahiaConsole.println("ContainerListTag.doAfterBody","End of declaration pass for container list '" + this.listName + "'");
271
// let's clear the evaluated body content since we won't use it on this pass...
272
BodyContent JavaDoc body= getBodyContent();
273             body.clearBody();
274             return EVAL_BODY_BUFFERED;
275         }
276
277         // JahiaConsole.println("ContainerListTag.doAfterBody", "Post-Declaration Pass");
278

279         if (this.display) {
280             try {
281                 bodyContent.writeOut(bodyContent.getEnclosingWriter());
282             } catch (IOException JavaDoc ioe) {
283                 JahiaConsole.println("ContainerListTag: doAfterBody ", ioe.toString());
284             }
285         }
286         return SKIP_BODY;
287     }
288
289     // the methods below are defined in order to be overrided in the subclasses
290
// AbsoluteContainerListTag and RelativeContainerListTag
291

292     protected void checkAttributes(JahiaData jData) throws JahiaException {
293     }
294
295     protected void checkDeclaration(JahiaData jData) throws JahiaException {
296         /** @todo : this was commented out by Serge Huber
297          * because the call to declareContainer already does these verifications
298          * for us at the beginning of the call.
299          */

300         JahiaConsole.println("ContainerListTag.checkDeclaration",
301                              "Declaring container list with name " +
302                              this.listName + " title=" + this.title +
303                              " size=" + this.fields.size());
304         jData.containers().declareContainer(this.listName, this.title, this.fields, intWindowSize, intWindowOffset);
305         JahiaConsole.println("ContainerListTag.checkDeclaration",
306                              "End declareContainer call with name " +
307                              this.listName);
308
309     }
310
311     // reads the container list from a container set
312
protected JahiaContainerList getContainerList( JahiaData jData,
313                                                    String JavaDoc listName ) throws JahiaException {
314
315         return jData.containers().getContainerList(listName);
316     }
317
318     //--------------------------------------------------------------------------
319
/**
320      * Called by AbstractFieldTag to check if this container list already
321      * contains a field of the given name.
322      *
323      * Author NK
324      */

325     public boolean isFieldAlreadyDeclared(String JavaDoc name){
326         int size = fields.size();
327         String JavaDoc fieldName = "";
328         for ( int i=0 ; i<size ; i++ ){
329             fieldName = (String JavaDoc)fields.get(i);
330             if ( fieldName.equals(name) )
331                 return true;
332         }
333         return false;
334     }
335
336     public int doEndTag() throws JspException JavaDoc {
337         // let's reinitialize the tag variables to allow tag object reuse in
338
// pooling.
339
listName = "";
340         title = "";
341         fields = new Vector JavaDoc();
342         containerList = null;
343
344         intWindowSize = -1; // -1 means functionality is deactivated by default
345
intWindowOffset = -1;
346
347         windowSize = null;
348         windowOffset = null;
349
350         jData = null;
351
352         display = true;
353         declarationPass = true;
354         hasParent = false;
355         parentListName = "";
356         return EVAL_PAGE;
357     }
358
359
360 }
361
Popular Tags