1 15 package org.apache.tapestry.vlib.pages; 16 17 import java.rmi.RemoteException ; 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 import javax.ejb.CreateException ; 22 import javax.ejb.FinderException ; 23 24 import org.apache.hivemind.ApplicationRuntimeException; 25 import org.apache.tapestry.IRequestCycle; 26 import org.apache.tapestry.Tapestry; 27 import org.apache.tapestry.event.PageEvent; 28 import org.apache.tapestry.event.PageRenderListener; 29 import org.apache.tapestry.vlib.Protected; 30 import org.apache.tapestry.vlib.VirtualLibraryEngine; 31 import org.apache.tapestry.vlib.ejb.IOperations; 32 33 39 40 public abstract class EditBook extends Protected implements PageRenderListener 41 { 42 public abstract Map getAttributes(); 43 44 public abstract void setAttributes(Map attributes); 45 46 public abstract String getPublisherName(); 47 48 public abstract Integer getBookId(); 49 50 public abstract void setBookId(Integer bookId); 51 52 58 59 public void beginEdit(IRequestCycle cycle, Integer bookId) 60 { 61 setBookId(bookId); 62 63 VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine(); 64 65 int i = 0; 66 while (true) 67 { 68 try 69 { 70 72 IOperations operations = vengine.getOperations(); 73 74 setAttributes(operations.getBookAttributes(bookId)); 75 76 break; 77 } 78 catch (FinderException ex) 79 { 80 throw new ApplicationRuntimeException(ex); 81 } 82 catch (RemoteException ex) 83 { 84 vengine.rmiFailure( 85 "Remote exception setting up page for book #" + bookId + ".", 86 ex, 87 i++); 88 } 89 } 90 91 cycle.activate(this); 92 } 93 94 98 99 public void formSubmit(IRequestCycle cycle) 100 { 101 Map attributes = getAttributes(); 102 103 Integer publisherId = (Integer ) attributes.get("publisherId"); 104 String publisherName = getPublisherName(); 105 106 if (publisherId == null && Tapestry.isBlank(publisherName)) 107 { 108 setErrorField("inputPublisherName", getMessage("need-publisher-name")); 109 return; 110 } 111 112 if (publisherId != null && Tapestry.isNonBlank(publisherName)) 113 { 114 setErrorField("inputPublisherName", getMessage("leave-publisher-name-empty")); 115 return; 116 } 117 118 120 if (isInError()) 121 return; 122 123 125 VirtualLibraryEngine vengine = (VirtualLibraryEngine)cycle.getEngine(); 126 Integer bookId = getBookId(); 127 128 int i = 0; 129 while (true) 130 { 131 IOperations bean = vengine.getOperations(); 132 133 try 134 { 135 if (publisherId != null) 136 bean.updateBook(bookId, attributes); 137 else 138 { 139 bean.updateBook(bookId, attributes, publisherName); 140 vengine.clearCache(); 141 } 142 143 break; 144 } 145 catch (FinderException ex) 146 { 147 throw new ApplicationRuntimeException(ex); 148 } 149 catch (CreateException ex) 150 { 151 throw new ApplicationRuntimeException(ex); 152 } 153 catch (RemoteException ex) 154 { 155 vengine.rmiFailure("Remote exception updating book #" + bookId + ".", ex, i++); 156 157 continue; 158 } 159 } 160 161 MyLibrary page = (MyLibrary) cycle.getPage("MyLibrary"); 162 page.setMessage(format("updated-book", attributes.get("title"))); 163 page.activate(cycle); 164 165 } 166 167 public void pageBeginRender(PageEvent event) 168 { 169 if (getAttributes() == null) 170 setAttributes(new HashMap ()); 171 } 172 173 } | Popular Tags |