1 package org.tigris.scarab.om; 2 3 48 49 import java.util.List ; 50 import java.io.Serializable ; 51 52 import org.apache.log4j.Logger; 53 import org.apache.torque.om.Persistent; 54 import org.apache.torque.TorqueException; 55 import org.apache.torque.util.Criteria; 56 import org.apache.turbine.Turbine; 57 import org.tigris.scarab.tools.localization.L10NKeySet; 58 import org.tigris.scarab.tools.localization.L10NMessage; 59 import org.tigris.scarab.util.ScarabRuntimeException; 60 61 62 71 public class GlobalParameterManager 72 extends BaseGlobalParameterManager 73 { 74 private static final String MANAGER_KEY = DEFAULT_MANAGER_CLASS; 75 private static final String GET_STRING = "getString"; 76 private static final String GET_BOOLEAN = "getBoolean"; 77 78 private static final Logger LOG = Logger.getLogger("org.tigris.scarab"); 79 80 85 public GlobalParameterManager() 86 throws TorqueException 87 { 88 super(); 89 setRegion(getClassName().replace('.', '_')); 90 } 91 92 protected Persistent putInstanceImpl(Persistent om) 93 throws TorqueException 94 { 95 Persistent oldOm = super.putInstanceImpl(om); 96 GlobalParameter gp = (GlobalParameter)om; 98 Serializable moduleId = gp.getModuleId(); 99 String name = gp.getName(); 100 if (moduleId == null) 101 { 102 getMethodResult().removeAll(MANAGER_KEY, name); 105 getMethodResult().removeAll(MANAGER_KEY, name); 106 } 107 else 108 { 109 getMethodResult().remove(MANAGER_KEY, name, GET_BOOLEAN, 110 moduleId); 111 getMethodResult().remove(MANAGER_KEY, name, GET_STRING, 112 moduleId); 113 } 114 127 return oldOm; 128 } 129 130 private static GlobalParameter getInstance(String name) 131 throws TorqueException 132 { 133 GlobalParameter p = getInstance(name, null); 135 if (p == null) 136 { 137 p = getInstance(); 139 p.setName(name); 140 } 141 return p; 142 } 143 144 private static GlobalParameter getInstance(String name, Module module) 145 throws TorqueException 146 { 147 GlobalParameter result = null; 148 Criteria crit = new Criteria(); 149 crit.add(GlobalParameterPeer.NAME, name); 150 if (module == null) 151 { 152 crit.add(GlobalParameterPeer.MODULE_ID, null); 153 } 154 else 155 { 156 crit.add(GlobalParameterPeer.MODULE_ID, module.getModuleId()); 157 } 158 List parameters = GlobalParameterPeer.doSelect(crit); 159 if (!parameters.isEmpty()) 160 { 161 result = (GlobalParameter)parameters.get(0); 162 } 163 return result; 164 } 165 166 public static String getString(String key) 167 throws TorqueException 168 { 169 String result = null; 173 Object obj = getMethodResult().get(MANAGER_KEY, key, GET_STRING); 176 if (obj == null) 177 { 178 result = getInstance(key).getValue(); 179 if (result == null) 180 { 181 result = Turbine.getConfiguration().getString(key); 182 if (result == null || result.trim().length() == 0) 183 { 184 result = ""; 185 } 186 } 187 if(!result.equals("")) 188 { 189 getMethodResult().put(result, MANAGER_KEY, key, GET_STRING); 190 } 191 } 192 else 193 { 194 result = (String )obj; 195 } 196 return result; 197 } 198 199 public static String getString(String name, Module module) 200 throws TorqueException 201 { 202 String result = null; 203 if (module == null) 204 { 205 result = getString(name); 206 } 207 else 208 { 209 Object obj = getMethodResult() 210 .get(MANAGER_KEY, name, GET_STRING, module); 211 if (obj == null) 212 { 213 GlobalParameter p = getInstance(name, module); 214 if (p == null) 215 { 216 result = getString(name); 218 } 219 else 220 { 221 result = p.getValue(); 222 getMethodResult() 223 .put(result, MANAGER_KEY, name, GET_STRING, module); 224 } 225 } 226 else 227 { 228 result = (String )obj; 229 } 230 } 231 return result; 232 } 233 234 public static void setString(String name, String value) 235 throws Exception 236 { 237 GlobalParameter p = getInstance(name); 238 p.setValue(value); 239 p.save(); 240 } 241 242 public static void setString(String name, Module module, String value) 243 throws Exception 244 { 245 if (module == null) 246 { 247 setString(name, value); 248 } 249 else 250 { 251 GlobalParameter p = getInstance(name, module); 252 if (p == null) 253 { 254 p = getInstance(name).copy(); 255 p.setModuleId(module.getModuleId()); 256 } 257 p.setValue(value); 258 p.save(); 259 260 getMethodResult().put(value, MANAGER_KEY, name, GET_STRING, module); 261 } 262 } 263 264 public static boolean getBoolean(String name) 265 throws TorqueException 266 { 267 Boolean result = null; 271 Object obj = getMethodResult().get(MANAGER_KEY, name, GET_BOOLEAN); 272 if (obj == null) 273 { 274 result = ("T".equals(getInstance(name).getValue())) ? 275 Boolean.TRUE : Boolean.FALSE; 276 getMethodResult() 277 .put(result, MANAGER_KEY, name, GET_BOOLEAN); 278 } 279 else 280 { 281 result = (Boolean )obj; 282 } 283 return result.booleanValue(); 284 } 285 286 public static boolean getBoolean(String name, Module module) 287 throws TorqueException 288 { 289 boolean b = false; 290 if (module == null) 291 { 292 b = getBoolean(name); 293 } 294 else 295 { 296 Object obj = getMethodResult() 297 .get(MANAGER_KEY, name, GET_BOOLEAN, module); 298 if (obj == null) 299 { 300 GlobalParameter p = getInstance(name, module); 301 if (p == null) 302 { 303 b = getBoolean(name); 305 } 306 else 307 { 308 b = "T".equals(p.getValue()); 309 getMethodResult().put((b ? Boolean.TRUE : Boolean.FALSE), 310 MANAGER_KEY, name, GET_BOOLEAN, module); 311 } 312 } 313 else 314 { 315 b = ((Boolean )obj).booleanValue(); 316 } 317 } 318 return b; 319 } 320 321 332 public static boolean getBooleanFromHierarchy(String key, Module module, boolean def) 333 { 334 String defAsString = (def)? "T":"F"; 335 String bp = getStringFromHierarchy(key,module, defAsString ); 336 337 boolean result = (bp.equals("T") || bp.equals("true"))? true:false; 340 341 return result; 342 } 343 344 359 public static String getStringFromHierarchy(String key, Module module, String def) 360 { 361 String result = null; 362 Module me = module; 363 try 364 { 365 do 366 { 367 Object obj = getMethodResult().get(MANAGER_KEY, key, GET_STRING, me); 368 if (obj == null) 369 { 370 GlobalParameter p = getInstance(key, me); 371 if(p != null) 372 { 373 result = p.getValue(); 374 getMethodResult() 375 .put(result, MANAGER_KEY, key, GET_STRING, me); 376 } 377 } 378 else 379 { 380 result = (String )obj; 381 } 382 if (me == null) { 383 384 break; 385 } 386 Module parent = me.getParent(); 387 if(parent==me) 388 { 389 break; 390 } 391 me = parent; 392 } while (result==null || result.equals("")); 393 394 if(result==null || result.equals("")) 395 { 396 result = getString(key); 399 400 if(result == null || result.equals("")) 402 { 403 result = def; 404 } 405 } 406 407 } 408 catch (Exception e) 409 { 410 LOG.warn("Internal error while retrieving data from GLOBAL_PRAMETER_TABLE: ["+e.getMessage()+"]"); 411 L10NMessage msg = new L10NMessage(L10NKeySet.ExceptionTorqueGeneric,e.getMessage()); 412 throw new ScarabRuntimeException(msg); 413 } 414 415 return result; 416 } 417 418 public static void setBoolean(String name, boolean value) 419 throws Exception 420 { 421 setString(name, (value ? "T" : "F")); 422 } 423 424 public static void setBoolean(String name, Module module, boolean value) 425 throws Exception 426 { 427 if (module == null) 428 { 429 setBoolean(name, value); 430 } 431 else 432 { 433 GlobalParameter p = getInstance(name, module); 434 if (p == null) 435 { 436 p = getInstance(name).copy(); 437 p.setModuleId(module.getModuleId()); 438 } 439 String booleanString =(value)?"T":"F"; 440 p.setValue(booleanString); 441 p.save(); 442 443 Boolean bool =new Boolean (value); 444 getMethodResult().put(bool, MANAGER_KEY, name, GET_BOOLEAN, module); 445 } 446 } 447 } 448 | Popular Tags |