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 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.vlib.Protected; 28 import org.apache.tapestry.vlib.VirtualLibraryEngine; 29 import org.apache.tapestry.vlib.Visit; 30 import org.apache.tapestry.vlib.ejb.IOperations; 31 32 39 40 public abstract class NewBook extends Protected implements PageRenderListener 41 { 42 43 public abstract Map getAttributes(); 44 45 public abstract void setAttributes(Map attributes); 46 47 public abstract String getPublisherName(); 48 49 public void addBook(IRequestCycle cycle) 50 { 51 Map attributes = getAttributes(); 52 53 Integer publisherId = (Integer ) attributes.get("publisherId"); 54 String publisherName = getPublisherName(); 55 56 if (publisherId == null && Tapestry.isBlank(publisherName)) 57 { 58 setErrorField("inputPublisherName", getMessage("need-publisher-name")); 59 return; 60 } 61 62 if (publisherId != null && Tapestry.isNonBlank(publisherName)) 63 { 64 setErrorField("inputPublisherName", getMessage("leave-publisher-name-empty")); 65 return; 66 } 67 68 if (isInError()) 69 return; 70 71 Visit visit = (Visit) getVisit(); 72 Integer userId = visit.getUserId(); 73 VirtualLibraryEngine vengine = (VirtualLibraryEngine) cycle.getEngine(); 74 75 attributes.put("ownerId", userId); 76 attributes.put("holderId", userId); 77 78 int i = 0; 79 while (true) 80 { 81 try 82 { 83 84 IOperations operations = vengine.getOperations(); 85 86 if (publisherId != null) 87 operations.addBook(attributes); 88 else 89 { 90 operations.addBook(attributes, publisherName); 91 92 94 vengine.clearCache(); 95 } 96 97 break; 98 } 99 catch (CreateException ex) 100 { 101 setError("Error adding book: " + ex.getMessage()); 102 return; 103 } 104 catch (RemoteException ex) 105 { 106 vengine.rmiFailure("Remote exception adding new book.", ex, i++); 107 } 108 } 109 110 112 MyLibrary myLibrary = (MyLibrary) cycle.getPage("MyLibrary"); 113 114 myLibrary.setMessage(format("added-book", attributes.get("title"))); 115 116 myLibrary.activate(cycle); 117 } 118 119 public void pageBeginRender(PageEvent event) 120 { 121 if (getAttributes() == null) 122 { 123 Map attributes = new HashMap (); 124 125 127 attributes.put("lendable", Boolean.TRUE); 128 129 setAttributes(attributes); 130 } 131 } 132 133 } | Popular Tags |