KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > vlib > pages > admin > EditUsers


1 // Copyright 2004 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.vlib.pages.admin;
16
17 import java.rmi.RemoteException JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.List JavaDoc;
20
21 import javax.ejb.FinderException JavaDoc;
22 import javax.ejb.RemoveException JavaDoc;
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 /**
37  * Allows editting of the users. Simple flags about the
38  * user can be changed; additionally, the user can have their
39  * password reset (to a random value, which is mailed to them),
40  * or the user can be out-right deleted.
41  *
42  *
43  * @author Howard Lewis Ship
44  * @version $Id: EditUsers.java,v 1.15 2004/04/30 15:17:21 hlship Exp $
45  *
46  **/

47
48 public abstract class EditUsers extends AdminPage implements PageRenderListener
49 {
50     public abstract String JavaDoc getPassword();
51     
52     public abstract void setPassword(String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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     /**
121      * Invoked when the form is submitted.
122      *
123      **/

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 JavaDoc updatedUsers = map.getValues();
136
137         Person[] updates = (Person[]) updatedUsers.toArray(new Person[updatedUsers.size()]);
138
139         Integer JavaDoc[] resetPasswordUserIds = toArray(map.getResetPasswordKeys());
140         Integer JavaDoc[] deletedUserIds = toArray(map.getDeletedKeys());
141
142         String JavaDoc 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 JavaDoc 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 JavaDoc ex)
169             {
170                 vengine.rmiFailure(getMessage("update-failure"), ex, i++);
171             }
172             catch (RemoveException JavaDoc ex)
173             {
174                 throw new ApplicationRuntimeException(ex);
175             }
176             catch (FinderException JavaDoc ex)
177             {
178                 throw new ApplicationRuntimeException(ex);
179             }
180         }
181
182         setMessage(getMessage("users-updated"));
183     }
184
185     private Integer JavaDoc[] toArray(Collection JavaDoc c)
186     {
187         int count = Tapestry.size(c);
188
189         if (count == 0)
190             return null;
191
192         return (Integer JavaDoc[]) c.toArray(new Integer JavaDoc[count]);
193     }
194 }
Popular Tags