1 15 package org.apache.tapestry.vlib.components; 16 17 import java.rmi.RemoteException ; 18 19 import javax.ejb.FinderException ; 20 21 import org.apache.hivemind.ApplicationRuntimeException; 22 import org.apache.tapestry.BaseComponent; 23 import org.apache.tapestry.IRequestCycle; 24 import org.apache.tapestry.vlib.VirtualLibraryEngine; 25 import org.apache.tapestry.vlib.Visit; 26 import org.apache.tapestry.vlib.ejb.Book; 27 import org.apache.tapestry.vlib.ejb.BorrowException; 28 import org.apache.tapestry.vlib.ejb.IOperations; 29 import org.apache.tapestry.vlib.pages.Home; 30 31 56 57 public abstract class Borrow extends BaseComponent 58 { 59 public abstract Book getBook(); 60 61 public boolean isLinkDisabled() 62 { 63 Visit visit = (Visit) getPage().getVisit(); 64 65 if (!visit.isUserLoggedIn()) 66 return true; 67 68 71 Book book = getBook(); 72 73 75 if (!book.isLendable()) 76 return true; 77 78 80 return visit.isLoggedInUser(book.getHolderId()); 81 } 82 83 public void borrow(IRequestCycle cycle) 84 { 85 Object [] parameters = cycle.getServiceParameters(); 86 Integer bookPK = (Integer ) parameters[0]; 87 88 Visit visit = (Visit) getPage().getVisit(); 89 Home home = (Home) cycle.getPage("Home"); 90 VirtualLibraryEngine vengine = (VirtualLibraryEngine)cycle.getEngine(); 91 92 int i = 0; 93 while (true) 94 { 95 try 96 { 97 IOperations bean = vengine.getOperations(); 98 Book book = bean.borrowBook(bookPK, visit.getUserId()); 99 100 home.setMessage("Borrowed: " + book.getTitle()); 101 102 break; 103 } 104 catch (BorrowException ex) 105 { 106 vengine.presentError(ex.getMessage(), cycle); 107 return; 108 } 109 catch (FinderException ex) 110 { 111 throw new ApplicationRuntimeException("Unable to find book or user. ", ex); 112 } 113 catch (RemoteException ex) 114 { 115 vengine.rmiFailure("Remote exception borrowing book.", ex, i++); 116 } 117 } 118 119 cycle.activate(home); 120 } 121 122 } | Popular Tags |