KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > airsent > presentation > customer > Register


1 /*
2  * Copyright (c) 1999-2001 Lutris Technologies, Inc. All Rights
3  * Reserved.
4  *
5  * This source code file is distributed by Lutris Technologies, Inc. for
6  * use only by licensed users of product(s) that include this source
7  * file. Use of this source file or the software that uses it is covered
8  * by the terms and conditions of the Lutris Enhydra Development License
9  * Agreement included with this product.
10  *
11  * This Software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
12  * ANY KIND, either express or implied. See the License for the specific terms
13  * governing rights and limitations under the License.
14  *
15  * Contributor(s):
16  *
17  * $Id: Register.java,v 1.1 2004/08/16 09:33:18 slobodan Exp $
18  */

19
20 package com.lutris.airsent.presentation.customer;
21
22 import com.lutris.appserver.server.httpPresentation.*;
23 import com.lutris.appserver.server.session.*;
24 import com.lutris.util.*;
25 //import com.lutris.xml.xmlc.*;
26
//import com.lutris.xml.xmlc.html.*;
27
import org.w3c.dom.*;
28 import org.w3c.dom.html.*;
29 import org.enhydra.xml.xmlc.XMLObject;
30 import com.lutris.airsent.spec.AirSentException;
31 import com.lutris.airsent.presentation.BasePO;
32 import com.lutris.airsent.presentation.AirSentPresentationException;
33 import com.lutris.airsent.presentation.AirSentConstants;
34 import com.lutris.airsent.spec.HomeManager;
35
36
37 import com.lutris.airsent.spec.customer.Customer;
38 import com.lutris.airsent.spec.address.*;
39
40 import java.util.*;
41
42 /**
43  * The customer register page of the AirSent app
44  *
45  */

46 public class Register extends BasePO {
47
48     /**
49      * Constants
50      */

51     private static final int AUTH_LEVEL = AirSentConstants.UNAUTH_USER;
52     String JavaDoc _login = null;
53     String JavaDoc _password = null;
54     String JavaDoc _firstname = null;
55     String JavaDoc _lastname = null;
56     String JavaDoc _repassword = null;
57     String JavaDoc _email = null;
58     String JavaDoc _postal = null;
59     String JavaDoc _street = null;
60     String JavaDoc _phone = null;
61     String JavaDoc _company = null;
62     String JavaDoc _city = null;
63
64     /**
65      * Superclass method override.
66      * returns the authorization level necessary to access this po.
67      *
68      * @return int required authorization level
69      */

70     public int getRequiredAuthLevel() {
71     return AUTH_LEVEL;
72     }
73
74     /**
75      * Default event. Just show the page.
76      */

77     public XMLObject handleDefault() throws HttpPresentationException {
78     return showPage(null);
79     }
80
81     /**
82      * Shows the register page
83      * @param error messages
84      * @return XMLObject page.
85      */

86     public XMLObject showPage(String JavaDoc errorMsg)
87         throws AirSentPresentationException {
88     RegisterHTML page = (RegisterHTML) myComms.xmlcFactory.create(RegisterHTML.class);
89     try {
90         initCustomer(page);
91         if (null != errorMsg || null != (errorMsg = getSessionData().getAndClearUserMessage())) {
92         page.setTextErrorText(errorMsg);
93         } else {
94         page.getElementErrorText().getParentNode().removeChild(page.getElementErrorText());
95         }
96             populateForm(page);
97     } catch (Exception JavaDoc e) {
98         throw new AirSentPresentationException("System error showing page", e);
99     }
100     return page;
101     }
102
103     /**
104      * Handles the registration.
105      * @return the page.
106      */

107     public XMLObject handleRegister()
108         throws HttpPresentationException {
109         try {
110             getParameters();
111             Customer user = getSessionData().getCustomer();
112             boolean virgin = true;
113             if (user != null) {
114                 virgin = false;
115             }
116
117             String JavaDoc error = validateForm(user);
118             if (error != null) {
119                 return showPage(error);
120             }
121             
122             if (_repassword != null && !_repassword.equals(_password)) {
123                 return showPage("Please make sure your password and password confirmation match exactly");
124             }
125             
126             HomeManager hf = getApplication().getHomeManager();
127             if (virgin == true) {
128                 user = hf.getCustomerManager().findByLogin(_login);
129                 if (user != null) {
130                     return showPage("The login name " + _login + " is already in use.");
131                 } else {
132                     user = hf.getCustomerManager().create();
133                 }
134             }
135             
136             if (virgin == true) {
137                 user.setLogin(_login);
138         user.setPassword(_password);
139         }
140         user.setFirstName(_firstname);
141         user.setLastName(_lastname);
142         user.setEmail(_email);
143         user.setBusiness(_company);
144             Address address = user.getAddress();
145         address.setPostalCode(_postal);
146         address.setStreet1(_street);
147         address.setCity(_city);
148         address.setLocalNumber(_phone);
149         user.save();
150
151             getSessionData().setCustomer(user);
152             getSessionData().setUserAuth(AirSentConstants.CUSTOMER_USER);
153             getSessionData().setUserMessage("Welcome, " + user.getFirstName());
154             throw new ClientPageRedirectException(AirSentConstants.ORDERSTEP1_PO);
155 /*
156  * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run AirSent_pres )
157  * We need to allow AirSent_pres to be functional , response
158  * will be default HTML page with message
159  *
160  */

161       }catch(NullPointerException JavaDoc e){
162         return showPage("You cannot register for an Account while running AirSent_pres") ;
163             
164     } catch (Exception JavaDoc e) {
165         throw new AirSentPresentationException("Exception logging in user: ", e);
166     }
167     }
168
169     /**
170      *
171      */

172     protected void initCustomer(RegisterHTML page)
173         throws AirSentPresentationException {
174
175     try {
176             Customer customer = getSessionData().getCustomer();
177         if(customer != null) {
178                 _login = customer.getLogin();
179         _password = customer.getPassword();
180         _firstname = customer.getFirstName();
181         _lastname = customer.getLastName();
182         _repassword = customer.getPassword();
183         _email = customer.getEmail();
184         _company = customer.getBusiness();
185         _postal = customer.getAddress().getPostalCode();
186         _street = customer.getAddress().getStreet1();
187         _phone = customer.getAddress().getLocalNumber();
188         _city = customer.getAddress().getCity();
189             }
190         } catch (AirSentException ex) {
191             throw new AirSentPresentationException("Exception initializing customer: ", ex);
192         }
193     }
194
195     /**
196      * Populates the form.
197      *
198      */

199     public void populateForm(RegisterHTML page) {
200         Customer customer = getSessionData().getCustomer();
201
202     if ((_login != null) && (customer != null)) {
203             // if customer exists in session, then poulate MyAccount
204
page.setTextTitle("My Account");
205             page.getElementLogin().getParentNode().removeChild(page.getElementLogin());
206         page.getElementPassword().getParentNode().removeChild(page.getElementPassword());
207         page.getElementPasswordverify().getParentNode().removeChild(page.getElementPasswordverify());
208             page.getElementCancelBtn().setHref(AirSentConstants.ORDERSTEP1_PO);
209             page.setTextLoginText(_login);
210         page.setTextPasswordText(_password);
211         page.setTextPasswordverifyText(_password);
212             page.setTextTableHeader("My Account");
213         } else {
214             
215         // assume new registration or editing bad submittal
216
page.getElementLoginText().getParentNode().removeChild(page.getElementLoginText());
217         page.getElementLogin().setValue(_login);
218         page.getElementPasswordText().getParentNode().removeChild(page.getElementPasswordText());
219         page.getElementPassword().setValue(_password);
220         page.getElementPasswordverifyText().getParentNode().removeChild(page.getElementPasswordverifyText());
221          page.getElementPasswordverify().setValue(_password);
222         
223     }
224     
225     if(_firstname != null) {
226         page.getElementFirstname().setValue(_firstname);
227         }
228     if(_lastname != null) {
229         page.getElementLastname().setValue(_lastname);
230         }
231     if(_repassword != null) {
232         page.getElementPasswordverify().setValue(_repassword);
233      }
234     if(_email != null) {
235         page.getElementEmail().setValue(_email);
236         }
237     if(_postal != null) {
238         page.getElementPostalcode().setValue(_postal);
239         }
240     if(_street != null) {
241         page.getElementStreet1().setValue(_street);
242         }
243     if(_phone != null) {
244         page.getElementPhone().setValue(_phone);
245         }
246     if(_city != null) {
247         page.getElementCity().setValue(_city);
248         }
249     if(_company != null) {
250         page.getElementCompany().setValue(_company);
251         }
252
253     }
254     
255     /**
256      * Validates the form data.
257      * @retutn error String
258      */

259     public String JavaDoc validateForm(Customer user) {
260
261         if (user == null) {
262
263             if (isNullField(_login)) {
264                 return "Please provide a login";
265             }
266         
267         if (isNullField(_password)) {
268         return "Please provide a password";
269         }
270         
271         if (isNullField(_repassword)) {
272         return "Please provide a password verification";
273         }
274         
275         }
276
277     if (isNullField(_firstname)) {
278         return "Please provide a first name";
279     }
280     
281     if (isNullField(_lastname)) {
282         return "Please provide a last name";
283     }
284     
285     if (isNullField(_email)) {
286         return "Please provide an email";
287     }
288
289     if (isNullField(_postal)) {
290         return "Please provide a postal code";
291     }
292
293     if (isNullField(_street)) {
294         return "Please provide a street address";
295     }
296
297     if (isNullField(_phone)) {
298         return "Please provide a phone number";
299     }
300
301     if (isNullField(_company)) {
302         return "Please provide a company name";
303     }
304
305     if (isNullField(_city)) {
306         return "Please provide a city";
307     }
308
309     return null;
310     }
311
312     /**
313      *
314      */

315     private void getParameters()
316         throws AirSentPresentationException {
317         
318         try {
319             _login = this.getComms().request.getParameter(AirSentConstants.LOGIN_NAME);
320             _password = this.getComms().request.getParameter(AirSentConstants.PASSWORD_NAME);
321             _firstname = this.getComms().request.getParameter(AirSentConstants.FIRST_NAME);
322             _lastname = this.getComms().request.getParameter(AirSentConstants.LAST_NAME);
323             _repassword = this.getComms().request.getParameter(AirSentConstants.PASSWORD_VERIFY_NAME);
324             _email = this.getComms().request.getParameter(AirSentConstants.EMAIL_NAME);
325             _postal = this.getComms().request.getParameter(AirSentConstants.POSTAL_CODE_NAME);
326             _street = this.getComms().request.getParameter(AirSentConstants.STREET1_NAME);
327             _phone = this.getComms().request.getParameter(AirSentConstants.PHONE_NAME);
328             _company = this.getComms().request.getParameter(AirSentConstants.COMPANY_NAME);
329             _city = this.getComms().request.getParameter(AirSentConstants.CITY_NAME);
330         } catch (Exception JavaDoc e) {
331             throw new AirSentPresentationException("ERROR: getting parameters", e);
332         }
333     }
334 }
335
336
337
338
339
Popular Tags