1 15 package org.apache.tapestry.vlib.pages; 16 17 import java.rmi.RemoteException ; 18 19 import javax.ejb.FinderException ; 20 21 import org.apache.hivemind.ApplicationRuntimeException; 22 import org.apache.tapestry.IRequestCycle; 23 import org.apache.tapestry.vlib.ActivatePage; 24 import org.apache.tapestry.vlib.IMessageProperty; 25 import org.apache.tapestry.vlib.VirtualLibraryEngine; 26 import org.apache.tapestry.vlib.Visit; 27 import org.apache.tapestry.vlib.components.Browser; 28 import org.apache.tapestry.vlib.ejb.Book; 29 import org.apache.tapestry.vlib.ejb.IBookQuery; 30 import org.apache.tapestry.vlib.ejb.IOperations; 31 import org.apache.tapestry.vlib.ejb.SortColumn; 32 import org.apache.tapestry.vlib.ejb.SortOrdering; 33 34 41 42 public abstract class BorrowedBooks extends ActivatePage implements IMessageProperty 43 { 44 private Browser _browser; 45 46 public abstract void setBorrowedQuery(IBookQuery value); 47 48 public abstract IBookQuery getBorrowedQuery(); 49 50 public abstract SortColumn getSortColumn(); 51 52 public abstract boolean isDescending(); 53 54 public void finishLoad() 55 { 56 _browser = (Browser) getComponent("browser"); 57 } 58 59 public void activate(IRequestCycle cycle) 60 { 61 runQuery(); 62 63 cycle.activate(this); 64 } 65 66 75 76 public void requery(IRequestCycle cycle) 77 { 78 runQuery(); 79 } 80 81 private void runQuery() 82 { 83 Visit visit = (Visit) getVisit(); 84 Integer userPK = visit.getUserId(); 85 86 VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine(); 87 88 SortOrdering ordering = new SortOrdering(getSortColumn(), isDescending()); 89 90 int i = 0; 91 while (true) 92 { 93 try 94 { 95 IBookQuery query = getBorrowedQuery(); 96 97 if (query == null) 98 { 99 query = vengine.createNewQuery(); 100 setBorrowedQuery(query); 101 } 102 103 int count = query.borrowerQuery(userPK, ordering); 104 105 if (count != _browser.getResultCount()) 106 _browser.initializeForResultCount(count); 107 108 break; 109 } 110 catch (RemoteException ex) 111 { 112 vengine.rmiFailure("Remote exception finding borrowed books.", ex, i++); 113 114 setBorrowedQuery(null); 115 } 116 } 117 } 118 119 123 124 public void returnBook(IRequestCycle cycle) 125 { 126 Object [] parameters = cycle.getServiceParameters(); 127 Integer bookPK = (Integer ) parameters[0]; 128 129 VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine(); 130 IOperations operations = vengine.getOperations(); 131 132 try 133 { 134 Book book = operations.returnBook(bookPK); 135 136 setMessage(format("returned-book", book.getTitle())); 137 138 runQuery(); 139 } 140 catch (FinderException ex) 141 { 142 setError(format("unable-to-return-book", ex.getMessage())); 143 return; 144 } 145 catch (RemoteException ex) 146 { 147 throw new ApplicationRuntimeException(ex); 148 } 149 } 150 151 } | Popular Tags |