1 20 21 package com.methodhead.property; 22 23 import java.util.HashMap ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 import com.methodhead.persistable.Persistable; 28 import com.methodhead.persistable.PersistableException; 29 30 import org.apache.commons.beanutils.DynaClass; 31 import org.apache.commons.beanutils.DynaProperty; 32 import org.apache.commons.beanutils.BasicDynaClass; 33 34 import com.methodhead.sitecontext.SiteContextCapable; 35 import com.methodhead.sitecontext.SiteContext; 36 37 48 public class Property 49 extends 50 Persistable 51 implements 52 SiteContextCapable { 53 54 private static DynaClass dynaClass_ = null; 55 56 static { 57 DynaProperty[] dynaProperties = 58 new DynaProperty[] { 59 new DynaProperty( "sitecontext_id", Integer .class ), 60 new DynaProperty( "name", String .class ), 61 new DynaProperty( "value", String .class ), 62 new DynaProperty( "description", String .class ), 63 new DynaProperty( "system_property", Boolean .class ) 64 }; 65 66 dynaClass_ = 67 new BasicDynaClass( 68 "mh_property", Property.class, dynaProperties ); 69 } 70 71 73 public Property() { 74 super( dynaClass_ ); 75 setInt( "sitecontext_id", 0 ); 76 setString( "name", "" ); 77 setString( "value", "" ); 78 setString( "description", "" ); 79 setBoolean( "system_property", false ); 80 } 81 82 public Property( 83 DynaClass dynaClass ) { 84 super( dynaClass ); 85 setInt( "sitecontext_id", 0 ); 86 setString( "name", "" ); 87 setString( "value", "" ); 88 setString( "description", "" ); 89 setBoolean( "system_property", false ); 90 } 91 92 94 96 98 public void setSiteContext( 99 SiteContext siteContext ) { 100 101 siteContext_ = siteContext; 102 } 103 104 public SiteContext getSiteContext() { 105 106 if ( siteContext_ == null ) 107 siteContext_ = SiteContext.getDefaultContext(); 108 109 return siteContext_; 110 } 111 112 121 public void setProperty( 122 String name, 123 String value, 124 String description, 125 Boolean system ) { 126 127 try { 128 load( 129 "sitecontext_id=" + getSiteContext().getInt( "id" ) + 130 " AND name=" + getSqlLiteral( name ) ); 131 } 132 catch ( PersistableException e ) { 133 setString( "name", name ); 137 138 setInt( "sitecontext_id", getSiteContext().getInt( "id" ) ); 139 140 if ( value != null ) 141 setString( "value", value ); 142 if ( description != null ) 143 setString( "description", description ); 144 if ( system != null ) 145 setBoolean( "system_property", system.booleanValue() ); 146 147 saveNew(); 148 149 return; 150 } 151 152 setInt( "sitecontext_id", getSiteContext().getInt( "id" ) ); 153 154 if ( value != null ) 155 setString( "value", value ); 156 if ( description != null ) 157 setString( "description", description ); 158 if ( system != null ) 159 setBoolean( "system_property", system.booleanValue() ); 160 161 save( 162 "sitecontext_id=" + getSiteContext().getInt( "id" ) + 163 " AND name=" + getSqlLiteral( name ) ); 164 } 165 166 public static void setProperty( 167 SiteContext siteContext, 168 String name, 169 String value, 170 String description, 171 Boolean system ) { 172 173 Property p = new Property(); 174 p.setSiteContext( siteContext ); 175 p.setProperty( name, value, description, system ); 176 } 177 178 181 public static void setProperty( 182 SiteContext siteContext, 183 String name, 184 String value ) { 185 186 setProperty( siteContext, name, value, null, null ); 187 } 188 189 193 public static String getProperty( 194 SiteContext siteContext, 195 String name ) { 196 197 Property p = new Property(); 198 p.setSiteContext( siteContext ); 199 return p.getProperty( name ); 200 } 201 202 206 public String getProperty( 207 String name ) { 208 209 try { 210 Property p = new Property(); 211 p.load( 212 "sitecontext_id=" + getSiteContext().getInt( "id" ) + 213 " AND name=" + getSqlLiteral( name ) ); 214 return p.getString( "value" ); 215 } 216 catch ( PersistableException e ) { 217 return null; 218 } 219 } 220 221 225 public static String getProperty( 226 SiteContext siteContext, 227 String name, 228 String defaultVal ) { 229 230 String val = getProperty( siteContext, name ); 231 232 if ( val == null ) 233 return defaultVal; 234 235 return val; 236 } 237 238 243 public static String [] getPropertyArray( 244 SiteContext siteContext, 245 String name ) { 246 247 String val = getProperty( siteContext, name ); 248 249 if ( val == null ) 250 return null; 251 252 String [] vals = val.split( "," ); 253 254 String [] trimmedVals = new String [ vals.length ]; 255 for ( int i = 0; i < vals.length; i++ ) 256 trimmedVals[ i ] = vals[ i ].trim(); 257 258 return trimmedVals; 259 } 260 261 264 public void loadForName( 265 String name ) { 266 267 load( 268 "sitecontext_id=" + getSiteContext().getInt( "id" ) + 269 " AND name=" + getSqlLiteral( name ) ); 270 } 271 272 275 public static List loadAll( 276 SiteContext siteContext ) { 277 278 Property p = new Property(); 279 p.setSiteContext( siteContext ); 280 return p.loadAll(); 281 } 282 283 286 public List loadAll() { 287 288 return loadAll( 289 dynaClass_, 290 "sitecontext_id=" + getSiteContext().getInt( "id" ), 291 "name" ); 292 } 293 294 296 298 private SiteContext siteContext_ = null; 299 } 300 | Popular Tags |