001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: Register.java,v 1.1 2006-09-11 12:32:06 sinisa Exp $
022: */
023:
024: package discRack.presentation.personMgmt;
025:
026: import discRack.presentation.BasePO;
027: import discRack.presentation.DiscRackPresentationException;
028: import discRack.spec.*;
029:
030: import com.lutris.appserver.server.httpPresentation.*;
031: import com.lutris.appserver.server.session.*;
032: import com.lutris.util.*; //import com.lutris.xml.xmlc.*;
033: //import com.lutris.xml.xmlc.html.*;
034: import org.w3c.dom.*;
035: import org.w3c.dom.html.*;
036: import org.enhydra.xml.xmlc.XMLObject;
037:
038: import org.enhydra.dods.exceptions.AssertionDataObjectException;
039:
040: /**
041: * Register.java handles the user registration functionality
042: * of the DiscRack app.
043: *
044: */
045: public class Register extends BasePO {
046:
047: /**
048: * Constants
049: */
050: private static String LOGINNAME = "login";
051: private static String PASSWORD = "password";
052: private static String REPASSWORD = "repassword";
053: private static String FIRSTNAME = "firstname";
054: private static String LASTNAME = "lastname";
055:
056: /**
057: * Superclass method override
058: */
059: public boolean loggedInUserRequired() {
060: return false;
061: }
062:
063: /**
064: * Default event. Just show the page.
065: *
066: * @return html document
067: * @exception HttpPresentationException
068: */
069: public XMLObject handleDefault() throws HttpPresentationException {
070: return showPage(null);
071: }
072:
073: /**
074: * process login data, save fields to PersonManager if correct,
075: * otherwise output error messages
076: *
077: * @return html document
078: * @exception HttpPresentationException
079: */
080: public XMLObject handleRegister() throws HttpPresentationException {
081:
082: String login = this .getComms().request.getParameter(LOGINNAME);
083: String password = this .getComms().request
084: .getParameter(PASSWORD);
085: String firstname = this .getComms().request
086: .getParameter(FIRSTNAME);
087: ;
088: String lastname = this .getComms().request
089: .getParameter(LASTNAME);
090: String repassword = this .getComms().request
091: .getParameter(REPASSWORD);
092:
093: // if login or password field is empty, generate error and redirect to this PO
094: if (login.length() == 0 || password.length() == 0
095: || firstname.length() == 0 || lastname.length() == 0) {
096: return showPage("Missing information. Please make sure all fields are filled out exactly");
097: }
098:
099: //Check that password was duplicated correctly
100: if (!repassword.equals(password)) {
101: return showPage("Please make sure your password and password confirmation match exactly");
102: }
103:
104: /*
105: * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run discRack_pres )
106: * We need to allow discRack_pres to be functional , response
107: * will be default HTML page with message
108: */
109: try {
110:
111: Person user = ((PersonGenerator) PersonGeneratorFactory
112: .getPersonGenerator("discRack.business.person.PersonGeneratorImpl"))
113: .findPerson(login);
114:
115: // Now check that the login name is not taken.
116: if (null == user) {
117:
118: user = PersonFactory
119: .getPerson("discRack.business.person.PersonImpl");
120:
121: user.setLogin(login);
122: user.setPassword(password);
123: user.setFirstname(firstname);
124: user.setLastname(lastname);
125: //Add the user to the database.
126: user.save();
127:
128: this .getSessionData().setUserMessage(
129: "You are registered, " + user.getFirstname()
130: + ", please log in");
131: throw new ClientPageRedirectException(
132: getComms().request.getApplicationPath()
133: + LOGIN_PAGE);
134: } else {
135: // Login name already taken
136: // Redirect to this same PO with informative error message
137: return showPage("The login name " + login
138: + " is already taken. Please try another.");
139: }
140: } catch (NullPointerException ex) {
141: return showPage("You canot register while you in discRack_pres ");
142:
143: } catch (AssertionDataObjectException ex) {
144: return showPage("Table person is read-only: can't register another person.");
145: } catch (DiscRackException ex) {
146: throw new DiscRackPresentationException(
147: "Exception logging in user: ", ex);
148: }
149: }
150:
151: /**
152: * display page
153: *
154: * @param errorMsg the error messages
155: * @return html document
156: * @exception HttpPresentationException
157: */
158: public XMLObject showPage(String errorMsg)
159: throws HttpPresentationException {
160:
161: RegisterHTML page = (RegisterHTML) myComms.xmlcFactory
162: .create(RegisterHTML.class);
163:
164: if (null == errorMsg) {
165: page.getElementErrorText().getParentNode().removeChild(
166: page.getElementErrorText());
167: } else {
168: page.setTextErrorText(errorMsg);
169: }
170:
171: if (null != this.getComms().request.getParameter(LOGINNAME)) {
172: page.getElementLogin().setValue(
173: this.getComms().request.getParameter(LOGINNAME));
174: }
175: if (null != this.getComms().request.getParameter(FIRSTNAME)) {
176: page.getElementFirstname().setValue(
177: this.getComms().request.getParameter(FIRSTNAME));
178: }
179: if (null != this.getComms().request.getParameter(LASTNAME)) {
180: page.getElementLastname().setValue(
181: this.getComms().request.getParameter(LASTNAME));
182: }
183:
184: return page;
185: }
186: }
|