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.tapestry.IRequestCycle; 22 import org.apache.tapestry.vlib.ActivatePage; 23 import org.apache.tapestry.vlib.IMessageProperty; 24 import org.apache.tapestry.vlib.VirtualLibraryEngine; 25 import org.apache.tapestry.vlib.Visit; 26 import org.apache.tapestry.vlib.components.Browser; 27 import org.apache.tapestry.vlib.ejb.Book; 28 import org.apache.tapestry.vlib.ejb.IBookQuery; 29 import org.apache.tapestry.vlib.ejb.IOperations; 30 import org.apache.tapestry.vlib.ejb.SortColumn; 31 import org.apache.tapestry.vlib.ejb.SortOrdering; 32 33 47 48 public abstract class MyLibrary 49 extends ActivatePage 50 implements IMessageProperty 51 { 52 public abstract void setOwnedQuery(IBookQuery value); 53 54 public abstract IBookQuery getOwnedQuery(); 55 56 public abstract SortColumn getSortColumn(); 57 58 public abstract boolean isDescending(); 59 60 private Browser _browser; 61 62 public void finishLoad() 63 { 64 _browser = (Browser) getComponent("browser"); 65 } 66 67 public void activate(IRequestCycle cycle) 68 { 69 runQuery(); 70 71 cycle.activate(this); 72 } 73 74 public void requery(IRequestCycle cycle) 75 { 76 runQuery(); 77 } 78 79 private void runQuery() 80 { 81 Visit visit = (Visit) getVisit(); 82 Integer userId = visit.getUserId(); 83 84 VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine(); 85 86 SortOrdering ordering = new SortOrdering(getSortColumn(), isDescending()); 87 88 int i = 0; 89 while (true) 90 { 91 try 92 { 93 IBookQuery query = getOwnedQuery(); 94 95 if (query == null) 96 { 97 query = vengine.createNewQuery(); 98 setOwnedQuery(query); 99 } 100 101 int count = query.ownerQuery(userId, ordering); 102 103 if (count != _browser.getResultCount()) 104 _browser.initializeForResultCount(count); 105 106 break; 107 } 108 catch (RemoteException ex) 109 { 110 vengine.rmiFailure("Remote exception accessing owned books.", ex, i++); 111 112 setOwnedQuery(null); 113 } 114 } 115 } 116 117 121 122 public void editBook(IRequestCycle cycle) 123 { 124 Object [] parameters = cycle.getServiceParameters(); 125 Integer bookId = (Integer ) parameters[0]; 126 EditBook page = (EditBook) cycle.getPage("EditBook"); 127 128 page.beginEdit(cycle, bookId); 129 } 130 131 135 136 public void deleteBook(IRequestCycle cycle) 137 { 138 Object [] parameters = cycle.getServiceParameters(); 139 Integer bookId = (Integer ) parameters[0]; 140 141 ConfirmBookDelete page = (ConfirmBookDelete) cycle.getPage("ConfirmBookDelete"); 142 page.selectBook(bookId, cycle); 143 } 144 145 private void returnBook(Integer bookId) 146 { 147 VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine(); 148 149 int i = 0; 150 while (true) 151 { 152 try 153 { 154 IOperations operations = vengine.getOperations(); 155 Book book = operations.returnBook(bookId); 156 157 setMessage(format("returned-book", book.getTitle())); 158 159 break; 160 } 161 catch (FinderException ex) 162 { 163 setError(format("unable-to-return-book", ex.getMessage())); 164 return; 165 } 166 catch (RemoteException ex) 167 { 168 vengine.rmiFailure("Remote exception returning book.", ex, i++); 169 } 170 } 171 172 runQuery(); 173 } 174 175 } | Popular Tags |