KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > discRack > 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.1 2004/08/16 09:39:21 slobodan Exp $
22  */

23
24 package discRack.presentation.discMgmt;
25
26 import discRack.spec.*;
27 import discRack.presentation.BasePO;
28 import discRack.presentation.DiscRackPresentationException;
29
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  */

43 public class DiscCatalog extends BasePO {
44
45     /**
46      * Superclass method override
47      */

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

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

62      DiscCatalogHTML page = (DiscCatalogHTML)myComms.xmlcFactory.create(DiscCatalogHTML.class);
63      HTMLScriptElement script = ((DiscCatalogScriptHTML)myComms.xmlcFactory.create(DiscCatalogScriptHTML.class)).getElementRealScript();
64 // HTMLScriptElement script = new DiscCatalogScriptHTML().getElementRealScript();
65
XMLCUtil.replaceNode(script, page.getElementDummyScript());
66     
67      
68  /*
69  * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run discRack_pres )
70  * We need to allow discRack_pres to be functional , response
71  * will be default HTML page
72  */

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

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