KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jahia.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.JahiaField;
14 import org.jahia.data.files.JahiaFile;
15 import org.jahia.data.files.JahiaFileField;
16 import org.jahia.exceptions.JahiaException;
17 import org.jahia.registries.ServicesRegistry;
18 import org.jahia.taglibs.container.ContainerListTag;
19 import org.jahia.taglibs.container.ContainerTag;
20 import org.jahia.utils.JahiaConsole;
21 import org.jahia.data.beans.FieldBean;
22
23 /**
24  * Class FileFieldTag : retrieves a Jahia file field inside the current container or on the
25  * page, so that the enclosed tags such as <fileDownloadURL> can access
26  * the page attributes and display their values.
27  *
28  * @author Jerome Tamiotti
29  */

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

193             return jData.fields().getField(this.name);
194         }
195         // according to the previous tests, the only remaining case is
196
// when the field is absolute and the current page is not the declaring page,
197
// so return an absolute field of another page
198
return jData.fields().getAbsoluteField(this.name, this.pageId);
199     }
200
201     public int doEndTag () throws JspException JavaDoc {
202         // let's reinitialize the tag variables to allow tag object reuse in
203
// pooling.
204
name = "";
205         pageId = -1;
206         pageLevel = -1;
207
208         file = null;
209         display = false;
210         valueId = null;
211         return EVAL_PAGE;
212     }
213
214     protected void setIDAttribute (JahiaField theField, JahiaData jData) {
215         if (theField != null) {
216             if (getId() != null) {
217                 pageContext.setAttribute(getId(),
218                                          new FieldBean(theField,
219                     jData.params()));
220             }
221         }
222     }
223
224 }
225
Popular Tags