1 23 package org.enhydra.barracuda.discRack.pres.screens; 24 25 import java.io.*; 26 import java.util.*; 27 import java.net.*; 28 import javax.servlet.*; 29 import javax.servlet.http.*; 30 31 import org.w3c.dom.*; 32 import org.w3c.dom.html.*; 33 34 import com.lutris.appserver.server.Enhydra; 35 import com.lutris.logging.LogChannel; 36 37 import org.enhydra.xml.xmlc.*; 38 39 import org.enhydra.barracuda.core.comp.*; 40 import org.enhydra.barracuda.core.event.*; 41 import org.enhydra.barracuda.core.event.helper.*; 42 import org.enhydra.barracuda.core.forms.*; 43 import org.enhydra.barracuda.core.forms.validators.*; 44 import org.enhydra.barracuda.plankton.data.*; 45 import org.enhydra.barracuda.core.util.dom.*; 46 import org.enhydra.barracuda.core.util.http.*; 47 import org.apache.log4j.*; 48 import org.enhydra.barracuda.core.util.srv.*; 49 50 import org.enhydra.barracuda.discRack.biz.*; 51 import org.enhydra.barracuda.discRack.biz.person.*; 52 import org.enhydra.barracuda.discRack.biz.disc.*; 53 import org.enhydra.barracuda.discRack.pres.events.*; 54 import org.enhydra.barracuda.discRack.pres.services.*; 55 import org.enhydra.barracuda.discRack.pres.xmlc.*; 56 57 106 public class DiscScreen extends DefaultEventGateway { 107 108 protected static Logger logger = Logger.getLogger(DiscScreen.class.getName()); 110 111 private static final int ADD = 0; 113 private static final int CHANGE = 1; 114 private static final int DELETE = 2; 115 116 private static final String ACTION = "Action"; private static final String DISC = "Disc"; 120 private static final String DISC_ERR = DiscScreen.class.getName()+".DiscErr"; private static final String EDIT_FORM = DiscScreen.class.getName()+".EditForm"; private static final String EDIT_ERR = DiscScreen.class.getName()+".EditErr"; 125 private ListenerFactory getCatalogFactory = new EventForwardingFactory(new RenderCatalog()); 127 private ListenerFactory getCatalogEditFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new GetCatalogEditHandler();} public String getListenerID() {return getID(GetCatalogEditHandler.class);}}; 128 private ListenerFactory getCatalogMaintFactory = new EventForwardingFactory(new RenderCatalogMaint()); 129 private ListenerFactory doCatalogMaintFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new DoCatalogMaintHandler();} public String getListenerID() {return getID(DoCatalogMaintHandler.class);}}; 130 private ListenerFactory renderCatalogFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new RenderCatalogHandler();} public String getListenerID() {return getID(RenderCatalogHandler.class);}}; 131 private ListenerFactory renderCatalogMaintFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new RenderCatalogMaintHandler();} public String getListenerID() {return getID(RenderCatalogMaintHandler.class);}}; 132 134 135 139 public DiscScreen() { 140 specifyLocalEventInterests(getCatalogFactory, GetCatalog.class); 142 specifyLocalEventInterests(getCatalogEditFactory, GetCatalogEdit.class); 143 specifyLocalEventInterests(getCatalogMaintFactory, GetCatalogMaint.class); 144 specifyLocalEventInterests(doCatalogMaintFactory, DoCatalogMaint.class); 145 specifyLocalEventInterests(renderCatalogFactory, RenderCatalog.class); 146 specifyLocalEventInterests(renderCatalogMaintFactory, RenderCatalogMaint.class); 147 148 specifyLocalEventAliases(GetCatalogAdd.class); 150 specifyLocalEventAliases(DoCatalogUpdate.class); 151 specifyLocalEventAliases(DoCatalogDelete.class); 152 } 153 154 155 162 class GetCatalogEditHandler extends DefaultBaseEventListener { 163 public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException { 164 HttpServletRequest req = context.getRequest(); 166 167 if (logger.isDebugEnabled()) logger.debug("Creating id form"); 169 DiscForm df = new DiscForm(); 170 EditForm ef = null; 171 172 try { 173 if (logger.isDebugEnabled()) logger.debug("Mapping id form"); 175 176 df.map(req).validate(true); 177 String id = df.getStringVal(DiscForm.ID); 178 179 Disc disc = (Disc) df.getState(DISC); 181 182 if (logger.isDebugEnabled()) logger.debug("Mapping disc record to disc form"); 185 if (disc!=null) { 186 StateMap sm = new DefaultStateMap(); 187 try {sm.putState(EditForm.ID, disc.getHandle());} catch (DiscRackBusinessException e) {} 188 try {sm.putState(EditForm.ARTIST, disc.getArtist());} catch (DiscRackBusinessException e) {} 189 try {sm.putState(EditForm.TITLE, disc.getTitle());} catch (DiscRackBusinessException e) {} 190 try {sm.putState(EditForm.GENRE, disc.getGenre());} catch (DiscRackBusinessException e) {} 191 try {sm.putState(EditForm.IS_LIKED, new Boolean (disc.isLiked()));} catch (DiscRackBusinessException e) {} 192 ef = new EditForm(); 193 ef.map(sm); 194 } 195 } catch (ValidationException e) { 196 if (logger.isDebugEnabled()) logger.debug("Validation Exception: "+e); 197 if (logger.isDebugEnabled()) CollectionsUtil.printStackTrace(e.getExceptionList(), 0, logger, null); 198 context.putState(DISC_ERR, e); 199 throw new InterruptDispatchException(new GetCatalog()); 200 } 201 202 if (logger.isDebugEnabled()) logger.debug("Saving form, errors"); 204 context.putState(EDIT_FORM, ef); 205 context.putState(EDIT_ERR, null); 206 } 207 } 208 209 213 class DoCatalogMaintHandler extends DefaultBaseEventListener { 214 public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException { 215 BaseEvent event = context.getEvent(); 217 HttpServletRequest req = context.getRequest(); 218 219 if (logger.isDebugEnabled()) logger.debug("Creating edit form"); 221 EditForm ef = new EditForm(); 222 223 ValidationException ve = null; 224 int action = -1; 225 try { 226 if (logger.isDebugEnabled()) logger.debug("Mapping edit form"); 228 ef.map(req); 229 String id = ef.getStringVal(EditForm.ID); 230 231 if (logger.isDebugEnabled()) logger.debug("Determining action: "); 235 BaseEvent rootEvent = DefaultBaseEvent.getOriginalEvent(event); 236 if (rootEvent instanceof DoCatalogUpdate) { 237 if (id.equals(EditForm.INVALID_ID)) action = ADD; 238 else action = CHANGE; 239 } else if (rootEvent instanceof DoCatalogDelete) { 240 action = DELETE; 241 } 242 ef.putState(ACTION, new Integer (action)); 243 244 if (logger.isDebugEnabled()) logger.debug("Validating edit form (action="+action+")"); 247 if (action==DELETE) ef.validateForm(true); 248 else ef.validate(true); 249 250 Disc disc = (Disc) ef.getState(DISC); 252 253 try { 255 if (action==DELETE) { 256 if (logger.isDebugEnabled()) logger.debug("Deleting Disc"); 257 if (disc!=null) disc.delete(); 258 } else { 259 if (logger.isDebugEnabled()) logger.debug("Updating Disc"); 260 if (disc==null) disc = new Disc(); 261 disc.setArtist(ef.getStringVal(EditForm.ARTIST)); 262 disc.setTitle(ef.getStringVal(EditForm.TITLE)); 263 disc.setGenre(ef.getStringVal(EditForm.GENRE)); 264 disc.setLiked(ef.getBooleanVal(EditForm.IS_LIKED).booleanValue()); 265 disc.setOwner(LoginServices.getCurrentUser(SessionServices.getSession(req))); 266 disc.save(); 267 } 268 } catch (DiscRackBusinessException e) { 269 throw new ValidationException(this, "Error updating disc: "+e.getMessage()+" Please try again. If the problem persists contact your system administrator.", e); 270 } 271 } catch (ValidationException e) { 272 if (logger.isDebugEnabled()) logger.debug("Validation Exception: "+e); 273 if (logger.isDebugEnabled()) CollectionsUtil.printStackTrace(e.getExceptionList(), 0, logger, null); 274 ve = e; 275 } 276 277 if (logger.isDebugEnabled()) logger.debug("Saving form, errors"); 279 context.putState(EDIT_FORM, ef); 280 context.putState(EDIT_ERR, ve); 281 282 if (ve==null) { 286 if (logger.isDebugEnabled()) logger.debug("Redirect to GetCatalog"); 287 throw new ClientSideRedirectException(new GetCatalog()); 288 } else if (action==DELETE) { 289 context.putState(DISC_ERR, ve); 290 if (logger.isDebugEnabled()) logger.debug("Redirect Delete to GetCatalog"); 291 throw new ClientSideRedirectException(new GetCatalog()); 292 } else { 293 if (logger.isDebugEnabled()) logger.debug("Redirect to GetCatalogMaint"); 294 throw new ClientSideRedirectException(new GetCatalogMaint()); 295 } 296 } 297 } 298 299 300 306 class RenderCatalogHandler extends DefaultBaseEventListener { 307 public void handleViewEvent(ViewEventContext context) throws EventException, ServletException, IOException { 308 HttpServletRequest req = context.getRequest(); 310 311 DiscCatalogHTML page = (DiscCatalogHTML) DefaultDOMLoader.getGlobalInstance().getDOM(DiscCatalogHTML.class); 315 316 317 321 423 if (logger.isDebugEnabled()) logger.debug("Loading person, disc info"); 426 Person person = LoginServices.getCurrentUser(SessionServices.getSession(context.getRequest())); 427 Disc[] discs = null; 428 try {discs = DiscFactory.findDiscsForPerson(person);} 429 catch (DiscRackBusinessException e) {if (logger.isDebugEnabled()) logger.debug("Err getting disc list:"+e);} 430 ValidationException ve = (ValidationException) context.getState(DISC_ERR); 431 432 if (logger.isDebugEnabled()) logger.debug("Creating template component"); 434 Node node = page.getDocument().getElementById("CatalogForm"); 435 BTemplate templateComp = new BTemplate(); 436 templateComp.setView(new DefaultTemplateView(node)); 437 templateComp.addModel(new CatalogModel(person, discs, ve)); 438 templateComp.addModel(new CatalogRowModel(person, discs, ve)); 439 try {templateComp.render(new DefaultViewContext(context));} 440 catch (RenderException re) {if (logger.isDebugEnabled()) logger.debug("Render err:"+re);} 441 442 if (logger.isDebugEnabled()) logger.debug("Rendering document"); 445 new DefaultDOMWriter().write(page, context.getResponse()); 446 } 447 } 448 449 452 class RenderCatalogMaintHandler extends DefaultBaseEventListener { 453 public void handleViewEvent(ViewEventContext context) throws EventException, ServletException, IOException { 454 EditHTML page = (EditHTML) DefaultDOMLoader.getGlobalInstance().getDOM(EditHTML.class); 458 459 488 489 if (logger.isDebugEnabled()) logger.debug("Creating template component"); 491 Node node = page.getDocument().getElementById("EditForm"); 492 BTemplate templateComp = new BTemplate(new EditModel(context)); 493 templateComp.setView(new DefaultTemplateView(node)); 494 try {templateComp.render(new DefaultViewContext(context));} 495 catch (RenderException re) {if (logger.isDebugEnabled()) logger.debug("Render err:"+re);} 496 497 if (logger.isDebugEnabled()) logger.debug("Rendering document"); 500 new DefaultDOMWriter().write(page, context.getResponse()); 501 } 502 } 503 504 505 511 class CatalogModel extends AbstractTemplateModel { 512 513 Person person = null; 514 Disc[] discs = null; 515 ValidationException ve = null; 516 517 public CatalogModel(Person iperson, Disc[] idiscs, ValidationException ive) { 519 person = iperson; 520 discs = idiscs; 521 ve = ive; 522 } 523 524 public String getName() {return "Catalog";} 526 527 public Object getItem(String key) { 529 ViewContext vc = getViewContext(); 530 try { 531 if (key.equals("Owner")) { 532 return person.getFirstname()+" "+person.getLastname()+"'s Discs"; 533 } else if (key.equals("AddMessage")) { 534 if (discs==null || discs.length<1) return vc.getTemplateNode().cloneNode(true); 535 else return ""; 536 } else if (key.equals("Selection")) { 537 DefaultListModel dlm = new DefaultListModel(); 539 dlm.add(new DefaultItemMap(EditForm.INVALID_ID, "Select One")); 540 if (discs!=null) for (int i=0; i<discs.length; i++) { 541 Disc d = discs[i]; 542 DefaultItemMap dim = new DefaultItemMap(d.getHandle(), d.getArtist()+": "+d.getTitle()); 543 dlm.add(dim); 544 } 545 BSelect bsComp = new BSelect(dlm); 547 bsComp.setEnabled(discs!=null && discs.length>0); 548 return bsComp; 549 } else if (key.equals("AddButton")) { 550 BAction baComp = new BAction(new GetCatalogAdd()); 551 baComp.setDisableBackButton(true); 552 return baComp; 553 } else if (key.equals("ChangeButton")) { 554 BAction baComp = new BAction(new GetCatalogEdit()); 555 baComp.setDisableBackButton(true); 556 baComp.setEnabled(discs!=null && discs.length>0); 557 return baComp; 558 } else if (key.equals("DeleteButton")) { 559 BAction baComp = new BAction(new DoCatalogDelete()); 560 baComp.setDisableBackButton(true); 561 baComp.setEnabled(discs!=null && discs.length>0); 562 return baComp; 563 } else if (key.equals("ExitButton")) { 564 BAction baComp = new BAction(new DoLogout()); 565 baComp.setDisableBackButton(true); 566 return baComp; 567 575 } else if (key.equals("Errors")) { 576 if (ve==null) return null; 577 List errlist = ve.getExceptionList(); 578 StringBuffer sb = new StringBuffer (errlist.size()>1 ? "There were several errors:" : ""); 579 Iterator it = errlist.iterator(); 580 while (it.hasNext()) { 581 sb.append(" "+((ValidationException) it.next()).getMessage()); 582 } 583 return sb.toString(); 584 } else { 585 return super.getItem(key); 586 } 587 } catch (DiscRackBusinessException e) { 588 return "Disc Err:"+e; 589 } 590 } 591 } 592 593 596 class CatalogRowModel extends AbstractTemplateModel implements IterativeModel { 597 598 Person person = null; 599 Disc[] discs = null; 600 Disc disc = null; 601 ValidationException ve = null; 602 int cntr = -1; 603 604 public CatalogRowModel(Person iperson, Disc[] idiscs, ValidationException ive) { 606 person = iperson; 607 discs = idiscs; 608 ve = ive; 609 } 610 611 public String getName() {return "CatalogRow";} 613 614 public Object getItem(String key) { 616 ViewContext vc = getViewContext(); 617 try { 618 if (key.equals("Artist")) { 619 return (disc!=null ? disc.getArtist() : "??"); 620 } else if (key.equals("Title")) { 621 return (disc!=null ? disc.getTitle() : "??"); 622 } else if (key.equals("Genre")) { 623 return (disc!=null ? disc.getGenre() : "??"); 624 } else if (key.equals("LikeIt")) { 625 return ((disc!=null && disc.isLiked()) ? "Yes sirree bob!" : "No way! Blech!"); 626 } else { 627 return super.getItem(key); 628 } 629 } catch (DiscRackBusinessException e) { 630 return "Disc Err:"+e; 631 } 632 } 633 634 public void preIterate() { 636 cntr = -1; 637 } 638 639 public boolean hasNext() { 641 return (discs!=null && cntr<(discs.length-1)); 642 } 643 644 public void loadNext() { 646 cntr++; 647 disc = discs[cntr]; 648 } 649 650 public void postIterate() {} 652 } 653 654 657 class EditModel extends AbstractTemplateModel { 658 659 ControlEventContext ec = null; 660 EditForm fm = null; 661 ValidationException ve = null; 662 int cntr = -1; 663 664 public EditModel(ControlEventContext iec) { 666 ec = iec; 667 fm = (EditForm) ec.getState(EDIT_FORM); 668 ve = (ValidationException) ec.getState(EDIT_ERR); 669 } 670 671 public String getName() {return "Edit";} 673 674 public Object getItem(String key) { 676 ViewContext vc = getViewContext(); 677 if (key.equals("ID")) { 678 String val = null; 679 if (fm!=null) val = fm.getStringVal(EditForm.ID); 680 if (val==null) val = EditForm.INVALID_ID; 681 return new BInput(BInput.HIDDEN, null, val); 684 } else if (key.equals("Artist")) { 685 return (fm!=null ? fm.getStringVal(EditForm.ARTIST, "") : ""); 686 } else if (key.equals("Title")) { 687 return (fm!=null ? fm.getStringVal(EditForm.TITLE, "") : ""); 688 } else if (key.equals("Genre")) { 689 return (fm!=null ? fm.getStringVal(EditForm.GENRE, "") : ""); 690 } else if (key.equals("LikeIt")) { 691 boolean sel = false; 692 if (fm!=null) { 693 try {sel = fm.getBooleanVal(EditForm.IS_LIKED).booleanValue();} 694 catch (Exception e) {} 695 } 696 BToggleButton btbComp = new BToggleButton(null, null, null, sel); 699 return btbComp; 700 } else if (key.equals("SaveButton")) { 701 BAction baComp = new BAction(new DoCatalogUpdate()); 702 baComp.setDisableBackButton(true); 703 return baComp; 704 } else if (key.equals("CancelButton")) { 705 BAction baComp = new BAction(new GetCatalog()); 706 baComp.setDisableBackButton(true); 707 return baComp; 708 } else if (key.equals("Errors")) { 709 if (ve==null) return ""; 710 List errlist = ve.getExceptionList(); 711 StringBuffer sb = new StringBuffer (errlist.size()>1 ? "There were several errors:" : ""); 712 Iterator it = errlist.iterator(); 713 while (it.hasNext()) { 714 sb.append(" "+((ValidationException) it.next()).getMessage()); 715 } 716 return sb.toString(); 717 } else { 718 return super.getItem(key); 719 } 720 } 721 } 722 723 724 730 class DiscForm extends DefaultFormMap { 731 static final String ID = "discID"; 733 734 static final String INVALID_ID = "invalidID"; 736 737 public DiscForm() { 738 if (logger.isDebugEnabled()) logger.debug("Defining ID form elements"); 740 this.defineElement(new DefaultFormElement(ID, FormType.STRING)); 741 742 if (logger.isDebugEnabled()) logger.debug("Defining ID form validator"); 744 this.defineValidator(new IDValidator(ID, INVALID_ID)); 745 } 746 } 747 748 751 class EditForm extends DefaultFormMap { 752 static final String ID = "discID"; 754 static final String ARTIST = "artist"; 755 static final String TITLE = "title"; 756 static final String GENRE = "genre"; 757 static final String IS_LIKED = "like"; 758 759 static final String INVALID_ID = "invalidID"; 761 762 public EditForm() { 763 if (logger.isDebugEnabled()) logger.debug("Defining Edit form elements"); 765 this.defineElement(new DefaultFormElement(ID)); 766 this.defineElement(new DefaultFormElement(ARTIST, FormType.STRING, null, new NotNullValidator("You must enter the Artist's name."))); 767 this.defineElement(new DefaultFormElement(TITLE, FormType.STRING, null, new NotNullValidator("You must enter a Title."))); 768 this.defineElement(new DefaultFormElement(GENRE, FormType.STRING, null, new NotNullValidator("You must enter a Genre."))); 769 this.defineElement(new DefaultFormElement(IS_LIKED, FormType.BOOLEAN, new Boolean (false))); 770 771 if (logger.isDebugEnabled()) logger.debug("Defining Edit form validator"); 773 this.defineValidator(new EditValidator()); 774 } 775 } 776 777 781 class EditValidator extends DefaultFormValidator { 782 public void validateForm(FormMap map, boolean deferExceptions) throws ValidationException { 783 if (logger.isDebugEnabled()) logger.debug("Validating edit form"); 784 785 Integer i = (Integer ) map.getState(ACTION); 787 if (i!=null) { 788 int action = i.intValue(); 790 if (action!=ADD && action!=CHANGE && action!=DELETE) throw new ValidationException(new Integer (action), "Error Validating Edit Form: Invalid action encountered. Please try again. If the problem persists contact your system administrator."); 791 792 new IDValidator(EditForm.ID, EditForm.INVALID_ID).validateForm(action, map, deferExceptions); 794 } 795 } 796 } 797 798 818 class IDValidator extends DefaultFormValidator { 819 String idKey = null; 820 String invalidID = null; 821 822 public IDValidator(String iidKey, String iinvalidID) { 823 idKey = iidKey; 824 invalidID = iinvalidID; 825 } 826 827 public void validateForm(FormMap map, boolean deferExceptions) throws ValidationException { 828 validateForm (CHANGE, map, deferExceptions); 829 } 830 831 public void validateForm(int action, FormMap map, boolean deferExceptions) throws ValidationException { 832 if (logger.isDebugEnabled()) logger.debug("Validating ID form"); 833 834 map.putState(DISC, null); 836 String id = ((DefaultFormMap) map).getStringVal(idKey); 837 if ((action!=ADD) && 838 (id==null || id.equals(invalidID))) throw new ValidationException(this, "You must first select a Disc! Please try again."); 839 840 boolean exists = false; 842 try { 843 Disc disc = DiscFactory.findDiscByID(id); 844 exists = (disc!=null); 845 map.putState(DISC, disc); } catch(Exception ex) {} 847 848 if (action==ADD && exists) throw new ValidationException(new Integer (action), "Record already exists. Please try again."); 850 851 if (action!=ADD && !exists) throw new ValidationException(new Integer (action), "Record no longer exists (someone else may have deleted it). Please try again."); 853 } 854 } 855 } 856 857 | Popular Tags |