KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > store > products > XmlProductExport


1 /*
2  * Created on Aug 24, 2006
3  */

4 package com.openedit.store.products;
5
6 import java.io.File JavaDoc;
7 import java.io.Writer JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.List JavaDoc;
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 JavaDoc inOut) throws OpenEditException
28     {
29         //loop over all the products.
30
//make one monster XML file with all the products
31

32         //make one monster XML file with all the catalogs
33
XmlProductArchive archive = new XmlProductArchive();
34         Element root = DocumentHelper.createDocument().addElement("products");
35         
36         List JavaDoc ids = inStore.getProductArchive().listAllProductIds();
37         for (Iterator JavaDoc iterator = ids.iterator(); iterator.hasNext();)
38         {
39             String JavaDoc id = (String JavaDoc) iterator.next();
40             Product product = inStore.getProductArchive().getProduct(id);
41
42             archive.saveProduct(product, root);
43             List JavaDoc 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 JavaDoc iterator2 = inStore.getCatalogArchive().getImageList().iterator(); iterator2.hasNext();) //TODO: Use ProductArchive
48
{
49                 Image image = (Image) iterator2.next();
50                 String JavaDoc url = "/products/images/" + image.getType() + "/" + id + image.getPostfix() +".jpg";
51                 
52                 File JavaDoc file = new File JavaDoc(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 JavaDoc 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 JavaDoc 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 JavaDoc iterator = inStore.getCatalogArchive().getImageList().iterator(); iterator.hasNext();)
86         {
87             Image image = (Image) iterator.next();
88             String JavaDoc url = "/catalogs/images/" + image.getType() + "/" + inCatalog.getId() + image.getPostfix() +".jpg";
89             
90             File JavaDoc file = new File JavaDoc(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 JavaDoc query = "catalogs:(" + inCatalog.getId() + ")";
99         LuceneHitTracker hits = inStore.getStoreSearcher().search( query , null);
100         if ( hits == null)
101         {
102             return;
103         }
104         for (Iterator JavaDoc it = hits.getAllHits(); it.hasNext();)
105         {
106             Document doc = (Document) it.next();
107             String JavaDoc id = doc.get("id");
108             Element child = cat.addElement("product");
109             child.addAttribute("id", id);
110         }
111         for (Iterator JavaDoc 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