1 23 24 package discRack.presentation.discMgmt; 25 26 import discRack.presentation.BasePO; 27 import discRack.presentation.DiscRackPresentationException; 28 import discRack.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 104 try { 105 106 disc = ((DiscGenerator)DiscGeneratorFactory.getDiscGenerator("discRack.business.disc.DiscGeneratorImpl")).findDiscByID(discID); 107 108 } catch(Exception ex) { 109 this.getSessionData().setUserMessage("Please choose a valid disc to edit"); 110 throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE); 111 } 112 113 try { 114 saveDisc(disc); 115 throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE); 116 } catch(Exception ex) { 117 return showEditPage("You must fill out all fields to edit this disc"); 118 } 119 } 120 121 127 public XMLObject handleAdd() 128 throws HttpPresentationException { 129 try { 130 131 132 Disc disc= DiscFactory.getDisc("discRack.business.disc.DiscImpl"); 133 saveDisc(disc); 134 135 throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE); 136 137 142 143 }catch(NullPointerException ex) { 144 return showAddPage("You cannot add disc when you in DiscRack_pres"); 145 } 146 catch(AssertionDataObjectException ex) { 147 return showAddPage("Dics table is read-only: no DML operations allowed"); 148 } 149 catch(Exception ex) { 150 return showAddPage("You must fill out all fields to add this disc"); 151 } 152 } 153 154 160 public XMLObject handleDelete() 161 throws HttpPresentationException, DiscRackPresentationException, AssertionDataObjectException { 162 String discID = this.getComms().request.getParameter(DISC_ID); 163 164 165 169 try { 170 171 Disc disc = ((DiscGenerator)DiscGeneratorFactory.getDiscGenerator("discRack.business.disc.DiscGeneratorImpl")).findDiscByID(discID); 172 173 174 175 String title = disc.getTitle(); 176 disc.delete(); 177 this.getSessionData().setUserMessage("The disc, " + title + 178 ", was deleted"); 179 180 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 throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE); 195 } 196 197 205 public XMLObject showEditPage(String errorMsg) 206 throws HttpPresentationException, DiscRackPresentationException { 207 String title = this.getComms().request.getParameter(TITLE_NAME); 208 String artist = this.getComms().request.getParameter(ARTIST_NAME); 209 String genre = this.getComms().request.getParameter(GENRE_NAME); 210 String discID = this.getComms().request.getParameter(DISC_ID); 211 EditHTML page = (EditHTML)myComms.xmlcFactory.create(EditHTML.class); 213 214 Disc disc = null; 215 216 221 try { 222 223 disc = ((DiscGenerator)DiscGeneratorFactory.getDiscGenerator("discRack.business.disc.DiscGeneratorImpl")).findDiscByID(discID); 224 225 226 }catch(NullPointerException e){ 227 page.setTextErrorText("You cannot edit disc when you are in DiscRack_pres"); 228 return page; 229 230 } catch(DiscRackException ex) { 232 this.getSessionData().setUserMessage("Please choose a valid disc to edit"); 233 throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE); 234 } 235 236 try { 237 page.getElementDiscID().setValue(disc.getHandle()); 240 241 if(null != title && title.length() != 0) { 242 page.getElementTitle().setValue(title); 243 } else { 244 page.getElementTitle().setValue(disc.getTitle()); 245 } 246 247 if(null != artist && artist.length() != 0) { 248 page.getElementArtist().setValue(artist); 249 } else { 250 page.getElementArtist().setValue(disc.getArtist()); 251 } 252 253 if(null != genre && genre.length() != 0) { 254 page.getElementGenre().setValue(genre); 255 } else { 256 page.getElementGenre().setValue(disc.getGenre()); 257 } 258 259 if(null != this.getComms().request.getParameter(IS_LIKED)) { 260 page.getElementLikeBox().setChecked(true); 261 } else { 262 page.getElementLikeBox().setChecked(disc.isLiked()); 263 } 264 265 if(null == errorMsg) { 266 page.getElementErrorText().getParentNode().removeChild(page.getElementErrorText()); 267 } else { 268 page.setTextErrorText(errorMsg); 269 } 270 }catch(NullPointerException ex) { 271 return showAddPage(errorMsg); 272 273 } catch(DiscRackException ex) { 274 throw new DiscRackPresentationException("Error populating page for disc editing", ex); 275 } 276 277 page.getElementEventValue().setValue(EDIT_COMMAND); 278 return page; 279 } 280 281 288 public XMLObject showAddPage(String errorMsg) 289 throws HttpPresentationException, DiscRackPresentationException { 290 String title = this.getComms().request.getParameter(TITLE_NAME); 291 String artist = this.getComms().request.getParameter(ARTIST_NAME); 292 String genre = this.getComms().request.getParameter(GENRE_NAME); 293 String discID = this.getComms().request.getParameter(DISC_ID); 294 296 297 EditHTML page = (EditHTML)myComms.xmlcFactory.create(EditHTML.class); 298 300 if(null != this.getComms().request.getParameter(TITLE_NAME)) { 301 page.getElementTitle().setValue(this.getComms().request.getParameter(TITLE_NAME)); 302 } 303 if(null != this.getComms().request.getParameter(ARTIST_NAME)) { 304 page.getElementArtist().setValue(this.getComms().request.getParameter(ARTIST_NAME)); 305 } 306 if(null != this.getComms().request.getParameter(GENRE_NAME)) { 307 page.getElementGenre().setValue(this.getComms().request.getParameter(GENRE_NAME)); 308 } 309 if(null != this.getComms().request.getParameter(IS_LIKED)) { 310 page.getElementLikeBox().setChecked(true); 311 } else { 312 page.getElementLikeBox().setChecked(false); 313 } 314 315 if(null == errorMsg) { 316 page.getElementErrorText().getParentNode().removeChild(page.getElementErrorText()); 317 } else { 318 page.setTextErrorText(errorMsg); 319 } 320 321 return page; 322 } 323 324 331 protected void saveDisc(Disc disc) 332 throws HttpPresentationException, AssertionDataObjectException,NullPointerException ,Exception { 333 334 String title = this.getComms().request.getParameter(TITLE_NAME); 335 String artist = this.getComms().request.getParameter(ARTIST_NAME); 336 String genre = this.getComms().request.getParameter(GENRE_NAME); 337 338 if (title.length() == 0 || artist.length() ==0 || 339 genre.length() == 0) { 340 throw new Exception (); 341 } 342 343 try { 344 disc.setTitle(title); 345 disc.setArtist(artist); 346 disc.setGenre(genre); 347 348 349 if(null != this.getComms().request.getParameter(IS_LIKED)) 350 { 351 disc.setLiked(true); 352 } else { 353 disc.setLiked(false); 354 } 355 356 disc.setOwner(this.getUser()); 357 disc.save(); 358 359 }catch(NullPointerException ex) { 360 throw new NullPointerException (); 361 } 362 catch (AssertionDataObjectException ex) { 363 throw new AssertionDataObjectException("Error adding disc: read-only table.", ex); 364 } 365 catch(Exception ex) { 366 throw new DiscRackPresentationException("Error adding disc", ex); 367 } 368 } 369 } 370 371 372 373 374 375 376 | Popular Tags |