KickJava   Java API By Example, From Geeks To Geeks.

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

23
24 package transactionsDiscRack.presentation.discMgmt;
25
26 import transactionsDiscRack.presentation.BasePO;
27 import transactionsDiscRack.presentation.TransactionsDiscRackPresentationException;
28 import transactionsDiscRack.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         try {
104       
105     disc = ((DiscGenerator)DiscGeneratorFactory.getDiscGenerator("transactionsDiscRack.business.disc.DiscGeneratorImpl")).findDiscByID(discID);
106        
107        
108         } catch(Exception JavaDoc ex) {
109             this.getSessionData().setUserMessage("Please choose a valid disc to edit");
110         if (disc != null)
111         disc.dbtRelease();
112             throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE);
113         }
114
115         try {
116             saveDisc(disc);
117             throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE);
118         } catch(Exception JavaDoc ex) {
119             return showEditPage("You must fill out all fields to edit this disc");
120         }
121     }
122
123     /*
124      * Adds a disc to the disc database
125      *
126      * @return html document
127      * @exception HttpPresentationException
128      */

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

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

165     public XMLObject handleDelete()
166         throws HttpPresentationException, TransactionsDiscRackPresentationException, AssertionDataObjectException {
167         String JavaDoc discID = this.getComms().request.getParameter(DISC_ID);
168         Disc disc = null;
169 /*
170  * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run transactionsDiscRack_pres )
171  * We need to allow transactionsDiscRack_pres to be functional
172  */

173         try {
174             
175        disc = ((DiscGenerator)DiscGeneratorFactory.getDiscGenerator("transactionsDiscRack.business.disc.DiscGeneratorImpl")).findDiscByID(discID);
176             
177             String JavaDoc title = disc.getTitle();
178             disc.delete();
179             this.getSessionData().setUserMessage("The disc, " + title +
180                                                  ", was deleted");
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
if (disc != null)
195         disc.dbtRelease();
196         throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE);
197     }
198
199     /**
200      * Produce HTML for this PO, populated by the disc information
201      * that the user wants to edit
202      *
203      * @param errorMsg the error messages
204      * @return html document
205      * @exception HttpPresentationException
206      */

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

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

294     public XMLObject showAddPage(String JavaDoc errorMsg)
295         throws HttpPresentationException, TransactionsDiscRackPresentationException {
296         String JavaDoc title = this.getComms().request.getParameter(TITLE_NAME);
297         String JavaDoc artist = this.getComms().request.getParameter(ARTIST_NAME);
298         String JavaDoc genre = this.getComms().request.getParameter(GENRE_NAME);
299         String JavaDoc discID = this.getComms().request.getParameter(DISC_ID);
300         // Instantiate the page object
301

302        
303         EditHTML page = (EditHTML)myComms.xmlcFactory.create(EditHTML.class);
304 // EditHTML page = new EditHTML();
305

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

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