KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.openedit.modules.cart;
2
3 import java.io.IOException JavaDoc;
4 import java.io.StringWriter JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.zip.ZipOutputStream JavaDoc;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.openedit.links.LinkTree;
11
12 import com.openedit.OpenEditException;
13 import com.openedit.WebPageRequest;
14 import com.openedit.modules.store.BaseStoreModule;
15 import com.openedit.store.Category;
16 import com.openedit.store.Product;
17 import com.openedit.store.Store;
18 import com.openedit.store.links.CatalogTreeRenderer;
19 import com.openedit.store.links.CatalogWebTreeModel;
20 import com.openedit.users.User;
21 import com.openedit.util.ZipUtil;
22 import com.openedit.webui.tree.WebTree;
23
24 public class CatalogModule extends BaseStoreModule
25 {
26     
27     private static final Log log = LogFactory.getLog(CatalogModule.class);
28     /**
29      * Installs a {@link WebTree} that shows the catalog tree from a specified
30      * root catalog on down.
31      *
32      * @param inRequest The web page request
33      */

34     public WebTree getCatalogTree( WebPageRequest inRequest ) throws OpenEditException
35     {
36         Store store = getStore( inRequest );
37
38         String JavaDoc treeid = inRequest.getRequestParameter("treeid");
39         String JavaDoc name = findValue("tree-name",inRequest);
40         if( treeid == null)
41         {
42             treeid = name + "_" + store.getCatalogId() + "_" + inRequest.getUserName();
43         }
44         WebTree webTree = (WebTree) inRequest.getSessionValue( treeid );
45
46         if ( webTree == null )
47         {
48             if( name == null)
49             {
50                 return null;
51             }
52             log.info("No Category in Session, creating new " + treeid);
53             String JavaDoc root = findValue("root",inRequest);
54
55             Category main = store.getCatalog( root );
56             if ( main == null)
57             {
58                 throw new OpenEditException("No catalog loaded named " + root);
59             }
60             CatalogWebTreeModel model = new CatalogWebTreeModel( );
61             model.setRootCatalog(main);
62             model.setCatalogArchive(store.getCatalogArchive());
63             model.setUser(inRequest.getUser());
64             webTree = new WebTree(model);
65             webTree.setName(name);
66             webTree.setId(treeid);
67
68             CatalogTreeRenderer renderer = new CatalogTreeRenderer( webTree );
69             renderer.setFoldersLinked( true );
70             String JavaDoc prefix = findValue( "url-prefix", inRequest );
71             prefix = inRequest.getPage().getPageSettings().replaceProperty(prefix);
72             renderer.setUrlPrefix(prefix );
73             String JavaDoc postfix = findValue( "url-postfix", inRequest );
74             renderer.setUrlPostfix(postfix );
75             webTree.setTreeRenderer(renderer);
76             String JavaDoc home = (String JavaDoc) inRequest.getPageValue( "home" );
77             renderer.setHome(home);
78             
79             //expand just the top level
80
/* for (Iterator iter = main.getChildren().iterator(); iter.hasNext();)
81             {
82                 Category child = (Category) iter.next();
83                 renderer.expandNode(child);
84             }
85 */
inRequest.putSessionValue(treeid, webTree);
86             inRequest.putPageValue(webTree.getName(), webTree);
87         }
88         else
89         {
90             inRequest.putPageValue(webTree.getName(), webTree);
91         }
92         return webTree;
93     }
94
95     public void exportAllProducts(WebPageRequest inReq) throws OpenEditException
96     {
97         Store store = getStore(inReq);
98         StringWriter JavaDoc products = new StringWriter JavaDoc();
99         store.getProductExport().exportAllProducts(store, products);
100         
101         ZipOutputStream JavaDoc finalZip = new ZipOutputStream JavaDoc(inReq.getOutputStream());
102         
103         try
104         {
105             new ZipUtil().addTozip(products.toString(),"products.xml" ,finalZip);
106             inReq.getOutputStream().flush();
107             StringWriter JavaDoc catalogs = new StringWriter JavaDoc();
108             store.getProductExport().exportCatalogsWithProducts(store, catalogs);
109             new ZipUtil().addTozip(catalogs.toString(),"categories.xml" ,finalZip);
110             finalZip.close();
111             inReq.setHasRedirected(true);
112         }
113         catch ( IOException JavaDoc ex )
114         {
115             throw new OpenEditException(ex);
116         }
117     }
118     public void loadCrumbs(WebPageRequest inReq) throws Exception JavaDoc
119     {
120         Category category = (Category)inReq.getPageValue("category");
121         if( category == null)
122         {
123             Product prod = (Product)inReq.getPageValue("product");
124             if( prod != null)
125             {
126                 category = prod.getDefaultCatalog();
127             }
128         }
129         if( category != null)
130         {
131             String JavaDoc name = findValue("tree-name", inReq);
132             if( name != null)
133             {
134                 Store store = getStore(inReq);
135                 LinkTree tree = (LinkTree)inReq.getPageValue(name);
136                 if( tree != null)
137                 {
138                     tree.clearCrumbs();
139                     for (Iterator JavaDoc iterator = category.listAncestorsAndSelf( 1 ).iterator(); iterator.hasNext();)
140                     {
141                         Category parent = (Category) iterator.next();
142                         tree.addCrumb( store.getStoreHome() + "/categories/" + parent.getId() + ".html",parent.getName());
143                     }
144                 }
145             }
146         }
147         
148     }
149 }
Popular Tags