001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: DiscCatalog.java,v 1.1 2006-09-11 12:47:00 sinisa Exp $
022: */
023:
024: package transactionsDiscRack.presentation.discMgmt;
025:
026: import transactionsDiscRack.spec.*;
027: import transactionsDiscRack.presentation.BasePO;
028: import transactionsDiscRack.presentation.TransactionsDiscRackPresentationException;
029:
030: import com.lutris.appserver.server.httpPresentation.*;
031:
032: import org.enhydra.xml.dom.DOMOps;
033: import org.enhydra.xml.xmlc.*;
034: import org.w3c.dom.*;
035: import org.w3c.dom.html.*;
036:
037: /**
038: * This class populates discs and display on the page.
039: *
040: */
041: public class DiscCatalog extends BasePO {
042:
043: /**
044: * Superclass method override
045: */
046: public boolean loggedInUserRequired() {
047: return true;
048: }
049:
050: /**
051: * Default event. Just show the page.
052: *
053: * @return html document
054: * @exception HttpPresentationException
055: */
056: public XMLObject handleDefault()
057: throws TransactionsDiscRackPresentationException {
058: // Swap in our real run-time JavaScript to repalce the storyboard
059: // JavaScript
060:
061: DiscCatalogHTML page = (DiscCatalogHTML) myComms.xmlcFactory
062: .create(DiscCatalogHTML.class);
063:
064: HTMLScriptElement script = ((DiscCatalogScriptHTML) myComms.xmlcFactory
065: .create(DiscCatalogScriptHTML.class))
066: .getElementRealScript();
067: DOMOps.replaceNode(script, page.getElementDummyScript());
068:
069: /*
070: * Catch Null pointer exception ( we canot make a instances of classes
071: * from business layer when we run transactionsdiscRack_pres ) We need
072: * to allow transactionsdiscRack_pres to be functional , response will
073: * be default HTML page
074: */
075:
076: try {
077:
078: Disc[] discList = ((DiscGenerator) DiscGeneratorFactory
079: .getDiscGenerator("transactionsDiscRack.business.disc.DiscGeneratorImpl"))
080: .findDiscsForPerson(this .getUserHandle(),
081: transaction);
082:
083: try {
084: page.setTextOwner(this .getSessionData()
085: .getUserFirstName()
086: + " "
087: + this .getSessionData().getUserLastName()
088: + "'s Discs");
089: } catch (Exception ex) {
090: writeDebugMsg("Error getting user info: " + ex);
091: }
092:
093: String errorMsg = this .getSessionData()
094: .getAndClearUserMessage();
095: if (null == errorMsg) {
096: page.getElementErrorText().getParentNode().removeChild(
097: page.getElementErrorText());
098: } else {
099: page.setTextErrorText(errorMsg);
100: }
101:
102: // Generate the person's Disc list and create the HTML template
103: // references
104: HTMLTableRowElement templateRow = page
105: .getElementTemplateRow();
106: HTMLOptionElement templateOption = page
107: .getElementTemplateOption();
108: Node discTable = templateRow.getParentNode();
109: Node discSelect = templateOption.getParentNode();
110:
111: // Remove ids to prevent duplicates
112: templateRow.removeAttribute("id");
113: templateOption.removeAttribute("id");
114:
115: // Remove id attributes from the cells in the template row
116: HTMLElement artistCellTemplate = page.getElementArtist();
117: HTMLElement titleCellTemplate = page.getElementTitle();
118: HTMLElement genreCellTemplate = page.getElementGenre();
119: HTMLElement likeThisDisc = page.getElementLikeThisDisc();
120:
121: artistCellTemplate.removeAttribute("id");
122: titleCellTemplate.removeAttribute("id");
123: genreCellTemplate.removeAttribute("id");
124: likeThisDisc.removeAttribute("id");
125:
126: // Remove the dummy storyboard text from the prototype HTML
127: templateOption.removeChild(templateOption.getFirstChild());
128:
129: // Get collection of Discs and loop through collection
130: // to add each disc as a row in the table.
131: for (int numDiscs = 0; numDiscs < discList.length; numDiscs++) {
132: Disc currentDisc = discList[numDiscs];
133: // set text of new cells to values from string array
134: page.setTextArtist(currentDisc.getArtist());
135: page.setTextTitle(currentDisc.getTitle());
136: page.setTextGenre(currentDisc.getGenre());
137:
138: if (currentDisc.isLiked()) {
139: page.setTextLikeThisDisc("Yes");
140: } else {
141: page.setTextLikeThisDisc("Not");
142: }
143:
144: // Add a deep clone of the row to the DOM
145: Node clonedNode = templateRow.cloneNode(true);
146: discTable.appendChild(clonedNode);
147: // Alternative way to insert nodes below
148: // insertBefore(newNode, oldNode);
149: // discTable.insertBefore(clonedNode, templateRow);
150:
151: // Now populate the select list
152: // This algorithm is obscure because options are not normal
153: // HTML elements
154: // First populate the option value (the disc database ID).
155: // Then append a text child as the option
156: // text, which is what is displayed as the text
157: // in each row of the select box
158: HTMLOptionElement clonedOption = (HTMLOptionElement) templateOption
159: .cloneNode(true);
160: clonedOption.setValue(currentDisc.getHandle());
161: Node optionTextNode = clonedOption.getOwnerDocument()
162: .createTextNode(
163: currentDisc.getArtist() + ": "
164: + currentDisc.getTitle());
165: clonedOption.appendChild(optionTextNode);
166: // Do only a shallow copy of the option as we don't want the
167: // text child
168: // of the node option
169: discSelect.appendChild(clonedOption);
170: // Alternative way to insert nodes below
171: // insertBefore(newNode, oldNode);
172: // discSelect.insertBefore(clonedOption, templateOption);
173: }
174:
175: // Finally remove the template row and template select option
176: // from the page
177: templateRow.getParentNode().removeChild(templateRow);
178: templateOption.getParentNode().removeChild(templateOption);
179:
180: } catch (NullPointerException ex) {
181: page.setTextErrorText("This is a default HTML page");
182: } catch (Exception ex) {
183: writeDebugMsg("Error populating disc catalog: " + ex);
184: throw new TransactionsDiscRackPresentationException(
185: "Error getting discs for user: ", ex);
186: }
187:
188: // write out HTML
189: return page;
190: }
191: }
|