1 4 package com.openedit.modules.cart; 5 6 import java.io.File ; 7 import java.io.FileWriter ; 8 import java.util.ArrayList ; 9 import java.util.HashMap ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 import java.util.Map ; 13 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 import org.dom4j.DocumentHelper; 17 import org.dom4j.Element; 18 import org.openedit.repository.filesystem.StringItem; 19 20 import com.openedit.OpenEditException; 21 import com.openedit.OpenEditRuntimeException; 22 import com.openedit.config.Configuration; 23 import com.openedit.config.XMLConfiguration; 24 import com.openedit.page.Page; 25 import com.openedit.page.manage.PageManager; 26 import com.openedit.store.CatalogArchive; 27 import com.openedit.store.Category; 28 import com.openedit.store.Image; 29 import com.openedit.store.Store; 30 import com.openedit.store.StoreException; 31 import com.openedit.store.products.PropertyDetails; 32 import com.openedit.util.XmlUtil; 33 34 38 public class XmlCatalogArchive extends BaseXmlArchive implements CatalogArchive 39 { 40 private static final Log log = LogFactory.getLog(XmlCatalogArchive.class); 41 protected Store fieldStore; 42 protected Map fieldCatalogMap; 43 protected Category fieldRootCatalog; 44 protected PageManager fieldPageManager; 45 protected List fieldImageList; 46 protected PropertyDetails fieldPropertyDetails; 47 48 public List listAllCatalogs() throws StoreException 49 { 50 List all = new ArrayList (); 51 addCatalogs(all,getRootCatalog()); 52 return all; 53 } 54 55 59 private void addCatalogs(List inAll, Category inRootCatalog) { 60 inAll.add(inRootCatalog); 62 for (Iterator iter = inRootCatalog.getChildren().iterator(); iter.hasNext();) { 63 Category child = (Category) iter.next(); 64 addCatalogs(inAll,child); 65 } 66 } 67 68 public Map getCatalogMap() throws StoreException 69 { 70 if (fieldCatalogMap == null) 71 { 72 fieldCatalogMap = new HashMap (); 73 } 74 return fieldCatalogMap; 75 } 76 77 80 public Category getCatalog(String inCategory) 81 { 82 try 83 { 84 getRootCatalog(); 85 return (Category) getCatalogMap().get(inCategory); 86 } 87 catch ( Exception ex) 88 { 89 throw new OpenEditRuntimeException(ex); 90 } 91 } 92 93 public Category getCatalogByName(String inCategoryName) throws StoreException 94 { 95 List catalogs = listAllCatalogs(); 96 for (Iterator iter = catalogs.iterator(); iter.hasNext();) 97 { 98 Category catalog = (Category) iter.next(); 99 if (catalog.getName().equals(inCategoryName)) 100 { 101 return catalog; 102 } 103 } 104 return null; 105 } 106 107 110 public Category getRootCatalog() throws StoreException 111 { 112 if ( fieldRootCatalog == null) 113 { 114 reloadCatalogs(); 115 } 116 return fieldRootCatalog; 117 } 118 119 124 public Category addCatalog(String inCategoryId, String inCategorydesc) throws StoreException 125 { 126 Category oldCatalog = getCatalog(inCategoryId); 127 if (oldCatalog == null) 128 { 129 oldCatalog = new Category(); 130 oldCatalog.setId(inCategoryId); 131 oldCatalog.setName(inCategorydesc); 132 addCatalog(oldCatalog); 133 getRootCatalog().addChild(oldCatalog); 134 try 135 { 136 Page des = getPageManager().getPage("/" + getStore().getCatalogId() + "/categories/" + inCategoryId + ".html"); 137 if ( !des.exists() ) 138 { 139 StringItem item = new StringItem(des.getPath()," ",des.getCharacterEncoding()); 140 item.setMakeVersion(false); 141 des.setContentItem(item); 142 getPageManager().putPage(des); 143 } 144 } 145 catch ( Exception ex) 146 { 147 throw new StoreException(ex); 148 } 149 } 150 else 151 { 152 oldCatalog.setName(inCategorydesc); 153 } 154 return oldCatalog; 155 } 156 157 public void addCatalog(Category inCategory) throws StoreException 158 { 159 if( inCategory == null) 160 { 161 return; 162 } 163 boolean wasInAlready = getCatalogMap().containsKey(inCategory.getId() ); 164 165 getCatalogMap().put(inCategory.getId(), inCategory); 166 if (!wasInAlready && inCategory.hasChildren()) { 168 for (Iterator iter = inCategory.getChildren().iterator(); iter.hasNext();) 169 { 170 Category child = (Category) iter.next(); 171 addCatalog(child); 173 } 174 } 175 } 176 177 public void deleteCatalog(Category inCategory) throws StoreException 178 { 179 getCatalogMap().remove(inCategory.getId()); 180 if (getRootCatalog().getId().equals( inCategory.getId() ) ) 181 { 182 setRootCatalog(new Category("index","Index")); 183 } 184 else 185 { 186 deleteAll(inCategory); 187 if (inCategory.getParentCatalog() != null) 188 { 189 inCategory.getParentCatalog().removeChild(inCategory); 190 } 191 } 192 saveCatalogs(); 193 } 194 195 protected void deleteAll(Category inCategory) throws StoreException 196 { 197 for (Iterator iter = inCategory.getChildren().iterator(); iter.hasNext();) 198 { 199 Category child = (Category) iter.next(); 200 child.setParentCatalog(null); deleteAll(child); 202 } 203 } 204 205 public void clearCatalogs() 206 { 207 fieldCatalogMap = null; 208 fieldRootCatalog = null; 209 } 210 211 public void setRootCatalog(Category inRootCatalog) throws StoreException 212 { 213 fieldRootCatalog = inRootCatalog; 214 addCatalog(fieldRootCatalog); 215 216 } 217 218 protected File listCatalogXml() 219 { 220 File catalogFile = new File (getStoreDirectory(), "categories/categories.xml"); 221 return catalogFile; 222 } 223 224 227 public void saveCatalogs() throws StoreException 228 { 229 try 230 { 231 File catalogFile = listCatalogXml(); 232 catalogFile.getParentFile().mkdirs(); 233 234 Element root = createElement(getRootCatalog()); 235 236 new XmlUtil().saveXml(root, new FileWriter (catalogFile), "UTF-8"); 238 239 246 } 247 catch (Exception e) 248 { 249 throw new StoreException(e); 250 } 251 252 } 253 254 258 protected Element createElement(Category inRootCatalog) throws OpenEditException 259 { 260 Element child = DocumentHelper.createElement("catalog"); 261 child.addAttribute("id", inRootCatalog.getId()); 262 child.addAttribute("name", inRootCatalog.getName()); 263 if (inRootCatalog.getShortDescription() != null) 264 { 265 child.addElement("shortdescription").setText(inRootCatalog.getShortDescription()); 266 } 267 if (inRootCatalog.getDescription() != null) 268 { 269 saveLongDescription( inRootCatalog ); 270 } 271 for (Iterator iter = inRootCatalog.getProperties().keySet().iterator(); iter.hasNext();) 273 { 274 String id = (String ) iter.next(); 275 if ( id != null && !"id".equals(id) && !"name".equals(id) ) 276 { 277 child.addAttribute(id, inRootCatalog.getProperty(id)); 278 } 279 } 280 saveOptions(inRootCatalog.getOptions(), child); 281 282 for (Iterator iter = inRootCatalog.getChildren().iterator(); iter.hasNext();) 283 { 284 Category subcatalog = (Category) iter.next(); 285 Element newchild = createElement(subcatalog); 286 child.add(newchild); 287 } 288 289 return child; 290 } 291 292 protected void saveLongDescription( Category inCategory ) throws StoreException 293 { 294 try 295 { 296 Page fulldesc = getPageManager().getPage("/" + getStore().getCatalogId() + "/categories/" + inCategory.getId() + ".html"); 297 if ( !fulldesc.exists() ) 298 { 299 String desc = inCategory.getDescription(); 300 if( desc == null || desc.trim().length() == 0 ) 301 { 302 return; 303 } 304 StringItem item = new StringItem(fulldesc.getPath(),desc,"UTF-8"); 305 fulldesc.setContentItem(item); 306 getPageManager().putPage(fulldesc); 307 } 308 } 309 catch ( OpenEditException oee ) 310 { 311 throw new StoreException( oee ); 312 } 313 } 314 protected Store getStore() 315 { 316 return fieldStore; 317 } 318 protected File getStoreDirectory() 319 { 320 return getStore().getStoreDirectory(); 321 } 322 public void setStore( Store inStore) 323 { 324 fieldStore = inStore; 325 } 326 330 public void reloadCatalogs() throws StoreException 331 { 332 getCatalogMap().clear(); 333 334 File catalogFile = listCatalogXml(); 335 if (catalogFile.exists()) 336 { 337 try 338 { 339 Element rootE = new XmlUtil().getXml(catalogFile,"UTF-8"); 340 XMLConfiguration rootConfig = new XMLConfiguration(); 341 rootConfig.populate(rootE); 342 343 Category root = createCatalog(rootConfig); 344 setRootCatalog(root); 345 } 346 catch (Exception ex) 347 { 348 ex.printStackTrace(); 349 throw new StoreException(ex); 350 } 351 } 352 else 353 { 354 log.error("No catalog file found " + catalogFile.getAbsolutePath()); 355 Category root = new Category(); 356 root.setId("index"); 357 root.setName("Index"); 358 setRootCatalog(root); 359 } 360 } 361 362 366 protected Category createCatalog(Configuration inRootConfig) throws OpenEditException 367 { 368 Category cat = new Category(); 369 cat.setId(inRootConfig.getAttribute("id")); 370 cat.setName(inRootConfig.getAttribute("name")); 371 for (Iterator iter = inRootConfig.getAttributeNames().iterator(); iter.hasNext();) 372 { 373 String attrName = (String )iter.next(); 374 if (!attrName.equals("id") && !attrName.equals("name")) 375 { 376 cat.setProperty(attrName, inRootConfig.getAttribute(attrName)); 377 } 378 } 379 380 String shortdesc = inRootConfig.getChildValue("shortdescription"); 381 cat.setShortDescription(shortdesc); 382 383 loadOptions(cat, inRootConfig); 384 385 for (Iterator iter = inRootConfig.getChildren("catalog").iterator(); iter.hasNext();) 386 { 387 Configuration config = (Configuration) iter.next(); 388 cat.addChild(createCatalog(config)); 389 } 390 return cat; 391 } 392 393 protected void loadOptions(Category inCategory, Configuration inConfig) 394 { 395 inCategory.clearOptions(); 396 for (Iterator iter = inConfig.getChildren("option").iterator(); iter.hasNext();) 397 { 398 Configuration optionConfig = (Configuration) iter.next(); 399 inCategory.addOption(createOption(optionConfig)); 400 } 401 } 402 403 404 407 public List listNonUserSelectedCatalogs() throws StoreException 408 { 409 List sublist = new ArrayList (); 410 for (Iterator iter = listAllCatalogs().iterator(); iter.hasNext();) 411 { 412 Category cat = (Category) iter.next(); 413 if (!cat.isUserSelected()) 414 { 415 sublist.add(cat); 416 } 417 } 418 return sublist; 419 } 420 421 public List listUserSelectedCatalogs() throws StoreException 422 { 423 List sublist = new ArrayList (); 424 for (Iterator iter = listAllCatalogs().iterator(); iter.hasNext();) 425 { 426 Category cat = (Category) iter.next(); 427 if (cat.isUserSelected()) 428 { 429 sublist.add(cat); 430 } 431 } 432 return sublist; 433 } 434 435 public PageManager getPageManager() 436 { 437 return fieldPageManager; 438 } 439 440 public void setPageManager(PageManager inPageManager) 441 { 442 fieldPageManager = inPageManager; 443 } 444 445 449 public void saveCatalog(Category inCategory) throws StoreException { 450 if ( inCategory.getParentCatalog() == null ) 451 { 452 addCatalog(inCategory); 453 } 454 else 455 { 456 getCatalogMap().put(inCategory.getId(), inCategory); 457 } 458 saveCatalogs(); 459 } 460 461 462 467 public List getImageList() throws StoreException 468 { 469 if (fieldImageList == null) 470 { 471 472 List arrayList = new ArrayList (); 473 try 474 { 475 Page config = getPageManager().getPage( "/" + getStore().getCatalogId() + "/configuration/imagelist.xml"); 476 477 Element root = new XmlUtil().getXml(config.getReader(), "UTF-8"); 478 for (Iterator iter = root.elementIterator("image"); iter.hasNext();) 479 { 480 Element type = (Element) iter.next(); 481 String name = type.attributeValue("name"); 482 String id = type.attributeValue("id"); 483 484 String postfix = type.attributeValue("postfix"); 485 int width = Integer.parseInt( type.attributeValue("width") ); 486 String sizetype = type.attributeValue("type"); 487 Image thumb = new Image(name, width, postfix); 488 thumb.setId(id); 489 thumb.setType(sizetype); 490 arrayList.add(thumb); 491 } 492 } 493 catch ( Exception ex) 494 { 495 throw new StoreException(ex); 496 } 497 fieldImageList = arrayList; 498 } 499 return fieldImageList; 500 } 501 502 public List getImageList(String inType) throws StoreException 503 { 504 List r = new ArrayList (); 505 List items = getImageList(); 506 for (Iterator iter = items.iterator(); iter.hasNext();) 507 { 508 Image image = (Image) iter.next(); 509 if ( image.getType().equals(inType)) 510 { 511 r.add(image); 512 } 513 } 514 return r; 515 } 516 517 public PropertyDetails getCatalogDetails() throws StoreException 518 { 519 if ( fieldPropertyDetails == null) 520 { 521 fieldPropertyDetails = new PropertyDetails(); 522 try 524 { 525 Page page = getPageManager().getPage("/" + getStore().getCatalogId() + "/configuration/catalogproperties.xml"); 526 if ( page.exists() ) 527 { 528 fieldPropertyDetails.addAllDetails(page.getReader()); 529 } 530 } catch ( Exception ex) 531 { 532 throw new StoreException(ex); 533 } 534 } 535 return fieldPropertyDetails; 536 } 537 538 539 } 540 | Popular Tags |