1 25 26 package com.j2biz.blogunity.web.actions.exec; 27 28 import java.util.ArrayList ; 29 import java.util.Calendar ; 30 import java.util.Locale ; 31 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 34 import javax.servlet.http.HttpSession ; 35 36 import org.apache.commons.lang.StringUtils; 37 38 import com.j2biz.blogunity.dao.UserDAO; 39 import com.j2biz.blogunity.exception.BlogunityException; 40 import com.j2biz.blogunity.i18n.I18N; 41 import com.j2biz.blogunity.i18n.I18NStatusFactory; 42 import com.j2biz.blogunity.pojo.User; 43 import com.j2biz.blogunity.util.BlogUtils; 44 import com.j2biz.blogunity.util.PasswordEncryptionService; 45 import com.j2biz.blogunity.util.RenderUtils; 46 import com.j2biz.blogunity.web.ActionResultFactory; 47 import com.j2biz.blogunity.web.FormError; 48 import com.j2biz.blogunity.web.FormErrorList; 49 import com.j2biz.blogunity.web.IActionResult; 50 import com.j2biz.blogunity.web.actions.AbstractAction; 51 52 59 public class RegisterUserExecAction extends AbstractAction { 60 61 private static final IActionResult REGISTER_FORM_FORWARD = ActionResultFactory 62 .buildForward("/jsp/register.jsp"); 63 64 private static final IActionResult REGISTER_SUCCESS_FORWARD = ActionResultFactory 65 .buildForward("/jsp/registerSuccess.jsp"); 66 67 73 public IActionResult execute(HttpServletRequest request, HttpServletResponse response) 74 throws BlogunityException { 75 76 if (!com.j2biz.blogunity.BlogunityManager.getSystemConfiguration().isAllowNewUsers()) 77 throw new BlogunityException(I18NStatusFactory 78 .create(I18N.ERRORS.USER_REGISTERING_NOT_ALLOWED)); 79 80 User ux = null; 81 HttpSession session = request.getSession(false); 82 if (session != null) ux = (User) session.getAttribute("user"); 83 84 if (ux != null) 85 throw new BlogunityException(I18NStatusFactory 86 .create(I18N.ERRORS.USER_REGISTERING_ERROR_CAUSE_ALREADY_EXISTS)); 87 88 String nickname = request.getParameter("nickname"); 89 String password = request.getParameter("password"); 90 String firstname = request.getParameter("firstname"); 91 String lastname = request.getParameter("lastname"); 92 String email = request.getParameter("email"); 93 String language = request.getParameter("language"); 94 String icq = request.getParameter("icq"); 95 String msn = request.getParameter("msn"); 96 String yahoo = request.getParameter("yahoo"); 97 String jabber = request.getParameter("jabber"); 98 String homepage = request.getParameter("homepage"); 99 String sex = request.getParameter("sex"); 100 String showEmailStr = request.getParameter("showEmail"); 101 boolean showEmail = false; 102 if (showEmailStr != null) showEmail = showEmailStr.equalsIgnoreCase("on") ? true : false; 103 104 String bio = request.getParameter("bio"); 105 String day = request.getParameter("day"); 106 String month = request.getParameter("month"); 107 String year = request.getParameter("year"); 108 109 int i_day = 1; 110 try { 111 i_day = Integer.parseInt(day); 112 } catch (NumberFormatException e1) { 113 114 } 115 116 int i_month = 0; 117 try { 118 i_month = Integer.parseInt(month); 119 } catch (NumberFormatException e1) { 120 121 } 122 123 int i_year = 2000; 124 try { 125 i_year = Integer.parseInt(year); 126 } catch (NumberFormatException e1) { 127 128 } 129 Calendar c = Calendar.getInstance(); 130 c.set(i_year, i_month - 1, i_day); 131 132 User user = new User(nickname, password, email, firstname, lastname); 133 user.setLanguage(StringUtils.isEmpty(language) ? null : new Locale (language)); 134 user.setIcq(StringUtils.isEmpty(icq) ? null : icq); 135 user.setMsn(StringUtils.isEmpty(msn) ? null : msn); 136 user.setYahoo(StringUtils.isEmpty(yahoo) ? null : yahoo); 137 user.setJabber(StringUtils.isEmpty(jabber) ? null : jabber); 138 user.setHomepage(StringUtils.isEmpty(homepage) ? null : homepage); 139 user.setBioRaw(StringUtils.isEmpty(bio) ? null : bio); 140 user.setBio(StringUtils.isEmpty(bio) ? null : RenderUtils.renderText(bio)); 141 user.setBirthday(c.getTime()); 142 143 try { 144 user.setSex(Integer.parseInt(sex)); 145 } catch (NumberFormatException e) { 146 user.setSex(User.MALE); 147 } 148 user.setShowEmail(showEmail); 149 150 String [] localeArray = Locale.getISOLanguages(); 151 ArrayList locales = new ArrayList (); 152 for (int i = 0; i < localeArray.length; i++) { 153 locales.add(new Locale (localeArray[i])); 154 } 155 request.setAttribute("locales", locales); 156 request.setAttribute("newUser", user); 157 158 FormErrorList errors = new FormErrorList(); 159 160 if (StringUtils.isEmpty(nickname)) errors.add(new FormError("nickname", 161 "Nickname is empty!")); 162 else if (!BlogUtils.getInstance().isValidNickname(nickname)) 163 errors.add(new FormError("nickname", "Nickname is not valid!")); 164 if (StringUtils.isEmpty(password)) 165 errors.add(new FormError("password", "Password is empty!")); 166 if (StringUtils.isEmpty(firstname)) 167 errors.add(new FormError("firstname", "Firstname is empty!")); 168 if (StringUtils.isEmpty(lastname)) 169 errors.add(new FormError("lastname", "Lastname is empty!")); 170 if (StringUtils.isEmpty(language)) { 171 errors.add(new FormError("language", "Language cannot be empty!")); 172 } 173 if (StringUtils.isEmpty(email)) errors.add(new FormError("email", "Email is empty!")); 174 else if (!BlogUtils.getInstance().isValidEmailAddress(email)) 175 errors.add(new FormError("email", "Email is not valid!")); 176 177 if (errors.size() > 0) { 178 request.setAttribute("errors", errors); 179 return REGISTER_FORM_FORWARD; 180 } 181 182 UserDAO dao = new UserDAO(); 184 User u = dao.getUserByName(nickname); 185 if (u != null) { 186 errors.add(new FormError("nickname", "User '" + nickname 187 + "' already exists! Please specify another one nickname!")); 188 request.setAttribute("errors", errors); 189 return REGISTER_FORM_FORWARD; 190 } 191 192 user = PasswordEncryptionService.encryptPasswordIfNecessary(user); 193 dao.createUser(user); 194 195 return REGISTER_SUCCESS_FORWARD; 196 } 197 198 } | Popular Tags |