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.FinderException ; 22 23 import org.apache.hivemind.ApplicationRuntimeException; 24 import org.apache.tapestry.IRequestCycle; 25 import org.apache.tapestry.Tapestry; 26 import org.apache.tapestry.event.PageEvent; 27 import org.apache.tapestry.event.PageRenderListener; 28 import org.apache.tapestry.form.IFormComponent; 29 import org.apache.tapestry.valid.IValidationDelegate; 30 import org.apache.tapestry.vlib.ActivatePage; 31 import org.apache.tapestry.vlib.VirtualLibraryEngine; 32 import org.apache.tapestry.vlib.Visit; 33 import org.apache.tapestry.vlib.ejb.IOperations; 34 35 42 43 public abstract class EditProfile extends ActivatePage implements PageRenderListener 44 { 45 public abstract String getPassword1(); 46 47 public abstract void setPassword1(String value); 48 49 public abstract String getPassword2(); 50 51 public abstract void setPassword2(String value); 52 53 public abstract Map getAttributes(); 54 55 public abstract void setAttributes(Map attributes); 56 57 66 67 public void activate(IRequestCycle cycle) 68 { 69 Visit visit = (Visit) getVisit(); 70 VirtualLibraryEngine vengine = (VirtualLibraryEngine) cycle.getEngine(); 71 72 Integer userId = visit.getUserId(); 73 Map attributes = null; 74 75 int i = 0; 76 while (true) 77 { 78 try 79 { 80 IOperations operations = vengine.getOperations(); 81 82 attributes = operations.getPersonAttributes(userId); 83 84 break; 85 } 86 catch (FinderException ex) 87 { 88 throw new ApplicationRuntimeException(ex); 89 } 90 catch (RemoteException ex) 91 { 92 vengine.rmiFailure("Remote exception reading user.", ex, i++); 93 } 94 } 95 96 attributes.remove("password"); 97 setAttributes(attributes); 98 99 cycle.activate(this); 100 } 101 102 public void updateProfile(IRequestCycle cycle) 103 { 104 String password1 = getPassword1(); 105 String password2 = getPassword2(); 106 107 setPassword1(null); 108 setPassword2(null); 109 110 IValidationDelegate delegate = getValidationDelegate(); 111 112 delegate.setFormComponent((IFormComponent) getComponent("inputPassword1")); 113 delegate.recordFieldInputValue(null); 114 115 delegate.setFormComponent((IFormComponent) getComponent("inputPassword2")); 116 delegate.recordFieldInputValue(null); 117 118 if (delegate.getHasErrors()) 119 return; 120 121 Map attributes = getAttributes(); 122 123 if (Tapestry.isBlank(password1) != Tapestry.isBlank(password2)) 124 { 125 setErrorField("inputPassword1", getMessage("enter-password-twice")); 126 127 return; 128 } 129 130 if (Tapestry.isNonBlank(password1)) 131 { 132 if (!password1.equals(password2)) 133 { 134 setErrorField("inputPassword1", getMessage("password-must-match")); 135 return; 136 } 137 138 attributes.put("password", password1); 139 } 140 141 Visit visit = (Visit) getVisit(); 142 VirtualLibraryEngine vengine = (VirtualLibraryEngine) cycle.getEngine(); 143 Integer userId = visit.getUserId(); 144 145 int i = 0; 146 while (true) 147 { 148 try 149 { 150 156 157 IOperations operations = vengine.getOperations(); 158 159 operations.updatePerson(userId, attributes); 160 break; 161 } 162 catch (FinderException ex) 163 { 164 throw new ApplicationRuntimeException(ex); 165 } 166 catch (RemoteException ex) 167 { 168 vengine.rmiFailure("Remote exception updating user attributes.", ex, i++); 169 } 170 } 171 172 vengine.clearCache(); 173 174 MyLibrary myLibrary = (MyLibrary) cycle.getPage("MyLibrary"); 175 myLibrary.activate(cycle); 176 } 177 178 public void pageBeginRender(PageEvent event) 179 { 180 if (getAttributes() == null) 181 setAttributes(new HashMap ()); 182 } 183 184 } | Popular Tags |