1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.util.Enumeration ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Locale ; 33 import java.util.Map ; 34 35 import org.apache.commons.collections.comparators.NullComparator; 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 import org.dom4j.Document; 39 import org.exolab.castor.jdo.Database; 40 import org.exolab.castor.jdo.OQLQuery; 41 import org.exolab.castor.jdo.QueryResults; 42 import org.infoglue.cms.entities.kernel.BaseEntityVO; 43 import org.infoglue.cms.entities.management.InfoGlueProperty; 44 import org.infoglue.cms.exception.Bug; 45 import org.infoglue.cms.exception.SystemException; 46 import org.infoglue.cms.util.CmsPropertyHandler; 47 import org.infoglue.cms.util.DateHelper; 48 import org.infoglue.cms.util.dom.DOMBuilder; 49 import org.infoglue.common.settings.controllers.CastorSettingsController; 50 import org.infoglue.common.settings.controllers.CastorSettingsPersister; 51 import org.infoglue.deliver.util.NullObject; 52 import org.infoglue.deliver.util.CacheController; 53 54 import webwork.action.ActionContext; 55 56 import com.opensymphony.module.propertyset.PropertySet; 57 import com.opensymphony.module.propertyset.PropertySetManager; 58 59 60 public class InfoGlueSettingsController extends BaseController implements CastorSettingsPersister 61 { 62 private static Log log = LogFactory.getLog(InfoGlueSettingsController.class); 63 64 private DOMBuilder domBuilder = new DOMBuilder(); 65 private CastorSettingsController settingsController = null; 66 68 71 72 public static InfoGlueSettingsController getInfoGlueSettingsController() 73 { 74 return new InfoGlueSettingsController(); 75 } 76 77 private InfoGlueSettingsController() 78 { 79 this.settingsController = CastorSettingsController.getController(this); 80 } 81 82 public InfoGlueProperty getPropertyWithId(Integer id, Database db) throws SystemException, Bug 83 { 84 return (InfoGlueProperty) getObjectWithId(InfoGlueProperty.class, id, db); 85 } 86 87 90 91 public List getSystemSettings(String bundleName) 92 { 93 return settingsController.getSystemSettings(bundleName); 94 } 95 96 99 100 public List getSettingsVariations(String nameSpace, String name, Database database) throws Exception 101 { 102 return settingsController.getSettingsVariations(nameSpace, name, database); 103 } 104 105 private Document getPropertyDocument(String nameSpace, String name, Database database) throws Exception 106 { 107 return settingsController.getPropertyDocument(nameSpace, name, database); 108 } 109 110 public void addVariation(String nameSpace, String variationId, Database database) throws Exception 111 { 112 settingsController.addVariation(nameSpace, "systemSettings", variationId, database); 113 } 114 115 public void addVariation(String nameSpace, String name, String variationId, Database database) throws Exception 116 { 117 settingsController.addVariation(nameSpace, name, variationId, database); 118 } 119 120 public void removeVariation(String nameSpace, String variationId, Database database) throws Exception 121 { 122 settingsController.removeVariation(nameSpace, "systemSettings", variationId, database); 123 } 124 125 public void removeVariation(String nameSpace, String name, String variationId, Database database) throws Exception 126 { 127 settingsController.removeVariation(nameSpace, name, variationId, database); 128 } 129 130 public void removeProperty(String nameSpace, String variationId, String key, Database database) throws Exception 131 { 132 settingsController.removeProperty(nameSpace, "systemSettings", variationId, key, database); 133 } 134 135 public void removeProperty(String nameSpace, String name, String variationId, String key, Database database) throws Exception 136 { 137 settingsController.removeProperty(nameSpace, name, variationId, key, database); 138 } 139 140 public void updateSettings(String nameSpace, String variationId, Map properties, Database database) throws Exception 141 { 142 settingsController.updateSettings(nameSpace, "systemSettings", variationId, properties, database); 143 } 144 145 public void updateSettings(String nameSpace, String name, String variationId, Map properties, Database database) throws Exception 146 { 147 settingsController.updateSettings(nameSpace, name, variationId, properties, database); 148 } 149 150 public void updateSetting(String nameSpace, String name, String variationId, String key, String value, Database database) throws Exception 151 { 152 Map propertiesMap = new HashMap (); 153 propertiesMap.put(key, value); 154 155 settingsController.updateSettings(nameSpace, name, variationId, propertiesMap, database); 156 } 157 158 public String getSetting(String nameSpace, String name, String derivedValue, String variationId, Database database) throws Exception 159 { 160 return settingsController.getSetting(nameSpace, name, derivedValue, variationId, database); 161 } 162 163 168 169 public InfoGlueProperty getProperty(Integer id, Database database) throws Exception 170 { 171 InfoGlueProperty property = (InfoGlueProperty)database.load(InfoGlueProperty.class, id); 172 173 return property; 174 } 175 176 177 182 183 public InfoGlueProperty getProperty(String nameSpace, String name, Database database) throws Exception 184 { 185 InfoGlueProperty property = null; 186 187 boolean localTransaction = false; 188 if(database == null) 189 { 190 localTransaction = true; 191 database = CastorDatabaseService.getDatabase(); 192 database.begin(); 193 } 194 195 OQLQuery oql = database.getOQLQuery("SELECT p FROM org.infoglue.cms.entities.management.InfoGlueProperty p WHERE p.nameSpace = $1 AND p.name = $2 ORDER BY p.id"); 196 oql.bind(nameSpace); 197 oql.bind(name); 198 199 QueryResults results = oql.execute(); 200 201 if (results.hasMore()) 202 { 203 property = (InfoGlueProperty)results.next(); 204 } 205 206 results.close(); 207 oql.close(); 208 209 if(localTransaction) 210 { 211 database.commit(); 212 database.close(); 213 } 214 215 return property; 216 } 217 218 219 222 223 public InfoGlueProperty createProperty(String nameSpace, String name, String value, Database database) throws Exception 224 { 225 boolean localTransaction = false; 226 if(database == null) 227 { 228 localTransaction = true; 229 database = CastorDatabaseService.getDatabase(); 230 database.begin(); 231 } 232 233 InfoGlueProperty property = new InfoGlueProperty(); 234 property.setNameSpace(nameSpace); 235 property.setName(name); 236 property.setValue(value); 237 238 database.create(property); 239 240 if(localTransaction) 241 { 242 database.commit(); 243 database.close(); 244 } 245 246 log.debug("Creating property with:" + nameSpace + ":" + name); 247 248 return property; 249 } 250 251 252 257 258 public void updateProperty(String nameSpace, String name, String value, Database database) throws Exception 259 { 260 boolean localTransaction = false; 261 if(database == null) 262 { 263 localTransaction = true; 264 database = CastorDatabaseService.getDatabase(); 265 database.begin(); 266 } 267 268 InfoGlueProperty property = getProperty(nameSpace, name, database); 269 if(property == null) 270 property = createProperty(nameSpace, name, value, database); 271 272 updateProperty(property, value, database); 273 274 if(localTransaction) 275 { 276 database.commit(); 277 database.close(); 278 } 279 } 280 281 286 287 public void updateProperty(InfoGlueProperty property, String value, Database database) throws Exception 288 { 289 System.out.println("sat value ...."); 290 property.setValue(value); 291 property.setComment("Updated on " + DateHelper.getSecondPreciseDate()); 292 } 293 294 295 public String getProperty(String key, String variationId, boolean skipInfoGlueProperty, boolean fallbackToDefault, boolean fallbackToKey, boolean useDerivedValue, Database database) 296 { 297 return getProperty(key, "systemSettings", variationId, skipInfoGlueProperty, fallbackToDefault, fallbackToKey, useDerivedValue, database); 298 } 299 300 public String getProperty(String key, String name, String variationId, boolean skipInfoGlueProperty, boolean fallbackToDefault, boolean fallbackToKey, boolean useDerivedValue, Database database) 301 { 302 return getPropertyFromPropertySet(key, name, variationId, skipInfoGlueProperty, fallbackToDefault, fallbackToKey, useDerivedValue, database); 303 } 305 306 public String getPropertyFromPropertySet(String key, String name, String variationId, boolean skipInfoGlueProperty, boolean fallbackToDefault, boolean fallbackToKey, boolean useDerivedValue, Database database) 307 { 308 Map args = new HashMap (); 309 args.put("globalKey", "infoglue"); 310 PropertySet ps = PropertySetManager.getInstance("jdbc", args); 311 return ps.getString(key); 312 } 313 314 public String getPropertyFromInfoGlue(String key, String name, String variationId, boolean skipInfoGlueProperty, boolean fallbackToDefault, boolean fallbackToKey, boolean useDerivedValue, Database database) 315 { 316 NullComparator nullComparator = new NullComparator(); 317 318 String value = null; 319 String newValue = null; 320 321 String cacheKey = "" + key + "_" + name + "_" + variationId + "_" + skipInfoGlueProperty + "_" + fallbackToDefault + "_" + fallbackToKey + "_" + useDerivedValue; 322 Object cacheObject = CacheController.getCachedObject(CacheController.SETTINGSPROPERTIESCACHENAME, cacheKey); 323 if(cacheObject instanceof NullObject) 325 { 326 return null; 328 } 329 else if(cacheObject != null) 330 { 331 return cacheObject.toString().trim(); 333 } 334 335 String property = ""; 336 if(fallbackToKey) 337 property = key; 338 339 try 340 { 341 String derivedValue = null; 342 if(useDerivedValue) 343 { 344 Object derivedObject = findOnValueStack(key); 345 if(derivedObject != null) 346 derivedValue = derivedObject.toString(); 347 } 348 349 if(!skipInfoGlueProperty) 352 { 353 if(derivedValue != null) 354 property = InfoGlueSettingsController.getInfoGlueSettingsController().getSetting("infoglueCMS", name, derivedValue, variationId, database); 355 else 356 property = InfoGlueSettingsController.getInfoGlueSettingsController().getSetting("infoglueCMS", name, key, variationId, database); 357 } 358 359 if(skipInfoGlueProperty || ((property == null || property.equals("")) && fallbackToDefault)) 360 { 361 if(derivedValue != null) 362 property = CmsPropertyHandler.getProperty(derivedValue); 363 else 364 property = CmsPropertyHandler.getProperty(key); 365 } 366 367 if((property == null || property.equals("")) && fallbackToKey) 368 property = key; 369 370 if(property == null) 371 { 372 CacheController.cacheObject(CacheController.SETTINGSPROPERTIESCACHENAME, cacheKey, new NullObject()); 374 } 375 else 376 { 377 CacheController.cacheObject(CacheController.SETTINGSPROPERTIESCACHENAME, cacheKey, property); 378 } 379 } 380 catch(Exception e) 381 { 382 log.warn("An property was not found for key: " + key + ": " + e.getMessage(), e); 383 } 384 385 return property; 386 } 387 388 public static Object findOnValueStack(String expr) 389 { 390 ActionContext a = ActionContext.getContext(); 391 Object value = a.getValueStack().findValue(expr); 392 return value; 393 } 394 400 401 public void setInfoGlueProperty(String string, String string2, String string3, String allowedContentTypeNames) 402 { 403 405 } 406 407 public BaseEntityVO getNewVO() 408 { 409 return null; 411 } 412 413 public InfoGlueProperty getProperty(Long id, Database database) throws Exception 414 { 415 return null; 417 } 418 } 419 | Popular Tags |