KickJava   Java API By Example, From Geeks To Geeks.

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

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

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

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

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

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

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

96     public XMLObject handleEdit()
97         throws HttpPresentationException {
98         String JavaDoc discID = this.getComms().request.getParameter(DISC_ID);
99         Disc disc = null;
100
101         // Try to get the disc by its ID
102

103  
104         try {
105        
106     disc = ((DiscGenerator)DiscGeneratorFactory.getDiscGenerator("discRack.business.disc.DiscGeneratorImpl")).findDiscByID(discID);
107        
108         } catch(Exception JavaDoc ex) {
109             this.getSessionData().setUserMessage("Please choose a valid disc to edit");
110             throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE);
111         }
112
113         try {
114             saveDisc(disc);
115             throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE);
116         } catch(Exception JavaDoc ex) {
117             return showEditPage("You must fill out all fields to edit this disc");
118         }
119     }
120
121     /*
122      * Adds a disc to the disc database
123      *
124      * @return html document
125      * @exception HttpPresentationException
126      */

127     public XMLObject handleAdd()
128         throws HttpPresentationException {
129         try {
130         
131            
132          Disc disc= DiscFactory.getDisc("discRack.business.disc.DiscImpl");
133          saveDisc(disc);
134           
135          throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE);
136         
137 /*
138  * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run discRack_pres )
139  * We need to allow discRack_pres to be functional , response
140  * will be default HTML page
141  */

142        
143         }catch(NullPointerException JavaDoc ex) {
144             return showAddPage("You cannot add disc when you in DiscRack_pres");
145         }
146         catch(AssertionDataObjectException ex) {
147             return showAddPage("Dics table is read-only: no DML operations allowed");
148         }
149         catch(Exception JavaDoc ex) {
150             return showAddPage("You must fill out all fields to add this disc");
151         }
152     }
153
154     /*
155      * Deletes a Disc from the Disc database
156      *
157      * @return html document
158      * @exception HttpPresentationException
159      */

160     public XMLObject handleDelete()
161         throws HttpPresentationException, DiscRackPresentationException, AssertionDataObjectException {
162         String JavaDoc discID = this.getComms().request.getParameter(DISC_ID);
163
164
165 /*
166  * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run discRack_pres )
167  * We need to allow discRack_pres to be functional
168  */

169 try {
170             
171         Disc disc = ((DiscGenerator)DiscGeneratorFactory.getDiscGenerator("discRack.business.disc.DiscGeneratorImpl")).findDiscByID(discID);
172   
173         
174                    
175             String JavaDoc title = disc.getTitle();
176             disc.delete();
177             this.getSessionData().setUserMessage("The disc, " + title +
178                                                  ", was deleted");
179             
180         
181         }catch(NullPointerException JavaDoc e){
182                     
183         // Catch any possible database exception as well as the null pointer
184
// exception if the disc is not found and is null after findDiscByID
185
}
186         catch (AssertionDataObjectException ex) {
187             this.getSessionData().setUserMessage("Read-only table!");
188         }
189         catch(Exception JavaDoc ex) {
190             this.getSessionData().setUserMessage("Please choose a valid disc to delete");
191         }
192         // Redirect to the catalog page which will display the error message,
193
// if there was one set by the above exception
194
throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE);
195     }
196
197     /**
198      * Produce HTML for this PO, populated by the disc information
199      * that the user wants to edit
200      *
201      * @param errorMsg the error messages
202      * @return html document
203      * @exception HttpPresentationException
204      */

205     public XMLObject showEditPage(String JavaDoc errorMsg)
206         throws HttpPresentationException, DiscRackPresentationException {
207         String JavaDoc title = this.getComms().request.getParameter(TITLE_NAME);
208         String JavaDoc artist = this.getComms().request.getParameter(ARTIST_NAME);
209         String JavaDoc genre = this.getComms().request.getParameter(GENRE_NAME);
210         String JavaDoc discID = this.getComms().request.getParameter(DISC_ID);
211         // Instantiate the page object
212
EditHTML page = (EditHTML)myComms.xmlcFactory.create(EditHTML.class);
213
214         Disc disc = null;
215
216 /*
217  * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run discRack_pres )
218  * We need to allow discRack_pres to be functional , response
219  * will be default HTML page with message
220  */

221 try {
222             
223        disc = ((DiscGenerator)DiscGeneratorFactory.getDiscGenerator("discRack.business.disc.DiscGeneratorImpl")).findDiscByID(discID);
224          
225             
226         }catch(NullPointerException JavaDoc e){
227           page.setTextErrorText("You cannot edit disc when you are in DiscRack_pres");
228            return page;
229        
230        // Catch any possible database exception in findDiscByID()
231
} catch(DiscRackException ex) {
232             this.getSessionData().setUserMessage("Please choose a valid disc to edit");
233             throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE);
234         }
235
236         try {
237             // If we received a valid discID then try to show the disc's contents,
238
// otherwise try to use the HTML input parameters
239
page.getElementDiscID().setValue(disc.getHandle());
240
241             if(null != title && title.length() != 0) {
242                 page.getElementTitle().setValue(title);
243             } else {
244                 page.getElementTitle().setValue(disc.getTitle());
245             }
246
247             if(null != artist && artist.length() != 0) {
248                 page.getElementArtist().setValue(artist);
249             } else {
250                 page.getElementArtist().setValue(disc.getArtist());
251             }
252
253             if(null != genre && genre.length() != 0) {
254                 page.getElementGenre().setValue(genre);
255             } else {
256                 page.getElementGenre().setValue(disc.getGenre());
257             }
258
259             if(null != this.getComms().request.getParameter(IS_LIKED)) {
260                 page.getElementLikeBox().setChecked(true);
261             } else {
262                 page.getElementLikeBox().setChecked(disc.isLiked());
263             }
264
265             if(null == errorMsg) {
266                 page.getElementErrorText().getParentNode().removeChild(page.getElementErrorText());
267             } else {
268                 page.setTextErrorText(errorMsg);
269             }
270         }catch(NullPointerException JavaDoc ex) {
271             return showAddPage(errorMsg);
272       
273         } catch(DiscRackException ex) {
274             throw new DiscRackPresentationException("Error populating page for disc editing", ex);
275         }
276
277         page.getElementEventValue().setValue(EDIT_COMMAND);
278         return page;
279     }
280
281     /**
282      * Produce HTML for this PO
283      *
284      * @param errorMsg the error messages
285      * @return html document
286      * @exception HttpPresentationException
287      */

288     public XMLObject showAddPage(String JavaDoc errorMsg)
289         throws HttpPresentationException, DiscRackPresentationException {
290         String JavaDoc title = this.getComms().request.getParameter(TITLE_NAME);
291         String JavaDoc artist = this.getComms().request.getParameter(ARTIST_NAME);
292         String JavaDoc genre = this.getComms().request.getParameter(GENRE_NAME);
293         String JavaDoc discID = this.getComms().request.getParameter(DISC_ID);
294         // Instantiate the page object
295

296        
297         EditHTML page = (EditHTML)myComms.xmlcFactory.create(EditHTML.class);
298 // EditHTML page = new EditHTML();
299

300         if(null != this.getComms().request.getParameter(TITLE_NAME)) {
301             page.getElementTitle().setValue(this.getComms().request.getParameter(TITLE_NAME));
302         }
303         if(null != this.getComms().request.getParameter(ARTIST_NAME)) {
304             page.getElementArtist().setValue(this.getComms().request.getParameter(ARTIST_NAME));
305         }
306         if(null != this.getComms().request.getParameter(GENRE_NAME)) {
307             page.getElementGenre().setValue(this.getComms().request.getParameter(GENRE_NAME));
308         }
309         if(null != this.getComms().request.getParameter(IS_LIKED)) {
310             page.getElementLikeBox().setChecked(true);
311         } else {
312             page.getElementLikeBox().setChecked(false);
313         }
314
315         if(null == errorMsg) {
316             page.getElementErrorText().getParentNode().removeChild(page.getElementErrorText());
317         } else {
318             page.setTextErrorText(errorMsg);
319         }
320
321         return page;
322     }
323
324     /**
325      * Method to save a new or existing disc to the database
326      *
327      * @param disc the disc to be saved
328      * @return html document
329      * @exception HttpPresentationException
330      */

331     protected void saveDisc(Disc disc)
332         throws HttpPresentationException, AssertionDataObjectException,NullPointerException JavaDoc,Exception JavaDoc{
333        
334         String JavaDoc title = this.getComms().request.getParameter(TITLE_NAME);
335         String JavaDoc artist = this.getComms().request.getParameter(ARTIST_NAME);
336         String JavaDoc genre = this.getComms().request.getParameter(GENRE_NAME);
337             
338     if (title.length() == 0 || artist.length() ==0 ||
339             genre.length() == 0) {
340             throw new Exception JavaDoc();
341         }
342     
343     try {
344             disc.setTitle(title);
345             disc.setArtist(artist);
346             disc.setGenre(genre);
347     
348            
349             if(null != this.getComms().request.getParameter(IS_LIKED))
350             {
351                 disc.setLiked(true);
352             } else {
353                 disc.setLiked(false);
354             }
355       
356             disc.setOwner(this.getUser());
357             disc.save();
358         
359         }catch(NullPointerException JavaDoc ex) {
360           throw new NullPointerException JavaDoc();
361         }
362         catch (AssertionDataObjectException ex) {
363             throw new AssertionDataObjectException("Error adding disc: read-only table.", ex);
364         }
365         catch(Exception JavaDoc ex) {
366             throw new DiscRackPresentationException("Error adding disc", ex);
367         }
368     }
369 }
370
371
372
373
374
375
376
Popular Tags