KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > taglibs > field > page > PageFieldTag


1 package org.jahia.taglibs.field.page;
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.JspTagException JavaDoc;
8 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
9
10 import org.jahia.data.JahiaData;
11 import org.jahia.data.containers.JahiaContainer;
12 import org.jahia.data.fields.JahiaField;
13 import org.jahia.exceptions.JahiaException;
14 import org.jahia.services.pages.JahiaPage;
15 import org.jahia.taglibs.container.ContainerListTag;
16 import org.jahia.taglibs.container.ContainerTag;
17 import org.jahia.utils.JahiaConsole;
18 import org.jahia.data.beans.PageBean;
19 import org.jahia.data.beans.FieldBean;
20
21 /**
22  * Class PageFieldTag : retrieves a Jahia page field inside the current container,
23  * so that the enclosed tags such as <pageTitle> can access
24  * the page attributes and display their values.
25  *
26  * @author Jerome Tamiotti
27  */

28 public class PageFieldTag extends BodyTagSupport JavaDoc {
29
30     protected String JavaDoc name = "";
31     private int pageId = -1;
32     private int pageLevel = -1;
33
34     private boolean display = false;
35
36     private JahiaPage page = null;
37     private String JavaDoc valueId = null;
38
39     public void setName (String JavaDoc name) {
40         this.name = name;
41     }
42
43     public void setPageId (String JavaDoc pageId) {
44         try {
45             this.pageId = Integer.parseInt(pageId);
46         } catch (NumberFormatException JavaDoc nfe) {
47             JahiaConsole.println("PageFieldTag: setPageId",
48                                  "The given page id is not a number");
49         }
50     }
51
52     public void setPageLevel (String JavaDoc pageLevel) {
53         try {
54             this.pageLevel = Integer.parseInt(pageLevel);
55         } catch (NumberFormatException JavaDoc nfe) {
56             JahiaConsole.println("PageFieldTag: setPageLevel",
57                                  "The given page level is not a number");
58         }
59     }
60
61     public JahiaPage getPage () {
62         return this.page;
63     }
64
65     public boolean displayBody () {
66         return display;
67     }
68
69     public String JavaDoc getValueId() {
70         return valueId;
71     }
72     public void setValueId(String JavaDoc valueId) {
73         this.valueId = valueId;
74     }
75
76     public int doStartTag () throws JspTagException JavaDoc {
77
78         // we must take care to reset page attribute first !!!!!
79
if ( getId() != null ){
80             pageContext.removeAttribute(getId());
81         }
82
83         // we must take care to reset page attribute first !!!!!
84
if ( getValueId() != null ){
85             pageContext.removeAttribute(getValueId());
86         }
87
88         // JahiaConsole.println("PageFieldTag.doStartTag", "called.");
89

90         ServletRequest JavaDoc request = pageContext.getRequest();
91
92         JahiaData jData = (JahiaData) request.getAttribute(
93             "org.jahia.data.JahiaData");
94
95         try {
96             // checks if the field is absolute
97
if (this.pageId == -1) {
98                 // check for page level
99
if (this.pageLevel != -1) {
100                     this.pageId = jData.gui().getLevelID(this.pageLevel);
101                 }
102             }
103         } catch (JahiaException je) {
104             JahiaConsole.printe("PageFieldTag.doStartTag:getLevelID call", je);
105         }
106
107         // checks if in declaration phase or if there are already container
108
JahiaContainer container = null;
109         // checks if we are inside a container
110
ContainerTag parent = (ContainerTag) findAncestorWithClass(this,
111             ContainerTag.class);
112         if (parent != null) {
113             container = parent.getContainer();
114         }
115
116         JahiaField theField = null;
117         try {
118             // searches for the field that contains the page
119
if (parent == null) {
120                 // at root
121
theField = getField(jData);
122                 this.display = true;
123
124             } else {
125                 // in a container
126
ContainerListTag containerListTag = (ContainerListTag) findAncestorWithClass(this,
127                 ContainerListTag.class);
128                 if (parent.displayBody()) {
129                     // must display the field of the current container
130
if (container != null) {
131                         theField = container.getField(this.name);
132                         this.display = true;
133                     }
134                 }
135             }
136
137             if (theField != null) {
138                 setIDAttribute(theField, jData);
139                 if (getValueId() != null){
140                     if ( theField.getObject() != null ){
141                         pageContext.setAttribute(getValueId(),
142                                 new PageBean((JahiaPage)theField.getObject(),jData.params()));
143                     } else {
144                         pageContext.removeAttribute(getValueId());
145                     }
146                 }
147                 this.page = (JahiaPage) theField.getObject();
148                 if (this.display) {
149                     return EVAL_BODY_BUFFERED;
150                 }
151             }
152         } catch (JahiaException je) {
153             JahiaConsole.println("PageFieldTag: doStartTag ", je.toString());
154         } catch (NullPointerException JavaDoc npe) {
155             JahiaConsole.println("PageFieldTag: doStartTag ", npe.toString());
156         }
157         return SKIP_BODY;
158     }
159
160     public int doAfterBody () throws JspException JavaDoc {
161
162         if (this.display) {
163             try {
164                 bodyContent.writeOut(bodyContent.getEnclosingWriter());
165             } catch (IOException JavaDoc ioe) {
166                 throw new JspTagException JavaDoc();
167             }
168         } else {
169             bodyContent.clearBody();
170         }
171         return SKIP_BODY;
172     }
173
174     private JahiaField getField (JahiaData jData) throws JahiaException {
175
176         if (this.pageId == -1 // the field is not absolute
177
|| jData.page().getID() == this.pageId) {
178             // the current page is the page that declares the absolute field
179

180             return jData.fields().getField(this.name);
181         }
182         // according to the previous tests, the only remaining case is
183
// when the field is absolute and the current page is not the declaring page,
184
// so return an absolute field of another page
185
return jData.fields().getAbsoluteField(this.name, this.pageId);
186     }
187
188     public int doEndTag () throws JspException JavaDoc {
189         // let's reinitialize the tag variables to allow tag object reuse in
190
// pooling.
191
name = "";
192         pageId = -1;
193         pageLevel = -1;
194
195         display = false;
196
197         page = null;
198         valueId = null;
199         return EVAL_PAGE;
200     }
201
202     protected void setIDAttribute (JahiaField theField, JahiaData jData) {
203
204         // we must take care to reset page attribute first !!!!!
205
if ( getId() != null ){
206             pageContext.removeAttribute(getId());
207         }
208
209         if (theField != null) {
210             if (getId() != null) {
211                 pageContext.setAttribute(getId(),
212                                          new FieldBean(theField,
213                     jData.params()));
214             }
215         }
216     }
217
218 }
219
Popular Tags