1 23 24 package org.infoglue.common.settings.controllers; 25 26 import java.io.File ; 27 import java.util.ArrayList ; 28 import java.util.Collections ; 29 import java.util.Enumeration ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Locale ; 33 import java.util.Map ; 34 import java.util.Properties ; 35 import java.util.ResourceBundle ; 36 37 import org.dom4j.Document; 38 import org.dom4j.Element; 39 import org.dom4j.Node; 40 import org.exolab.castor.jdo.Database; 41 import org.infoglue.cms.entities.management.InfoGlueProperty; 42 import org.infoglue.cms.util.CmsPropertyHandler; 43 import org.infoglue.cms.util.dom.DOMBuilder; 44 import org.infoglue.deliver.util.CacheController; 45 import org.infoglue.deliver.util.NullObject; 46 47 public class CastorSettingsController 48 { 49 private DOMBuilder domBuilder = new DOMBuilder(); 50 private CastorSettingsPersister settingsPersister = null; 51 52 55 56 public static CastorSettingsController getController(CastorSettingsPersister labelsPersister) 57 { 58 return new CastorSettingsController(labelsPersister); 59 } 60 61 private CastorSettingsController(CastorSettingsPersister settingsPersister) 62 { 63 this.settingsPersister = settingsPersister; 64 } 65 66 69 70 public List getSystemSettings(String bundleName) 71 { 72 List labels = new ArrayList (); 73 74 Properties properties = CmsPropertyHandler.getProperties(); 75 76 Enumeration enumeration = properties.keys(); 77 while(enumeration.hasMoreElements()) 78 { 79 String key = (String )enumeration.nextElement(); 80 labels.add(key); 81 } 82 83 Collections.sort(labels); 84 85 return labels; 86 } 87 88 91 92 public List getSettingsVariations(String nameSpace, String name, Database database) throws Exception 93 { 94 List locales = new ArrayList (); 95 96 Document document = getPropertyDocument(nameSpace, name, database); 97 if(document != null) 98 { 99 List languageNodes = document.selectNodes("/variations/variation"); 100 Iterator languageNodesIterator = languageNodes.iterator(); 101 while(languageNodesIterator.hasNext()) 102 { 103 Node node = (Node)languageNodesIterator.next(); 104 Element element = (Element)node; 105 String id = element.attributeValue("id"); 106 107 locales.add(id); 108 } 109 } 110 111 return locales; 112 } 113 114 115 116 public Document getPropertyDocument(String nameSpace, String name, Database database) throws Exception 117 { 118 String key = "propertyDocument_" + nameSpace + "_" + name; 119 120 Object object = CacheController.getCachedObject(CacheController.SETTINGSPROPERTIESDOCUMENTCACHENAME, key); 121 if(object instanceof NullObject) 124 return null; 125 126 Document document = (Document)object; 127 128 if(document == null) 129 { 130 InfoGlueProperty property = settingsPersister.getProperty(nameSpace, name, database); 132 if(property != null) 133 { 134 String xml = property.getValue(); 135 if(xml != null && xml.length() > 0) 136 { 137 try 139 { 140 document = domBuilder.getDocument(xml); 141 } 144 catch(Exception e) 145 { 146 document = domBuilder.createDocument(); 147 Element variationsElement = domBuilder.addElement(document, "variations"); 148 Element languageElement = domBuilder.addElement(variationsElement, "variation"); 149 domBuilder.addAttribute(languageElement, "id", "default"); 150 Element labelsElement = domBuilder.addElement(languageElement, "setting"); 151 } 152 } 153 } 154 else 155 { 156 document = domBuilder.createDocument(); 158 Element variationsElement = domBuilder.addElement(document, "variations"); 159 Element languageElement = domBuilder.addElement(variationsElement, "variation"); 160 domBuilder.addAttribute(languageElement, "id", "default"); 161 Element labelsElement = domBuilder.addElement(languageElement, "setting"); 162 String xml = domBuilder.getFormattedDocument(document, "UTF-8"); 163 165 settingsPersister.createProperty(nameSpace, name, xml, database); 166 167 169 document = domBuilder.getDocument(xml); 170 } 171 172 if(document != null) 173 { 174 CacheController.cacheObject(CacheController.SETTINGSPROPERTIESDOCUMENTCACHENAME, key, document); 176 } 177 else 178 { 179 CacheController.cacheObject(CacheController.SETTINGSPROPERTIESDOCUMENTCACHENAME, key, new NullObject()); 181 } 182 } 183 184 return document; 185 } 186 187 public void addVariation(String nameSpace, String name, String id, Database database) throws Exception 188 { 189 if(id == null || id.equals("-1")) 190 id = "default"; 191 192 Document document = getPropertyDocument(nameSpace, name, database); 193 Element variationElement = (Element)document.selectSingleNode("/variations/variation[@id='" + id + "']"); 194 if(variationElement == null) 195 { 196 Element variationsElement = (Element)document.selectSingleNode("/variations"); 197 Element languageElement = domBuilder.addElement(variationsElement, "variation"); 198 domBuilder.addAttribute(languageElement, "id", id); 199 Element labelsElement = domBuilder.addElement(languageElement, "setting"); 200 String xml = domBuilder.getFormattedDocument(document, "UTF-8"); 201 203 settingsPersister.updateProperty(nameSpace, name, xml, database); 204 205 CacheController.clearCache(CacheController.SETTINGSPROPERTIESCACHENAME); 206 CacheController.clearCache(CacheController.SETTINGSPROPERTIESDOCUMENTCACHENAME); 207 } 208 } 209 210 public void removeVariation(String nameSpace, String name, String id, Database database) throws Exception 211 { 212 if(id == null || id.equals("-1")) 213 id = "default"; 214 215 Document document = getPropertyDocument(nameSpace, name, database); 216 Element variationsElement = (Element)document.selectSingleNode("/variations/variation[@id='" + id + "']"); 217 if(variationsElement != null) 218 { 219 variationsElement.getParent().remove(variationsElement); 220 String xml = domBuilder.getFormattedDocument(document, "UTF-8"); 221 223 settingsPersister.updateProperty(nameSpace, name, xml, database); 224 225 CacheController.clearCache(CacheController.SETTINGSPROPERTIESCACHENAME); 226 CacheController.clearCache(CacheController.SETTINGSPROPERTIESDOCUMENTCACHENAME); 227 } 228 } 229 230 public void removeProperty(String nameSpace, String name, String id, String key, Database database) throws Exception 231 { 232 if(id == null || id.equals("-1")) 233 id = "default"; 234 235 Document document = getPropertyDocument(nameSpace, name, database); 236 Element settingElement = (Element)document.selectSingleNode("/variations/variation[@id='" + id + "']/setting/" + key); 237 if(settingElement != null) 238 { 239 settingElement.getParent().remove(settingElement); 240 String xml = domBuilder.getFormattedDocument(document, "UTF-8"); 241 settingsPersister.updateProperty(nameSpace, name, xml, database); 243 244 CacheController.clearCache(CacheController.SETTINGSPROPERTIESCACHENAME); 245 CacheController.clearCache(CacheController.SETTINGSPROPERTIESDOCUMENTCACHENAME); 246 } 247 } 248 249 public void updateSettings(String nameSpace, String name, String id, Map properties, Database database) throws Exception 250 { 251 if(id == null || id.equals("-1")) 252 id = "default"; 253 254 Document document = getPropertyDocument(nameSpace, name, database); 255 String xpath = "/variations/variation[@id='" + id +"']/setting"; 258 260 Element labelsElement = (Element)document.selectSingleNode(xpath); 261 263 Iterator keyInterator = properties.keySet().iterator(); 264 while(keyInterator.hasNext()) 265 { 266 String key = (String )keyInterator.next(); 267 String value = (String )properties.get(key); 268 if(!Character.isLetter(key.charAt(0))) 269 key = "NP" + key; 270 271 if(key != null && value != null && labelsElement != null) 272 { 273 Element labelElement = labelsElement.element(key); 274 if(labelElement == null) 275 { 276 System.out.println("labelElement was unexisting for key " + key); 277 labelElement = domBuilder.addElement(labelsElement, key); 278 } 279 else 280 { 281 System.out.println("labelElement was existing for key " + key); 282 labelElement.clearContent(); 283 284 List elements = labelElement.elements(); 285 Iterator elementsIterator = elements.iterator(); 286 while(elementsIterator.hasNext()) 287 { 288 Element element = (Element)elementsIterator.next(); 289 labelElement.remove(element); 291 } 292 } 293 294 domBuilder.addCDATAElement(labelElement, value); 295 System.out.println("Adding element " + key + ":" + labelElement.asXML()); 296 } 297 } 298 299 String xml = domBuilder.getFormattedDocument(document, "UTF-8"); 300 System.out.println("xml:" + xml); 301 302 settingsPersister.updateProperty(nameSpace, name, xml, database); 303 304 CacheController.clearCache(CacheController.SETTINGSPROPERTIESCACHENAME); 305 CacheController.clearCache(CacheController.SETTINGSPROPERTIESDOCUMENTCACHENAME); 306 307 } 308 309 318 public String getSetting(String nameSpace, String name, String key, String id, Database database) throws Exception 319 { 320 String setting = null; 321 322 324 if(id == null || id.equals("-1")) 325 id = "default"; 326 327 if(key != null && !key.equals("")) 328 { 329 if(!Character.isLetter(key.charAt(0))) 330 key = "NP" + key; 331 332 Document document = getPropertyDocument(nameSpace, name, database); 333 335 337 if(document != null) 341 { 342 if(id == null) 343 id = "default"; 344 345 String xpath = "/variations/variation[@id='" + id +"']/setting/" + key; 346 349 Element labelElement = (Element)document.selectSingleNode(xpath); 350 353 if(labelElement != null) 354 setting = labelElement.getText(); 355 356 } 358 } 359 360 return setting; 361 } 362 363 } 364 | Popular Tags |