KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > transactionsDiscRack > 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 10:48:52 slobodan Exp $
22  */

23
24 package transactionsDiscRack.presentation.discMgmt;
25
26 import transactionsDiscRack.spec.*;
27 import transactionsDiscRack.presentation.BasePO;
28 import transactionsDiscRack.presentation.TransactionsDiscRackPresentationException;
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 TransactionsDiscRackPresentationException {
60         // Swap in our real run-time JavaScript to repalce the storyboard JavaScript
61

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

73
74 try {
75     
76       Disc[] discList = ((DiscGenerator)DiscGeneratorFactory.getDiscGenerator("transactionsDiscRack.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         // Generate the person's Disc list and create the HTML template references
93
HTMLTableRowElement templateRow = page.getElementTemplateRow();
94         HTMLOptionElement templateOption = page.getElementTemplateOption();
95         Node discTable = templateRow.getParentNode();
96         Node discSelect = templateOption.getParentNode();
97         
98         // Remove ids to prevent duplicates
99
templateRow.removeAttribute("id");
100         templateOption.removeAttribute("id");
101         
102         // Remove id attributes from the cells in the template row
103
HTMLElement artistCellTemplate = page.getElementArtist();
104         HTMLElement titleCellTemplate = page.getElementTitle();
105         HTMLElement genreCellTemplate = page.getElementGenre();
106         HTMLElement likeThisDisc = page.getElementLikeThisDisc();
107         
108         artistCellTemplate.removeAttribute("id");
109         titleCellTemplate.removeAttribute("id");
110         genreCellTemplate.removeAttribute("id");
111         likeThisDisc.removeAttribute("id");
112         
113         // Remove the dummy storyboard text from the prototype HTML
114
templateOption.removeChild(templateOption.getFirstChild());
115     
116                
117             // Get collection of Discs and loop through collection
118
// to add each disc as a row in the table.
119
for(int numDiscs = 0; numDiscs < discList.length; numDiscs++) {
120                 Disc currentDisc = discList[numDiscs];
121                 // set text of new cells to values from string array
122
page.setTextArtist(currentDisc.getArtist());
123                 page.setTextTitle(currentDisc.getTitle());
124                 page.setTextGenre(currentDisc.getGenre());
125                 
126                 if(currentDisc.isLiked()) {
127                     page.setTextLikeThisDisc("Yes");
128                 } else {
129                     page.setTextLikeThisDisc("Not");
130                 }
131                 
132             // Add a deep clone of the row to the DOM
133
Node clonedNode = templateRow.cloneNode(true);
134                 discTable.appendChild(clonedNode);
135                 // Alternative way to insert nodes below
136
// insertBefore(newNode, oldNode);
137
// discTable.insertBefore(clonedNode, templateRow);
138

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