001: /*
002: * Copyright (c) 1999-2001 Lutris Technologies, Inc. All Rights
003: * Reserved.
004: *
005: * This source code file is distributed by Lutris Technologies, Inc. for
006: * use only by licensed users of product(s) that include this source
007: * file. Use of this source file or the software that uses it is covered
008: * by the terms and conditions of the Lutris Enhydra Development License
009: * Agreement included with this product.
010: *
011: * This Software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
012: * ANY KIND, either express or implied. See the License for the specific terms
013: * governing rights and limitations under the License.
014: *
015: * Contributor(s):
016: *
017: * $Id: Register.java,v 1.1 2006-09-11 12:29:26 sinisa Exp $
018: */
019:
020: package com.lutris.airsent.presentation.customer;
021:
022: import com.lutris.appserver.server.httpPresentation.*;
023: import com.lutris.appserver.server.session.*;
024: import com.lutris.util.*; //import com.lutris.xml.xmlc.*;
025: //import com.lutris.xml.xmlc.html.*;
026: import org.w3c.dom.*;
027: import org.w3c.dom.html.*;
028: import org.enhydra.xml.xmlc.XMLObject;
029: import com.lutris.airsent.spec.AirSentException;
030: import com.lutris.airsent.presentation.BasePO;
031: import com.lutris.airsent.presentation.AirSentPresentationException;
032: import com.lutris.airsent.presentation.AirSentConstants;
033: import com.lutris.airsent.spec.HomeManager;
034:
035: import com.lutris.airsent.spec.customer.Customer;
036: import com.lutris.airsent.spec.address.*;
037:
038: import java.util.*;
039:
040: /**
041: * The customer register page of the AirSent app
042: *
043: */
044: public class Register extends BasePO {
045:
046: /**
047: * Constants
048: */
049: private static final int AUTH_LEVEL = AirSentConstants.UNAUTH_USER;
050: String _login = null;
051: String _password = null;
052: String _firstname = null;
053: String _lastname = null;
054: String _repassword = null;
055: String _email = null;
056: String _postal = null;
057: String _street = null;
058: String _phone = null;
059: String _company = null;
060: String _city = null;
061:
062: /**
063: * Superclass method override.
064: * returns the authorization level necessary to access this po.
065: *
066: * @return int required authorization level
067: */
068: public int getRequiredAuthLevel() {
069: return AUTH_LEVEL;
070: }
071:
072: /**
073: * Default event. Just show the page.
074: */
075: public XMLObject handleDefault() throws HttpPresentationException {
076: return showPage(null);
077: }
078:
079: /**
080: * Shows the register page
081: * @param error messages
082: * @return XMLObject page.
083: */
084: public XMLObject showPage(String errorMsg)
085: throws AirSentPresentationException {
086: RegisterHTML page = (RegisterHTML) myComms.xmlcFactory
087: .create(RegisterHTML.class);
088: try {
089: initCustomer(page);
090: if (null != errorMsg
091: || null != (errorMsg = getSessionData()
092: .getAndClearUserMessage())) {
093: page.setTextErrorText(errorMsg);
094: } else {
095: page.getElementErrorText().getParentNode().removeChild(
096: page.getElementErrorText());
097: }
098: populateForm(page);
099: } catch (Exception e) {
100: throw new AirSentPresentationException(
101: "System error showing page", e);
102: }
103: return page;
104: }
105:
106: /**
107: * Handles the registration.
108: * @return the page.
109: */
110: public XMLObject handleRegister() throws HttpPresentationException {
111: try {
112: getParameters();
113: Customer user = getSessionData().getCustomer();
114: boolean virgin = true;
115: if (user != null) {
116: virgin = false;
117: }
118:
119: String error = validateForm(user);
120: if (error != null) {
121: return showPage(error);
122: }
123:
124: if (_repassword != null && !_repassword.equals(_password)) {
125: return showPage("Please make sure your password and password confirmation match exactly");
126: }
127:
128: HomeManager hf = getApplication().getHomeManager();
129: if (virgin == true) {
130: user = hf.getCustomerManager().findByLogin(_login);
131: if (user != null) {
132: return showPage("The login name " + _login
133: + " is already in use.");
134: } else {
135: user = hf.getCustomerManager().create();
136: }
137: }
138:
139: if (virgin == true) {
140: user.setLogin(_login);
141: user.setPassword(_password);
142: }
143: user.setFirstName(_firstname);
144: user.setLastName(_lastname);
145: user.setEmail(_email);
146: user.setBusiness(_company);
147: Address address = user.getAddress();
148: address.setPostalCode(_postal);
149: address.setStreet1(_street);
150: address.setCity(_city);
151: address.setLocalNumber(_phone);
152: user.save();
153:
154: getSessionData().setCustomer(user);
155: getSessionData()
156: .setUserAuth(AirSentConstants.CUSTOMER_USER);
157: getSessionData().setUserMessage(
158: "Welcome, " + user.getFirstName());
159: throw new ClientPageRedirectException(
160: AirSentConstants.ORDERSTEP1_PO);
161: /*
162: * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run AirSent_pres )
163: * We need to allow AirSent_pres to be functional , response
164: * will be default HTML page with message
165: *
166: */
167: } catch (NullPointerException e) {
168: return showPage("You cannot register for an Account while running AirSent_pres");
169:
170: } catch (Exception e) {
171: throw new AirSentPresentationException(
172: "Exception logging in user: ", e);
173: }
174: }
175:
176: /**
177: *
178: */
179: protected void initCustomer(RegisterHTML page)
180: throws AirSentPresentationException {
181:
182: try {
183: Customer customer = getSessionData().getCustomer();
184: if (customer != null) {
185: _login = customer.getLogin();
186: _password = customer.getPassword();
187: _firstname = customer.getFirstName();
188: _lastname = customer.getLastName();
189: _repassword = customer.getPassword();
190: _email = customer.getEmail();
191: _company = customer.getBusiness();
192: _postal = customer.getAddress().getPostalCode();
193: _street = customer.getAddress().getStreet1();
194: _phone = customer.getAddress().getLocalNumber();
195: _city = customer.getAddress().getCity();
196: }
197: } catch (AirSentException ex) {
198: throw new AirSentPresentationException(
199: "Exception initializing customer: ", ex);
200: }
201: }
202:
203: /**
204: * Populates the form.
205: *
206: */
207: public void populateForm(RegisterHTML page) {
208: Customer customer = getSessionData().getCustomer();
209:
210: if ((_login != null) && (customer != null)) {
211: // if customer exists in session, then poulate MyAccount
212: page.setTextTitle("My Account");
213: page.getElementLogin().getParentNode().removeChild(
214: page.getElementLogin());
215: page.getElementPassword().getParentNode().removeChild(
216: page.getElementPassword());
217: page.getElementPasswordverify().getParentNode()
218: .removeChild(page.getElementPasswordverify());
219: page.getElementCancelBtn().setHref(
220: AirSentConstants.ORDERSTEP1_PO);
221: page.setTextLoginText(_login);
222: page.setTextPasswordText(_password);
223: page.setTextPasswordverifyText(_password);
224: page.setTextTableHeader("My Account");
225: } else {
226:
227: // assume new registration or editing bad submittal
228: page.getElementLoginText().getParentNode().removeChild(
229: page.getElementLoginText());
230: page.getElementLogin().setValue(_login);
231: page.getElementPasswordText().getParentNode().removeChild(
232: page.getElementPasswordText());
233: page.getElementPassword().setValue(_password);
234: page.getElementPasswordverifyText().getParentNode()
235: .removeChild(page.getElementPasswordverifyText());
236: page.getElementPasswordverify().setValue(_password);
237:
238: }
239:
240: if (_firstname != null) {
241: page.getElementFirstname().setValue(_firstname);
242: }
243: if (_lastname != null) {
244: page.getElementLastname().setValue(_lastname);
245: }
246: if (_repassword != null) {
247: page.getElementPasswordverify().setValue(_repassword);
248: }
249: if (_email != null) {
250: page.getElementEmail().setValue(_email);
251: }
252: if (_postal != null) {
253: page.getElementPostalcode().setValue(_postal);
254: }
255: if (_street != null) {
256: page.getElementStreet1().setValue(_street);
257: }
258: if (_phone != null) {
259: page.getElementPhone().setValue(_phone);
260: }
261: if (_city != null) {
262: page.getElementCity().setValue(_city);
263: }
264: if (_company != null) {
265: page.getElementCompany().setValue(_company);
266: }
267:
268: }
269:
270: /**
271: * Validates the form data.
272: * @retutn error String
273: */
274: public String validateForm(Customer user) {
275:
276: if (user == null) {
277:
278: if (isNullField(_login)) {
279: return "Please provide a login";
280: }
281:
282: if (isNullField(_password)) {
283: return "Please provide a password";
284: }
285:
286: if (isNullField(_repassword)) {
287: return "Please provide a password verification";
288: }
289:
290: }
291:
292: if (isNullField(_firstname)) {
293: return "Please provide a first name";
294: }
295:
296: if (isNullField(_lastname)) {
297: return "Please provide a last name";
298: }
299:
300: if (isNullField(_email)) {
301: return "Please provide an email";
302: }
303:
304: if (isNullField(_postal)) {
305: return "Please provide a postal code";
306: }
307:
308: if (isNullField(_street)) {
309: return "Please provide a street address";
310: }
311:
312: if (isNullField(_phone)) {
313: return "Please provide a phone number";
314: }
315:
316: if (isNullField(_company)) {
317: return "Please provide a company name";
318: }
319:
320: if (isNullField(_city)) {
321: return "Please provide a city";
322: }
323:
324: return null;
325: }
326:
327: /**
328: *
329: */
330: private void getParameters() throws AirSentPresentationException {
331:
332: try {
333: _login = this .getComms().request
334: .getParameter(AirSentConstants.LOGIN_NAME);
335: _password = this .getComms().request
336: .getParameter(AirSentConstants.PASSWORD_NAME);
337: _firstname = this .getComms().request
338: .getParameter(AirSentConstants.FIRST_NAME);
339: _lastname = this .getComms().request
340: .getParameter(AirSentConstants.LAST_NAME);
341: _repassword = this .getComms().request
342: .getParameter(AirSentConstants.PASSWORD_VERIFY_NAME);
343: _email = this .getComms().request
344: .getParameter(AirSentConstants.EMAIL_NAME);
345: _postal = this .getComms().request
346: .getParameter(AirSentConstants.POSTAL_CODE_NAME);
347: _street = this .getComms().request
348: .getParameter(AirSentConstants.STREET1_NAME);
349: _phone = this .getComms().request
350: .getParameter(AirSentConstants.PHONE_NAME);
351: _company = this .getComms().request
352: .getParameter(AirSentConstants.COMPANY_NAME);
353: _city = this .getComms().request
354: .getParameter(AirSentConstants.CITY_NAME);
355: } catch (Exception e) {
356: throw new AirSentPresentationException(
357: "ERROR: getting parameters", e);
358: }
359: }
360: }
|