KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > golfShop > presentation > xmlc > cart > ContentsTableFormatter


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.cart;
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 org.w3c.dom.*;
29 import org.w3c.dom.html.*;
30 import golfShop.presentation.xmlc.utilities.*;
31 import java.math.BigDecimal JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import golfShop.spec.cart.*;
34 /**
35  * Generate a HTML table to display the cart contents. It is based on the
36  * prototype table in the Contents.html or Confirm.html page, which
37  * implement ContentsTable.
38  */

39 public class ContentsTableFormatter {
40     /**
41      * Prefix for dynamic quantify field; will have object id added.
42      */

43     public static final String JavaDoc QTY_PREFIX = "qty_";
44
45     /**
46      * Prefix for delete field; will have object id added.
47      */

48     public static final String JavaDoc DEL_PREFIX = "del_";
49
50     /**
51      * Id for item num in prototype row.
52      */

53     private static final String JavaDoc ITEM_NUM_ID = "itemNum";
54
55     /**
56      * Id for item quantity in prototype row.
57      */

58     private static final String JavaDoc ITEM_QUANTITY_ID = "itemQuantity";
59
60     /**
61      * Id for item desc in prototype row.
62      */

63     private static final String JavaDoc ITEM_DESC_ID = "itemDesc";
64
65     /**
66      * Object being operated on.
67      */

68     private ContentsTable htmlObj;
69
70     /**
71      * Prototype row for the table.
72      */

73     private HTMLTableRowElement protoRow;
74     
75     /**
76      * Is table dynamic?
77      */

78     private boolean dynamic;
79
80     /**
81      * Cart to add to HTML object.
82      */

83     private Cart cart;
84
85     /**
86      * Generate a table row for an item based on the prototype.
87      *
88      * @param rowNum Row number in table.
89      * @param itemPair The item information to display.
90      */

91     private HTMLTableRowElement makeItemRow(int rowNum, CartItemPair itemPair) {
92     // Get necessary info.
93
int quantity = itemPair.getQuantity();
94     CartItem item = itemPair.getItem();
95     BigDecimal JavaDoc price = new BigDecimal JavaDoc(item.getPrice());
96     BigDecimal JavaDoc total = new BigDecimal JavaDoc(quantity * item.getPrice());
97
98     // Format price and total to have 2 digits past the decimal.
99
price = price.setScale(2, BigDecimal.ROUND_HALF_UP);
100     total = total.setScale(2, BigDecimal.ROUND_HALF_UP);
101
102         // Setup new row.
103
HTMLTableRowElement newRow = (HTMLTableRowElement)protoRow.cloneNode(true);
104
105         // Item number
106
Element itemNumElem = XMLCUtil.getRequiredElementById(ITEM_NUM_ID, newRow);
107         XMLCUtil.getFirstText(itemNumElem).setData(Integer.toString(rowNum));
108
109         // Quantity
110
String JavaDoc quantityStr = Integer.toString(quantity);
111         if (dynamic) {
112             // Update input node.
113
HTMLInputElement quantityElem = (HTMLInputElement)XMLCUtil.getRequiredElementById(ITEM_QUANTITY_ID, newRow);
114             quantityElem.setName(QTY_PREFIX + item.getObjectId());
115             quantityElem.setValue(quantityStr);
116         } else {
117             // The node will only be an input node if it came from the
118
// cart HTML. Figure it out and either replace input node or
119
// change the next node.
120
Element quantityElem = XMLCUtil.getRequiredElementById(ITEM_QUANTITY_ID, newRow);
121             if (quantityElem instanceof HTMLInputElement) {
122                 Text text = htmlObj.createTextNode(quantityStr);
123                 quantityElem.getParentNode().replaceChild(text, quantityElem);
124             } else {
125                 XMLCUtil.getFirstText(quantityElem).setData(quantityStr);
126             }
127         }
128
129         // Description
130
Element itemDescElem = XMLCUtil.getRequiredElementById(ITEM_DESC_ID, newRow);
131         XMLCUtil.getFirstText(itemDescElem).setData(item.getName());
132
133         // Price
134
Element itemPriceElem = XMLCUtil.getRequiredElementById("itemPrice", newRow);
135         XMLCUtil.getFirstText(itemPriceElem).setData(price.toString());
136         
137         // Total
138
Element itemTotalElem = XMLCUtil.getRequiredElementById("itemTotal", newRow);
139         XMLCUtil.getFirstText(itemTotalElem).setData(total.toString());
140
141         // Delete checkbox, only in a dynamic table.
142
if (dynamic) {
143             HTMLInputElement itemDeleteElem = (HTMLInputElement)XMLCUtil.getRequiredElementById("itemDelete", newRow);
144             itemDeleteElem.setName(DEL_PREFIX + item.getObjectId());
145         }
146         return newRow;
147     }
148
149     /**
150      * Fill in table rows from cart and total cost.
151      */

152     private void fillInRows() {
153         
154 try{
155         Enumeration JavaDoc items = cart.getContents();
156         HTMLTableRowElement costRow = htmlObj.getElementCostRow();
157         Node parent = costRow.getParentNode();
158        
159         int rowNum = 1;
160     while (items.hasMoreElements()) {
161             HTMLTableRowElement newRow =
162                 makeItemRow(rowNum++, (CartItemPair)items.nextElement());
163             parent.insertBefore(newRow, costRow);
164         }
165
166     BigDecimal JavaDoc total = new BigDecimal JavaDoc(cart.getTotal());
167         total = total.setScale(2, BigDecimal.ROUND_HALF_UP);
168         htmlObj.setTextCost(total.toString());
169         //same thing
170
} catch(NullPointerException JavaDoc ex) {
171      }
172     }
173
174     /**
175      * Recursively search for a row and deleted it if it doesn't have an id
176      * or if its the prototype row.
177      */

178     private void deleteDemoRows(Node node) {
179         if (node instanceof HTMLTableRowElement) {
180             String JavaDoc id = ((HTMLTableRowElement)node).getId();
181             if ((id == null) || (id.length() == 0) || (node == protoRow)) {
182                 node.getParentNode().removeChild(node);
183             }
184         } else {
185             // Not a row, search children.
186
Node child = node.getFirstChild();
187             while (child != null) {
188                 Node next = child.getNextSibling();
189                 deleteDemoRows(child);
190                 child = next;
191                 
192             }
193         }
194     }
195
196     /**
197      * Constructor. External instances are never created.
198      *
199      * @param tableHtmlObj Object with HTML table for items.
200      * @param tableCart Cart used to generate the table.
201      */

202     private ContentsTableFormatter(ContentsTable tableHtmlObj, Cart tableCart,
203                                    boolean isDynamic) {
204         htmlObj = tableHtmlObj;
205         cart = tableCart;
206         dynamic = isDynamic;
207         protoRow = htmlObj.getElementProtoRow();
208         deleteDemoRows(htmlObj.getElementItemTable());
209     }
210
211     /**
212      * File in the table object from a cart.
213      */

214     public static void fillInTable(ContentsTable tableHtmlObj, Cart tableCart,
215                                    boolean isDynamic) {
216         ContentsTableFormatter table =
217             new ContentsTableFormatter(tableHtmlObj, tableCart, isDynamic);
218         table.fillInRows();
219     }
220 }
221
Popular Tags