KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > barracudaDiscRack > presentation > personMgmt > Register


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: Register.java,v 1.4 2004/12/03 14:12:34 slobodan Exp $
22  */

23
24 package barracudaDiscRack.presentation.personMgmt;
25
26 import barracudaDiscRack.business.person.*;
27 import barracudaDiscRack.presentation.BasePO;
28 import com.lutris.appserver.server.httpPresentation.*;
29 import com.lutris.appserver.server.session.*;
30 import com.lutris.util.*;
31 import org.enhydra.xml.xmlc.*;
32 import org.enhydra.xml.xmlc.html.*;
33 import org.w3c.dom.*;
34 import org.w3c.dom.html.*;
35 import org.enhydra.xml.xmlc.XMLObject;
36 import barracudaDiscRack.presentation.DiscRackPresentationException;
37 import barracudaDiscRack.business.DiscRackBusinessException;
38
39 /**
40  * Register.java handles the user registration functionality
41  * of the DiscRack app.
42  *
43  * @author
44  * @version
45  */

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

51     private static String JavaDoc LOGINNAME = "login";
52     private static String JavaDoc PASSWORD = "password";
53     private static String JavaDoc REPASSWORD = "repassword";
54     private static String JavaDoc FIRSTNAME = "firstname";
55     private static String JavaDoc LASTNAME = "lastname";
56
57     /**
58      * Superclass method override
59      */

60     public boolean loggedInUserRequired() {
61         return false;
62     }
63
64     /**
65      * Default event. Just show the page.
66      *
67      * @return html document
68      * @exception HttpPresentationException
69      */

70     public XMLObject handleDefault()
71         throws HttpPresentationException {
72         return showPage(null);
73     }
74
75     /**
76      * process login data, save fields to PersonManager if correct,
77      * otherwise output error messages
78      *
79      * @return html document
80      * @exception HttpPresentationException
81      */

82     public XMLObject handleRegister()
83         throws HttpPresentationException {
84
85         String JavaDoc login = this.getComms().request.getParameter(LOGINNAME);
86         String JavaDoc password = this.getComms().request.getParameter(PASSWORD);
87         String JavaDoc firstname = this.getComms().request.getParameter(FIRSTNAME);;
88         String JavaDoc lastname = this.getComms().request.getParameter(LASTNAME);
89         String JavaDoc repassword = this.getComms().request.getParameter(REPASSWORD);
90
91         // if login or password field is empty, generate error and redirect to this PO
92
if (login.length() == 0 || password.length() ==0 ||
93             firstname.length() == 0 || lastname.length() == 0) {
94             return showPage("Missing information. Please make sure all fields are filled out exactly");
95         }
96
97         //Check that password was duplicated correctly
98
if(!repassword.equals(password)) {
99             return showPage("Please make sure your password and password confirmation match exactly");
100         }
101
102         try {
103             // Now check that the login name is not taken.
104
if(null == PersonFactory.findPerson(login)) {
105                 Person user = new Person();
106                 user.setLogin(login);
107                 user.setPassword(password);
108                 user.setFirstname(firstname);
109                 user.setLastname(lastname);
110                 //Add the user to the database.
111
user.save();
112                 this.getSessionData().setUserMessage("You are registered, " +
113                                                      user.getFirstname() + ", please log in");
114                 throw new ClientPageRedirectException(getComms().request.getApplicationPath() + LOGIN_PAGE);
115             } else {
116                 // Login name already taken
117
// Redirect to this same PO with informative error message
118
return showPage("The login name " + login + " is already taken. Please try another.");
119             }
120         } catch(DiscRackBusinessException ex) {
121             throw new DiscRackPresentationException("Exception logging in user: ", ex);
122         }
123     }
124
125     /**
126      * display page
127      *
128      * @param errorMsg, the error messages
129      * @return html document
130      * @exception HttpPresentationException
131      */

132     public XMLObject showPage(String JavaDoc errorMsg)
133         throws HttpPresentationException {
134
135         RegisterHTML page = (RegisterHTML)myComms.xmlcFactory.create(RegisterHTML.class);
136
137         if(null == errorMsg) {
138             page.getElementErrorText().getParentNode().removeChild(page.getElementErrorText());
139         } else {
140             page.setTextErrorText(errorMsg);
141         }
142
143         if(null != this.getComms().request.getParameter(LOGINNAME)) {
144             page.getElementLogin().setValue(this.getComms().request.getParameter(LOGINNAME));
145         }
146         if(null != this.getComms().request.getParameter(FIRSTNAME)) {
147             page.getElementFirstname().setValue(this.getComms().request.getParameter(FIRSTNAME));
148         }
149         if(null != this.getComms().request.getParameter(LASTNAME)) {
150             page.getElementLastname().setValue(this.getComms().request.getParameter(LASTNAME));
151         }
152
153         return page;
154     }
155 }
156
Popular Tags