KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jahia.deprecated.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.deprecated.taglibs.container.ContainerListTag;
15 import org.jahia.deprecated.taglibs.container.ContainerTag;
16 import org.jahia.exceptions.JahiaException;
17 import org.jahia.utils.JahiaConsole;
18
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     protected String JavaDoc name = "";
31     private String JavaDoc title = "";
32     private String JavaDoc value = "";
33     private int pageId = -1;
34     private int pageLevel = -1;
35     private String JavaDoc declarationOnly = "false";
36
37     public void setName(String JavaDoc name) {
38         this.name = name;
39     }
40
41     public void setTitle(String JavaDoc title) {
42         this.title = title;
43     }
44
45     public void setValue(String JavaDoc value) {
46         this.value = value;
47     }
48
49     public void setDeclarationOnly(String JavaDoc value) {
50         this.declarationOnly = value;
51     }
52
53     public void setPageId(String JavaDoc pageId) {
54         try {
55             this.pageId = Integer.parseInt(pageId);
56         } catch (NumberFormatException JavaDoc nfe) {
57             JahiaConsole.println("AbstractFieldTag: setPageId", "The given page id is not a number");
58         }
59     }
60
61     public void setPageLevel(String JavaDoc pageLevel) {
62         try {
63             this.pageLevel = Integer.parseInt(pageLevel);
64         } catch (NumberFormatException JavaDoc nfe) {
65             JahiaConsole.println("AbstractFieldTag: setPageLevel", "The given page level is not a number");
66         }
67     }
68
69     public int doStartTag() throws JspTagException JavaDoc {
70
71         if (this.title.equals("")) {
72             this.title = this.name;
73         }
74
75         ServletRequest JavaDoc request = pageContext.getRequest();
76         JspWriter JavaDoc out = pageContext.getOut();
77
78         JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
79
80         try {
81             // checks if the field is absolute
82
if (this.pageId == -1) {
83                 // check for page level
84
if (this.pageLevel != -1) {
85                     this.pageId = jData.gui().getLevelID(this.pageLevel);
86                 }
87             }
88         } catch (JahiaException je) {
89             JahiaConsole.printe("AbstractField.doStartTag:getLevelID call", je);
90         }
91
92         // checks if in declaration phase or if there are already container
93
JahiaContainer container = null;
94         // checks if we are inside a container
95
ContainerTag parent = (ContainerTag) findAncestorWithClass(this, ContainerTag.class);
96         if (parent != null) {
97
98             ContainerListTag containerListTag = (ContainerListTag)findAncestorWithClass(this, ContainerListTag.class);
99             if (containerListTag != null) {
100                 if (containerListTag.isDeclarationPass()) {
101                     // must be declared as a field of the enclosing container list, if not already done
102
// 28.02.2002 NK, added the check bellow to avoid multiple declaration
103
// of the same field in the container list structure.
104
if ( !containerListTag.isFieldAlreadyDeclared(this.name) ) {
105
106                         containerListTag.addField(this.name);
107                         // declares the field when it is not an absolute field,
108
// or when it is an absolute one and the current page is the home page
109
if ( this.pageId == -1 || this.pageId == jData.page().getID() ) {
110                             try {
111                                 jData.containers().declareField( containerListTag.getName(), this.name, this.title, getFieldType(), this.value );
112                             } catch (JahiaException je) {
113                                 // this case can be valid if we are using the field multiple times...
114
// JahiaConsole.println("AbstractField.doStartTag:declaredField call", "Field was probably already declared");
115
}
116                         }
117                     }
118                     return SKIP_BODY;
119                 } else {
120                 }
121             }
122
123             container = parent.getContainer();
124         }
125
126
127         try {
128
129             if (parent == null) {
130
131                 JahiaField theField = getField(jData);
132                 if (theField != null) {
133                     if ( !declarationOnly.equals("true") ){
134                         out.print( readValue(jData, theField.getValue()) );
135                     }
136                 }
137             } else {
138                 if (parent.displayBody()) {
139
140                     // must display the field of the current container
141
if (container != null) {
142                         //out.print(container.getField(this.name).getValue());
143
if ( !declarationOnly.equals("true") ){
144                             String JavaDoc val = container.getField(this.name).getValue();
145                             out.print( readValue( jData, val ) );
146                         }
147                     }
148                 }
149             }
150         } catch (IOException JavaDoc ioe) {
151             JahiaConsole.println("AbstractFieldTag: doStartTag","Error while displaying tag '" + this.name + "' : " + ioe.toString());
152         } catch (JahiaException je) {
153             JahiaConsole.println("AbstractFieldTag: doStartTag","Error while displaying tag '" + this.name + "' : " + je.toString());
154         } catch (NullPointerException JavaDoc npe) {
155             JahiaConsole.println("AbstractFieldTag: doStartTag","Error while displaying tag '" + this.name + "' : " + npe.toString());
156         }
157         return SKIP_BODY;
158     }
159
160
161     private JahiaField getField(JahiaData jData) throws JahiaException {
162
163         if ( this.pageId == -1 // the field is not absolute
164
|| jData.page().getID()== this.pageId ) {
165
166              // the current page is the page that declares the absolute field
167
// then we have to check if the field is already declared
168
if ( !jData.fields().checkDeclared(this.name) ) {
169                 // declare it now !
170
jData.fields().declareField( this.name, this.title, getFieldType(), this.value );
171              }
172              return jData.fields().getField(this.name);
173         }
174         // according to the previous tests, the only remaining case is
175
// when the field is absolute and the current page is not the declaring page,
176
// so return an absolute field of another page
177
return jData.fields().getAbsoluteField(this.name, this.pageId);
178     }
179
180
181     public String JavaDoc readValue(JahiaData jData, String JavaDoc value) {
182         // this method body can change according to the field type
183
// for example, text fields may be truncated, so this method
184
// will be overloaded in the subclass TextFieldTag
185
return value;
186     }
187
188     // method to implement in subclasses
189
public abstract int getFieldType();
190
191     public int doEndTag() throws JspException JavaDoc {
192         // let's reinitialize the tag variables to allow tag object reuse in
193
// pooling.
194
name = "";
195         title = "";
196         value = "";
197         pageId = -1;
198         pageLevel = -1;
199         declarationOnly = "false";
200         return EVAL_PAGE;
201     }
202
203
204 }
205
Popular Tags