KickJava   Java API By Example, From Geeks To Geeks.

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


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: Edit.java,v 1.4 2004/12/03 14:12:34 slobodan Exp $
22  */

23
24 package barracudaDiscRack.presentation.discMgmt;
25
26 import barracudaDiscRack.presentation.BasePO;
27 import barracudaDiscRack.business.person.*;
28 import barracudaDiscRack.business.disc.*;
29 import barracudaDiscRack.presentation.DiscRackPresentationException;
30 import barracudaDiscRack.business.DiscRackBusinessException;
31 import com.lutris.appserver.server.httpPresentation.*;
32 import com.lutris.appserver.server.session.*;
33 import com.lutris.util.*;
34 import org.enhydra.xml.xmlc.*;
35 import org.enhydra.xml.xmlc.html.*;
36 import org.w3c.dom.*;
37 import org.w3c.dom.html.*;
38 import org.enhydra.xml.xmlc.XMLObject;
39
40 /**
41  * Edit class handles the disc management (add, edit or delete)
42  * of the DiscRack app
43  *
44  * @author
45  * @version
46  */

47 public class Edit extends BasePO {
48     
49     /**
50      * Constants representing HTTP parameters passed in from
51      * the form submission
52      */

53     private static String JavaDoc TITLE_NAME = "title";
54     private static String JavaDoc ARTIST_NAME = "artist";
55     private static String JavaDoc GENRE_NAME = "genre";
56     private static String JavaDoc DISC_ID = "discID";
57     private static String JavaDoc IS_LIKED = "like";
58     private static String JavaDoc INVALID_ID = "invalidID";
59     private static String JavaDoc EDIT_COMMAND = "edit";
60
61     /**
62      * Superclass method override
63      */

64     public boolean loggedInUserRequired() {
65         return true;
66     }
67
68     /**
69      * Default event. Just show the page populated with any disc
70      * parameters that were passed along.
71      *
72      * @return html document
73      * @exception HttpPresentationException
74      */

75     public XMLObject handleDefault()
76         throws HttpPresentationException {
77         return showEditPage(null);
78     }
79     
80     /**
81      * handle show add disc page event.
82      *
83      * @return html document
84      * @exception HttpPresentationException
85      */

86     public XMLObject handleShowAddPage()
87         throws HttpPresentationException {
88         return showAddPage(null);
89     }
90     
91     /*
92      * Edits an existing disc
93      *
94      * @return html document
95      * @exception HttpPresentationException
96      */

97     public XMLObject handleEdit()
98         throws HttpPresentationException {
99         String JavaDoc discID = this.getComms().request.getParameter(DISC_ID);
100         Disc disc = null;
101         
102         // Try to get the disc by its ID
103
try {
104             disc = DiscFactory.findDiscByID(discID);
105         } catch(Exception JavaDoc ex) {
106             this.getSessionData().setUserMessage("Please choose a valid disc to edit");
107             throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE);
108         }
109         
110         try {
111             saveDisc(disc);
112             throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE);
113         } catch(Exception JavaDoc ex) {
114             return showEditPage("You must fill out all fields to edit this disc");
115         }
116     }
117     
118     /*
119      * Adds a disc to the disc database
120      *
121      * @return html document
122      * @exception HttpPresentationException
123      */

124     public XMLObject handleAdd()
125         throws HttpPresentationException {
126         try {
127             Disc disc = new Disc();
128             saveDisc(disc);
129             throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE);
130         } catch(Exception JavaDoc ex) {
131             return showAddPage("You must fill out all fields to add this disc");
132         }
133     }
134     
135     /*
136      * Deletes a Disc from the Disc database
137      *
138      * @return html document
139      * @exception HttpPresentationException
140      */

141     public XMLObject handleDelete()
142         throws HttpPresentationException, DiscRackPresentationException {
143         String JavaDoc discID = this.getComms().request.getParameter(DISC_ID);
144         
145         try {
146             Disc disc = DiscFactory.findDiscByID(discID);
147             String JavaDoc title = disc.getTitle();
148             disc.delete();
149             this.getSessionData().setUserMessage("The disc, " + title +
150                                                  ", was deleted");
151             // Catch any possible database exception as well as the null pointer
152
// exception if the disc is not found and is null after findDiscByID
153
} catch(Exception JavaDoc ex) {
154             this.getSessionData().setUserMessage("Please choose a valid disc to delete");
155         }
156         // Redirect to the catalog page which will display the error message,
157
// if there was one set by the above exception
158
throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE);
159     }
160     
161     /**
162      * Produce HTML for this PO, populated by the disc information
163      * that the user wants to edit
164      *
165      * @param errorMsg, the error messages
166      * @return html document
167      * @exception HttpPresentationException
168      */

169     public XMLObject showEditPage(String JavaDoc errorMsg)
170         throws HttpPresentationException, DiscRackPresentationException {
171         String JavaDoc title = this.getComms().request.getParameter(TITLE_NAME);
172         String JavaDoc artist = this.getComms().request.getParameter(ARTIST_NAME);
173         String JavaDoc genre = this.getComms().request.getParameter(GENRE_NAME);
174         String JavaDoc discID = this.getComms().request.getParameter(DISC_ID);
175         // Instantiate the page object
176
EditHTML page = (EditHTML)myComms.xmlcFactory.create(EditHTML.class);
177
178         Disc disc = null;
179         
180         try {
181             disc = DiscFactory.findDiscByID(discID);
182             // Catch any possible database exception in findDiscByID()
183
} catch(DiscRackBusinessException ex) {
184             this.getSessionData().setUserMessage("Please choose a valid disc to edit");
185             throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE);
186         }
187         
188         try {
189             // If we received a valid discID then try to show the disc's contents,
190
// otherwise try to use the HTML input parameters
191
page.getElementDiscID().setValue(disc.getHandle());
192             
193             if(null != title && title.length() != 0) {
194                 page.getElementTitle().setValue(title);
195             } else {
196                 page.getElementTitle().setValue(disc.getTitle());
197             }
198             
199             if(null != artist && artist.length() != 0) {
200                 page.getElementArtist().setValue(artist);
201             } else {
202                 page.getElementArtist().setValue(disc.getArtist());
203             }
204             
205             if(null != genre && genre.length() != 0) {
206                 page.getElementGenre().setValue(genre);
207             } else {
208                 page.getElementGenre().setValue(disc.getGenre());
209             }
210             
211             if(null != this.getComms().request.getParameter(IS_LIKED)) {
212                 page.getElementLikeBox().setChecked(true);
213             } else {
214                 page.getElementLikeBox().setChecked(disc.isLiked());
215             }
216             
217             if(null == errorMsg) {
218                 page.getElementErrorText().getParentNode().removeChild(page.getElementErrorText());
219             } else {
220                 page.setTextErrorText(errorMsg);
221             }
222         } catch(DiscRackBusinessException ex) {
223             throw new DiscRackPresentationException("Error populating page for disc editing", ex);
224         }
225         
226         page.getElementEventValue().setValue(EDIT_COMMAND);
227         return page;
228     }
229     
230     /**
231      * Produce HTML for this PO
232      *
233      * @param errorMsg, the error messages
234      * @return html document
235      * @exception HttpPresentationException
236      */

237     public XMLObject showAddPage(String JavaDoc errorMsg)
238         throws HttpPresentationException, DiscRackPresentationException {
239         String JavaDoc title = this.getComms().request.getParameter(TITLE_NAME);
240         String JavaDoc artist = this.getComms().request.getParameter(ARTIST_NAME);
241         String JavaDoc genre = this.getComms().request.getParameter(GENRE_NAME);
242         String JavaDoc discID = this.getComms().request.getParameter(DISC_ID);
243         // Instantiate the page object
244
EditHTML page = (EditHTML)myComms.xmlcFactory.create(EditHTML.class);
245         
246         if(null != this.getComms().request.getParameter(TITLE_NAME)) {
247             page.getElementTitle().setValue(this.getComms().request.getParameter(TITLE_NAME));
248         }
249         if(null != this.getComms().request.getParameter(ARTIST_NAME)) {
250             page.getElementArtist().setValue(this.getComms().request.getParameter(ARTIST_NAME));
251         }
252         if(null != this.getComms().request.getParameter(GENRE_NAME)) {
253             page.getElementGenre().setValue(this.getComms().request.getParameter(GENRE_NAME));
254         }
255         if(null != this.getComms().request.getParameter(IS_LIKED)) {
256             page.getElementLikeBox().setChecked(true);
257         } else {
258             page.getElementLikeBox().setChecked(false);
259         }
260         
261         if(null == errorMsg) {
262             page.getElementErrorText().getParentNode().removeChild(page.getElementErrorText());
263         } else {
264             page.setTextErrorText(errorMsg);
265         }
266         
267         return page;
268     }
269     
270     /**
271      * Method to save a new or existing disc to the database
272      *
273      * @param disc, the disc to be saved
274      * @return html document
275      * @exception HttpPresentationException
276      */

277     protected void saveDisc(Disc theDisc)
278         throws HttpPresentationException {
279         String JavaDoc title = this.getComms().request.getParameter(TITLE_NAME);
280         String JavaDoc artist = this.getComms().request.getParameter(ARTIST_NAME);
281         String JavaDoc genre = this.getComms().request.getParameter(GENRE_NAME);
282         
283         try {
284             theDisc.setTitle(title);
285             theDisc.setArtist(artist);
286             theDisc.setGenre(genre);
287             
288             if(null != this.getComms().request.getParameter(IS_LIKED)) {
289                 theDisc.setLiked(true);
290             } else {
291                 theDisc.setLiked(false);
292             }
293             theDisc.setOwner(this.getUser());
294             theDisc.save();
295         } catch(Exception JavaDoc ex) {
296             throw new DiscRackPresentationException("Error adding disc", ex);
297         }
298     }
299 }
300
301
302
303
304
305
306
Popular Tags