KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > raptus > owxv3 > XMLCategoryDataManager


1 /*
2  * eAdmin/OWX
3  * Copyright (C) 1996-2003 OWX-Project Team <owx-team@gmx.net>
4  */

5
6 package com.raptus.owxv3;
7
8 import java.io.File JavaDoc;
9 import java.util.*;
10
11 import org.jdom.*;
12 import org.jdom.input.SAXBuilder;
13
14 import com.raptus.owxv3.api.*;
15 import com.raptus.owxv3.modules.categories.CategoryConstants;
16
17 /**
18  * <hr>
19  * <table width="100%" border="0">
20  * <tr>
21  * <td width="24%"><b>Filename</b></td><td width="76%">XMLCategoryDataManager.java</td>
22  * </tr>
23  * <tr>
24  * <td width="24%"><b>Author</b></td><td width="76%">Reea)</td>
25  * </tr>
26  * <tr>
27  * <td width="24%"><b>Date</b></td><td width="76%">7th of April 2001</td>
28  * </tr>
29  * </table>
30  * <hr>
31  * <table width="100%" border="0">
32  * <tr>
33  * <td width="24%"><b>Date / Author</b></td><td width="76%"><b>Changes</b></td>
34  * </tr>
35  * </table>
36  * <hr>
37  */

38
39 /*
40  * The xml file should look like:
41  *
42  * <categorylist>
43  * <category id='daily' owner='news'> <!-- level is 0 -->
44  * <catname locale='en_GB' text='Daily news'>
45  * <catname locale='de_CH' text='Daily news(de)'>
46  * <catname locale='fr_CH' text='Daily news(fr)'>
47  * <category id='stockmarket' owner='news'> <!-- level is 1 -->
48  * <catname locale='en_GB' text='Stockmarket news'>
49  * <catname locale='de_CH' text=' Stockmarket news(de)'>
50  * <catname locale='fr_CH' text=' Stockmarket news(fr)'>
51  * </category>
52  * </category>
53  *
54  * <category id='computer accessories' owner='products'> <!-- level is 0 -->
55  * <catname locale='en_GB' text='Computer Accessories'>
56  * <catname locale='de_CH' text='Computer Accessories (de)'>
57  * <catname locale='fr_CH' text='Computer Accessories (fr)'>
58  * </category>
59  *
60  * </categorylist>
61  */

62
63 public class XMLCategoryDataManager extends Object JavaDoc implements DataManager
64 {
65     /**
66      * our virtual module?
67      */

68     protected VModule vmodule = null;
69
70     /**
71      * global resource refference
72      */

73     GlobalResources gres = null;
74
75
76     /**
77      * set a category
78      *
79      * @param category an Category as defined in XML file
80      * @param parent the parent category
81      */

82     protected void doCategory(Element cat, GResCategory parent)
83     throws java.sql.SQLException JavaDoc
84     {
85
86         if(cat == null)
87         {
88             return;
89         }
90
91         if(parent != null)
92         {
93             LoggingManager.log("'"+cat.getAttribute("id").getValue()+"' in "+parent.getCategoryID(),this);
94         }
95         else
96         {
97             LoggingManager.log("'"+cat.getAttribute("id").getValue()+"' in root",this);
98         }
99
100         // check if category exists;
101
GResCategory c = (GResCategory)statics.get(cat.getAttribute("id").getValue());
102
103         if(c == null)
104         {
105             // create the category, beacuse not found
106
//LoggingManager.log("Creating category "+
107
// cat.getAttribute("id").getValue()+" owner "+
108
// cat.getAttribute("owner").getValue()
109
// );
110

111             c = new GResCategory();
112             c.setCategoryID(cat.getAttribute("id").getValue());
113             c.setOwner(cat.getAttribute("owner").getValue());
114             c.setStatic(1); // static
115
c.setUsageCount(0);
116             if(parent != null)
117             {
118                 c.setLevel(parent.getLevel()+1);
119                 c.setParent(parent.getRowID());
120             }
121             else
122             {
123                 c.setLevel(0);
124                 c.setParent(0);
125             }
126             gres.saveCategory(c);
127
128             // save category name in different locales
129
List cnames = cat.getChildren("catname");
130             Iterator i = cnames.iterator();
131             while (i.hasNext())
132             {
133                 Element cname = (Element) i.next();
134                 if(cname != null)
135                 {
136                     //doCategory(category, c);
137
String JavaDoc loc = cname.getAttribute("locale").getValue();
138                     String JavaDoc nam = cname.getAttribute("text").getValue();
139                     if((loc == null) || (nam == null))
140                     {
141                         LoggingManager.log("Invalid category name for "+c.getCategoryID());
142                     }
143                     PairOfObjects po = LocaleManager.stripLocaleString(loc);
144                     if(po != null && po.isValid())
145                     {
146                         Locale l = new Locale((String JavaDoc) po.getObjectOne(), (String JavaDoc) po.getObjectTwo());
147                         gres.saveMessage(
148                             gres.getCategoryTableName(),
149                             c.getRowID(),
150                             CategoryConstants.RESCATFIELD_NAME,
151                             l,
152                             nam);
153                     }
154                 }
155             }
156         }
157         else
158         {
159             // category was already defined
160
//LoggingManager.log("reusing category "+
161
// cat.getAttribute("id").getValue()+" owner "+
162
// cat.getAttribute("owner").getValue()
163
// );
164

165             //c = new GResCategory();
166
//c.setCategoryID(cat.getAttribute("id").getValue());
167
c.setOwner(cat.getAttribute("owner").getValue());
168             c.setStatic(1); // static
169
//c.setUsageCount(0);
170
if(parent != null)
171             {
172                 c.setLevel(parent.getLevel()+1);
173                 c.setParent(parent.getRowID());
174             }
175             else
176             {
177                 c.setLevel(0);
178                 c.setParent(0);
179             }
180             //LoggingManager.log("Saving.");
181
gres.saveCategory(c);
182             statics.remove(c.getCategoryID());
183
184             // save category name in different locales
185
// first delete all previous messages
186
gres.deleteMessages(
187                 gres.getCategoryTableName(),
188                 c.getRowID(),
189                 CategoryConstants.RESCATFIELD_NAME);
190             List cnames = cat.getChildren("catname");
191             Iterator i = cnames.iterator();
192             while (i.hasNext())
193             {
194                 Element cname = (Element) i.next();
195                 if(cname != null)
196                 {
197                     //doCategory(category, c);
198
String JavaDoc loc = cname.getAttribute("locale").getValue();
199                     String JavaDoc nam = cname.getAttribute("text").getValue();
200
201                     if((loc == null) || (nam == null))
202                     {
203                         LoggingManager.log("Invalid category name for "+c.getCategoryID());
204                     }
205                     PairOfObjects po = LocaleManager.stripLocaleString(loc);
206                     if(po != null && po.isValid())
207                     {
208                         Locale l = new Locale((String JavaDoc) po.getObjectOne(), (String JavaDoc) po.getObjectTwo());
209                         gres.saveMessage(
210                             gres.getCategoryTableName(),
211                             c.getRowID(),
212                             CategoryConstants.RESCATFIELD_NAME,
213                             l,
214                             nam);
215                     }
216                 }
217             }// end save in different locales
218
}
219
220         List categories = cat.getChildren("category");
221         Iterator i = categories.iterator();
222         while (i.hasNext())
223         {
224             Element category = (Element) i.next();
225             if(category != null)
226             {
227                 doCategory(category, c);
228             }
229         }
230     }
231
232     /**
233      * static categories
234      */

235     Hashtable statics = new Hashtable();
236
237     /**
238      * initializer which is reading the configuration file.
239      *
240      * @param m The virtual module.
241      */

242     public boolean initialize(VModule m)
243     {
244         vmodule = m;
245         VModuleManager vmm = VModuleManager.getInstance();
246         //String file = vmm.getWEBINFDir() + vmodule.getDatamanagerCfgFile();
247
XMLConfigManager cm = XMLConfigManager.getInstance();
248         String JavaDoc file=vmm.getWEBINFDir()+cm.getPropertyByTree("virtualhost/vmodules/vmodule?name="+vmodule.getIdentification()+"/properties/property?name=dataconfig", "value");
249
250         try
251         {
252             // save all static category names and their id
253
// and then remove them from db
254
gres = new GlobalResources();
255             Vector categs = gres.loadStaticCategories();
256             Iterator it = categs.iterator();
257             while(it.hasNext())
258             {
259                 GResCategory cat = (GResCategory)it.next();
260                 if(cat != null) // just to be sure
261
{
262                     statics.put(cat.getCategoryID(), cat);
263                     //gres.deleteCategory(cat);
264
}
265             }
266
267             // Request document building without validation
268
SAXBuilder builder = new SAXBuilder(false);
269             Document doc = builder.build(new File JavaDoc(file));
270
271             // Get the root element (categorylist)
272
Element root = doc.getRootElement();
273
274             List categories = root.getChildren("category");
275             Iterator i = categories.iterator();
276
277             while (i.hasNext())
278             {
279                 Element category = (Element) i.next();
280                 if(category != null)
281                 {
282                     doCategory(category, null);
283                 }
284             }
285
286             // category tree updated. Now remove all not matched categoryes
287
Enumeration en = statics.keys();
288             while(en.hasMoreElements())
289             {
290                 GResCategory _c = (GResCategory)statics.get(en.nextElement());
291                 if(_c != null)
292                 {
293                     gres.deleteCategory(_c);
294                 }
295             }
296         }
297         catch(Exception JavaDoc ex)
298         {
299             LoggingManager.log("Error loading static categories.", this);
300             ex.printStackTrace();
301         }
302         LoggingManager.log("Data configuration file " + file + " successfully loaded", this);
303         return true;
304     }
305
306     /**
307      *
308      */

309     public String JavaDoc getProperty(String JavaDoc property)
310     {
311         return "";
312     }
313
314     /**
315      *
316      */

317     public void setProperty(String JavaDoc property, String JavaDoc value)
318     {
319     }
320
321     /**
322      * preparing the data for a virtual module means, creating SQL tables
323      * or XML files or stuff like that
324      */

325     public boolean prepareData()
326     {
327         LoggingManager.log("Preparing data ", this);
328         return true;
329     }
330
331      public void saveProperties()
332      {
333          LoggingManager.log("Saving properties (not needed)", this);
334      }
335 }
336
337 /* end class XMLCategoryDataManager */
338
Popular Tags