1 37 38 package com.sun.j2ee.blueprints.consumerwebsite.actions; 39 40 import javax.servlet.http.*; 41 42 import com.sun.j2ee.blueprints.signon.web.SignOnFilter; 44 45 import com.sun.j2ee.blueprints.waf.controller.Event; 47 import com.sun.j2ee.blueprints.waf.controller.web.html.*; 48 49 import com.sun.j2ee.blueprints.customer.*; 51 52 import com.sun.j2ee.blueprints.signon.web.SignOnFilter; 54 import com.sun.j2ee.blueprints.signon.SignOnFacade; 55 56 import com.sun.j2ee.blueprints.consumerwebsite.*; 58 import com.sun.j2ee.blueprints.consumerwebsite.exceptions.CustomerException; 59 import com.sun.j2ee.blueprints.consumerwebsite.exceptions.SignOnException; 60 65 public final class CustomerHTMLAction extends HTMLActionSupport { 66 67 public static final String ACCOUNT_READ_ACTION = "readAccount"; 68 public static final String ACCOUNT_CREATE_ACTION = "createAccount"; 69 70 76 public Event perform(HttpServletRequest request) 77 throws HTMLActionException { 78 79 CustomerBean resultBean = null; 80 81 String targetAction =request.getParameter("target_action"); 83 HttpSession session = request.getSession(); 85 AdventureComponentManager acm = 86 (AdventureComponentManager)session.getAttribute(AdventureKeys.COMPONENT_MANAGER); 87 CustomerFacade facade = acm.getCustomerFacade(session); 89 if ((targetAction != null) && 90 targetAction.equals(ACCOUNT_CREATE_ACTION)) { 91 resultBean = createAccount(request, facade); 92 } else { 93 Boolean signedOn = (Boolean )request.getSession().getAttribute(SignOnFilter.SIGNED_ON_USER); 94 if ((signedOn != null) && signedOn.booleanValue()) { 96 resultBean = readAccount(session,facade); 98 } else { 99 throw new CustomerException("CustomerHTMLAction: User is not signed on."); 100 } 101 } 102 session.setAttribute(AdventureKeys.CUSTOMER_BEAN, resultBean); 104 return null; 105 } 106 107 110 protected void validate(String userId) 111 throws CustomerException { 112 if ((userId == null) || userId.trim().length() == 0) { 113 throw new CustomerException("Unfortunately, there was a problem: The userId must have data. Your request has not been sent."); 114 } 115 } 116 117 120 public CustomerBean readAccount(HttpSession session, 121 CustomerFacade facade) 122 throws CustomerException { 123 124 String userId = (String )session.getAttribute(SignOnFilter.USER_NAME); 125 Account acct = null; 126 try { 128 acct = facade.getAccount(userId); 129 }catch (Exception e) { 132 e.printStackTrace(); 133 throw new CustomerException("CustomerHTMLAction:: CustomerAppException accessing Customer Component: ", e); 134 } 135 136 return new CustomerBean(acct); 137 } 138 139 142 private CustomerBean createAccount(HttpServletRequest request, 143 CustomerFacade facade) 144 throws CustomerException, SignOnException { 145 146 Boolean result = new Boolean (false); 148 String userId = null; 149 150 result = new Boolean (createSignOn(request)); 152 if (result.booleanValue()) { 153 userId = (String )request.getSession().getAttribute(SignOnFilter.USER_NAME); 154 } else { 155 throw new SignOnException("CustomerHTMLAction: failed to create SignOn for " + userId); 156 } 157 158 request.getSession().setAttribute(SignOnFilter.SIGNED_ON_USER, result); 161 String familyName =request.getParameter("acct_familyName"); 162 String givenName =request.getParameter("acct_givenName"); 163 String telephone =request.getParameter("acct_telephone"); 164 String email =request.getParameter("acct_email"); 165 String street1 =request.getParameter("acct_street1"); 166 String street2 =request.getParameter("acct_street2"); 167 String city =request.getParameter("acct_city"); 168 String state =request.getParameter("acct_state"); 169 String zipCode =request.getParameter("acct_zipCode"); 170 String country =request.getParameter("acct_country"); 171 172 com.sun.j2ee.blueprints.customer.Address address = 173 new com.sun.j2ee.blueprints.customer.Address(street1, street2, city, state, zipCode, country); 174 com.sun.j2ee.blueprints.customer.ContactInformation info = 175 new com.sun.j2ee.blueprints.customer.ContactInformation(familyName, givenName,telephone, 176 email, address); 177 178 com.sun.j2ee.blueprints.customer.Account acct = new com.sun.j2ee.blueprints.customer.Account(userId, info); 179 180 try { 182 facade.createAccount(acct); 183 } catch (Exception e) { 186 System.out.println("**** Customer Error"); 187 e.printStackTrace(); 188 throw new CustomerException("CustomerBD:: CustomerAppException Error Creating Customer", e); 189 } 190 191 return new CustomerBean(acct); 193 } 194 195 198 private boolean createSignOn(HttpServletRequest request) 199 throws SignOnException { 200 try { 201 HttpSession session = request.getSession(); 202 String userId = (String )session.getAttribute(AdventureKeys.SIGN_ON_TEMP_USERNAME); 203 String password = (String )session.getAttribute(AdventureKeys.SIGN_ON_TEMP_PASSWORD); 204 session.removeAttribute(AdventureKeys.SIGN_ON_TEMP_USERNAME); 205 session.removeAttribute(AdventureKeys.SIGN_ON_TEMP_PASSWORD); 206 session.setAttribute(SignOnFilter.USER_NAME, userId); 208 AdventureComponentManager acm = 209 (AdventureComponentManager)session.getAttribute(AdventureKeys.COMPONENT_MANAGER); 210 SignOnFacade facade = acm.getSignOnFacade(session); 211 facade.createSignOn(userId, password); 212 return true; 213 } catch (Exception e) { 216 throw new SignOnException("SignOnHTMLAction:: Exception creating new signon: ", e); 217 } 218 } 219 } 220 | Popular Tags |