1 package org.jahia.taglibs.declarations; 2 3 import java.util.Hashtable ; 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.fields.FieldTypes; 12 import org.jahia.exceptions.JahiaException; 13 import org.jahia.resourcebundle.ResourceBundleMarker; 14 15 25 26 public class DeclareFieldTag extends BodyTagSupport { 27 28 private static org.apache.log4j.Logger logger = 29 org.apache.log4j.Logger.getLogger(DeclareFieldTag.class); 30 31 public DeclareFieldTag () { 32 } 33 34 protected String name = ""; 35 private String title = ""; 36 private String value = ""; 37 private String type = null; 38 private String titleKey = null; 39 private String bundleKey = null; 40 private String valueKey = 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 String getType () { 55 return type; 56 } 57 58 public void setType (String type) { 59 this.type = type; 60 } 61 62 public String getTitleKey () { 63 return titleKey; 64 } 65 66 public void setTitleKey (String titleKey) { 67 this.titleKey = titleKey; 68 } 69 70 public String getBundleKey () { 71 return bundleKey; 72 } 73 74 public void setBundleKey (String bundleKey) { 75 this.bundleKey = bundleKey; 76 } 77 78 public String getValueKey() { 79 return valueKey; 80 } 81 82 public void setValueKey(String valueKey) { 83 this.valueKey = valueKey; 84 } 85 86 public int doStartTag () 87 throws JspTagException { 88 89 if (this.title.equals("")) { 90 this.title = this.name; 91 } 92 93 ServletRequest request = pageContext.getRequest(); 94 95 JahiaData jData = (JahiaData) request.getAttribute( 96 "org.jahia.data.JahiaData"); 97 98 DeclareContainerTag parent = (DeclareContainerTag) 100 findAncestorWithClass(this, 101 DeclareContainerTag.class); 102 if (parent != null) { 103 104 DeclareContainerListTag declareContainerListTag = ( 105 DeclareContainerListTag) parent.getParent(); 106 if (declareContainerListTag != null) { 107 if (!declareContainerListTag.isChildAlreadyDeclared(this.name)) { 111 112 declareContainerListTag.addChild(this.name); 113 try { 114 jData.containers().declareField( declareContainerListTag.getName(), this.name, resolveTitle(), 115 resolveType(), resolveValue()); 116 } catch (JahiaException je) { 117 logger.error("Field " + this.name + 118 " was already in container", je); 119 } 120 } 121 return SKIP_BODY; 122 } 123 124 } else { 125 if (!jData.fields().checkDeclared(this.name)) { 128 try { 130 jData.fields().declareField(this.name, resolveTitle(), 131 resolveType(), 132 resolveValue()); 133 } catch (JahiaException je) { 134 logger.error( 135 "Error while declaring field attached to page " + 136 this.name + " : ", je); 137 } 138 } 139 140 } 141 142 return SKIP_BODY; 143 } 144 145 private int resolveType () { 146 String typeName = getType(); 147 Hashtable fieldTypes = FieldTypes.getInstance().getFieldTypes(); 148 Integer fieldTypeInt = (Integer ) fieldTypes.get(typeName); 149 return fieldTypeInt.intValue(); 150 } 151 152 private String resolveTitle() { 153 if ((titleKey != null) && (bundleKey != null)) { 154 return ResourceBundleMarker.drawMarker(bundleKey, titleKey, title); 155 } else { 156 return title; 157 } 158 } 159 160 private String resolveValue() { 161 if ((valueKey != null) && (bundleKey != null)) { 162 return ResourceBundleMarker.drawMarker(bundleKey, valueKey, value); 163 } else { 164 return value; 165 } 166 } 167 168 public int doEndTag () 169 throws JspException { 170 name = ""; 173 title = ""; 174 value = ""; 175 type = ""; 176 titleKey = null; 177 bundleKey = null; 178 valueKey = null; 179 return EVAL_PAGE; 180 } 181 182 } 183 | Popular Tags |