1 20 package org.jahia.data.fields; 21 22 import java.util.Hashtable ; 23 import java.util.Locale ; 24 import java.util.Properties ; 25 26 import org.jahia.content.ContentDefinition; 27 import org.jahia.content.ContentObject; 28 import org.jahia.content.FieldDefinitionKey; 29 import org.jahia.exceptions.JahiaException; 30 import org.jahia.resourcebundle.ResourceBundleMarker; 31 import org.jahia.services.fields.ContentField; 32 import org.jahia.services.pages.ContentPage; 33 import org.jahia.services.version.ContentObjectEntryState; 34 import org.jahia.services.version.EntryLoadRequest; 35 import org.jahia.utils.LanguageCodeConverters; 36 import java.io.Serializable ; 37 38 public class JahiaFieldDefinition extends ContentDefinition implements Serializable { 39 40 private static org.apache.log4j.Logger logger = 41 org.apache.log4j.Logger.getLogger(JahiaFieldDefinition.class); 42 43 static { 44 ContentDefinition.registerType(FieldDefinitionKey.FIELD_TYPE, 45 JahiaFieldDefinition.class.getName()); 46 } 47 48 49 private int ID; 50 private int jahiaID; 51 private String name; 52 53 56 private Hashtable subDefs; 57 58 59 private Properties props = new Properties (); 60 61 66 public JahiaFieldDefinition(int ID, 67 int jahiaID, 68 String name, 69 Hashtable subDefs ) 70 { 71 super(new FieldDefinitionKey(ID)); 72 this.ID = ID; 73 this.jahiaID = jahiaID; 74 this.name = name; 75 this.subDefs = subDefs; 76 } 78 79 82 protected JahiaFieldDefinition() { 83 84 } 85 86 91 public int getID() { return ID; } 92 public int getJahiaID() { return jahiaID; } 93 public String getName() { return name; } 94 public Hashtable getSubDefs() { return subDefs; } 95 public Properties getProperties() { return this.props; } 96 97 public void setID( int value ) { 98 this.ID = value; 99 setObjectKey(new FieldDefinitionKey(ID)); 100 } 101 102 public void setProperties( Properties props ){ 103 if ( props != null ){ 104 this.props = props; 105 } 106 } 107 108 public String getProperty(String name){ 109 return this.props.getProperty(name); 110 } 111 112 public void setProperty(String name, String value){ 113 if ( name != null && value != null ){ 114 this.props.setProperty(name,value); 115 } 116 } 117 118 124 public static ContentDefinition getChildInstance(String IDInType) { 125 126 try { 127 return org.jahia.registries.JahiaFieldDefinitionsRegistry 128 .getInstance().getDefinition(Integer.parseInt(IDInType)); 129 } catch (JahiaException je) { 130 logger.debug("Error retrieving ContentDefinition instance for id : " + IDInType, je); 131 } 132 return null; 133 } 134 135 141 public String getTitle( ContentObject contentObject, 142 ContentObjectEntryState entryState ) { 143 String title = ""; 144 int pageID = ((ContentField)contentObject).getPageID(); 145 try { 146 int pageDefID = ContentPage.getPage(pageID) 147 .getPageTemplateID(new EntryLoadRequest(entryState)); 148 return this.getTitle(pageDefID, 149 LanguageCodeConverters 150 .languageCodeToLocale(entryState.getLanguageCode())); 151 } catch ( Throwable t ){ 152 t.printStackTrace(); 153 } 154 return title; 155 } 156 157 public String getTitle( int pageDefID ) { 158 JahiaFieldSubDefinition theSubDef = getSubDef( pageDefID ); 159 if (theSubDef != null) { 160 return theSubDef.getTitle(); 161 } else { 162 return ""; 163 } 164 } 165 166 public String getTitle( int pageDefID , Locale locale) { 167 String title = ""; 168 169 title = getTitle(pageDefID); 170 ResourceBundleMarker resMarker = ResourceBundleMarker.parseMarkerValue(title); 171 if ( resMarker == null ){ 172 return title; 173 } 174 try { 175 title = resMarker.getValue(locale); 176 } catch ( Throwable t ){ 177 title = this.getName(); 178 } 179 return title; 180 } 181 182 public String getDefaultValue( int pageDefID ) { 183 JahiaFieldSubDefinition theSubDef = getSubDef( pageDefID ); 184 if (theSubDef != null) { 185 return theSubDef.getDefaultValue(); 186 } else { 187 return ""; 188 } 189 } 190 191 public int getType( int pageDefID ) { 192 JahiaFieldSubDefinition theSubDef = getSubDef( pageDefID ); 193 if (theSubDef != null) { 194 return theSubDef.getType(); 195 } else { 196 return -1; 197 } 198 } 199 200 public void setTitle( String title, int pageDefID ) { 201 JahiaFieldSubDefinition theSubDef = getSubDef( pageDefID ); 202 if (theSubDef == null) { 203 theSubDef = createSubDef( pageDefID ); 204 } 205 theSubDef.setTitle( title ); 206 } 207 208 public void setDefaultValue( String defaultValue, int pageDefID ) { 209 JahiaFieldSubDefinition theSubDef = getSubDef( pageDefID ); 210 if (theSubDef == null) { 211 theSubDef = createSubDef( pageDefID ); 212 } 213 theSubDef.setDefaultValue( defaultValue ); 214 } 215 216 public void setType( int type, int pageDefID ) { 217 JahiaFieldSubDefinition theSubDef = getSubDef( pageDefID ); 218 if (theSubDef == null) { 219 theSubDef = createSubDef( pageDefID ); 220 } 221 theSubDef.setType( type ); 222 } 223 225 226 227 231 private JahiaFieldSubDefinition getSubDef( int pageDefID ) 232 { 233 234 236 JahiaFieldSubDefinition sd = (JahiaFieldSubDefinition)subDefs.get( new Integer (pageDefID) ); 237 244 return sd; 245 } 247 248 249 253 private JahiaFieldSubDefinition createSubDef( int pageDefID ) 254 { 255 JahiaFieldSubDefinition theSubDef = new JahiaFieldSubDefinition( 0, 0, pageDefID, "", -1, "" ); 256 if (subDefs == null) { 257 subDefs = new Hashtable (); 258 } 259 subDefs.put( new Integer (theSubDef.getPageDefID()), theSubDef ); 260 return theSubDef; 261 } 263 264 265 266 } | Popular Tags |