001: /*
002: * Enhydra Java Application Server
003: * The Initial Developer of the Original Code is Lutris Technologies Inc.
004: * Portions created by Lutris are Copyright (C) 1997-2000 Lutris Technologies
005: * Inc.
006: * All Rights Reserved.
007: *
008: * The contents of this file are subject to the Enhydra Public License Version
009: * 1.0 (the "License"); you may not use this file except in compliance with the
010: * License. You may obtain a copy of the License at
011: * http://www.enhydra.org/software/license/epl.html
012: *
013: * Software distributed under the License is distributed on an "AS IS" basis,
014: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
015: * License for the specific language governing rights and limitations under the
016: * License.
017: *
018: *
019: */
020:
021: package golfShop.presentation.xmlc.cart;
022:
023: import org.enhydra.xml.xmlc.*;
024: import org.enhydra.xml.xmlc.html.*;
025: import com.lutris.appserver.server.httpPresentation.*;
026: import java.io.*;
027: import java.net.URLEncoder;
028: import org.w3c.dom.*;
029: import org.w3c.dom.html.*;
030: import golfShop.presentation.xmlc.utilities.*;
031: import java.math.BigDecimal;
032: import java.util.Enumeration;
033: import golfShop.spec.cart.*;
034:
035: /**
036: * Generate a HTML table to display the cart contents. It is based on the
037: * prototype table in the Contents.html or Confirm.html page, which
038: * implement ContentsTable.
039: */
040: public class ContentsTableFormatter {
041: /**
042: * Prefix for dynamic quantify field; will have object id added.
043: */
044: public static final String QTY_PREFIX = "qty_";
045:
046: /**
047: * Prefix for delete field; will have object id added.
048: */
049: public static final String DEL_PREFIX = "del_";
050:
051: /**
052: * Id for item num in prototype row.
053: */
054: private static final String ITEM_NUM_ID = "itemNum";
055:
056: /**
057: * Id for item quantity in prototype row.
058: */
059: private static final String ITEM_QUANTITY_ID = "itemQuantity";
060:
061: /**
062: * Id for item desc in prototype row.
063: */
064: private static final String ITEM_DESC_ID = "itemDesc";
065:
066: /**
067: * Object being operated on.
068: */
069: private ContentsTable htmlObj;
070:
071: /**
072: * Prototype row for the table.
073: */
074: private HTMLTableRowElement protoRow;
075:
076: /**
077: * Is table dynamic?
078: */
079: private boolean dynamic;
080:
081: /**
082: * Cart to add to HTML object.
083: */
084: private Cart cart;
085:
086: /**
087: * Generate a table row for an item based on the prototype.
088: *
089: * @param rowNum Row number in table.
090: * @param itemPair The item information to display.
091: */
092: private HTMLTableRowElement makeItemRow(int rowNum,
093: CartItemPair itemPair) {
094: // Get necessary info.
095: int quantity = itemPair.getQuantity();
096: CartItem item = itemPair.getItem();
097: BigDecimal price = new BigDecimal(item.getPrice());
098: BigDecimal total = new BigDecimal(quantity * item.getPrice());
099:
100: // Format price and total to have 2 digits past the decimal.
101: price = price.setScale(2, BigDecimal.ROUND_HALF_UP);
102: total = total.setScale(2, BigDecimal.ROUND_HALF_UP);
103:
104: // Setup new row.
105: HTMLTableRowElement newRow = (HTMLTableRowElement) protoRow
106: .cloneNode(true);
107:
108: // Item number
109: Element itemNumElem = XMLCUtil.getRequiredElementById(
110: ITEM_NUM_ID, newRow);
111: XMLCUtil.getFirstText(itemNumElem).setData(
112: Integer.toString(rowNum));
113:
114: // Quantity
115: String quantityStr = Integer.toString(quantity);
116: if (dynamic) {
117: // Update input node.
118: HTMLInputElement quantityElem = (HTMLInputElement) XMLCUtil
119: .getRequiredElementById(ITEM_QUANTITY_ID, newRow);
120: quantityElem.setName(QTY_PREFIX + item.getObjectId());
121: quantityElem.setValue(quantityStr);
122: } else {
123: // The node will only be an input node if it came from the
124: // cart HTML. Figure it out and either replace input node or
125: // change the next node.
126: Element quantityElem = XMLCUtil.getRequiredElementById(
127: ITEM_QUANTITY_ID, newRow);
128: if (quantityElem instanceof HTMLInputElement) {
129: Text text = htmlObj.createTextNode(quantityStr);
130: quantityElem.getParentNode().replaceChild(text,
131: quantityElem);
132: } else {
133: XMLCUtil.getFirstText(quantityElem)
134: .setData(quantityStr);
135: }
136: }
137:
138: // Description
139: Element itemDescElem = XMLCUtil.getRequiredElementById(
140: ITEM_DESC_ID, newRow);
141: XMLCUtil.getFirstText(itemDescElem).setData(item.getName());
142:
143: // Price
144: Element itemPriceElem = XMLCUtil.getRequiredElementById(
145: "itemPrice", newRow);
146: XMLCUtil.getFirstText(itemPriceElem).setData(price.toString());
147:
148: // Total
149: Element itemTotalElem = XMLCUtil.getRequiredElementById(
150: "itemTotal", newRow);
151: XMLCUtil.getFirstText(itemTotalElem).setData(total.toString());
152:
153: // Delete checkbox, only in a dynamic table.
154: if (dynamic) {
155: HTMLInputElement itemDeleteElem = (HTMLInputElement) XMLCUtil
156: .getRequiredElementById("itemDelete", newRow);
157: itemDeleteElem.setName(DEL_PREFIX + item.getObjectId());
158: }
159: return newRow;
160: }
161:
162: /**
163: * Fill in table rows from cart and total cost.
164: */
165: private void fillInRows() {
166:
167: try {
168: Enumeration items = cart.getContents();
169: HTMLTableRowElement costRow = htmlObj.getElementCostRow();
170: Node parent = costRow.getParentNode();
171:
172: int rowNum = 1;
173: while (items.hasMoreElements()) {
174: HTMLTableRowElement newRow = makeItemRow(rowNum++,
175: (CartItemPair) items.nextElement());
176: parent.insertBefore(newRow, costRow);
177: }
178:
179: BigDecimal total = new BigDecimal(cart.getTotal());
180: total = total.setScale(2, BigDecimal.ROUND_HALF_UP);
181: htmlObj.setTextCost(total.toString());
182: //same thing
183: } catch (NullPointerException ex) {
184: }
185: }
186:
187: /**
188: * Recursively search for a row and deleted it if it doesn't have an id
189: * or if its the prototype row.
190: */
191: private void deleteDemoRows(Node node) {
192: if (node instanceof HTMLTableRowElement) {
193: String id = ((HTMLTableRowElement) node).getId();
194: if ((id == null) || (id.length() == 0)
195: || (node == protoRow)) {
196: node.getParentNode().removeChild(node);
197: }
198: } else {
199: // Not a row, search children.
200: Node child = node.getFirstChild();
201: while (child != null) {
202: Node next = child.getNextSibling();
203: deleteDemoRows(child);
204: child = next;
205:
206: }
207: }
208: }
209:
210: /**
211: * Constructor. External instances are never created.
212: *
213: * @param tableHtmlObj Object with HTML table for items.
214: * @param tableCart Cart used to generate the table.
215: */
216: private ContentsTableFormatter(ContentsTable tableHtmlObj,
217: Cart tableCart, boolean isDynamic) {
218: htmlObj = tableHtmlObj;
219: cart = tableCart;
220: dynamic = isDynamic;
221: protoRow = htmlObj.getElementProtoRow();
222: deleteDemoRows(htmlObj.getElementItemTable());
223: }
224:
225: /**
226: * File in the table object from a cart.
227: */
228: public static void fillInTable(ContentsTable tableHtmlObj,
229: Cart tableCart, boolean isDynamic) {
230: ContentsTableFormatter table = new ContentsTableFormatter(
231: tableHtmlObj, tableCart, isDynamic);
232: table.fillInRows();
233: }
234: }
|