1 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 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 46 public class Edit extends BasePO { 47 48 52 private static String TITLE_NAME = "title"; 53 private static String ARTIST_NAME = "artist"; 54 private static String GENRE_NAME = "genre"; 55 private static String DISC_ID = "discID"; 56 private static String IS_LIKED = "like"; 57 private static String INVALID_ID = "invalidID"; 58 private static String EDIT_COMMAND = "edit"; 59 60 63 public boolean loggedInUserRequired() { 64 return true; 65 } 66 67 74 public XMLObject handleDefault() 75 throws HttpPresentationException { 76 return showEditPage(null); 77 } 78 79 85 public XMLObject handleShowAddPage() 86 throws HttpPresentationException { 87 return showAddPage(null); 88 } 89 90 96 public XMLObject handleEdit() 97 throws HttpPresentationException { 98 String discID = this.getComms().request.getParameter(DISC_ID); 99 Disc disc = null; 100 101 103 try { 104 105 disc = ((DiscGenerator)DiscGeneratorFactory.getDiscGenerator("transactionsDiscRack.business.disc.DiscGeneratorImpl")).findDiscByID(discID); 106 107 108 } catch(Exception 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 ex) { 119 return showEditPage("You must fill out all fields to edit this disc"); 120 } 121 } 122 123 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 146 147 }catch(NullPointerException 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 ex) { 154 return showAddPage("You must fill out all fields to add this disc"); 155 } 156 157 } 158 159 165 public XMLObject handleDelete() 166 throws HttpPresentationException, TransactionsDiscRackPresentationException, AssertionDataObjectException { 167 String discID = this.getComms().request.getParameter(DISC_ID); 168 Disc disc = null; 169 173 try { 174 175 disc = ((DiscGenerator)DiscGeneratorFactory.getDiscGenerator("transactionsDiscRack.business.disc.DiscGeneratorImpl")).findDiscByID(discID); 176 177 String title = disc.getTitle(); 178 disc.delete(); 179 this.getSessionData().setUserMessage("The disc, " + title + 180 ", was deleted"); 181 }catch(NullPointerException e){ 182 183 } 186 catch (AssertionDataObjectException ex) { 187 this.getSessionData().setUserMessage("Read-only table!"); 188 } 189 catch(Exception ex) { 190 this.getSessionData().setUserMessage("Please choose a valid disc to delete"); 191 } 192 if (disc != null) 195 disc.dbtRelease(); 196 throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE); 197 } 198 199 207 public XMLObject showEditPage(String errorMsg) 208 throws HttpPresentationException, TransactionsDiscRackPresentationException { 209 String title = this.getComms().request.getParameter(TITLE_NAME); 210 String artist = this.getComms().request.getParameter(ARTIST_NAME); 211 String genre = this.getComms().request.getParameter(GENRE_NAME); 212 String discID = this.getComms().request.getParameter(DISC_ID); 213 EditHTML page = (EditHTML)myComms.xmlcFactory.create(EditHTML.class); 215 216 Disc disc = null; 217 218 223 try { 224 225 DiscGenerator discGenerator = DiscGeneratorFactory.getDiscGenerator("transactionsDiscRack.business.disc.DiscGeneratorImpl"); 226 227 disc= discGenerator.findDiscByID(discID); 228 229 }catch(NullPointerException e){ 230 page.setTextErrorText("You cannot edit disc when you are in TransactionsDiscRack_pres"); 231 return page; 232 } 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 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 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 294 public XMLObject showAddPage(String errorMsg) 295 throws HttpPresentationException, TransactionsDiscRackPresentationException { 296 String title = this.getComms().request.getParameter(TITLE_NAME); 297 String artist = this.getComms().request.getParameter(ARTIST_NAME); 298 String genre = this.getComms().request.getParameter(GENRE_NAME); 299 String discID = this.getComms().request.getParameter(DISC_ID); 300 302 303 EditHTML page = (EditHTML)myComms.xmlcFactory.create(EditHTML.class); 304 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 337 protected void saveDisc(Disc theDisc) 338 throws HttpPresentationException, AssertionDataObjectException,NullPointerException ,Exception { 339 340 String title = this.getComms().request.getParameter(TITLE_NAME); 341 String artist = this.getComms().request.getParameter(ARTIST_NAME); 342 String genre = this.getComms().request.getParameter(GENRE_NAME); 343 344 if (title.length() == 0 || artist.length() ==0 || 345 genre.length() == 0) { 346 throw new Exception (); 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 ex) { 366 throw new NullPointerException (); 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 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 |