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:32:06 sinisa Exp $
022: */
023:
024: package discRack.presentation.discMgmt;
025:
026: import discRack.spec.*;
027: import discRack.presentation.BasePO;
028: import discRack.presentation.DiscRackPresentationException;
029:
030: import com.lutris.appserver.server.httpPresentation.*;
031: import com.lutris.appserver.server.session.*;
032: import com.lutris.util.*;
033: import org.enhydra.xml.xmlc.*;
034: import org.enhydra.xml.xmlc.html.*;
035: import org.enhydra.xml.xmlc.XMLObject;
036: import org.w3c.dom.*;
037: import org.w3c.dom.html.*;
038:
039: /**
040: * This class populates discs and display on the page.
041: *
042: */
043: public class DiscCatalog extends BasePO {
044:
045: /**
046: * Superclass method override
047: */
048: public boolean loggedInUserRequired() {
049: return true;
050: }
051:
052: /**
053: * Default event. Just show the page.
054: *
055: * @return html document
056: * @exception HttpPresentationException
057: */
058: public XMLObject handleDefault()
059: throws DiscRackPresentationException {
060: // Swap in our real run-time JavaScript to repalce the storyboard JavaScript
061:
062: DiscCatalogHTML page = (DiscCatalogHTML) myComms.xmlcFactory
063: .create(DiscCatalogHTML.class);
064: HTMLScriptElement script = ((DiscCatalogScriptHTML) myComms.xmlcFactory
065: .create(DiscCatalogScriptHTML.class))
066: .getElementRealScript();
067: // HTMLScriptElement script = new DiscCatalogScriptHTML().getElementRealScript();
068: XMLCUtil.replaceNode(script, page.getElementDummyScript());
069:
070: /*
071: * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run discRack_pres )
072: * We need to allow discRack_pres to be functional , response
073: * will be default HTML page
074: */
075:
076: try {
077:
078: Disc[] discList = ((DiscGenerator) DiscGeneratorFactory
079: .getDiscGenerator("discRack.business.disc.DiscGeneratorImpl"))
080: .findDiscsForPerson(this .getUser());
081:
082: try {
083: page.setTextOwner(this .getUser().getFirstname() + " "
084: + this .getUser().getLastname() + "'s Discs");
085: } catch (Exception ex) {
086: this .writeDebugMsg("Error getting user info: " + ex);
087: }
088:
089: String errorMsg = this .getSessionData()
090: .getAndClearUserMessage();
091: if (null == errorMsg) {
092: page.getElementErrorText().getParentNode().removeChild(
093: page.getElementErrorText());
094: } else {
095: page.setTextErrorText(errorMsg);
096: }
097:
098: // Generate the person's Disc list and create the HTML template references
099: HTMLTableRowElement templateRow = page
100: .getElementTemplateRow();
101: HTMLOptionElement templateOption = page
102: .getElementTemplateOption();
103: Node discTable = templateRow.getParentNode();
104: Node discSelect = templateOption.getParentNode();
105:
106: // Remove ids to prevent duplicates
107: templateRow.removeAttribute("id");
108: templateOption.removeAttribute("id");
109:
110: // Remove id attributes from the cells in the template row
111: HTMLElement artistCellTemplate = page.getElementArtist();
112: HTMLElement titleCellTemplate = page.getElementTitle();
113: HTMLElement genreCellTemplate = page.getElementGenre();
114: HTMLElement likeThisDisc = page.getElementLikeThisDisc();
115:
116: artistCellTemplate.removeAttribute("id");
117: titleCellTemplate.removeAttribute("id");
118: genreCellTemplate.removeAttribute("id");
119: likeThisDisc.removeAttribute("id");
120:
121: // Remove the dummy storyboard text from the prototype HTML
122: templateOption.removeChild(templateOption.getFirstChild());
123:
124: // Get collection of Discs and loop through collection
125: // to add each disc as a row in the table.
126: for (int numDiscs = 0; numDiscs < discList.length; numDiscs++) {
127: Disc currentDisc = discList[numDiscs];
128: // set text of new cells to values from string array
129: page.setTextArtist(currentDisc.getArtist());
130: page.setTextTitle(currentDisc.getTitle());
131: page.setTextGenre(currentDisc.getGenre());
132:
133: if (currentDisc.isLiked()) {
134: page.setTextLikeThisDisc("Yes");
135: } else {
136: page.setTextLikeThisDisc("Not");
137: }
138:
139: // Add a deep clone of the row to the DOM
140: Node clonedNode = templateRow.cloneNode(true);
141: discTable.appendChild(clonedNode);
142: // Alternative way to insert nodes below
143: // insertBefore(newNode, oldNode);
144: // discTable.insertBefore(clonedNode, templateRow);
145:
146: // Now populate the select list
147: // This algorithm is obscure because options are not normal
148: // HTML elements
149: // First populate the option value (the disc database ID).
150: // Then append a text child as the option
151: // text, which is what is displayed as the text
152: // in each row of the select box
153: HTMLOptionElement clonedOption = (HTMLOptionElement) templateOption
154: .cloneNode(true);
155: clonedOption.setValue(currentDisc.getHandle());
156: Node optionTextNode = clonedOption.getOwnerDocument()
157: .createTextNode(
158: currentDisc.getArtist() + ": "
159: + currentDisc.getTitle());
160: clonedOption.appendChild(optionTextNode);
161: // Do only a shallow copy of the option as we don't want the text child
162: // of the node option
163: discSelect.appendChild(clonedOption);
164: // Alternative way to insert nodes below
165: // insertBefore(newNode, oldNode);
166: // discSelect.insertBefore(clonedOption, templateOption);
167: }
168:
169: // Finally remove the template row and template select option
170: // from the page
171: templateRow.getParentNode().removeChild(templateRow);
172: templateOption.getParentNode().removeChild(templateOption);
173:
174: } catch (NullPointerException ex) {
175: page.setTextErrorText("This is a default HTML page");
176: } catch (Exception ex) {
177: this .writeDebugMsg("Error populating disc catalog: " + ex);
178: throw new DiscRackPresentationException(
179: "Error getting discs for user: ", ex);
180: }
181:
182: // write out HTML
183: return page;
184: }
185: }
|