1 package org.jahia.taglibs.field; 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.JspWriter ; 9 import javax.servlet.jsp.tagext.TagSupport ; 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.exceptions.JahiaException; 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.FieldBean; 19 20 28 public abstract class AbstractFieldTag extends TagSupport { 29 30 private boolean display = true; 31 protected String name = ""; 32 private int pageId = -1; 33 private int pageLevel = -1; 34 private boolean diffActive = false; 35 private String defaultValue = null; 36 private String fieldValue = null; 37 private String valueId = null; 38 39 public boolean isDisplay() { 40 return display; 41 } 42 public void setDisplay(boolean display) { 43 this.display = display; 44 } 45 46 public void setName (String name) { 47 this.name = name; 48 } 49 50 public void setDefaultValue (String value) { 51 this.defaultValue = value; 52 } 53 54 public void setPageId (String pageId) { 55 try { 56 this.pageId = Integer.parseInt(pageId); 57 } catch (NumberFormatException nfe) { 58 JahiaConsole.println("AbstractFieldTag: setPageId", 59 "The given page id is not a number"); 60 } 61 } 62 63 public void setPageLevel (String pageLevel) { 64 try { 65 this.pageLevel = Integer.parseInt(pageLevel); 66 } catch (NumberFormatException nfe) { 67 JahiaConsole.println("AbstractFieldTag: setPageLevel", 68 "The given page level is not a number"); 69 } 70 } 71 72 public boolean isDiffActive() { 73 return diffActive; 74 } 75 76 public void setDiffActive(boolean diffActive) { 77 this.diffActive = diffActive; 78 } 79 public String getValueId() { 80 return valueId; 81 } 82 public void setValueId(String valueId) { 83 this.valueId = valueId; 84 } 85 86 public int doStartTag () throws JspTagException { 87 88 if ( getId() != null ){ 90 pageContext.removeAttribute(getId()); 91 } 92 93 if ( getValueId() != null ){ 95 pageContext.removeAttribute(getValueId()); 96 } 97 98 ServletRequest request = pageContext.getRequest(); 99 JspWriter out = pageContext.getOut(); 100 101 JahiaData jData = (JahiaData) request.getAttribute( 102 "org.jahia.data.JahiaData"); 103 104 try { 105 if (this.pageId == -1) { 107 if (this.pageLevel != -1) { 109 this.pageId = jData.gui().getLevelID(this.pageLevel); 110 } 111 } 112 } catch (JahiaException je) { 113 JahiaConsole.printe("AbstractField.doStartTag:getLevelID call", je); 114 } 115 116 JahiaContainer container = null; 118 ContainerTag parent = (ContainerTag) findAncestorWithClass(this, 120 ContainerTag.class); 121 if (parent != null) { 122 ContainerListTag containerListTag = (ContainerListTag) findAncestorWithClass(this, 123 ContainerListTag.class); 124 container = parent.getContainer(); 125 } 126 127 try { 128 129 JahiaField theField = null; 130 if (parent == null) { 131 theField = getField(jData); 132 } else { 133 if (parent.displayBody()) { 134 if (container != null) { 136 theField = container.getField(this.name); 137 } 138 } 139 } 140 141 setIDAttribute(theField,jData); 142 setValueIDAttribute(theField,jData); 143 144 if ( isDisplay() ){ 145 out.print(readValue(jData, fieldValue)); 146 } 147 148 } catch (IOException ioe) { 149 JahiaConsole.println("AbstractFieldTag: doStartTag", 150 "Error while displaying tag '" + this.name + 151 "' : " + ioe.toString()); 152 } catch (JahiaException je) { 153 JahiaConsole.println("AbstractFieldTag: doStartTag", 154 "Error while displaying tag '" + this.name + 155 "' : " + je.toString()); 156 } catch (NullPointerException npe) { 157 JahiaConsole.println("AbstractFieldTag: doStartTag", 158 "Error while displaying tag '" + this.name + 159 "' : " + npe.toString()); 160 } 161 return SKIP_BODY; 162 } 163 164 private JahiaField getField (JahiaData jData) throws JahiaException { 165 166 if (this.pageId == -1 || jData.page().getID() == this.pageId) { 168 169 return jData.fields().getField(this.name); 170 } 171 return jData.fields().getAbsoluteField(this.name, this.pageId); 175 } 176 177 public String readValue (JahiaData jData, String value) { 178 return value; 182 } 183 184 public abstract int getFieldType (); 186 187 public int doEndTag () throws JspException { 188 name = ""; 191 pageId = -1; 192 pageLevel = -1; 193 diffActive = false; 194 display = true; 195 defaultValue = null; 196 fieldValue = null; 197 valueId = null; 198 return EVAL_PAGE; 199 } 200 201 protected void setIDAttribute (JahiaField theField, JahiaData jData) { 202 203 if ( getId() != null ){ 205 pageContext.removeAttribute(getId()); 206 } 207 208 if (theField != null) { 209 if (getId() != null) { 210 pageContext.setAttribute(getId(), 211 new FieldBean(theField, 212 jData.params())); 213 } 214 } 215 } 216 217 221 protected void setValueIDAttribute(JahiaField theField, JahiaData jData){ 222 223 if (theField != null) { 224 230 if (isDiffActive()) { 231 fieldValue = theField.getHighLightDiffValue(jData.params()); 232 } else { 233 fieldValue = theField.getValue(); 234 } 235 } 236 237 if ( fieldValue == null ){ 238 fieldValue = defaultValue; 239 } 240 241 if (getValueId() != null) { 242 pageContext.setAttribute(getValueId(),fieldValue); 243 } 244 } 245 } 246 | Popular Tags |