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:40:05 sinisa Exp $
022: */
023:
024: package jtaDiscRack.presentation.discMgmt;
025:
026: import jtaDiscRack.spec.*;
027: import jtaDiscRack.presentation.BasePO;
028: import jtaDiscRack.presentation.JtaDiscRackPresentationException;
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 JtaDiscRackPresentationException {
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 in presentation mode ) We need
072: * to allow presentation mode to be functional , response will
073: * be default HTML page
074: */
075:
076: try {
077:
078: Disc[] discList = ((DiscGenerator) DiscGeneratorFactory
079: .getDiscGenerator("jtaDiscRack.business.disc.DiscGeneratorImpl"))
080: .findDiscsForPerson(this .getUserHandle());
081:
082: try {
083: page.setTextOwner(this .getSessionData()
084: .getUserFirstName()
085: + " "
086: + this .getSessionData().getUserLastName()
087: + "'s Discs");
088: } catch (Exception ex) {
089: writeDebugMsg("Error getting user info: " + ex);
090: }
091:
092: String errorMsg = this .getSessionData()
093: .getAndClearUserMessage();
094: if (null == errorMsg) {
095: page.getElementErrorText().getParentNode().removeChild(
096: page.getElementErrorText());
097: } else {
098: page.setTextErrorText(errorMsg);
099: }
100:
101: // Generate the person's Disc list and create the HTML template
102: // references
103: HTMLTableRowElement templateRow = page
104: .getElementTemplateRow();
105: HTMLOptionElement templateOption = page
106: .getElementTemplateOption();
107: Node discTable = templateRow.getParentNode();
108: Node discSelect = templateOption.getParentNode();
109:
110: // Remove ids to prevent duplicates
111: templateRow.removeAttribute("id");
112: templateOption.removeAttribute("id");
113:
114: // Remove id attributes from the cells in the template row
115: HTMLElement artistCellTemplate = page.getElementArtist();
116: HTMLElement titleCellTemplate = page.getElementTitle();
117: HTMLElement genreCellTemplate = page.getElementGenre();
118: HTMLElement likeThisDisc = page.getElementLikeThisDisc();
119:
120: artistCellTemplate.removeAttribute("id");
121: titleCellTemplate.removeAttribute("id");
122: genreCellTemplate.removeAttribute("id");
123: likeThisDisc.removeAttribute("id");
124:
125: // Remove the dummy storyboard text from the prototype HTML
126: templateOption.removeChild(templateOption.getFirstChild());
127:
128: // Get collection of Discs and loop through collection
129: // to add each disc as a row in the table.
130: for (int numDiscs = 0; numDiscs < discList.length; numDiscs++) {
131: Disc currentDisc = discList[numDiscs];
132: // set text of new cells to values from string array
133: page.setTextArtist(currentDisc.getArtist());
134: page.setTextTitle(currentDisc.getTitle());
135: page.setTextGenre(currentDisc.getGenre());
136:
137: if (currentDisc.isLiked()) {
138: page.setTextLikeThisDisc("Yes");
139: } else {
140: page.setTextLikeThisDisc("Not");
141: }
142:
143: // Add a deep clone of the row to the DOM
144: Node clonedNode = templateRow.cloneNode(true);
145: discTable.appendChild(clonedNode);
146: // Alternative way to insert nodes below
147: // insertBefore(newNode, oldNode);
148: // discTable.insertBefore(clonedNode, templateRow);
149:
150: // Now populate the select list
151: // This algorithm is obscure because options are not normal
152: // HTML elements
153: // First populate the option value (the disc database ID).
154: // Then append a text child as the option
155: // text, which is what is displayed as the text
156: // in each row of the select box
157: HTMLOptionElement clonedOption = (HTMLOptionElement) templateOption
158: .cloneNode(true);
159: clonedOption.setValue(currentDisc.getHandle());
160: Node optionTextNode = clonedOption.getOwnerDocument()
161: .createTextNode(
162: currentDisc.getArtist() + ": "
163: + currentDisc.getTitle());
164: clonedOption.appendChild(optionTextNode);
165: // Do only a shallow copy of the option as we don't want the
166: // text child
167: // of the node option
168: discSelect.appendChild(clonedOption);
169: // Alternative way to insert nodes below
170: // insertBefore(newNode, oldNode);
171: // discSelect.insertBefore(clonedOption, templateOption);
172: }
173:
174: // Finally remove the template row and template select option
175: // from the page
176: templateRow.getParentNode().removeChild(templateRow);
177: templateOption.getParentNode().removeChild(templateOption);
178:
179: } catch (NullPointerException ex) {
180: page.setTextErrorText("This is a default HTML page");
181: } catch (Exception ex) {
182: writeDebugMsg("Error populating disc catalog: " + ex);
183: throw new JtaDiscRackPresentationException(
184: "Error getting discs for user: ", ex);
185: }
186:
187: // write out HTML
188: return page;
189: }
190: }
|