1 4 package com.openedit.store.products; 5 6 import java.io.File ; 7 import java.io.Writer ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 11 import org.apache.lucene.document.Document; 12 import org.dom4j.DocumentHelper; 13 import org.dom4j.Element; 14 15 import com.openedit.OpenEditException; 16 import com.openedit.modules.cart.XmlProductArchive; 17 import com.openedit.modules.search.LuceneHitTracker; 18 import com.openedit.store.Category; 19 import com.openedit.store.Image; 20 import com.openedit.store.Product; 21 import com.openedit.store.ProductExport; 22 import com.openedit.store.Store; 23 import com.openedit.util.XmlUtil; 24 25 public class XmlProductExport implements ProductExport 26 { 27 public void exportAllProducts(Store inStore, Writer inOut) throws OpenEditException 28 { 29 32 XmlProductArchive archive = new XmlProductArchive(); 34 Element root = DocumentHelper.createDocument().addElement("products"); 35 36 List ids = inStore.getProductArchive().listAllProductIds(); 37 for (Iterator iterator = ids.iterator(); iterator.hasNext();) 38 { 39 String id = (String ) iterator.next(); 40 Product product = inStore.getProductArchive().getProduct(id); 41 42 archive.saveProduct(product, root); 43 List elements = root.elements(); 44 Element lastOne = (Element)elements.get(elements.size()-1); 45 lastOne.addAttribute("path", "/store/products/" + inStore.getProductArchive().getProductPathFinder().idToPath(id) + ".html"); 46 47 for (Iterator iterator2 = inStore.getCatalogArchive().getImageList().iterator(); iterator2.hasNext();) { 49 Image image = (Image) iterator2.next(); 50 String url = "/products/images/" + image.getType() + "/" + id + image.getPostfix() +".jpg"; 51 52 File file = new File (inStore.getProductArchive().getStore().getStoreDirectory(),url); 53 if( file.exists() ) 54 { 55 Element imagee = lastOne.addElement("image"); 56 imagee.addAttribute("id", id +"-" + image.getId()); 57 imagee.addAttribute("path", "/store" + url); 58 } 59 } 60 61 Element desc = lastOne.addElement("description"); 62 String description = inStore.getProductArchive().loadDescription(product); 63 if ( description != null) 64 { 65 desc.setText(description); 66 } 67 } 68 new XmlUtil().saveXml(root, inOut, "UTF-8"); 69 } 70 71 public void exportCatalogsWithProducts(Store inStore, Writer inOut) throws OpenEditException 72 { 73 Element root = DocumentHelper.createDocument().addElement("catalogtree"); 74 addCatalog( inStore, inStore.getCatalogArchive().getRootCatalog(), root ); 75 new XmlUtil().saveXml(root, inOut, "UTF-8"); 76 } 77 78 protected void addCatalog(Store inStore, Category inCatalog, Element inRoot) throws OpenEditException 79 { 80 Element cat = inRoot.addElement("catalog"); 81 cat.addAttribute("name", inCatalog.getName()); 82 cat.addAttribute("id", inCatalog.getId()); 83 cat.addAttribute("path", "/store/catalogs/" + inCatalog.getId() + ".html"); 84 85 for (Iterator iterator = inStore.getCatalogArchive().getImageList().iterator(); iterator.hasNext();) 86 { 87 Image image = (Image) iterator.next(); 88 String url = "/catalogs/images/" + image.getType() + "/" + inCatalog.getId() + image.getPostfix() +".jpg"; 89 90 File file = new File (inStore.getProductArchive().getStore().getStoreDirectory(),url); 91 if( file.exists() ) 92 { 93 Element imagee = cat.addElement("image"); 94 imagee.addAttribute("id", inCatalog.getId() +"-" + image.getId()); 95 imagee.addAttribute("path", "/store" + url); 96 } 97 } 98 String query = "catalogs:(" + inCatalog.getId() + ")"; 99 LuceneHitTracker hits = inStore.getStoreSearcher().search( query , null); 100 if ( hits == null) 101 { 102 return; 103 } 104 for (Iterator it = hits.getAllHits(); it.hasNext();) 105 { 106 Document doc = (Document) it.next(); 107 String id = doc.get("id"); 108 Element child = cat.addElement("product"); 109 child.addAttribute("id", id); 110 } 111 for (Iterator iterator = inCatalog.getChildren().iterator(); iterator.hasNext();) 112 { 113 Category child = (Category) iterator.next(); 114 addCatalog(inStore, child, cat); 115 } 116 } 117 118 } 119 | Popular Tags |