KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > vlib > pages > EditProfile


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;
16
17 import java.rmi.RemoteException JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import javax.ejb.FinderException JavaDoc;
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 /**
36  * Edit's a user's profile: names, email and password.
37  *
38  * @author Howard Lewis Ship
39  * @version $Id: EditProfile.java,v 1.16 2004/04/30 15:17:21 hlship Exp $
40  *
41  **/

42
43 public abstract class EditProfile extends ActivatePage implements PageRenderListener
44 {
45     public abstract String JavaDoc getPassword1();
46
47     public abstract void setPassword1(String JavaDoc value);
48
49     public abstract String JavaDoc getPassword2();
50
51     public abstract void setPassword2(String JavaDoc value);
52
53     public abstract Map JavaDoc getAttributes();
54
55     public abstract void setAttributes(Map JavaDoc attributes);
56
57     /**
58      * Invoked (from {@link MyLibrary}) to begin editting the user's
59      * profile. We get the entity attributes from the
60      * {@link org.apache.tapestry.vlib.ejb.IPerson},
61      * and store them in the attributes page property, ready to provide
62      * default values to the {@link org.apache.tapestry.valid.ValidField}
63      * components.
64      *
65      **/

66
67     public void activate(IRequestCycle cycle)
68     {
69         Visit visit = (Visit) getVisit();
70         VirtualLibraryEngine vengine = (VirtualLibraryEngine) cycle.getEngine();
71
72         Integer JavaDoc userId = visit.getUserId();
73         Map JavaDoc 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 JavaDoc ex)
87             {
88                 throw new ApplicationRuntimeException(ex);
89             }
90             catch (RemoteException JavaDoc 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 JavaDoc password1 = getPassword1();
105         String JavaDoc 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 JavaDoc 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 JavaDoc userId = visit.getUserId();
144
145         int i = 0;
146         while (true)
147         {
148             try
149             {
150                 /**
151                  * Note: this allows the user to change thier e-mail
152                  * such that it conflicts with another user! Need yet-another
153                  * IOperations method to perform the update!
154                  *
155                  **/

156
157                 IOperations operations = vengine.getOperations();
158
159                 operations.updatePerson(userId, attributes);
160                 break;
161             }
162             catch (FinderException JavaDoc ex)
163             {
164                 throw new ApplicationRuntimeException(ex);
165             }
166             catch (RemoteException JavaDoc 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 JavaDoc());
182     }
183
184 }
Popular Tags