KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > controllers > kernel > impl > simple > InfoGlueSettingsController


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.cms.controllers.kernel.impl.simple;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.HashMap 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
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     //private InfoGluePropertyHelper propertyHelper = new FileInfoGluePropertyHelper();
67

68     /**
69      * A simple factory method
70      */

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 JavaDoc id, Database db) throws SystemException, Bug
83     {
84         return (InfoGlueProperty) getObjectWithId(InfoGlueProperty.class, id, db);
85     }
86
87     /**
88      * This method returns a list (of strings) of all label-keys the system uses.
89      */

90     
91     public List JavaDoc getSystemSettings(String JavaDoc bundleName)
92     {
93         return settingsController.getSystemSettings(bundleName);
94     }
95
96     /**
97      * This method returns a list (of locales) of all defined label-languages.
98      */

99     
100     public List JavaDoc getSettingsVariations(String JavaDoc nameSpace, String JavaDoc name, Database database) throws Exception JavaDoc
101     {
102         return settingsController.getSettingsVariations(nameSpace, name, database);
103     }
104
105     private Document getPropertyDocument(String JavaDoc nameSpace, String JavaDoc name, Database database) throws Exception JavaDoc
106     {
107         return settingsController.getPropertyDocument(nameSpace, name, database);
108     }
109
110     public void addVariation(String JavaDoc nameSpace, String JavaDoc variationId, Database database) throws Exception JavaDoc
111     {
112         settingsController.addVariation(nameSpace, "systemSettings", variationId, database);
113     }
114
115     public void addVariation(String JavaDoc nameSpace, String JavaDoc name, String JavaDoc variationId, Database database) throws Exception JavaDoc
116     {
117         settingsController.addVariation(nameSpace, name, variationId, database);
118     }
119
120     public void removeVariation(String JavaDoc nameSpace, String JavaDoc variationId, Database database) throws Exception JavaDoc
121     {
122         settingsController.removeVariation(nameSpace, "systemSettings", variationId, database);
123     }
124
125     public void removeVariation(String JavaDoc nameSpace, String JavaDoc name, String JavaDoc variationId, Database database) throws Exception JavaDoc
126     {
127         settingsController.removeVariation(nameSpace, name, variationId, database);
128     }
129
130     public void removeProperty(String JavaDoc nameSpace, String JavaDoc variationId, String JavaDoc key, Database database) throws Exception JavaDoc
131     {
132         settingsController.removeProperty(nameSpace, "systemSettings", variationId, key, database);
133     }
134
135     public void removeProperty(String JavaDoc nameSpace, String JavaDoc name, String JavaDoc variationId, String JavaDoc key, Database database) throws Exception JavaDoc
136     {
137         settingsController.removeProperty(nameSpace, name, variationId, key, database);
138     }
139
140     public void updateSettings(String JavaDoc nameSpace, String JavaDoc variationId, Map JavaDoc properties, Database database) throws Exception JavaDoc
141     {
142         settingsController.updateSettings(nameSpace, "systemSettings", variationId, properties, database);
143     }
144
145     public void updateSettings(String JavaDoc nameSpace, String JavaDoc name, String JavaDoc variationId, Map JavaDoc properties, Database database) throws Exception JavaDoc
146     {
147         settingsController.updateSettings(nameSpace, name, variationId, properties, database);
148     }
149
150     public void updateSetting(String JavaDoc nameSpace, String JavaDoc name, String JavaDoc variationId, String JavaDoc key, String JavaDoc value, Database database) throws Exception JavaDoc
151     {
152         Map JavaDoc propertiesMap = new HashMap JavaDoc();
153         propertiesMap.put(key, value);
154         
155         settingsController.updateSettings(nameSpace, name, variationId, propertiesMap, database);
156     }
157
158     public String JavaDoc getSetting(String JavaDoc nameSpace, String JavaDoc name, String JavaDoc derivedValue, String JavaDoc variationId, Database database) throws Exception JavaDoc
159     {
160         return settingsController.getSetting(nameSpace, name, derivedValue, variationId, database);
161     }
162
163     /**
164      * This method returns a InfoGlueProperty based on it's primary key inside a transaction
165      * @return InfoGlueProperty
166      * @throws Exception
167      */

168
169     public InfoGlueProperty getProperty(Integer JavaDoc id, Database database) throws Exception JavaDoc
170     {
171         InfoGlueProperty property = (InfoGlueProperty)database.load(InfoGlueProperty.class, id);
172         
173         return property;
174     }
175     
176     
177     /**
178      * Gets a list of all events available for a particular day.
179      * @return List of Event
180      * @throws Exception
181      */

182     
183     public InfoGlueProperty getProperty(String JavaDoc nameSpace, String JavaDoc name, Database database) throws Exception JavaDoc
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     /**
220      * This method is used to create a new InfoGlueProperty object in the database inside a transaction.
221      */

222     
223     public InfoGlueProperty createProperty(String JavaDoc nameSpace, String JavaDoc name, String JavaDoc value, Database database) throws Exception JavaDoc
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     /**
253      * Updates an property.
254      *
255      * @throws Exception
256      */

257     
258     public void updateProperty(String JavaDoc nameSpace, String JavaDoc name, String JavaDoc value, Database database) throws Exception JavaDoc
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     /**
282      * Updates an property inside an transaction.
283      *
284      * @throws Exception
285      */

286     
287     public void updateProperty(InfoGlueProperty property, String JavaDoc value, Database database) throws Exception JavaDoc
288     {
289         System.out.println("sat value ....");
290         property.setValue(value);
291         property.setComment("Updated on " + DateHelper.getSecondPreciseDate());
292     }
293
294
295     public String JavaDoc getProperty(String JavaDoc key, String JavaDoc 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 JavaDoc getProperty(String JavaDoc key, String JavaDoc name, String JavaDoc variationId, boolean skipInfoGlueProperty, boolean fallbackToDefault, boolean fallbackToKey, boolean useDerivedValue, Database database)
301     {
302         return getPropertyFromPropertySet(key, name, variationId, skipInfoGlueProperty, fallbackToDefault, fallbackToKey, useDerivedValue, database);
303         //return getPropertyFromInfoGlue(key, name, variationId, skipInfoGlueProperty, fallbackToDefault, fallbackToKey, useDerivedValue, database);
304
}
305     
306     public String JavaDoc getPropertyFromPropertySet(String JavaDoc key, String JavaDoc name, String JavaDoc variationId, boolean skipInfoGlueProperty, boolean fallbackToDefault, boolean fallbackToKey, boolean useDerivedValue, Database database)
307     {
308         Map JavaDoc args = new HashMap JavaDoc();
309         args.put("globalKey", "infoglue");
310         PropertySet ps = PropertySetManager.getInstance("jdbc", args);
311         return ps.getString(key);
312     }
313     
314     public String JavaDoc getPropertyFromInfoGlue(String JavaDoc key, String JavaDoc name, String JavaDoc variationId, boolean skipInfoGlueProperty, boolean fallbackToDefault, boolean fallbackToKey, boolean useDerivedValue, Database database)
315     {
316         NullComparator nullComparator = new NullComparator();
317
318         String JavaDoc value = null;
319         String JavaDoc newValue = null;
320         
321         String JavaDoc cacheKey = "" + key + "_" + name + "_" + variationId + "_" + skipInfoGlueProperty + "_" + fallbackToDefault + "_" + fallbackToKey + "_" + useDerivedValue;
322         Object JavaDoc cacheObject = CacheController.getCachedObject(CacheController.SETTINGSPROPERTIESCACHENAME, cacheKey);
323         //System.out.println("cacheObject:" + cacheObject);
324
if(cacheObject instanceof NullObject)
325         {
326             //System.out.println("Returning nullObject for " + key);
327
return null;
328         }
329         else if(cacheObject != null)
330         {
331             //System.out.println("Key:" + key + " was " + cacheObject.toString().trim());
332
return cacheObject.toString().trim();
333         }
334
335         String JavaDoc property = "";
336         if(fallbackToKey)
337             property = key;
338         
339         try
340         {
341             String JavaDoc derivedValue = null;
342             if(useDerivedValue)
343             {
344                 Object JavaDoc derivedObject = findOnValueStack(key);
345                 if(derivedObject != null)
346                     derivedValue = derivedObject.toString();
347             }
348             
349             //System.out.println("key:" + key);
350
//System.out.println("derivedValue:" + derivedValue);
351
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                 //System.out.println("Caching null object for " + key);
373
CacheController.cacheObject(CacheController.SETTINGSPROPERTIESCACHENAME, cacheKey, new NullObject());
374             }
375             else
376             {
377                 CacheController.cacheObject(CacheController.SETTINGSPROPERTIESCACHENAME, cacheKey, property);
378             }
379         }
380         catch(Exception JavaDoc 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 JavaDoc findOnValueStack(String JavaDoc expr)
389     {
390         ActionContext a = ActionContext.getContext();
391         Object JavaDoc value = a.getValueStack().findValue(expr);
392         return value;
393     }
394 /*
395     public void setInfoGluePropertyHelper(InfoGluePropertyHelper propertyHelper)
396     {
397         this.propertyHelper = propertyHelper;
398     }
399 */

400
401     public void setInfoGlueProperty(String JavaDoc string, String JavaDoc string2, String JavaDoc string3, String JavaDoc allowedContentTypeNames)
402     {
403         // TODO Auto-generated method stub
404

405     }
406
407     public BaseEntityVO getNewVO()
408     {
409         // TODO Auto-generated method stub
410
return null;
411     }
412
413     public InfoGlueProperty getProperty(Long JavaDoc id, Database database) throws Exception JavaDoc
414     {
415         // TODO Auto-generated method stub
416
return null;
417     }
418 }
419
Popular Tags