001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: Edit.java,v 1.1 2006-09-11 12:32:06 sinisa Exp $
022: */
023:
024: package discRack.presentation.discMgmt;
025:
026: import discRack.presentation.BasePO;
027: import discRack.presentation.DiscRackPresentationException;
028: import discRack.spec.*;
029:
030: import com.lutris.appserver.server.httpPresentation.*;
031: import com.lutris.appserver.server.session.*;
032: import com.lutris.util.*; //import com.lutris.xml.xmlc.*;
033: //import com.lutris.xml.xmlc.html.*;
034: import org.w3c.dom.*;
035: import org.w3c.dom.html.*;
036: import org.enhydra.xml.xmlc.XMLObject;
037: import org.enhydra.dods.exceptions.AssertionDataObjectException;
038:
039: /**
040: * Edit class handles the disc management (add, edit or delete)
041: * of the DiscRack app
042: *
043: */
044: public class Edit extends BasePO {
045:
046: /**
047: * Constants representing HTTP parameters passed in from
048: * the form submission
049: */
050: private static String TITLE_NAME = "title";
051: private static String ARTIST_NAME = "artist";
052: private static String GENRE_NAME = "genre";
053: private static String DISC_ID = "discID";
054: private static String IS_LIKED = "like";
055: private static String INVALID_ID = "invalidID";
056: private static String EDIT_COMMAND = "edit";
057:
058: /**
059: * Superclass method override
060: */
061: public boolean loggedInUserRequired() {
062: return true;
063: }
064:
065: /**
066: * Default event. Just show the page populated with any disc
067: * parameters that were passed along.
068: *
069: * @return html document
070: * @exception HttpPresentationException
071: */
072: public XMLObject handleDefault() throws HttpPresentationException {
073: return showEditPage(null);
074: }
075:
076: /**
077: * handle show add disc page event.
078: *
079: * @return html document
080: * @exception HttpPresentationException
081: */
082: public XMLObject handleShowAddPage()
083: throws HttpPresentationException {
084: return showAddPage(null);
085: }
086:
087: /*
088: * Edits an existing disc
089: *
090: * @return html document
091: * @exception HttpPresentationException
092: */
093: public XMLObject handleEdit() throws HttpPresentationException {
094: String discID = this .getComms().request.getParameter(DISC_ID);
095: Disc disc = null;
096:
097: // Try to get the disc by its ID
098:
099: try {
100:
101: disc = ((DiscGenerator) DiscGeneratorFactory
102: .getDiscGenerator("discRack.business.disc.DiscGeneratorImpl"))
103: .findDiscByID(discID);
104:
105: } catch (Exception ex) {
106: this .getSessionData().setUserMessage(
107: "Please choose a valid disc to edit");
108: throw new ClientPageRedirectException(getComms().request
109: .getApplicationPath()
110: + DISC_CATALOG_PAGE);
111: }
112:
113: try {
114: saveDisc(disc);
115: throw new ClientPageRedirectException(getComms().request
116: .getApplicationPath()
117: + DISC_CATALOG_PAGE);
118: } catch (Exception 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() throws HttpPresentationException {
130: try {
131:
132: Disc disc = DiscFactory
133: .getDisc("discRack.business.disc.DiscImpl");
134: saveDisc(disc);
135:
136: throw new ClientPageRedirectException(getComms().request
137: .getApplicationPath()
138: + DISC_CATALOG_PAGE);
139:
140: /*
141: * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run discRack_pres )
142: * We need to allow discRack_pres to be functional , response
143: * will be default HTML page
144: */
145:
146: } catch (NullPointerException ex) {
147: return showAddPage("You cannot add disc when you in DiscRack_pres");
148: } catch (AssertionDataObjectException ex) {
149: return showAddPage("Dics table is read-only: no DML operations allowed");
150: } catch (Exception ex) {
151: return showAddPage("You must fill out all fields to add this disc");
152: }
153: }
154:
155: /*
156: * Deletes a Disc from the Disc database
157: *
158: * @return html document
159: * @exception HttpPresentationException
160: */
161: public XMLObject handleDelete() throws HttpPresentationException,
162: DiscRackPresentationException, AssertionDataObjectException {
163: String discID = this .getComms().request.getParameter(DISC_ID);
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
172: .getDiscGenerator("discRack.business.disc.DiscGeneratorImpl"))
173: .findDiscByID(discID);
174:
175: String title = disc.getTitle();
176: disc.delete();
177: this .getSessionData().setUserMessage(
178: "The disc, " + title + ", was deleted");
179:
180: } catch (NullPointerException e) {
181:
182: // Catch any possible database exception as well as the null pointer
183: // exception if the disc is not found and is null after findDiscByID
184: } catch (AssertionDataObjectException ex) {
185: this .getSessionData().setUserMessage("Read-only table!");
186: } catch (Exception ex) {
187: this .getSessionData().setUserMessage(
188: "Please choose a valid disc to delete");
189: }
190: // Redirect to the catalog page which will display the error message,
191: // if there was one set by the above exception
192: throw new ClientPageRedirectException(getComms().request
193: .getApplicationPath()
194: + 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 errorMsg)
206: throws HttpPresentationException,
207: DiscRackPresentationException {
208: String title = this .getComms().request.getParameter(TITLE_NAME);
209: String artist = this .getComms().request
210: .getParameter(ARTIST_NAME);
211: String genre = this .getComms().request.getParameter(GENRE_NAME);
212: String discID = this .getComms().request.getParameter(DISC_ID);
213: // Instantiate the page object
214: EditHTML page = (EditHTML) myComms.xmlcFactory
215: .create(EditHTML.class);
216:
217: Disc disc = null;
218:
219: /*
220: * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run discRack_pres )
221: * We need to allow discRack_pres to be functional , response
222: * will be default HTML page with message
223: */
224: try {
225:
226: disc = ((DiscGenerator) DiscGeneratorFactory
227: .getDiscGenerator("discRack.business.disc.DiscGeneratorImpl"))
228: .findDiscByID(discID);
229:
230: } catch (NullPointerException e) {
231: page
232: .setTextErrorText("You cannot edit disc when you are in DiscRack_pres");
233: return page;
234:
235: // Catch any possible database exception in findDiscByID()
236: } catch (DiscRackException ex) {
237: this .getSessionData().setUserMessage(
238: "Please choose a valid disc to edit");
239: throw new ClientPageRedirectException(getComms().request
240: .getApplicationPath()
241: + DISC_CATALOG_PAGE);
242: }
243:
244: try {
245: // If we received a valid discID then try to show the disc's contents,
246: // otherwise try to use the HTML input parameters
247: page.getElementDiscID().setValue(disc.getHandle());
248:
249: if (null != title && title.length() != 0) {
250: page.getElementTitle().setValue(title);
251: } else {
252: page.getElementTitle().setValue(disc.getTitle());
253: }
254:
255: if (null != artist && artist.length() != 0) {
256: page.getElementArtist().setValue(artist);
257: } else {
258: page.getElementArtist().setValue(disc.getArtist());
259: }
260:
261: if (null != genre && genre.length() != 0) {
262: page.getElementGenre().setValue(genre);
263: } else {
264: page.getElementGenre().setValue(disc.getGenre());
265: }
266:
267: if (null != this .getComms().request.getParameter(IS_LIKED)) {
268: page.getElementLikeBox().setChecked(true);
269: } else {
270: page.getElementLikeBox().setChecked(disc.isLiked());
271: }
272:
273: if (null == errorMsg) {
274: page.getElementErrorText().getParentNode().removeChild(
275: page.getElementErrorText());
276: } else {
277: page.setTextErrorText(errorMsg);
278: }
279: } catch (NullPointerException ex) {
280: return showAddPage(errorMsg);
281:
282: } catch (DiscRackException ex) {
283: throw new DiscRackPresentationException(
284: "Error populating page for disc editing", ex);
285: }
286:
287: page.getElementEventValue().setValue(EDIT_COMMAND);
288: return page;
289: }
290:
291: /**
292: * Produce HTML for this PO
293: *
294: * @param errorMsg the error messages
295: * @return html document
296: * @exception HttpPresentationException
297: */
298: public XMLObject showAddPage(String errorMsg)
299: throws HttpPresentationException,
300: DiscRackPresentationException {
301: String title = this .getComms().request.getParameter(TITLE_NAME);
302: String artist = this .getComms().request
303: .getParameter(ARTIST_NAME);
304: String genre = this .getComms().request.getParameter(GENRE_NAME);
305: String discID = this .getComms().request.getParameter(DISC_ID);
306: // Instantiate the page object
307:
308: EditHTML page = (EditHTML) myComms.xmlcFactory
309: .create(EditHTML.class);
310: // EditHTML page = new EditHTML();
311:
312: if (null != this .getComms().request.getParameter(TITLE_NAME)) {
313: page.getElementTitle().setValue(
314: this .getComms().request.getParameter(TITLE_NAME));
315: }
316: if (null != this .getComms().request.getParameter(ARTIST_NAME)) {
317: page.getElementArtist().setValue(
318: this .getComms().request.getParameter(ARTIST_NAME));
319: }
320: if (null != this .getComms().request.getParameter(GENRE_NAME)) {
321: page.getElementGenre().setValue(
322: this .getComms().request.getParameter(GENRE_NAME));
323: }
324: if (null != this .getComms().request.getParameter(IS_LIKED)) {
325: page.getElementLikeBox().setChecked(true);
326: } else {
327: page.getElementLikeBox().setChecked(false);
328: }
329:
330: if (null == errorMsg) {
331: page.getElementErrorText().getParentNode().removeChild(
332: page.getElementErrorText());
333: } else {
334: page.setTextErrorText(errorMsg);
335: }
336:
337: return page;
338: }
339:
340: /**
341: * Method to save a new or existing disc to the database
342: *
343: * @param disc the disc to be saved
344: * @return html document
345: * @exception HttpPresentationException
346: */
347: protected void saveDisc(Disc disc)
348: throws HttpPresentationException,
349: AssertionDataObjectException, NullPointerException,
350: Exception {
351:
352: String title = this .getComms().request.getParameter(TITLE_NAME);
353: String artist = this .getComms().request
354: .getParameter(ARTIST_NAME);
355: String genre = this .getComms().request.getParameter(GENRE_NAME);
356:
357: if (title.length() == 0 || artist.length() == 0
358: || genre.length() == 0) {
359: throw new Exception();
360: }
361:
362: try {
363: disc.setTitle(title);
364: disc.setArtist(artist);
365: disc.setGenre(genre);
366:
367: if (null != this .getComms().request.getParameter(IS_LIKED)) {
368: disc.setLiked(true);
369: } else {
370: disc.setLiked(false);
371: }
372:
373: disc.setOwner(this .getUser());
374: disc.save();
375:
376: } catch (NullPointerException ex) {
377: throw new NullPointerException();
378: } catch (AssertionDataObjectException ex) {
379: throw new AssertionDataObjectException(
380: "Error adding disc: read-only table.", ex);
381: } catch (Exception ex) {
382: throw new DiscRackPresentationException(
383: "Error adding disc", ex);
384: }
385: }
386: }
|