KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > cart > XmlCatalogArchive


1     /*
2  * Created on Oct 3, 2004
3  */

4 package com.openedit.modules.cart;
5
6 import java.io.File JavaDoc;
7 import java.io.FileWriter JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Map JavaDoc;
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 /**
35  * @author cburkey
36  *
37  */

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 JavaDoc fieldCatalogMap;
43     protected Category fieldRootCatalog;
44     protected PageManager fieldPageManager;
45     protected List JavaDoc fieldImageList;
46     protected PropertyDetails fieldPropertyDetails;
47
48     public List JavaDoc listAllCatalogs() throws StoreException
49     {
50         List JavaDoc all = new ArrayList JavaDoc();
51         addCatalogs(all,getRootCatalog());
52         return all;
53     }
54
55     /**
56      * @param inAll
57      * @param inRootCatalog
58      */

59     private void addCatalogs(List JavaDoc inAll, Category inRootCatalog) {
60         // TODO Auto-generated method stub
61
inAll.add(inRootCatalog);
62         for (Iterator JavaDoc iter = inRootCatalog.getChildren().iterator(); iter.hasNext();) {
63             Category child = (Category) iter.next();
64             addCatalogs(inAll,child);
65         }
66     }
67
68     public Map JavaDoc getCatalogMap() throws StoreException
69     {
70         if (fieldCatalogMap == null)
71         {
72             fieldCatalogMap = new HashMap JavaDoc();
73         }
74         return fieldCatalogMap;
75     }
76
77     /* (non-javadoc)
78      * @see com.openedit.store.CatalogReader#getCatalog(java.lang.String)
79      */

80     public Category getCatalog(String JavaDoc inCategory)
81     {
82         try
83         {
84             getRootCatalog();
85             return (Category) getCatalogMap().get(inCategory);
86         }
87         catch ( Exception JavaDoc ex)
88         {
89             throw new OpenEditRuntimeException(ex);
90         }
91     }
92     
93     public Category getCatalogByName(String JavaDoc inCategoryName) throws StoreException
94     {
95         List JavaDoc catalogs = listAllCatalogs();
96         for (Iterator JavaDoc 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     /* (non-javadoc)
108      * @see com.openedit.store.CatalogReader#getRootCatalog()
109      */

110     public Category getRootCatalog() throws StoreException
111     {
112         if ( fieldRootCatalog == null)
113         {
114             reloadCatalogs();
115         }
116         return fieldRootCatalog;
117     }
118
119     /**
120      * @param inCategoryId
121      * @param inCategorydesc
122      * @return
123      */

124     public Category addCatalog(String JavaDoc inCategoryId, String JavaDoc 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 JavaDoc 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()) //we dont want an infinite loop so we check to see if it was in already
167
{
168             for (Iterator JavaDoc iter = inCategory.getChildren().iterator(); iter.hasNext();)
169             {
170                 Category child = (Category) iter.next();
171                 //log.info("adding " + child);
172
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 JavaDoc iter = inCategory.getChildren().iterator(); iter.hasNext();)
198         {
199             Category child = (Category) iter.next();
200             child.setParentCatalog(null); // to prevent ConcurrentModificationException
201
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 JavaDoc listCatalogXml()
219     {
220         File JavaDoc catalogFile = new File JavaDoc(getStoreDirectory(), "categories/categories.xml");
221         return catalogFile;
222     }
223
224     /* (non-javadoc)
225      * @see com.openedit.store.CatalogArchive#saveCatalogs()
226      */

227     public void saveCatalogs() throws StoreException
228     {
229         try
230         {
231             File JavaDoc catalogFile = listCatalogXml();
232             catalogFile.getParentFile().mkdirs();
233
234             Element root = createElement(getRootCatalog());
235
236             // lets write to a file
237
new XmlUtil().saveXml(root, new FileWriter JavaDoc(catalogFile), "UTF-8");
238
239             //check on descriptions
240
/* for (Iterator iter = listAllCatalogs().iterator(); iter.hasNext();)
241             {
242                 Category cat = (Category) iter.next();
243                 saveLongDescription(cat);
244             }
245 */

246         }
247         catch (Exception JavaDoc e)
248         {
249             throw new StoreException(e);
250         }
251
252     }
253
254     /**
255      * @param inRootCatalog
256      * @return
257      */

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         //add any other attributes we might have
272
for (Iterator JavaDoc iter = inRootCatalog.getProperties().keySet().iterator(); iter.hasNext();)
273         {
274             String JavaDoc id = (String JavaDoc) 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 JavaDoc 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 JavaDoc 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 JavaDoc getStoreDirectory()
319     {
320         return getStore().getStoreDirectory();
321     }
322     public void setStore( Store inStore)
323     {
324         fieldStore = inStore;
325     }
326     /**
327      *
328      *
329      */

330     public void reloadCatalogs() throws StoreException
331     {
332         getCatalogMap().clear();
333
334         File JavaDoc 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 JavaDoc 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     /**
363      * @param inRootElement
364      * @return
365      */

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 JavaDoc iter = inRootConfig.getAttributeNames().iterator(); iter.hasNext();)
372         {
373             String JavaDoc attrName = (String JavaDoc)iter.next();
374             if (!attrName.equals("id") && !attrName.equals("name"))
375             {
376                 cat.setProperty(attrName, inRootConfig.getAttribute(attrName));
377             }
378         }
379
380         String JavaDoc shortdesc = inRootConfig.getChildValue("shortdescription");
381         cat.setShortDescription(shortdesc);
382
383         loadOptions(cat, inRootConfig);
384         
385         for (Iterator JavaDoc 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 JavaDoc iter = inConfig.getChildren("option").iterator(); iter.hasNext();)
397         {
398             Configuration optionConfig = (Configuration) iter.next();
399             inCategory.addOption(createOption(optionConfig));
400         }
401     }
402     
403     
404     /* (non-javadoc)
405      * @see com.openedit.store.CatalogArchive#listNonUserSelectedCatalogs()
406      */

407     public List JavaDoc listNonUserSelectedCatalogs() throws StoreException
408     {
409         List JavaDoc sublist = new ArrayList JavaDoc();
410         for (Iterator JavaDoc 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 JavaDoc listUserSelectedCatalogs() throws StoreException
422     {
423         List JavaDoc sublist = new ArrayList JavaDoc();
424         for (Iterator JavaDoc 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     /* (non-Javadoc)
446      * @deprecated
447      * @see com.openedit.store.CatalogArchive#saveCatalog(com.openedit.store.Category)
448      */

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     /**
463      * Returns a list of potential (not actual) images, valid for any catalog.
464      *
465      * @return A {@link List} of {@link Image}s
466      */

467     public List JavaDoc getImageList() throws StoreException
468     {
469         if (fieldImageList == null)
470         {
471             
472             List JavaDoc arrayList = new ArrayList JavaDoc();
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 JavaDoc iter = root.elementIterator("image"); iter.hasNext();)
479                 {
480                     Element type = (Element) iter.next();
481                     String JavaDoc name = type.attributeValue("name");
482                     String JavaDoc id = type.attributeValue("id");
483                     
484                     String JavaDoc postfix = type.attributeValue("postfix");
485                     int width = Integer.parseInt( type.attributeValue("width") );
486                     String JavaDoc 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 JavaDoc ex)
494             {
495                 throw new StoreException(ex);
496             }
497             fieldImageList = arrayList;
498         }
499         return fieldImageList;
500     }
501
502     public List JavaDoc getImageList(String JavaDoc inType) throws StoreException
503     {
504         List JavaDoc r = new ArrayList JavaDoc();
505         List JavaDoc items = getImageList();
506         for (Iterator JavaDoc 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             //load up another xml file
523
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 JavaDoc ex)
531             {
532                 throw new StoreException(ex);
533             }
534         }
535         return fieldPropertyDetails;
536     }
537
538     
539 }
540
Popular Tags