KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > taglibs > field > AbstractFieldTag


1 package org.jahia.taglibs.field;
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.JspWriter JavaDoc;
9 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
10
11 import org.jahia.data.JahiaData;
12 import org.jahia.data.containers.JahiaContainer;
13 import org.jahia.data.fields.JahiaField;
14 import org.jahia.exceptions.JahiaException;
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.FieldBean;
19
20 /**
21  * Class AbstractFieldTag : defines common code for Jahia fields
22  *
23      * This class check if the field belongs to a container, or it has been declared
24  * directly on the page
25  *
26  * @author Jerome Tamiotti
27  */

28 public abstract class AbstractFieldTag extends TagSupport JavaDoc {
29
30     private boolean display = true;
31     protected String JavaDoc name = "";
32     private int pageId = -1;
33     private int pageLevel = -1;
34     private boolean diffActive = false;
35     private String JavaDoc defaultValue = null;
36     private String JavaDoc fieldValue = null;
37     private String JavaDoc valueId = null;
38
39     public boolean isDisplay() {
40         return display;
41     }
42     public void setDisplay(boolean display) {
43         this.display = display;
44     }
45
46     public void setName (String JavaDoc name) {
47         this.name = name;
48     }
49
50     public void setDefaultValue (String JavaDoc value) {
51         this.defaultValue = value;
52     }
53
54     public void setPageId (String JavaDoc pageId) {
55         try {
56             this.pageId = Integer.parseInt(pageId);
57         } catch (NumberFormatException JavaDoc nfe) {
58             JahiaConsole.println("AbstractFieldTag: setPageId",
59                                  "The given page id is not a number");
60         }
61     }
62
63     public void setPageLevel (String JavaDoc pageLevel) {
64         try {
65             this.pageLevel = Integer.parseInt(pageLevel);
66         } catch (NumberFormatException JavaDoc nfe) {
67             JahiaConsole.println("AbstractFieldTag: setPageLevel",
68                                  "The given page level is not a number");
69         }
70     }
71
72     public boolean isDiffActive() {
73         return diffActive;
74     }
75
76     public void setDiffActive(boolean diffActive) {
77         this.diffActive = diffActive;
78     }
79     public String JavaDoc getValueId() {
80         return valueId;
81     }
82     public void setValueId(String JavaDoc valueId) {
83         this.valueId = valueId;
84     }
85
86     public int doStartTag () throws JspTagException JavaDoc {
87
88         // we must take care to reset page attribute first !!!!!
89
if ( getId() != null ){
90             pageContext.removeAttribute(getId());
91         }
92
93         // we must take care to reset page attribute first !!!!!
94
if ( getValueId() != null ){
95             pageContext.removeAttribute(getValueId());
96         }
97
98         ServletRequest JavaDoc request = pageContext.getRequest();
99         JspWriter JavaDoc out = pageContext.getOut();
100
101         JahiaData jData = (JahiaData) request.getAttribute(
102             "org.jahia.data.JahiaData");
103
104         try {
105             // checks if the field is absolute
106
if (this.pageId == -1) {
107                 // check for page level
108
if (this.pageLevel != -1) {
109                     this.pageId = jData.gui().getLevelID(this.pageLevel);
110                 }
111             }
112         } catch (JahiaException je) {
113             JahiaConsole.printe("AbstractField.doStartTag:getLevelID call", je);
114         }
115
116         // checks if in declaration phase or if there are already container
117
JahiaContainer container = null;
118         // checks if we are inside a container
119
ContainerTag parent = (ContainerTag) findAncestorWithClass(this,
120             ContainerTag.class);
121         if (parent != null) {
122             ContainerListTag containerListTag = (ContainerListTag) findAncestorWithClass(this,
123                 ContainerListTag.class);
124             container = parent.getContainer();
125         }
126
127         try {
128
129             JahiaField theField = null;
130             if (parent == null) {
131                 theField = getField(jData);
132             } else {
133                 if (parent.displayBody()) {
134                     // must display the field of the current container
135
if (container != null) {
136                         theField = container.getField(this.name);
137                     }
138                 }
139             }
140
141             setIDAttribute(theField,jData);
142             setValueIDAttribute(theField,jData);
143
144             if ( isDisplay() ){
145                 out.print(readValue(jData, fieldValue));
146             }
147
148         } catch (IOException JavaDoc ioe) {
149             JahiaConsole.println("AbstractFieldTag: doStartTag",
150                                  "Error while displaying tag '" + this.name +
151                                  "' : " + ioe.toString());
152         } catch (JahiaException je) {
153             JahiaConsole.println("AbstractFieldTag: doStartTag",
154                                  "Error while displaying tag '" + this.name +
155                                  "' : " + je.toString());
156         } catch (NullPointerException JavaDoc npe) {
157             JahiaConsole.println("AbstractFieldTag: doStartTag",
158                                  "Error while displaying tag '" + this.name +
159                                  "' : " + npe.toString());
160         }
161         return SKIP_BODY;
162     }
163
164     private JahiaField getField (JahiaData jData) throws JahiaException {
165
166         if (this.pageId == -1 // the field is not absolute
167
|| jData.page().getID() == this.pageId) {
168
169             return jData.fields().getField(this.name);
170         }
171         // according to the previous tests, the only remaining case is
172
// when the field is absolute and the current page is not the declaring page,
173
// so return an absolute field of another page
174
return jData.fields().getAbsoluteField(this.name, this.pageId);
175     }
176
177     public String JavaDoc readValue (JahiaData jData, String JavaDoc value) {
178         // this method body can change according to the field type
179
// for example, text fields may be truncated, so this method
180
// will be overloaded in the subclass TextFieldTag
181
return value;
182     }
183
184     // method to implement in subclasses
185
public abstract int getFieldType ();
186
187     public int doEndTag () throws JspException JavaDoc {
188         // let's reinitialize the tag variables to allow tag object reuse in
189
// pooling.
190
name = "";
191         pageId = -1;
192         pageLevel = -1;
193         diffActive = false;
194         display = true;
195         defaultValue = null;
196         fieldValue = null;
197         valueId = null;
198         return EVAL_PAGE;
199     }
200
201     protected void setIDAttribute (JahiaField theField, JahiaData jData) {
202
203         // we must take care to reset page attribute first !!!!!
204
if ( getId() != null ){
205             pageContext.removeAttribute(getId());
206         }
207
208         if (theField != null) {
209             if (getId() != null) {
210                 pageContext.setAttribute(getId(),
211                                          new FieldBean(theField,
212                     jData.params()));
213             }
214         }
215     }
216
217     /**
218      * Let's set the value into the page context if the valueId parameter
219      * was set.
220      */

221     protected void setValueIDAttribute(JahiaField theField, JahiaData jData){
222
223         if (theField != null) {
224             /*
225             if (getId() != null) {
226                 pageContext.setAttribute(getId(),
227                                          new FieldBean(theField,
228                     jData.params()));
229             }*/

230             if (isDiffActive()) {
231                 fieldValue = theField.getHighLightDiffValue(jData.params());
232             } else {
233                 fieldValue = theField.getValue();
234             }
235         }
236
237         if ( fieldValue == null ){
238             fieldValue = defaultValue;
239         }
240
241         if (getValueId() != null) {
242             pageContext.setAttribute(getValueId(),fieldValue);
243         }
244     }
245 }
246
Popular Tags