1 package org.jahia.taglibs.field.page; 2 3 import java.io.IOException ; 4 5 import javax.servlet.ServletRequest ; 6 import javax.servlet.jsp.JspException ; 7 import javax.servlet.jsp.JspTagException ; 8 import javax.servlet.jsp.tagext.BodyTagSupport ; 9 10 import org.jahia.data.JahiaData; 11 import org.jahia.data.containers.JahiaContainer; 12 import org.jahia.data.fields.JahiaField; 13 import org.jahia.exceptions.JahiaException; 14 import org.jahia.services.pages.JahiaPage; 15 import org.jahia.taglibs.container.ContainerListTag; 16 import org.jahia.taglibs.container.ContainerTag; 17 import org.jahia.utils.JahiaConsole; 18 import org.jahia.data.beans.PageBean; 19 import org.jahia.data.beans.FieldBean; 20 21 28 public class PageFieldTag extends BodyTagSupport { 29 30 protected String name = ""; 31 private int pageId = -1; 32 private int pageLevel = -1; 33 34 private boolean display = false; 35 36 private JahiaPage page = null; 37 private String valueId = null; 38 39 public void setName (String name) { 40 this.name = name; 41 } 42 43 public void setPageId (String pageId) { 44 try { 45 this.pageId = Integer.parseInt(pageId); 46 } catch (NumberFormatException nfe) { 47 JahiaConsole.println("PageFieldTag: setPageId", 48 "The given page id is not a number"); 49 } 50 } 51 52 public void setPageLevel (String pageLevel) { 53 try { 54 this.pageLevel = Integer.parseInt(pageLevel); 55 } catch (NumberFormatException nfe) { 56 JahiaConsole.println("PageFieldTag: setPageLevel", 57 "The given page level is not a number"); 58 } 59 } 60 61 public JahiaPage getPage () { 62 return this.page; 63 } 64 65 public boolean displayBody () { 66 return display; 67 } 68 69 public String getValueId() { 70 return valueId; 71 } 72 public void setValueId(String valueId) { 73 this.valueId = valueId; 74 } 75 76 public int doStartTag () throws JspTagException { 77 78 if ( getId() != null ){ 80 pageContext.removeAttribute(getId()); 81 } 82 83 if ( getValueId() != null ){ 85 pageContext.removeAttribute(getValueId()); 86 } 87 88 90 ServletRequest request = pageContext.getRequest(); 91 92 JahiaData jData = (JahiaData) request.getAttribute( 93 "org.jahia.data.JahiaData"); 94 95 try { 96 if (this.pageId == -1) { 98 if (this.pageLevel != -1) { 100 this.pageId = jData.gui().getLevelID(this.pageLevel); 101 } 102 } 103 } catch (JahiaException je) { 104 JahiaConsole.printe("PageFieldTag.doStartTag:getLevelID call", je); 105 } 106 107 JahiaContainer container = null; 109 ContainerTag parent = (ContainerTag) findAncestorWithClass(this, 111 ContainerTag.class); 112 if (parent != null) { 113 container = parent.getContainer(); 114 } 115 116 JahiaField theField = null; 117 try { 118 if (parent == null) { 120 theField = getField(jData); 122 this.display = true; 123 124 } else { 125 ContainerListTag containerListTag = (ContainerListTag) findAncestorWithClass(this, 127 ContainerListTag.class); 128 if (parent.displayBody()) { 129 if (container != null) { 131 theField = container.getField(this.name); 132 this.display = true; 133 } 134 } 135 } 136 137 if (theField != null) { 138 setIDAttribute(theField, jData); 139 if (getValueId() != null){ 140 if ( theField.getObject() != null ){ 141 pageContext.setAttribute(getValueId(), 142 new PageBean((JahiaPage)theField.getObject(),jData.params())); 143 } else { 144 pageContext.removeAttribute(getValueId()); 145 } 146 } 147 this.page = (JahiaPage) theField.getObject(); 148 if (this.display) { 149 return EVAL_BODY_BUFFERED; 150 } 151 } 152 } catch (JahiaException je) { 153 JahiaConsole.println("PageFieldTag: doStartTag ", je.toString()); 154 } catch (NullPointerException npe) { 155 JahiaConsole.println("PageFieldTag: doStartTag ", npe.toString()); 156 } 157 return SKIP_BODY; 158 } 159 160 public int doAfterBody () throws JspException { 161 162 if (this.display) { 163 try { 164 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 165 } catch (IOException ioe) { 166 throw new JspTagException (); 167 } 168 } else { 169 bodyContent.clearBody(); 170 } 171 return SKIP_BODY; 172 } 173 174 private JahiaField getField (JahiaData jData) throws JahiaException { 175 176 if (this.pageId == -1 || jData.page().getID() == this.pageId) { 178 180 return jData.fields().getField(this.name); 181 } 182 return jData.fields().getAbsoluteField(this.name, this.pageId); 186 } 187 188 public int doEndTag () throws JspException { 189 name = ""; 192 pageId = -1; 193 pageLevel = -1; 194 195 display = false; 196 197 page = null; 198 valueId = null; 199 return EVAL_PAGE; 200 } 201 202 protected void setIDAttribute (JahiaField theField, JahiaData jData) { 203 204 if ( getId() != null ){ 206 pageContext.removeAttribute(getId()); 207 } 208 209 if (theField != null) { 210 if (getId() != null) { 211 pageContext.setAttribute(getId(), 212 new FieldBean(theField, 213 jData.params())); 214 } 215 } 216 } 217 218 } 219 | Popular Tags |