1 23 24 package barracudaDiscRack.presentation.discMgmt; 25 26 import barracudaDiscRack.presentation.BasePO; 27 import barracudaDiscRack.business.person.*; 28 import barracudaDiscRack.business.disc.*; 29 import barracudaDiscRack.presentation.DiscRackPresentationException; 30 import barracudaDiscRack.business.DiscRackBusinessException; 31 import com.lutris.appserver.server.httpPresentation.*; 32 import com.lutris.appserver.server.session.*; 33 import com.lutris.util.*; 34 import org.enhydra.xml.xmlc.*; 35 import org.enhydra.xml.xmlc.html.*; 36 import org.w3c.dom.*; 37 import org.w3c.dom.html.*; 38 import org.enhydra.xml.xmlc.XMLObject; 39 40 47 public class Edit extends BasePO { 48 49 53 private static String TITLE_NAME = "title"; 54 private static String ARTIST_NAME = "artist"; 55 private static String GENRE_NAME = "genre"; 56 private static String DISC_ID = "discID"; 57 private static String IS_LIKED = "like"; 58 private static String INVALID_ID = "invalidID"; 59 private static String EDIT_COMMAND = "edit"; 60 61 64 public boolean loggedInUserRequired() { 65 return true; 66 } 67 68 75 public XMLObject handleDefault() 76 throws HttpPresentationException { 77 return showEditPage(null); 78 } 79 80 86 public XMLObject handleShowAddPage() 87 throws HttpPresentationException { 88 return showAddPage(null); 89 } 90 91 97 public XMLObject handleEdit() 98 throws HttpPresentationException { 99 String discID = this.getComms().request.getParameter(DISC_ID); 100 Disc disc = null; 101 102 try { 104 disc = DiscFactory.findDiscByID(discID); 105 } catch(Exception ex) { 106 this.getSessionData().setUserMessage("Please choose a valid disc to edit"); 107 throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE); 108 } 109 110 try { 111 saveDisc(disc); 112 throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE); 113 } catch(Exception ex) { 114 return showEditPage("You must fill out all fields to edit this disc"); 115 } 116 } 117 118 124 public XMLObject handleAdd() 125 throws HttpPresentationException { 126 try { 127 Disc disc = new Disc(); 128 saveDisc(disc); 129 throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE); 130 } catch(Exception ex) { 131 return showAddPage("You must fill out all fields to add this disc"); 132 } 133 } 134 135 141 public XMLObject handleDelete() 142 throws HttpPresentationException, DiscRackPresentationException { 143 String discID = this.getComms().request.getParameter(DISC_ID); 144 145 try { 146 Disc disc = DiscFactory.findDiscByID(discID); 147 String title = disc.getTitle(); 148 disc.delete(); 149 this.getSessionData().setUserMessage("The disc, " + title + 150 ", was deleted"); 151 } catch(Exception ex) { 154 this.getSessionData().setUserMessage("Please choose a valid disc to delete"); 155 } 156 throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE); 159 } 160 161 169 public XMLObject showEditPage(String errorMsg) 170 throws HttpPresentationException, DiscRackPresentationException { 171 String title = this.getComms().request.getParameter(TITLE_NAME); 172 String artist = this.getComms().request.getParameter(ARTIST_NAME); 173 String genre = this.getComms().request.getParameter(GENRE_NAME); 174 String discID = this.getComms().request.getParameter(DISC_ID); 175 EditHTML page = (EditHTML)myComms.xmlcFactory.create(EditHTML.class); 177 178 Disc disc = null; 179 180 try { 181 disc = DiscFactory.findDiscByID(discID); 182 } catch(DiscRackBusinessException ex) { 184 this.getSessionData().setUserMessage("Please choose a valid disc to edit"); 185 throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE); 186 } 187 188 try { 189 page.getElementDiscID().setValue(disc.getHandle()); 192 193 if(null != title && title.length() != 0) { 194 page.getElementTitle().setValue(title); 195 } else { 196 page.getElementTitle().setValue(disc.getTitle()); 197 } 198 199 if(null != artist && artist.length() != 0) { 200 page.getElementArtist().setValue(artist); 201 } else { 202 page.getElementArtist().setValue(disc.getArtist()); 203 } 204 205 if(null != genre && genre.length() != 0) { 206 page.getElementGenre().setValue(genre); 207 } else { 208 page.getElementGenre().setValue(disc.getGenre()); 209 } 210 211 if(null != this.getComms().request.getParameter(IS_LIKED)) { 212 page.getElementLikeBox().setChecked(true); 213 } else { 214 page.getElementLikeBox().setChecked(disc.isLiked()); 215 } 216 217 if(null == errorMsg) { 218 page.getElementErrorText().getParentNode().removeChild(page.getElementErrorText()); 219 } else { 220 page.setTextErrorText(errorMsg); 221 } 222 } catch(DiscRackBusinessException ex) { 223 throw new DiscRackPresentationException("Error populating page for disc editing", ex); 224 } 225 226 page.getElementEventValue().setValue(EDIT_COMMAND); 227 return page; 228 } 229 230 237 public XMLObject showAddPage(String errorMsg) 238 throws HttpPresentationException, DiscRackPresentationException { 239 String title = this.getComms().request.getParameter(TITLE_NAME); 240 String artist = this.getComms().request.getParameter(ARTIST_NAME); 241 String genre = this.getComms().request.getParameter(GENRE_NAME); 242 String discID = this.getComms().request.getParameter(DISC_ID); 243 EditHTML page = (EditHTML)myComms.xmlcFactory.create(EditHTML.class); 245 246 if(null != this.getComms().request.getParameter(TITLE_NAME)) { 247 page.getElementTitle().setValue(this.getComms().request.getParameter(TITLE_NAME)); 248 } 249 if(null != this.getComms().request.getParameter(ARTIST_NAME)) { 250 page.getElementArtist().setValue(this.getComms().request.getParameter(ARTIST_NAME)); 251 } 252 if(null != this.getComms().request.getParameter(GENRE_NAME)) { 253 page.getElementGenre().setValue(this.getComms().request.getParameter(GENRE_NAME)); 254 } 255 if(null != this.getComms().request.getParameter(IS_LIKED)) { 256 page.getElementLikeBox().setChecked(true); 257 } else { 258 page.getElementLikeBox().setChecked(false); 259 } 260 261 if(null == errorMsg) { 262 page.getElementErrorText().getParentNode().removeChild(page.getElementErrorText()); 263 } else { 264 page.setTextErrorText(errorMsg); 265 } 266 267 return page; 268 } 269 270 277 protected void saveDisc(Disc theDisc) 278 throws HttpPresentationException { 279 String title = this.getComms().request.getParameter(TITLE_NAME); 280 String artist = this.getComms().request.getParameter(ARTIST_NAME); 281 String genre = this.getComms().request.getParameter(GENRE_NAME); 282 283 try { 284 theDisc.setTitle(title); 285 theDisc.setArtist(artist); 286 theDisc.setGenre(genre); 287 288 if(null != this.getComms().request.getParameter(IS_LIKED)) { 289 theDisc.setLiked(true); 290 } else { 291 theDisc.setLiked(false); 292 } 293 theDisc.setOwner(this.getUser()); 294 theDisc.save(); 295 } catch(Exception ex) { 296 throw new DiscRackPresentationException("Error adding disc", ex); 297 } 298 } 299 } 300 301 302 303 304 305 306 | Popular Tags |