KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > golfShop > presentation > xmlc > main > Shelf


1 /*
2  * Enhydra Java Application Server
3  * The Initial Developer of the Original Code is Lutris Technologies Inc.
4  * Portions created by Lutris are Copyright (C) 1997-2000 Lutris Technologies
5  * Inc.
6  * All Rights Reserved.
7  *
8  * The contents of this file are subject to the Enhydra Public License Version
9  * 1.0 (the "License"); you may not use this file except in compliance with the
10  * License. You may obtain a copy of the License at
11  * http://www.enhydra.org/software/license/epl.html
12  *
13  * Software distributed under the License is distributed on an "AS IS" basis,
14  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
15  * License for the specific language governing rights and limitations under the
16  * License.
17  *
18  *
19  */

20
21 package golfShop.presentation.xmlc.main;
22
23 import org.enhydra.xml.xmlc.*;
24 import org.enhydra.xml.xmlc.html.*;
25 import com.lutris.appserver.server.httpPresentation.*;
26 import java.io.*;
27 import java.net.URLEncoder JavaDoc;
28 import java.util.*;
29 import org.w3c.dom.*;
30 import org.w3c.dom.html.*;
31 import golfShop.presentation.xmlc.utilities.*;
32 import golfShop.spec.inventory.*;
33
34 /**
35  * This presentation object dynamically creates the hierarchical shelf
36  * (directory of categories and items in the store). With categories either
37  * open or closed. This is a fairly ugly thing to do in HTML.
38  *
39  * Parameters:
40  * <UL>
41  * <LI> button - If a open/close button was pressed, this is the category
42  * id associated with that button.
43  * <LI> open - A comma-seperated list of the catagory ids of the categories
44  * that are open.
45  * </UL>
46  */

47 public class Shelf implements HttpPresentation {
48     /**
49      * URL to reference ourselve.
50      */

51     private static final String JavaDoc SHELF_URL = "Shelf.po";
52
53     /**
54      * URL to reference the item presentation object.
55      */

56     private static final String JavaDoc ITEM_URL = "Item.po";
57
58     /**
59      * Image to use for open categories.
60      */

61     private static final String JavaDoc OPEN_CAT_IMAGE = "../media/true.gif";
62     private static final String JavaDoc CLOSED_CAT_IMAGE = "../media/false.gif";
63
64     /**
65      * The catagory id of the button that was pressed or NULL if
66      * one was not pressed.
67      */

68     private Long JavaDoc buttonCatId;
69
70     /**
71      * Table of category ids that are open. Each entry contains a Long
72      * object.
73      */

74     private Hashtable openCatIds = new Hashtable();
75
76     /**
77      * CGI argument containing all open category ids. If null,
78      * then none are open.
79      */

80     private String JavaDoc openCatCgiArg;
81     
82     /**
83      * Shelf HTML object.
84      */

85     private ShelfHTML shelfHtml;
86     
87     /**
88      * Pointers to category prototype entries.
89      */

90     HTMLParagraphElement sampleCatBlock;
91     HTMLAnchorElement sampleCatAnchor;
92     HTMLImageElement sampleCatImg;
93     Text sampleCatText;
94
95     /**
96      * Pointers to item prototype entries.
97      */

98     HTMLParagraphElement sampleItemBlock;
99     HTMLAnchorElement sampleItemAnchor;
100     Text sampleItemText;
101     
102     /**
103      * Remove prototype table and save pointers to the prototype
104      * entries. For easy of use, prototypes are edited and then
105      * cloned.
106      */

107     private void prepareHtml(HttpPresentationComms comms) {
108        
109         shelfHtml = (ShelfHTML)comms.xmlcFactory.create(ShelfHTML.class);
110        
111         // Save pointers to prototypes, remove ids as they shouldn't be duplicated.
112
sampleCatBlock = shelfHtml.getElementSampleCatBlock();
113         sampleCatAnchor = shelfHtml.getElementSampleCatAnchor();
114         sampleCatImg = shelfHtml.getElementSampleCatImg();
115         sampleCatText = XMLCUtil.getFirstText(shelfHtml.getElementSampleCatText());
116
117         sampleItemBlock = shelfHtml.getElementSampleItemBlock();
118         sampleItemAnchor = shelfHtml.getElementSampleItemAnchor();
119         sampleItemText = XMLCUtil.getFirstText(shelfHtml.getElementSampleItemText());
120
121         // Remove children of prototype list.
122
Node listNode = shelfHtml.getElementList();
123         Node child = listNode.getFirstChild();
124         while (child != null) {
125             Node nextChild = child.getNextSibling();
126             listNode.removeChild(child);
127             child = nextChild;
128         }
129         
130     }
131
132     /**
133      * Create a new HTML entry for a category.
134      */

135     private void makeCategoryHtml(Inventory category, Node parent,
136                                   boolean isOpen, int level) {
137         long id = category.getObjectId();
138
139         String JavaDoc url = SHELF_URL + "?button=" + id;
140         if (openCatCgiArg != null) {
141             url += "&" + openCatCgiArg;
142         }
143         sampleCatAnchor.setHref(url);
144         if (isOpen) {
145             sampleCatImg.setSrc(OPEN_CAT_IMAGE);
146         } else {
147             sampleCatImg.setSrc(CLOSED_CAT_IMAGE);
148         }
149         sampleCatText.setData(category.getDesc());
150         sampleCatBlock.setClassName("level" + level);
151         parent.appendChild(sampleCatBlock.cloneNode(true));
152     }
153
154     /**
155      * Create a new HTML entry for an item.
156      */

157     private void makeItemHtml(Inventory item, Node parent, int level) {
158         String JavaDoc url = ITEM_URL + "?ref=../contents/" + item.getRef()
159             + "&add=" + item.getObjectId();
160         sampleItemAnchor.setHref(url);
161         sampleItemAnchor.setTarget("item");
162         sampleItemText.setData(item.getDesc());
163         sampleItemBlock.setClassName("level" + level);
164         parent.appendChild(sampleItemBlock.cloneNode(true));
165     }
166
167     /**
168      * Recursively process a category or item entry, adding it to the DOM.
169      * Sets node class using level for CSS.
170      */

171     private void processInventoryEntry(Inventory entry, Node parent, int level) {
172         if (entry.isCategory()) {
173             boolean isOpen = openCatIds.contains(new Long JavaDoc(entry.getObjectId()));
174             makeCategoryHtml(entry, parent, isOpen, level);
175             if (isOpen) {
176                 Vector entries = entry.getItems(entry.getObjectId());
177                 for (int idx = 0; idx < entries.size(); idx++) {
178                     processInventoryEntry((Inventory)entries.elementAt(idx), parent, level+1);
179                 }
180             }
181         } else {
182             makeItemHtml(entry, parent, level);
183         }
184     }
185
186     /**
187      * Generate the DOM entries for the current state of the inventory.
188      */

189     private void processInventory() {
190     try{
191     Inventory inventory = InventoryFactory.getInventory("golfShop.business.inventory.InventoryImpl");
192     processInventoryEntry(inventory.getInventoryById(0),
193                               shelfHtml.getElementList(), 1);
194 /*
195  * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run GolfShop_pres ) so
196  * we cannot create a list of category
197  * We need to allow GolfShop_pres to be functional
198  */

199  
200         } catch(NullPointerException JavaDoc ex) {
201           
202         }
203     }
204
205     /**
206      * Parse CGI arguments into fields.
207      */

208     private void parseArgs(HttpPresentationRequest request)
209         throws HttpPresentationException {
210
211         // Parse button id.
212
String JavaDoc button = request.getParameter("button");
213         if (button != null) {
214             try {
215                 buttonCatId = new Long JavaDoc(button);
216             } catch (NumberFormatException JavaDoc except) {
217                 // Ignore, probably a bogus URL.
218
}
219         }
220         
221         String JavaDoc open = request.getParameter("open");
222         if (open != null) {
223             StringTokenizer tokens = new StringTokenizer(open, ",");
224             while (tokens.hasMoreTokens()) {
225                 String JavaDoc token = tokens.nextToken();
226                 try {
227                     Long JavaDoc id = new Long JavaDoc(token);
228                     openCatIds.put(id, id);
229                 } catch (NumberFormatException JavaDoc except) {
230                     // Ignore, probably a bogus URL.
231
}
232             }
233         }
234     }
235
236     /**
237      * Modify the open category list based on the currenmt button.
238      * Also generate CGI argument for the open categories.
239      */

240     private void adjustOpenCategories() {
241         if (buttonCatId != null) {
242             // Adjust the open entry for the current button.
243
if (openCatIds.containsKey(buttonCatId)) {
244                 openCatIds.remove(buttonCatId);
245             } else {
246                 openCatIds.put(buttonCatId, buttonCatId);
247             }
248         }
249
250         // Generate CGI arguments.
251
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
252         Enumeration openIds = openCatIds.keys();
253
254         while (openIds.hasMoreElements()) {
255             if (buf.length() > 0) {
256                 buf.append(",");
257             }
258             buf.append(((Long JavaDoc)openIds.nextElement()).toString());
259         }
260         if (buf.length() > 0) {
261             openCatCgiArg = "open=" + buf.toString();
262         }
263     }
264
265
266     /**
267      * Entry.
268      */

269     public void run(HttpPresentationComms comms)
270         throws HttpPresentationException {
271
272         parseArgs(comms.request);
273         prepareHtml(comms);
274         adjustOpenCategories();
275         processInventory();
276         comms.response.writeDOM(shelfHtml);
277     }
278 }
279
Popular Tags