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