KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > common > settings > controllers > CastorSettingsController


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23
24 package org.infoglue.common.settings.controllers;
25
26 import java.io.File JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Locale JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Properties JavaDoc;
35 import java.util.ResourceBundle JavaDoc;
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     /**
53      * A simple factory method
54      */

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     /**
67      * This method returns a list (of strings) of all label-keys the system uses.
68      */

69     
70     public List JavaDoc getSystemSettings(String JavaDoc bundleName)
71     {
72         List JavaDoc labels = new ArrayList JavaDoc();
73         
74         Properties JavaDoc properties = CmsPropertyHandler.getProperties();
75         
76         Enumeration JavaDoc enumeration = properties.keys();
77         while(enumeration.hasMoreElements())
78         {
79             String JavaDoc key = (String JavaDoc)enumeration.nextElement();
80             labels.add(key);
81         }
82         
83         Collections.sort(labels);
84         
85         return labels;
86     }
87
88     /**
89      * This method returns a list (of locales) of all defined label-languages.
90      */

91     
92     public List JavaDoc getSettingsVariations(String JavaDoc nameSpace, String JavaDoc name, Database database) throws Exception JavaDoc
93     {
94         List JavaDoc locales = new ArrayList JavaDoc();
95         
96         Document document = getPropertyDocument(nameSpace, name, database);
97         if(document != null)
98         {
99             List JavaDoc languageNodes = document.selectNodes("/variations/variation");
100             Iterator JavaDoc languageNodesIterator = languageNodes.iterator();
101             while(languageNodesIterator.hasNext())
102             {
103                 Node node = (Node)languageNodesIterator.next();
104                 Element element = (Element)node;
105                 String JavaDoc id = element.attributeValue("id");
106                 
107                 locales.add(id);
108             }
109         }
110         
111         return locales;
112     }
113
114     
115     
116     public Document getPropertyDocument(String JavaDoc nameSpace, String JavaDoc name, Database database) throws Exception JavaDoc
117     {
118         String JavaDoc key = "propertyDocument_" + nameSpace + "_" + name;
119         
120         Object JavaDoc object = CacheController.getCachedObject(CacheController.SETTINGSPROPERTIESDOCUMENTCACHENAME, key);
121         //System.out.println("propertyDocument:" + key + "=" + object);
122
//log.debug("Cached object:" + object);
123
if(object instanceof NullObject)
124             return null;
125         
126         Document document = (Document)object;
127         
128         if(document == null)
129         {
130             //System.out.println("Reading document from database as it was uncached...");
131
InfoGlueProperty property = settingsPersister.getProperty(nameSpace, name, database);
132             if(property != null)
133             {
134                 String JavaDoc xml = property.getValue();
135                 if(xml != null && xml.length() > 0)
136                 {
137                     //log.debug("xml:" + xml);
138
try
139                     {
140                         document = domBuilder.getDocument(xml);
141                         //String xml2 = domBuilder.getFormattedDocument(document, false, false, "UTF-8");
142
//log.debug("xml2:" + xml2);
143
}
144                     catch(Exception JavaDoc 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                 //log.debug("Property was null...");
157
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 JavaDoc xml = domBuilder.getFormattedDocument(document, "UTF-8");
163                 //log.debug("xml:" + xml);
164

165                 settingsPersister.createProperty(nameSpace, name, xml, database);
166             
167                 //log.debug("Creating property:" + xml);
168

169                 document = domBuilder.getDocument(xml);
170             }
171         
172             if(document != null)
173             {
174                 //System.out.println("caching document:" + CacheController.SETTINGSPROPERTIESDOCUMENTCACHENAME + ":" + key + ":" + document);
175
CacheController.cacheObject(CacheController.SETTINGSPROPERTIESDOCUMENTCACHENAME, key, document);
176             }
177             else
178             {
179                 //System.out.println("caching document:" + CacheController.SETTINGSPROPERTIESDOCUMENTCACHENAME + ":" + key + ":" + new NullObject());
180
CacheController.cacheObject(CacheController.SETTINGSPROPERTIESDOCUMENTCACHENAME, key, new NullObject());
181             }
182         }
183         
184         return document;
185     }
186
187     public void addVariation(String JavaDoc nameSpace, String JavaDoc name, String JavaDoc id, Database database) throws Exception JavaDoc
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 JavaDoc xml = domBuilder.getFormattedDocument(document, "UTF-8");
201             //log.debug("xml:" + xml);
202

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 JavaDoc nameSpace, String JavaDoc name, String JavaDoc id, Database database) throws Exception JavaDoc
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 JavaDoc xml = domBuilder.getFormattedDocument(document, "UTF-8");
221             //log.debug("xml:" + xml);
222

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 JavaDoc nameSpace, String JavaDoc name, String JavaDoc id, String JavaDoc key, Database database) throws Exception JavaDoc
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 JavaDoc xml = domBuilder.getFormattedDocument(document, "UTF-8");
241             //log.debug("xml:" + xml);
242
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 JavaDoc nameSpace, String JavaDoc name, String JavaDoc id, Map JavaDoc properties, Database database) throws Exception JavaDoc
250     {
251         if(id == null || id.equals("-1"))
252             id = "default";
253
254         Document document = getPropertyDocument(nameSpace, name, database);
255         //String xml1 = domBuilder.getFormattedDocument(document, "UTF-8");
256
//log.debug("xml1:" + xml1);
257
String JavaDoc xpath = "/variations/variation[@id='" + id +"']/setting";
258         //log.debug("xpath:" + xpath);
259

260         Element labelsElement = (Element)document.selectSingleNode(xpath);
261         //log.debug("labelsElement:" + labelsElement);
262

263         Iterator JavaDoc keyInterator = properties.keySet().iterator();
264         while(keyInterator.hasNext())
265         {
266             String JavaDoc key = (String JavaDoc)keyInterator.next();
267             String JavaDoc value = (String JavaDoc)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 JavaDoc elements = labelElement.elements();
285                     Iterator JavaDoc elementsIterator = elements.iterator();
286                     while(elementsIterator.hasNext())
287                     {
288                         Element element = (Element)elementsIterator.next();
289                         //log.debug("Removing element:" + element.asXML());
290
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 JavaDoc 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     /**
310      * This method returns a label from the label system
311      * @param nameSpace
312      * @param key
313      * @param locale
314      * @param database
315      * @return
316      * @throws Exception
317      */

318     public String JavaDoc getSetting(String JavaDoc nameSpace, String JavaDoc name, String JavaDoc key, String JavaDoc id, Database database) throws Exception JavaDoc
319     {
320         String JavaDoc setting = null;
321
322         //System.out.println("name: " + name);
323

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             //System.out.println("name: " + name);
334

335             //System.out.println("Document: " + document.asXML());
336

337             //System.out.println("key: " + key);
338
//log.debug("key:" + key);
339
//log.debug("locale.getLanguage():" + locale.getLanguage());
340
if(document != null)
341             {
342                 if(id == null)
343                     id = "default";
344                 
345                 String JavaDoc xpath = "/variations/variation[@id='" + id +"']/setting/" + key;
346                 //log.debug("xpath:" + xpath);
347
//System.out.println("xpath:" + xpath);
348

349                 Element labelElement = (Element)document.selectSingleNode(xpath);
350                 //log.debug("labelElement:" + labelElement);
351
//System.out.println("labelElement: " + labelElement);
352

353                 if(labelElement != null)
354                     setting = labelElement.getText();
355
356                 //System.out.println("setting: " + setting);
357
}
358         }
359         
360         return setting;
361     }
362
363 }
364
Popular Tags