1 15 package org.apache.tapestry.vlib.pages.admin; 16 17 import java.rmi.RemoteException ; 18 import java.util.Collection ; 19 import java.util.List ; 20 21 import javax.ejb.FinderException ; 22 import javax.ejb.RemoveException ; 23 24 import org.apache.hivemind.ApplicationRuntimeException; 25 import org.apache.tapestry.IRequestCycle; 26 import org.apache.tapestry.PageRedirectException; 27 import org.apache.tapestry.Tapestry; 28 import org.apache.tapestry.event.PageEvent; 29 import org.apache.tapestry.event.PageRenderListener; 30 import org.apache.tapestry.vlib.AdminPage; 31 import org.apache.tapestry.vlib.VirtualLibraryEngine; 32 import org.apache.tapestry.vlib.Visit; 33 import org.apache.tapestry.vlib.ejb.IOperations; 34 import org.apache.tapestry.vlib.ejb.Person; 35 36 47 48 public abstract class EditUsers extends AdminPage implements PageRenderListener 49 { 50 public abstract String getPassword(); 51 52 public abstract void setPassword(String password); 53 54 public abstract UserListEditMap getListEditMap(); 55 56 public abstract void setListEditMap(UserListEditMap listEditMap); 57 58 public abstract void setUser(Person person); 59 60 public void synchronizeUser(IRequestCycle cycle) 61 { 62 UserListEditMap map = getListEditMap(); 63 64 Person user = (Person) map.getValue(); 65 66 if (user == null) 67 { 68 setError(getMessage("out-of-date")); 69 throw new PageRedirectException(this); 70 } 71 72 setUser(user); 73 } 74 75 public void pageBeginRender(PageEvent event) 76 { 77 setupListEditMap(); 78 } 79 80 private void setupListEditMap() 81 { 82 VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine(); 83 Visit visit = (Visit) vengine.getVisit(); 84 85 Integer userId = visit.getUserId(); 86 Person[] users = null; 87 88 int i = 0; 89 while (true) 90 { 91 try 92 { 93 IOperations operations = vengine.getOperations(); 94 95 users = operations.getPersons(); 96 97 break; 98 } 99 catch (RemoteException ex) 100 { 101 vengine.rmiFailure(getMessage("read-failure"), ex, i++); 102 } 103 } 104 105 UserListEditMap map = new UserListEditMap(); 106 107 for (i = 0; i < users.length; i++) 108 { 109 Integer id = users[i].getId(); 110 111 if (id.equals(userId)) 112 continue; 113 114 map.add(id, users[i]); 115 } 116 117 setListEditMap(map); 118 } 119 120 124 125 public void updateUsers(IRequestCycle cycle) 126 { 127 if (isInError()) 128 return; 129 130 Visit visit = (Visit) getVisit(); 131 VirtualLibraryEngine vengine = (VirtualLibraryEngine) cycle.getEngine(); 132 133 UserListEditMap map = getListEditMap(); 134 135 List updatedUsers = map.getValues(); 136 137 Person[] updates = (Person[]) updatedUsers.toArray(new Person[updatedUsers.size()]); 138 139 Integer [] resetPasswordUserIds = toArray(map.getResetPasswordKeys()); 140 Integer [] deletedUserIds = toArray(map.getDeletedKeys()); 141 142 String password = getPassword(); 143 setPassword(null); 144 145 if (Tapestry.isBlank(password) && Tapestry.size(resetPasswordUserIds) != 0) 146 { 147 setErrorField("inputPassword", getMessage("need-password")); 148 return; 149 } 150 151 Integer adminId = visit.getUserId(); 152 153 int i = 0; 154 while (true) 155 { 156 try 157 { 158 IOperations operations = vengine.getOperations(); 159 160 operations.updatePersons( 161 updates, 162 resetPasswordUserIds, 163 password, 164 deletedUserIds, 165 adminId); 166 break; 167 } 168 catch (RemoteException ex) 169 { 170 vengine.rmiFailure(getMessage("update-failure"), ex, i++); 171 } 172 catch (RemoveException ex) 173 { 174 throw new ApplicationRuntimeException(ex); 175 } 176 catch (FinderException ex) 177 { 178 throw new ApplicationRuntimeException(ex); 179 } 180 } 181 182 setMessage(getMessage("users-updated")); 183 } 184 185 private Integer [] toArray(Collection c) 186 { 187 int count = Tapestry.size(c); 188 189 if (count == 0) 190 return null; 191 192 return (Integer []) c.toArray(new Integer [count]); 193 } 194 } | Popular Tags |