1 package org.jahia.deprecated.taglibs.field.file; 2 3 import java.io.IOException ; 4 import java.util.Properties ; 5 6 import javax.servlet.http.HttpServletRequest ; 7 import javax.servlet.jsp.JspException ; 8 import javax.servlet.jsp.JspTagException ; 9 import javax.servlet.jsp.tagext.BodyTagSupport ; 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 31 public class FileFieldTag extends BodyTagSupport { 32 33 34 protected String name = ""; 35 protected String title = ""; 36 protected String 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 name) { 45 this.name = name; 46 } 47 48 public void setTitle(String title) { 49 this.title = title; 50 } 51 52 public void setValue(String value) { 53 this.value = value; 54 } 55 56 public void setPageId(String pageId) { 57 try { 58 this.pageId = Integer.parseInt(pageId); 59 } catch (NumberFormatException nfe) { 60 JahiaConsole.println("FileFieldTag: setPageId", "The given page id is not a number"); 61 } 62 } 63 64 public void setPageLevel(String pageLevel) { 65 try { 66 this.pageLevel = Integer.parseInt(pageLevel); 67 } catch (NumberFormatException 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 { 82 83 if (this.title.equals("")) { 84 this.title = this.name; 85 } 86 HttpServletRequest request = (HttpServletRequest )pageContext.getRequest(); 87 88 JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData"); 89 90 try { 91 if (this.pageId == -1) { 93 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 JahiaContainer container = null; 104 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 if (containerListTag != null) { 113 containerListTag.addField(this.name); 115 } 116 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 if (parent == null) { 137 theField = getField(jData); 139 this.display = true; 140 141 } else { 142 if (parent.displayBody()) { 144 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 ()); 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 npe) { 175 JahiaConsole.println("FileFieldTag: doStartTag ", npe.toString()); 176 } 177 return SKIP_BODY; 178 } 179 180 181 public int doAfterBody() throws JspException { 182 183 if (this.display) { 184 try { 185 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 186 } catch (IOException ioe) { 187 throw new JspTagException (); 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 || jData.page().getID()== this.pageId ) { 200 202 if ( !jData.fields().checkDeclared(this.name) ) { 204 jData.fields().declareField( this.name, this.title, FieldTypes.FILE, this.value ); 206 } 207 return jData.fields().getField(this.name); 208 } 209 return jData.fields().getAbsoluteField(this.name, this.pageId); 213 } 214 215 public int doEndTag() throws JspException { 216 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 |