1 5 6 package com.raptus.owxv3; 7 8 import java.io.File ; 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 38 39 62 63 public class XMLCategoryDataManager extends Object implements DataManager 64 { 65 68 protected VModule vmodule = null; 69 70 73 GlobalResources gres = null; 74 75 76 82 protected void doCategory(Element cat, GResCategory parent) 83 throws java.sql.SQLException 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 GResCategory c = (GResCategory)statics.get(cat.getAttribute("id").getValue()); 102 103 if(c == null) 104 { 105 111 c = new GResCategory(); 112 c.setCategoryID(cat.getAttribute("id").getValue()); 113 c.setOwner(cat.getAttribute("owner").getValue()); 114 c.setStatic(1); 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 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 String loc = cname.getAttribute("locale").getValue(); 138 String 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 ) po.getObjectOne(), (String ) 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 165 c.setOwner(cat.getAttribute("owner").getValue()); 168 c.setStatic(1); 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 gres.saveCategory(c); 182 statics.remove(c.getCategoryID()); 183 184 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 String loc = cname.getAttribute("locale").getValue(); 199 String 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 ) po.getObjectOne(), (String ) po.getObjectTwo()); 209 gres.saveMessage( 210 gres.getCategoryTableName(), 211 c.getRowID(), 212 CategoryConstants.RESCATFIELD_NAME, 213 l, 214 nam); 215 } 216 } 217 } } 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 235 Hashtable statics = new Hashtable(); 236 237 242 public boolean initialize(VModule m) 243 { 244 vmodule = m; 245 VModuleManager vmm = VModuleManager.getInstance(); 246 XMLConfigManager cm = XMLConfigManager.getInstance(); 248 String file=vmm.getWEBINFDir()+cm.getPropertyByTree("virtualhost/vmodules/vmodule?name="+vmodule.getIdentification()+"/properties/property?name=dataconfig", "value"); 249 250 try 251 { 252 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) { 262 statics.put(cat.getCategoryID(), cat); 263 } 265 } 266 267 SAXBuilder builder = new SAXBuilder(false); 269 Document doc = builder.build(new File (file)); 270 271 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 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 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 309 public String getProperty(String property) 310 { 311 return ""; 312 } 313 314 317 public void setProperty(String property, String value) 318 { 319 } 320 321 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 338 | Popular Tags |