1 package org.jahia.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.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 30 public class FileFieldTag extends BodyTagSupport { 31 32 protected String 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 valueId = null; 39 40 public void setName (String name) { 41 this.name = name; 42 } 43 44 public void setPageId (String pageId) { 45 try { 46 this.pageId = Integer.parseInt(pageId); 47 } catch (NumberFormatException nfe) { 48 JahiaConsole.println("FileFieldTag: setPageId", 49 "The given page id is not a number"); 50 } 51 } 52 53 public void setPageLevel (String pageLevel) { 54 try { 55 this.pageLevel = Integer.parseInt(pageLevel); 56 } catch (NumberFormatException 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 getValueId() { 71 return valueId; 72 } 73 public void setValueId(String valueId) { 74 this.valueId = valueId; 75 } 76 77 public int doStartTag () throws JspTagException { 78 79 if ( getId() != null ){ 81 pageContext.removeAttribute(getId()); 82 } 83 84 if ( getValueId() != null ){ 85 pageContext.removeAttribute(getValueId()); 86 } 87 88 HttpServletRequest request = (HttpServletRequest ) pageContext. 89 getRequest(); 90 91 JahiaData jData = (JahiaData) request.getAttribute( 92 "org.jahia.data.JahiaData"); 93 94 try { 95 if (this.pageId == -1) { 97 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 JahiaContainer container = null; 108 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 if (parent == null) { 121 theField = getField(jData); 123 this.display = true; 124 125 } else { 126 if (parent.displayBody()) { 128 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 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 ()); 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 npe) { 168 JahiaConsole.println("FileFieldTag: doStartTag ", npe.toString()); 169 } 170 return SKIP_BODY; 171 } 172 173 public int doAfterBody () throws JspException { 174 175 if (this.display) { 176 try { 177 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 178 } catch (IOException ioe) { 179 throw new JspTagException (); 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 || jData.page().getID() == this.pageId) { 191 193 return jData.fields().getField(this.name); 194 } 195 return jData.fields().getAbsoluteField(this.name, this.pageId); 199 } 200 201 public int doEndTag () throws JspException { 202 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 |