1 15 package org.apache.tapestry.vlib.pages.admin; 16 17 import java.rmi.RemoteException ; 18 import java.util.List ; 19 20 import javax.ejb.FinderException ; 21 22 import org.apache.hivemind.ApplicationRuntimeException; 23 import org.apache.tapestry.IRequestCycle; 24 import org.apache.tapestry.Tapestry; 25 import org.apache.tapestry.event.PageEvent; 26 import org.apache.tapestry.event.PageRenderListener; 27 import org.apache.tapestry.form.IPropertySelectionModel; 28 import org.apache.tapestry.vlib.AdminPage; 29 import org.apache.tapestry.vlib.EntitySelectionModel; 30 import org.apache.tapestry.vlib.IErrorProperty; 31 import org.apache.tapestry.vlib.IMessageProperty; 32 import org.apache.tapestry.vlib.VirtualLibraryEngine; 33 import org.apache.tapestry.vlib.ejb.Book; 34 import org.apache.tapestry.vlib.ejb.IBookQuery; 35 import org.apache.tapestry.vlib.ejb.IOperations; 36 import org.apache.tapestry.vlib.ejb.Person; 37 38 47 48 public abstract class TransferBooksTransfer extends AdminPage implements PageRenderListener 49 { 50 public abstract Person getFromUser(); 51 52 public abstract void setFromUser(Person fromUser); 53 54 public abstract Integer getFromUserId(); 55 56 public abstract void setFromUserId(Integer fromUserId); 57 58 public abstract Integer getToUserId(); 59 60 public abstract void setToUserId(Integer toUserId); 61 62 public abstract Person getToUser(); 63 64 public abstract void setToUser(Person toUser); 65 66 public abstract List getSelectedBooks(); 67 68 public abstract IPropertySelectionModel getUserBookModel(); 69 70 public abstract void setUserBookModel(IPropertySelectionModel userBookModel); 71 72 public void activate(IRequestCycle cycle, Integer fromUserId, Integer toUserId) 73 { 74 Person fromUser = readPerson(fromUserId); 75 76 IPropertySelectionModel model = buildUserBookModel(fromUser); 77 78 if (model.getOptionCount() == 0) 79 { 80 IErrorProperty page = (IErrorProperty) cycle.getPage(); 81 page.setError(format("user-has-no-books", fromUser.getNaturalName())); 82 return; 83 } 84 85 setFromUserId(fromUserId); 86 setFromUser(fromUser); 87 88 setToUserId(toUserId); 89 90 setUserBookModel(model); 91 92 cycle.activate(this); 93 } 94 95 101 102 public void pageBeginRender(PageEvent event) 103 { 104 Person fromUser = getFromUser(); 105 106 if (fromUser == null) 107 { 108 fromUser = readPerson(getFromUserId()); 109 setFromUser(fromUser); 110 } 111 112 if (getUserBookModel() == null) 113 setUserBookModel(buildUserBookModel(fromUser)); 114 115 Person toUser = getToUser(); 116 if (toUser == null) 117 { 118 toUser = readPerson(getToUserId()); 119 setToUser(toUser); 120 } 121 } 122 123 public void formSubmit(IRequestCycle cycle) 124 { 125 List selectedBooks = getSelectedBooks(); 126 127 int count = Tapestry.size(selectedBooks); 128 129 if (count == 0) 130 { 131 setError(getMessage("no-books-selected")); 132 return; 133 } 134 135 Integer [] keys = (Integer []) selectedBooks.toArray(new Integer [count]); 136 Person toUser = getToUser(); 137 138 VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine(); 139 140 int i = 0; 141 while (true) 142 { 143 try 144 { 145 IOperations operations = vengine.getOperations(); 146 147 operations.transferBooks(toUser.getId(), keys); 148 149 break; 150 } 151 catch (FinderException ex) 152 { 153 throw new ApplicationRuntimeException(ex); 154 } 155 catch (RemoteException ex) 156 { 157 vengine.rmiFailure("Unable to transfer ownership of books.", ex, i++); 158 } 159 } 160 161 Person fromUser = getFromUser(); 162 IMessageProperty selectPage = (TransferBooksSelect) cycle.getPage("TransferBooksSelect"); 163 selectPage.setMessage( 164 format( 165 "transfered-books", 166 Integer.toString(count), 167 fromUser.getNaturalName(), 168 toUser.getNaturalName())); 169 cycle.activate(selectPage); 170 } 171 172 private IPropertySelectionModel buildUserBookModel(Person user) 173 { 174 VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine(); 175 Book[] books = null; 176 177 int i = 0; 178 while (true) 179 { 180 books = null; 181 182 try 183 { 184 IBookQuery query = vengine.createNewQuery(); 185 186 int count = query.ownerQuery(user.getId(), null); 187 188 if (count > 0) 189 books = query.get(0, count); 190 191 break; 192 } 193 catch (RemoteException ex) 194 { 195 vengine.rmiFailure( 196 "Unable to retrieve books owned by " + user.getNaturalName() + ".", 197 ex, 198 i++); 199 } 200 } 201 202 EntitySelectionModel model = new EntitySelectionModel(); 203 204 if (books != null) 205 for (i = 0; i < books.length; i++) 206 model.add(books[i].getId(), books[i].getTitle()); 207 208 return model; 209 } 210 211 private Person readPerson(Integer personId) 212 { 213 VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine(); 214 215 return vengine.readPerson(personId); 216 } 217 } 218 | Popular Tags |