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:30:36 sinisa Exp $
022: */
023:
024: package barracudaDiscRack.presentation.discMgmt;
025:
026: import barracudaDiscRack.presentation.BasePO;
027: import barracudaDiscRack.business.person.*;
028: import barracudaDiscRack.business.disc.*;
029: import barracudaDiscRack.presentation.DiscRackPresentationException;
030: import barracudaDiscRack.business.DiscRackBusinessException;
031: import com.lutris.appserver.server.httpPresentation.*;
032: import com.lutris.appserver.server.session.*;
033: import com.lutris.util.*;
034: import org.enhydra.xml.xmlc.*;
035: import org.enhydra.xml.xmlc.html.*;
036: import org.w3c.dom.*;
037: import org.w3c.dom.html.*;
038: import org.enhydra.xml.xmlc.XMLObject;
039:
040: /**
041: * Edit class handles the disc management (add, edit or delete)
042: * of the DiscRack app
043: *
044: * @author
045: * @version
046: */
047: public class Edit extends BasePO {
048:
049: /**
050: * Constants representing HTTP parameters passed in from
051: * the form submission
052: */
053: private static String TITLE_NAME = "title";
054: private static String ARTIST_NAME = "artist";
055: private static String GENRE_NAME = "genre";
056: private static String DISC_ID = "discID";
057: private static String IS_LIKED = "like";
058: private static String INVALID_ID = "invalidID";
059: private static String EDIT_COMMAND = "edit";
060:
061: /**
062: * Superclass method override
063: */
064: public boolean loggedInUserRequired() {
065: return true;
066: }
067:
068: /**
069: * Default event. Just show the page populated with any disc
070: * parameters that were passed along.
071: *
072: * @return html document
073: * @exception HttpPresentationException
074: */
075: public XMLObject handleDefault() throws HttpPresentationException {
076: return showEditPage(null);
077: }
078:
079: /**
080: * handle show add disc page event.
081: *
082: * @return html document
083: * @exception HttpPresentationException
084: */
085: public XMLObject handleShowAddPage()
086: throws HttpPresentationException {
087: return showAddPage(null);
088: }
089:
090: /*
091: * Edits an existing disc
092: *
093: * @return html document
094: * @exception HttpPresentationException
095: */
096: public XMLObject handleEdit() throws HttpPresentationException {
097: String discID = this .getComms().request.getParameter(DISC_ID);
098: Disc disc = null;
099:
100: // Try to get the disc by its ID
101: try {
102: disc = DiscFactory.findDiscByID(discID);
103: } catch (Exception ex) {
104: this .getSessionData().setUserMessage(
105: "Please choose a valid disc to edit");
106: throw new ClientPageRedirectException(getComms().request
107: .getApplicationPath()
108: + DISC_CATALOG_PAGE);
109: }
110:
111: try {
112: saveDisc(disc);
113: throw new ClientPageRedirectException(getComms().request
114: .getApplicationPath()
115: + DISC_CATALOG_PAGE);
116: } catch (Exception 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() throws HttpPresentationException {
128: try {
129: Disc disc = new Disc();
130: saveDisc(disc);
131: throw new ClientPageRedirectException(getComms().request
132: .getApplicationPath()
133: + DISC_CATALOG_PAGE);
134: } catch (Exception ex) {
135: return showAddPage("You must fill out all fields to add this disc");
136: }
137: }
138:
139: /*
140: * Deletes a Disc from the Disc database
141: *
142: * @return html document
143: * @exception HttpPresentationException
144: */
145: public XMLObject handleDelete() throws HttpPresentationException,
146: DiscRackPresentationException {
147: String discID = this .getComms().request.getParameter(DISC_ID);
148:
149: try {
150: Disc disc = DiscFactory.findDiscByID(discID);
151: String title = disc.getTitle();
152: disc.delete();
153: this .getSessionData().setUserMessage(
154: "The disc, " + title + ", was deleted");
155: // Catch any possible database exception as well as the null pointer
156: // exception if the disc is not found and is null after findDiscByID
157: } catch (Exception ex) {
158: this .getSessionData().setUserMessage(
159: "Please choose a valid disc to delete");
160: }
161: // Redirect to the catalog page which will display the error message,
162: // if there was one set by the above exception
163: throw new ClientPageRedirectException(getComms().request
164: .getApplicationPath()
165: + DISC_CATALOG_PAGE);
166: }
167:
168: /**
169: * Produce HTML for this PO, populated by the disc information
170: * that the user wants to edit
171: *
172: * @param errorMsg, the error messages
173: * @return html document
174: * @exception HttpPresentationException
175: */
176: public XMLObject showEditPage(String errorMsg)
177: throws HttpPresentationException,
178: DiscRackPresentationException {
179: String title = this .getComms().request.getParameter(TITLE_NAME);
180: String artist = this .getComms().request
181: .getParameter(ARTIST_NAME);
182: String genre = this .getComms().request.getParameter(GENRE_NAME);
183: String discID = this .getComms().request.getParameter(DISC_ID);
184: // Instantiate the page object
185: EditHTML page = (EditHTML) myComms.xmlcFactory
186: .create(EditHTML.class);
187:
188: Disc disc = null;
189:
190: try {
191: disc = DiscFactory.findDiscByID(discID);
192: // Catch any possible database exception in findDiscByID()
193: } catch (DiscRackBusinessException ex) {
194: this .getSessionData().setUserMessage(
195: "Please choose a valid disc to edit");
196: throw new ClientPageRedirectException(getComms().request
197: .getApplicationPath()
198: + DISC_CATALOG_PAGE);
199: }
200:
201: try {
202: // If we received a valid discID then try to show the disc's contents,
203: // otherwise try to use the HTML input parameters
204: page.getElementDiscID().setValue(disc.getHandle());
205:
206: if (null != title && title.length() != 0) {
207: page.getElementTitle().setValue(title);
208: } else {
209: page.getElementTitle().setValue(disc.getTitle());
210: }
211:
212: if (null != artist && artist.length() != 0) {
213: page.getElementArtist().setValue(artist);
214: } else {
215: page.getElementArtist().setValue(disc.getArtist());
216: }
217:
218: if (null != genre && genre.length() != 0) {
219: page.getElementGenre().setValue(genre);
220: } else {
221: page.getElementGenre().setValue(disc.getGenre());
222: }
223:
224: if (null != this .getComms().request.getParameter(IS_LIKED)) {
225: page.getElementLikeBox().setChecked(true);
226: } else {
227: page.getElementLikeBox().setChecked(disc.isLiked());
228: }
229:
230: if (null == errorMsg) {
231: page.getElementErrorText().getParentNode().removeChild(
232: page.getElementErrorText());
233: } else {
234: page.setTextErrorText(errorMsg);
235: }
236: } catch (DiscRackBusinessException ex) {
237: throw new DiscRackPresentationException(
238: "Error populating page for disc editing", ex);
239: }
240:
241: page.getElementEventValue().setValue(EDIT_COMMAND);
242: return page;
243: }
244:
245: /**
246: * Produce HTML for this PO
247: *
248: * @param errorMsg, the error messages
249: * @return html document
250: * @exception HttpPresentationException
251: */
252: public XMLObject showAddPage(String errorMsg)
253: throws HttpPresentationException,
254: DiscRackPresentationException {
255: String title = this .getComms().request.getParameter(TITLE_NAME);
256: String artist = this .getComms().request
257: .getParameter(ARTIST_NAME);
258: String genre = this .getComms().request.getParameter(GENRE_NAME);
259: String discID = this .getComms().request.getParameter(DISC_ID);
260: // Instantiate the page object
261: EditHTML page = (EditHTML) myComms.xmlcFactory
262: .create(EditHTML.class);
263:
264: if (null != this .getComms().request.getParameter(TITLE_NAME)) {
265: page.getElementTitle().setValue(
266: this .getComms().request.getParameter(TITLE_NAME));
267: }
268: if (null != this .getComms().request.getParameter(ARTIST_NAME)) {
269: page.getElementArtist().setValue(
270: this .getComms().request.getParameter(ARTIST_NAME));
271: }
272: if (null != this .getComms().request.getParameter(GENRE_NAME)) {
273: page.getElementGenre().setValue(
274: this .getComms().request.getParameter(GENRE_NAME));
275: }
276: if (null != this .getComms().request.getParameter(IS_LIKED)) {
277: page.getElementLikeBox().setChecked(true);
278: } else {
279: page.getElementLikeBox().setChecked(false);
280: }
281:
282: if (null == errorMsg) {
283: page.getElementErrorText().getParentNode().removeChild(
284: page.getElementErrorText());
285: } else {
286: page.setTextErrorText(errorMsg);
287: }
288:
289: return page;
290: }
291:
292: /**
293: * Method to save a new or existing disc to the database
294: *
295: * @param disc, the disc to be saved
296: * @return html document
297: * @exception HttpPresentationException
298: */
299: protected void saveDisc(Disc theDisc)
300: throws HttpPresentationException {
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:
306: try {
307: theDisc.setTitle(title);
308: theDisc.setArtist(artist);
309: theDisc.setGenre(genre);
310:
311: if (null != this .getComms().request.getParameter(IS_LIKED)) {
312: theDisc.setLiked(true);
313: } else {
314: theDisc.setLiked(false);
315: }
316: theDisc.setOwner(this .getUser());
317: theDisc.save();
318: } catch (Exception ex) {
319: throw new DiscRackPresentationException(
320: "Error adding disc", ex);
321: }
322: }
323: }
|