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:47:00 sinisa Exp $
022: */
023:
024: package transactionsDiscRack.presentation.discMgmt;
025:
026: import transactionsDiscRack.presentation.BasePO;
027: import transactionsDiscRack.presentation.TransactionsDiscRackPresentationException;
028: import transactionsDiscRack.spec.*;
029:
030: import com.lutris.appserver.server.httpPresentation.*;
031: import org.enhydra.xml.xmlc.XMLObject;
032: import org.enhydra.dods.exceptions.AssertionDataObjectException;
033:
034: /**
035: * Edit class handles the disc management (add, edit or delete) of the DiscRack
036: * app
037: *
038: */
039: public class Edit extends BasePO {
040:
041: /**
042: * Constants representing HTTP parameters passed in from the form submission
043: */
044: private static String TITLE_NAME = "title";
045:
046: private static String ARTIST_NAME = "artist";
047:
048: private static String GENRE_NAME = "genre";
049:
050: private static String DISC_ID = "discID";
051:
052: private static String IS_LIKED = "like";
053:
054: private static String EDIT_COMMAND = "edit";
055:
056: /**
057: * Superclass method override
058: */
059: public boolean loggedInUserRequired() {
060: return true;
061: }
062:
063: /**
064: * Default event. Just show the page populated with any disc parameters that
065: * were passed along.
066: *
067: * @return html document
068: * @exception HttpPresentationException
069: */
070: public XMLObject handleDefault() throws HttpPresentationException {
071: return showEditPage(null);
072: }
073:
074: /**
075: * handle show add disc page event.
076: *
077: * @return html document
078: * @exception HttpPresentationException
079: */
080: public XMLObject handleShowAddPage()
081: throws HttpPresentationException {
082: return showAddPage(null);
083: }
084:
085: /*
086: * Edits an existing disc
087: *
088: * @return html document @exception HttpPresentationException
089: */
090: public XMLObject handleEdit() throws HttpPresentationException {
091: String discID = this .getComms().request.getParameter(DISC_ID);
092: Disc disc = null;
093:
094: // Try to get the disc by its ID
095:
096: try {
097: disc = ((DiscGenerator) DiscGeneratorFactory
098: .getDiscGenerator("transactionsDiscRack.business.disc.DiscGeneratorImpl"))
099: .findDiscByID(discID, transaction);
100: } catch (Exception ex) {
101: this .getSessionData().setUserMessage(
102: "Please choose a valid disc to edit");
103: throw new ClientPageRedirectException(getComms().request
104: .getApplicationPath()
105: + DISC_CATALOG_PAGE);
106: }
107:
108: try {
109: saveDisc(disc);
110: throw new ClientPageRedirectException(getComms().request
111: .getApplicationPath()
112: + DISC_CATALOG_PAGE);
113: } catch (Exception 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 @exception HttpPresentationException
122: */
123: public XMLObject handleAdd() throws HttpPresentationException {
124:
125: Disc disc = null;
126: try {
127:
128: disc = DiscFactory
129: .getDisc("transactionsDiscRack.business.disc.DiscImpl");
130: saveDisc(disc);
131:
132: throw new ClientPageRedirectException(getComms().request
133: .getApplicationPath()
134: + DISC_CATALOG_PAGE);
135: /*
136: * Catch Null pointer exception ( we canot make a instances of
137: * classes from business layer when we run transactionsDiscRack_pres )
138: * We need to allow transactionsDiscRack_pres to be functional ,
139: * response will be default HTML page with message
140: */
141:
142: } catch (NullPointerException ex) {
143: return showAddPage("You cannot add disc when you in transactionsDiscRack_pres");
144: } catch (AssertionDataObjectException ex) {
145: return showAddPage("Dics table is read-only: no DML operations allowed");
146: } catch (Exception ex) {
147: return showAddPage("You must fill out all fields to add this disc");
148: }
149:
150: }
151:
152: /*
153: * Deletes a Disc from the Disc database
154: *
155: * @return html document @exception HttpPresentationException
156: */
157: public XMLObject handleDelete() throws HttpPresentationException,
158: TransactionsDiscRackPresentationException,
159: AssertionDataObjectException {
160: String discID = this .getComms().request.getParameter(DISC_ID);
161: Disc disc = null;
162: /*
163: * Catch Null pointer exception ( we canot make a instances of classes
164: * from business layer when we run transactionsDiscRack_pres ) We need
165: * to allow transactionsDiscRack_pres to be functional
166: */
167: try {
168:
169: disc = ((DiscGenerator) DiscGeneratorFactory
170: .getDiscGenerator("transactionsDiscRack.business.disc.DiscGeneratorImpl"))
171: .findDiscByID(discID, transaction);
172:
173: String title = disc.getTitle();
174: disc.delete(transaction);
175: this .getSessionData().setUserMessage(
176: "The disc, " + title + ", was deleted");
177: } catch (NullPointerException e) {
178:
179: // Catch any possible database exception as well as the null pointer
180: // exception if the disc is not found and is null after findDiscByID
181: } catch (AssertionDataObjectException ex) {
182: this .getSessionData().setUserMessage("Read-only table!");
183: } catch (Exception ex) {
184: this .getSessionData().setUserMessage(
185: "Please choose a valid disc to delete");
186: }
187: // Redirect to the catalog page which will display the error message,
188: // if there was one set by the above exception
189: throw new ClientPageRedirectException(getComms().request
190: .getApplicationPath()
191: + DISC_CATALOG_PAGE);
192: }
193:
194: /**
195: * Produce HTML for this PO, populated by the disc information that the user
196: * wants to edit
197: *
198: * @param errorMsg
199: * the error messages
200: * @return html document
201: * @exception HttpPresentationException
202: */
203: public XMLObject showEditPage(String errorMsg)
204: throws HttpPresentationException,
205: TransactionsDiscRackPresentationException {
206: String title = this .getComms().request.getParameter(TITLE_NAME);
207: String artist = this .getComms().request
208: .getParameter(ARTIST_NAME);
209: String genre = this .getComms().request.getParameter(GENRE_NAME);
210: String discID = this .getComms().request.getParameter(DISC_ID);
211: // Instantiate the page object
212: EditHTML page = (EditHTML) myComms.xmlcFactory
213: .create(EditHTML.class);
214:
215: Disc disc = null;
216:
217: /*
218: * Catch Null pointer exception ( we canot make a instances of classes
219: * from business layer when we run transactionsDiscRack_pres ) We need
220: * to allow transactionsDiscRack_pres to be functional , response will
221: * be default HTML page with message
222: */
223: try {
224: DiscGenerator discGenerator = DiscGeneratorFactory
225: .getDiscGenerator("transactionsDiscRack.business.disc.DiscGeneratorImpl");
226: disc = discGenerator.findDiscByID(discID, transaction);
227: } catch (TransactionsDiscRackException ex) {
228: this .getSessionData().setUserMessage(
229: "Please choose a valid disc to edit");
230: throw new ClientPageRedirectException(getComms().request
231: .getApplicationPath()
232: + DISC_CATALOG_PAGE);
233: } catch (Exception e) {
234: page
235: .setTextErrorText("You cannot edit disc when you are in presentation mode");
236: return page;
237: // Catch any possible database exception in findDiscByID()
238: }
239:
240: try {
241: // If we received a valid discID then try to show the disc's
242: // contents,
243: // otherwise try to use the HTML input parameters
244: page.getElementDiscID().setValue(disc.getHandle());
245:
246: if (null != title && title.length() != 0) {
247: page.getElementTitle().setValue(title);
248: } else {
249: page.getElementTitle().setValue(disc.getTitle());
250: }
251:
252: if (null != artist && artist.length() != 0) {
253: page.getElementArtist().setValue(artist);
254: } else {
255: page.getElementArtist().setValue(disc.getArtist());
256: }
257:
258: if (null != genre && genre.length() != 0) {
259: page.getElementGenre().setValue(genre);
260: } else {
261: page.getElementGenre().setValue(disc.getGenre());
262: }
263:
264: if (null != this .getComms().request.getParameter(IS_LIKED)) {
265: page.getElementLikeBox().setChecked(true);
266: } else {
267: page.getElementLikeBox().setChecked(disc.isLiked());
268: }
269:
270: if (null == errorMsg) {
271: page.getElementErrorText().getParentNode().removeChild(
272: page.getElementErrorText());
273: } else {
274: page.setTextErrorText(errorMsg);
275: }
276: } catch (NullPointerException ex) {
277: return showAddPage(errorMsg);
278:
279: }
280:
281: page.getElementEventValue().setValue(EDIT_COMMAND);
282: return page;
283: }
284:
285: /**
286: * Produce HTML for this PO
287: *
288: * @param errorMsg
289: * the error messages
290: * @return html document
291: * @exception HttpPresentationException
292: */
293: public XMLObject showAddPage(String errorMsg)
294: throws HttpPresentationException,
295: TransactionsDiscRackPresentationException {
296: String title = this .getComms().request.getParameter(TITLE_NAME);
297: String artist = this .getComms().request
298: .getParameter(ARTIST_NAME);
299: String genre = this .getComms().request.getParameter(GENRE_NAME);
300: String isLiked = this .getComms().request.getParameter(IS_LIKED);
301:
302: // Instantiate the page object
303: EditHTML page = (EditHTML) myComms.xmlcFactory
304: .create(EditHTML.class);
305:
306: if (null != title) {
307: page.getElementTitle().setValue(title);
308: }
309: if (null != artist) {
310: page.getElementArtist().setValue(artist);
311: }
312: if (null != genre) {
313: page.getElementGenre().setValue(genre);
314: }
315: if (null != isLiked) {
316: page.getElementLikeBox().setChecked(true);
317: } else {
318: page.getElementLikeBox().setChecked(false);
319: }
320:
321: if (null == errorMsg) {
322: page.getElementErrorText().getParentNode().removeChild(
323: page.getElementErrorText());
324: } else {
325: page.setTextErrorText(errorMsg);
326: }
327:
328: return page;
329: }
330:
331: /**
332: * Method to save a new or existing disc to the database
333: *
334: * @param theDisc
335: * the disc to be saved
336: * @return html document
337: * @exception HttpPresentationException
338: */
339: protected void saveDisc(Disc theDisc)
340: throws HttpPresentationException,
341: AssertionDataObjectException, NullPointerException,
342: Exception {
343:
344: String title = this .getComms().request.getParameter(TITLE_NAME);
345: String artist = this .getComms().request
346: .getParameter(ARTIST_NAME);
347: String genre = this .getComms().request.getParameter(GENRE_NAME);
348:
349: if (title.length() == 0 || artist.length() == 0
350: || genre.length() == 0) {
351: throw new Exception();
352: }
353:
354: try {
355: theDisc.setOwner(this .getUserHandle());
356: theDisc.setTitle(title);
357: theDisc.setArtist(artist);
358: theDisc.setGenre(genre);
359:
360: if (null != this .getComms().request.getParameter(IS_LIKED)) {
361: theDisc.setLiked(true);
362: } else {
363: theDisc.setLiked(false);
364: }
365:
366: theDisc.save(transaction);
367:
368: } catch (NullPointerException ex) {
369: throw new NullPointerException();
370: } catch (AssertionDataObjectException ex) {
371: throw new AssertionDataObjectException(
372: "Error adding disc: read-only table.", ex);
373: } catch (Exception ex) {
374: throw new TransactionsDiscRackPresentationException(
375: "Error adding disc", ex);
376: }
377: }
378: }
|