KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > barracudaDiscRack > presentation > discMgmt > DiscCatalog


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: DiscCatalog.java,v 1.4 2004/12/03 14:12:34 slobodan Exp $
22  */

23
24 package barracudaDiscRack.presentation.discMgmt;
25
26 import barracudaDiscRack.business.person.*;
27 import barracudaDiscRack.business.disc.*;
28 import barracudaDiscRack.presentation.BasePO;
29 import barracudaDiscRack.presentation.DiscRackPresentationException;
30 import com.lutris.appserver.server.httpPresentation.*;
31 import com.lutris.appserver.server.session.*;
32 import com.lutris.util.*;
33 import org.enhydra.xml.xmlc.*;
34 import org.enhydra.xml.xmlc.html.*;
35 import org.enhydra.xml.xmlc.XMLObject;
36 import org.w3c.dom.*;
37 import org.w3c.dom.html.*;
38
39 /**
40  * This class populates discs and display on the page.
41  *
42  * @author
43  * @version
44  */

45 public class DiscCatalog extends BasePO {
46
47     /**
48      * Superclass method override
49      */

50     public boolean loggedInUserRequired() {
51         return true;
52     }
53     
54     /**
55      * Default event. Just show the page.
56      *
57      * @return html document
58      * @exception HttpPresentationException
59      */

60     public XMLObject handleDefault()
61         throws DiscRackPresentationException {
62         // Swap in our real run-time JavaScript to repalce the storyboard JavaScript
63

64     DiscCatalogHTML page = (DiscCatalogHTML)myComms.xmlcFactory.create(DiscCatalogHTML.class);
65         HTMLScriptElement script = ((DiscCatalogScriptHTML)myComms.xmlcFactory.create(DiscCatalogScriptHTML.class)).getElementRealScript();
66         XMLCUtil.replaceNode(script, page.getElementDummyScript());
67         
68         try {
69             page.setTextOwner(this.getUser().getFirstname() + " " +
70                               this.getUser().getLastname() + "'s Discs");
71         } catch(Exception JavaDoc ex) {
72             this.writeDebugMsg("Error getting user info: " + ex);
73         }
74         
75         String JavaDoc errorMsg = this.getSessionData().getAndClearUserMessage();
76         if(null == errorMsg) {
77             page.getElementErrorText().getParentNode().removeChild(page.getElementErrorText());
78         } else {
79             page.setTextErrorText(errorMsg);
80         }
81     
82         // Generate the person's Disc list and create the HTML template references
83
HTMLTableRowElement templateRow = page.getElementTemplateRow();
84         HTMLOptionElement templateOption = page.getElementTemplateOption();
85         Node discTable = templateRow.getParentNode();
86         Node discSelect = templateOption.getParentNode();
87         
88         // Remove ids to prevent duplicates
89
templateRow.removeAttribute("id");
90         templateOption.removeAttribute("id");
91         
92         // Remove id attributes from the cells in the template row
93
HTMLElement artistCellTemplate = page.getElementArtist();
94         HTMLElement titleCellTemplate = page.getElementTitle();
95         HTMLElement genreCellTemplate = page.getElementGenre();
96         HTMLElement likeThisDisc = page.getElementLikeThisDisc();
97         
98         artistCellTemplate.removeAttribute("id");
99         titleCellTemplate.removeAttribute("id");
100         genreCellTemplate.removeAttribute("id");
101         likeThisDisc.removeAttribute("id");
102         
103         // Remove the dummy storyboard text from the prototype HTML
104
templateOption.removeChild(templateOption.getFirstChild());
105     
106         try {
107             Disc[] discList = DiscFactory.findDiscsForPerson(this.getUser());
108             
109             // Get collection of Discs and loop through collection
110
// to add each disc as a row in the table.
111
for(int numDiscs = 0; numDiscs < discList.length; numDiscs++) {
112                 Disc currentDisc = discList[numDiscs];
113                 // set text of new cells to values from string array
114
page.setTextArtist(currentDisc.getArtist());
115                 page.setTextTitle(currentDisc.getTitle());
116                 page.setTextGenre(currentDisc.getGenre());
117                 
118                 if(currentDisc.isLiked()) {
119                     page.setTextLikeThisDisc("Yes");
120                 } else {
121                     page.setTextLikeThisDisc("Not");
122                 }
123                 
124             // Add a deep clone of the row to the DOM
125
Node clonedNode = templateRow.cloneNode(true);
126                 discTable.appendChild(clonedNode);
127                 // Alternative way to insert nodes below
128
// insertBefore(newNode, oldNode);
129
// discTable.insertBefore(clonedNode, templateRow);
130

131                 // Now populate the select list
132
// This algorithm is obscure because options are not normal
133
// HTML elements
134
// First populate the option value (the disc database ID).
135
// Then append a text child as the option
136
// text, which is what is displayed as the text
137
// in each row of the select box
138
HTMLOptionElement clonedOption = (HTMLOptionElement) templateOption.cloneNode(true);
139                 clonedOption.setValue(currentDisc.getHandle());
140                 Node optionTextNode = clonedOption.getOwnerDocument().
141                     createTextNode(currentDisc.getArtist() + ": " +
142                                    currentDisc.getTitle());
143                 clonedOption.appendChild(optionTextNode);
144                 // Do only a shallow copy of the option as we don't want the text child
145
// of the node option
146
discSelect.appendChild(clonedOption);
147                 // Alternative way to insert nodes below
148
// insertBefore(newNode, oldNode);
149
// discSelect.insertBefore(clonedOption, templateOption);
150
}
151         } catch(Exception JavaDoc ex) {
152             this.writeDebugMsg("Error populating disc catalog: " + ex);
153             throw new DiscRackPresentationException("Error getting discs for user: ", ex);
154         }
155         
156         // Finally remove the template row and template select option
157
// from the page
158
templateRow.getParentNode().removeChild(templateRow);
159         templateOption.getParentNode().removeChild(templateOption);
160         
161         // write out HTML
162
return page;
163     }
164 }
165
166
Popular Tags