KickJava   Java API By Example, From Geeks To Geeks.

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


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

26 public class ContainerListTag extends BodyTagSupport JavaDoc {
27
28     private static org.apache.log4j.Logger logger =
29         org.apache.log4j.Logger.getLogger(ContainerListTag.class);
30
31     private String JavaDoc listName = "";
32     private JahiaContainerList containerList = null;
33
34     // we have two versions of these properties because we have to respect the
35
// JavaBeans design pattern.
36
private int intWindowSize = -1; // -1 means functionality is deactivated by default
37
private int intWindowOffset = -1;
38
39     private String JavaDoc windowSize;
40     private String JavaDoc windowOffset;
41
42     private JahiaData jData = null;
43
44     private boolean hasParent = false;
45     private String JavaDoc parentListName = "";
46     private String JavaDoc parentContainerName = null;
47
48     public void setName (String JavaDoc name) {
49         this.listName = name;
50     }
51
52     public String JavaDoc getName () {
53         return this.listName;
54     }
55
56     public int getSize () {
57         if (this.getContainerList() != null) {
58             return this.getContainerList().size();
59         }
60         return 0;
61     }
62
63     public void setWindowSize (String JavaDoc windowSize) {
64         try {
65             this.windowSize = windowSize;
66             this.intWindowSize = Integer.parseInt(windowSize);
67         } catch (NumberFormatException JavaDoc nfe) {
68             this.windowSize = "";
69             this.intWindowSize = -1;
70         }
71     }
72
73     public String JavaDoc getWindowSize () {
74         return this.windowSize;
75     }
76
77     public void setWindowOffset (String JavaDoc windowOffset) {
78         try {
79             this.windowOffset = windowOffset;
80             this.intWindowOffset = Integer.parseInt(windowOffset);
81         } catch (NumberFormatException JavaDoc nfe) {
82             this.intWindowOffset = -1;
83         }
84     }
85
86     public String JavaDoc getWindowOffset () {
87         return this.windowOffset;
88     }
89
90     public JahiaContainerList getContainerList () {
91         return this.containerList;
92     }
93
94     public ContainerListBean getContainerListBean() {
95         if (this.containerList == null) {
96             return null;
97         }
98         return new ContainerListBean(this.containerList, jData.params());
99     }
100
101     public String JavaDoc getParentContainerName() {
102         return parentContainerName;
103     }
104
105     public void setParentContainerName(String JavaDoc parentContainerName) {
106         this.parentContainerName = parentContainerName;
107     }
108
109     public int doStartTag () {
110
111         ServletRequest JavaDoc request = pageContext.getRequest();
112         jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
113
114         JahiaContainer parentContainer = null;
115         if (parentContainerName != null) {
116             // we were given an attribute referencing a parent container bean,
117
// let's try to use that if it is valid.
118
ContainerBean parentContainerBean = (ContainerBean) pageContext.findAttribute(parentContainerName);
119             if (parentContainerBean != null) {
120                 parentContainer = parentContainerBean.getJahiaContainer();
121                 try {
122                     this.parentListName = parentContainer.getDefinition().
123                                      getName();
124                     retrieveContainerList(parentContainer);
125                 } catch (JahiaException je) {
126                     logger.error("Error while retrieving container list for parent container " + parentContainer.getID(), je);
127                 }
128             }
129         }
130
131         if (this.containerList == null) {
132             // but we must also test the case where this containerList is enclosed
133
// in a parent containerList in which case we just trickle down the
134
// state of the parent list...
135
ContainerListTag parentContainerListTag = (ContainerListTag)
136                 findAncestorWithClass(this,
137                                       ContainerListTag.class);
138             if (parentContainerListTag != null) {
139                 this.parentListName = parentContainerListTag.getName();
140                 // we are in an embedded containerList tag, let's check in which
141
// pass we are...
142
hasParent = true;
143
144                 // this.listName = Utils.buildUniqueName(parentListName, this.listName);
145
ContainerTag parentContainerTag = (ContainerTag)
146                                                   findAncestorWithClass(this,
147                     ContainerTag.class);
148                 try {
149                     // this is not right we must attach the right instance of the container to the parent container...
150
if (parentContainerTag != null) {
151                         parentContainer = parentContainerTag.
152                                           getContainer();
153                         if (parentContainer != null) {
154                             retrieveContainerList(parentContainer);
155                         } else {
156                             logger.error("Parent container not found...");
157                         }
158                     } else {
159                         logger.error("Parent container tag not found...");
160                     }
161                 } catch (JahiaException je) {
162                     logger.error("Error:", je);
163                 }
164             } else {
165                 // we found no parent, let's try to load a top-level container
166
// list.
167
try {
168                     this.containerList = getContainerList(jData, getName());
169                 } catch (JahiaException je) {
170                     logger.error("Error:", je);
171                 }
172             }
173         }
174         if (getId() != null) {
175             if (getContainerListBean() != null) {
176                 pageContext.setAttribute(getId(), getContainerListBean());
177             } else {
178                 pageContext.removeAttribute(getId());
179             }
180         }
181
182         return EVAL_BODY_BUFFERED;
183     }
184
185     private void retrieveContainerList (JahiaContainer parentContainer)
186         throws JahiaException {
187         this.containerList = parentContainer.
188                              getContainerList(this.
189             listName);
190         if (this.containerList == null) {
191                 /** @todo FIXME this should be deprecated but is left
192                  * here because some databases might be corrupted.
193              */

194             this.listName = Utils.buildUniqueName(
195                 parentListName, this.listName);
196             logger.warn(
197                 "Using legacy method to find sub-container list " +
198                 this.listName);
199             this.containerList = parentContainer.
200                 getContainerList(this.
201                 listName);
202         }
203     }
204
205     // Body is evaluated one time, so just writes it on standard output
206
public int doAfterBody () {
207
208         try {
209             bodyContent.writeOut(bodyContent.getEnclosingWriter());
210         } catch (IOException JavaDoc ioe) {
211             logger.error("Error:", ioe);
212         }
213         return SKIP_BODY;
214     }
215
216     // reads the container list from a container set
217
protected JahiaContainerList getContainerList (JahiaData jData,
218         String JavaDoc listName)
219         throws JahiaException {
220
221         return jData.containers().getContainerList(listName);
222     }
223
224     public int doEndTag ()
225         throws JspException JavaDoc {
226         // let's reinitialize the tag variables to allow tag object reuse in
227
// pooling.
228
listName = "";
229         containerList = null;
230
231         intWindowSize = -1; // -1 means functionality is deactivated by default
232
intWindowOffset = -1;
233
234         windowSize = null;
235         windowOffset = null;
236
237         jData = null;
238
239         hasParent = false;
240         parentListName = "";
241         parentContainerName = null;
242         return EVAL_PAGE;
243     }
244
245 }
246
Popular Tags