1 package org.jahia.deprecated.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.FieldTypes; 13 import org.jahia.data.fields.JahiaField; 14 import org.jahia.deprecated.taglibs.container.ContainerListTag; 15 import org.jahia.deprecated.taglibs.container.ContainerTag; 16 import org.jahia.exceptions.JahiaException; 17 import org.jahia.services.pages.JahiaPage; 18 import org.jahia.utils.JahiaConsole; 19 20 21 22 29 public class PageFieldTag extends BodyTagSupport { 30 31 32 protected String name = ""; 33 protected String title = ""; 34 protected String value = ""; 35 private int pageId = -1; 36 private int pageLevel = -1; 37 38 private boolean display = false; 39 40 private JahiaPage page = null; 41 42 public void setName(String name) { 43 this.name = name; 44 } 45 46 public void setTitle(String title) { 47 this.title = title; 48 } 49 50 public void setValue(String value) { 51 this.value = value; 52 } 53 54 public void setPageId(String pageId) { 55 try { 56 this.pageId = Integer.parseInt(pageId); 57 } catch (NumberFormatException nfe) { 58 JahiaConsole.println("PageFieldTag: setPageId", "The given page id is not a number"); 59 } 60 } 61 62 public void setPageLevel(String pageLevel) { 63 try { 64 this.pageLevel = Integer.parseInt(pageLevel); 65 } catch (NumberFormatException nfe) { 66 JahiaConsole.println("PageFieldTag: setPageLevel", "The given page level is not a number"); 67 } 68 } 69 70 public JahiaPage getPage() { 71 return this.page; 72 } 73 74 public boolean displayBody() { 75 return display; 76 } 77 78 79 public int doStartTag() throws JspTagException { 80 81 83 if (this.title.equals("")) { 84 this.title = this.name; 85 } 86 ServletRequest request = 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("PageFieldTag.doStartTag:getLevelID call", je); 100 } 101 102 JahiaContainer container = null; 104 ContainerTag parent = (ContainerTag) findAncestorWithClass(this,ContainerTag.class); 106 if (parent != null) { 107 container = parent.getContainer(); 108 } 109 110 111 JahiaField theField = null; 112 try { 113 if (parent == null) { 115 theField = getField(jData); 117 this.display = true; 118 119 } else { 120 ContainerListTag containerListTag = (ContainerListTag)findAncestorWithClass(this, 122 ContainerListTag.class); 123 if (containerListTag != null) { 124 if (containerListTag.isDeclarationPass()) { 126 containerListTag.addField(this.name); 127 if ( this.pageId == -1 || this.pageId == jData.page().getID() ) { 130 jData.containers().declareField( containerListTag.getName(), this.name, this.title, FieldTypes.PAGE, this.value ); 132 } 133 this.display = false; 134 return EVAL_BODY_BUFFERED; 135 } 136 } 137 if (parent.displayBody()) { 138 if (container != null) { 140 theField = container.getField(this.name); 141 this.display = true; 142 } 143 } 144 } 145 if (theField != null) { 146 this.page = (JahiaPage) theField.getObject(); 147 if ( this.display ) { 148 return EVAL_BODY_BUFFERED; 149 } 150 } 151 } catch (JahiaException je) { 152 JahiaConsole.println("PageFieldTag: doStartTag ", je.toString()); 153 } catch (NullPointerException npe) { 154 JahiaConsole.println("PageFieldTag: doStartTag ", npe.toString()); 155 } 156 return SKIP_BODY; 157 } 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 175 private JahiaField getField(JahiaData jData) throws JahiaException { 176 177 if ( this.pageId == -1 || jData.page().getID()== this.pageId ) { 179 181 if ( !jData.fields().checkDeclared(this.name) ) { 183 jData.fields().declareField( this.name, this.title, FieldTypes.PAGE, this.value ); 185 } 186 return jData.fields().getField(this.name); 187 } 188 return jData.fields().getAbsoluteField(this.name, this.pageId); 192 } 193 194 public int doEndTag() throws JspException { 195 name = ""; 198 title = ""; 199 value = ""; 200 pageId = -1; 201 pageLevel = -1; 202 203 display = false; 204 205 page = null; 206 return EVAL_PAGE; 207 } 208 209 } 210 | Popular Tags |