KickJava   Java API By Example, From Geeks To Geeks.

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


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
19 import javax.ejb.CreateException JavaDoc;
20
21 import org.apache.hivemind.ApplicationRuntimeException;
22 import org.apache.tapestry.IRequestCycle;
23 import org.apache.tapestry.form.IFormComponent;
24 import org.apache.tapestry.html.BasePage;
25 import org.apache.tapestry.valid.IValidationDelegate;
26 import org.apache.tapestry.vlib.IErrorProperty;
27 import org.apache.tapestry.vlib.VirtualLibraryEngine;
28 import org.apache.tapestry.vlib.ejb.IOperations;
29 import org.apache.tapestry.vlib.ejb.Person;
30 import org.apache.tapestry.vlib.ejb.RegistrationException;
31
32 /**
33  * Invoked from the {@link Login} page, to allow a user to register
34  * into the system on-the-fly.
35  *
36  * @author Howard Lewis Ship
37  * @version $Id: Register.java,v 1.9 2004/04/30 15:17:21 hlship Exp $
38  *
39  **/

40
41 public abstract class Register extends BasePage implements IErrorProperty
42 {
43
44     public abstract String JavaDoc getFirstName();
45
46     public abstract String JavaDoc getLastName();
47
48     public abstract String JavaDoc getEmail();
49
50     public abstract String JavaDoc getPassword1();
51
52     public abstract String JavaDoc getPassword2();
53
54     public abstract void setPassword1(String JavaDoc value);
55
56     public abstract void setPassword2(String JavaDoc value);
57
58     private IValidationDelegate getValidationDelegate()
59     {
60         return (IValidationDelegate) getBeans().getBean("delegate");
61     }
62
63     private void setErrorField(String JavaDoc componentId, String JavaDoc message)
64     {
65         IValidationDelegate delegate = getValidationDelegate();
66         IFormComponent field = (IFormComponent) getComponent(componentId);
67
68         delegate.setFormComponent(field);
69         delegate.record(message, null);
70     }
71
72     private void clear(String JavaDoc componentId)
73     {
74         IValidationDelegate delegate = getValidationDelegate();
75         IFormComponent component = (IFormComponent) getComponent(componentId);
76
77         delegate.setFormComponent(component);
78         delegate.recordFieldInputValue(null);
79     }
80
81     public void attemptRegister(IRequestCycle cycle)
82     {
83         IValidationDelegate delegate = getValidationDelegate();
84
85         String JavaDoc password1 = getPassword1();
86         String JavaDoc password2 = getPassword2();
87
88         setPassword1(null);
89         setPassword2(null);
90
91         clear("inputPassword1");
92         clear("inputPassword2");
93
94         if (delegate.getHasErrors())
95             return;
96         // Note: we know password1 and password2 are not null
97
// because they are required fields.
98

99         if (!password1.equals(password2))
100         {
101             setErrorField("inputPassword1", getMessage("password-must-match"));
102             return;
103         }
104
105         VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine();
106         Login login = (Login) cycle.getPage("Login");
107
108         int i = 0;
109         while (true)
110         {
111             try
112             {
113                 IOperations bean = vengine.getOperations();
114                 Person user =
115                     bean.registerNewUser(getFirstName(), getLastName(), getEmail(), password1);
116
117                 // Ask the login page to return us to the proper place, as well
118
// as set a cookie identifying the user for next time.
119

120                 login.loginUser(user, cycle);
121
122                 break;
123             }
124             catch (RegistrationException ex)
125             {
126                 setError(ex.getMessage());
127                 return;
128             }
129             catch (CreateException JavaDoc ex)
130             {
131                 throw new ApplicationRuntimeException(ex);
132             }
133             catch (RemoteException JavaDoc ex)
134             {
135                 vengine.rmiFailure("Remote exception registering new user.", ex, i++);
136             }
137         }
138     }
139 }
Popular Tags