KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > deprecated > taglibs > field > file > FileFieldTag


1 package org.jahia.deprecated.taglibs.field.file;
2
3 import java.io.IOException JavaDoc;
4 import java.util.Properties JavaDoc;
5
6 import javax.servlet.http.HttpServletRequest JavaDoc;
7 import javax.servlet.jsp.JspException JavaDoc;
8 import javax.servlet.jsp.JspTagException 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.fields.FieldTypes;
14 import org.jahia.data.fields.JahiaField;
15 import org.jahia.data.files.JahiaFile;
16 import org.jahia.data.files.JahiaFileField;
17 import org.jahia.deprecated.taglibs.container.ContainerListTag;
18 import org.jahia.deprecated.taglibs.container.ContainerTag;
19 import org.jahia.exceptions.JahiaException;
20 import org.jahia.registries.ServicesRegistry;
21 import org.jahia.utils.JahiaConsole;
22
23
24 /**
25  * Class FileFieldTag : retrieves a Jahia file field inside the current container or on the
26  * page, so that the enclosed tags such as <fileDownloadURL> can access
27  * the page attributes and display their values.
28  *
29  * @author Jerome Tamiotti
30  */

31 public class FileFieldTag extends BodyTagSupport JavaDoc {
32
33
34     protected String JavaDoc name = "";
35     protected String JavaDoc title = "";
36     protected String JavaDoc value = "";
37     private int pageId = -1;
38     private int pageLevel = -1;
39
40     private JahiaFileField file = null;
41     private boolean display = false;
42
43
44     public void setName(String JavaDoc name) {
45         this.name = name;
46     }
47
48     public void setTitle(String JavaDoc title) {
49         this.title = title;
50     }
51
52     public void setValue(String JavaDoc value) {
53         this.value = value;
54     }
55
56     public void setPageId(String JavaDoc pageId) {
57         try {
58             this.pageId = Integer.parseInt(pageId);
59         } catch (NumberFormatException JavaDoc nfe) {
60             JahiaConsole.println("FileFieldTag: setPageId", "The given page id is not a number");
61         }
62     }
63
64     public void setPageLevel(String JavaDoc pageLevel) {
65         try {
66             this.pageLevel = Integer.parseInt(pageLevel);
67         } catch (NumberFormatException JavaDoc nfe) {
68             JahiaConsole.println("FileFieldTag: setPageLevel", "The given page level is not a number");
69         }
70     }
71
72     public JahiaFileField getFile() {
73         return this.file;
74     }
75
76     public boolean displayBody() {
77         return display;
78     }
79
80
81     public int doStartTag() throws JspTagException JavaDoc {
82
83         if (this.title.equals("")) {
84             this.title = this.name;
85         }
86         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)pageContext.getRequest();
87
88         JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
89
90         try {
91             // checks if the field is absolute
92
if (this.pageId == -1) {
93                 // check for page level
94
if (this.pageLevel != -1) {
95                     this.pageId = jData.gui().getLevelID(this.pageLevel);
96                 }
97             }
98         } catch (JahiaException je) {
99             JahiaConsole.printe("FileFieldTag.doStartTag.getLevelID call", je);
100         }
101
102         // checks if in declaration phase or if there are already container
103
JahiaContainer container = null;
104         // checks if we are inside a container
105
ContainerTag parent = (ContainerTag) findAncestorWithClass(this,ContainerTag.class);
106         if (parent != null) {
107             ContainerListTag containerListTag = (ContainerListTag)findAncestorWithClass(this,
108                 ContainerListTag.class);
109             if (containerListTag != null) {
110                 if (containerListTag.isDeclarationPass()) {
111                     // JahiaConsole.println("FileFieldTag.doStartTag", "DECLARATION PASS.");
112
if (containerListTag != null) {
113                         // declares the file as a field of the container list
114
containerListTag.addField(this.name);
115                     }
116                     // declares the field when it is not an absolute field,
117
// or when it is an absolute one and the current page is the home page
118
if ( this.pageId == -1 || this.pageId == jData.page().getID() ) {
119                         try {
120                             jData.containers().declareField( containerListTag.getName(), this.name, this.title, FieldTypes.FILE, this.value );
121                         } catch (JahiaException je) {
122                             JahiaConsole.println("FileFieldTag.doStartTag:declareField call",
123                                                  "Warning : Field " + this.name + " probably already previously declared in template ");
124                         }
125                     }
126                     this.display = false;
127                     return EVAL_BODY_BUFFERED;
128                 }
129             }
130             container = parent.getContainer();
131         }
132
133         JahiaField theField = null;
134         try {
135             // searches for the field that contains the file
136
if (parent == null) {
137                 // at root
138
theField = getField(jData);
139                 this.display = true;
140
141             } else {
142                 // in a container
143
if (parent.displayBody()) {
144                     // must display the field of the current container
145
if (container != null) {
146                         theField = container.getField(this.name);
147                         this.display = true;
148                     }
149                 }
150             }
151             if (theField != null) {
152                 this.file = (JahiaFileField) theField.getObject();
153                 if (this.file == null) {
154                     if (!this.value.equals("")) {
155                         JahiaFile tempFile =
156                                 new JahiaFile(-1, -1, "", "", "", 0, 0, "", "", "",
157                                 String.valueOf(ServicesRegistry.getInstance().getJahiaVersionService()
158                                 .getCurrentVersionID()),
159                                 JahiaFile.STATE_ACTIVE);
160                         this.file = new JahiaFileField(tempFile, new Properties JavaDoc());
161                         this.file.setDownloadUrl(jData.gui().drawHttpJspContext(request) + "/" + this.value);
162                         this.display = true;
163                     } else {
164                         this.display = jData.gui().isEditMode();
165                     }
166                 }
167             }
168             if ( this.display ) {
169                 return EVAL_BODY_BUFFERED;
170             }
171
172         } catch (JahiaException je) {
173             JahiaConsole.println("FileFieldTag: doStartTag ", je.toString());
174         } catch (NullPointerException JavaDoc npe) {
175             JahiaConsole.println("FileFieldTag: doStartTag ", npe.toString());
176         }
177         return SKIP_BODY;
178     }
179
180
181     public int doAfterBody() throws JspException JavaDoc {
182
183         if (this.display) {
184             try {
185                 bodyContent.writeOut(bodyContent.getEnclosingWriter());
186             } catch (IOException JavaDoc ioe) {
187                 throw new JspTagException JavaDoc();
188             }
189         } else {
190             bodyContent.clearBody();
191         }
192         return SKIP_BODY;
193     }
194
195
196     private JahiaField getField(JahiaData jData) throws JahiaException {
197
198         if ( this.pageId == -1 // the field is not absolute
199
|| jData.page().getID()== this.pageId ) {
200              // the current page is the page that declares the absolute field
201

202              // then we have to check if the field is already declared
203
if ( !jData.fields().checkDeclared(this.name) ) {
204                 // declare it now !
205
jData.fields().declareField( this.name, this.title, FieldTypes.FILE, this.value );
206              }
207              return jData.fields().getField(this.name);
208         }
209         // according to the previous tests, the only remaining case is
210
// when the field is absolute and the current page is not the declaring page,
211
// so return an absolute field of another page
212
return jData.fields().getAbsoluteField(this.name, this.pageId);
213     }
214
215     public int doEndTag() throws JspException JavaDoc {
216         // let's reinitialize the tag variables to allow tag object reuse in
217
// pooling.
218
name = "";
219         title = "";
220         value = "";
221         pageId = -1;
222         pageLevel = -1;
223
224         file = null;
225         display = false;
226         return EVAL_PAGE;
227     }
228
229 }
230
Popular Tags