PageRenderTime 69ms CodeModel.GetById 41ms RepoModel.GetById 0ms app.codeStats 0ms

/RezervacijaSoba/src/main/java/hr/fer/opp/projekt/web/servlets/form/RegisterUserForm.java

https://gitlab.com/Balun/jaganjci
Java | 250 lines | 173 code | 35 blank | 42 comment | 27 complexity | e7dbbff275665eaf6241820717ed9e4d MD5 | raw file
  1. package hr.fer.opp.projekt.web.servlets.form;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. import javax.servlet.http.HttpServletRequest;
  5. import hr.fer.opp.projekt.dao.DAOProvider;
  6. import hr.fer.opp.projekt.model.User;
  7. /**
  8. * Class representing the form used for registering the blog users.
  9. *
  10. * @author Borna Zeba
  11. * @version 1.0
  12. *
  13. */
  14. public class RegisterUserForm extends AbstractForm {
  15. /**
  16. * User's first name.
  17. */
  18. private String firstName;
  19. /**
  20. * User's last name.
  21. */
  22. private String lastName;
  23. /**
  24. * User's email.
  25. */
  26. private String email;
  27. /**
  28. * User's password.
  29. */
  30. private String password;
  31. /**
  32. * Confirmation of the provided password.
  33. */
  34. private String passwordConfirmation;
  35. private String streetName;
  36. private String streetNumber;
  37. private String city;
  38. private String country;
  39. private String phoneNumber;
  40. @Override
  41. public void fillFromHttpRequest(HttpServletRequest req) {
  42. this.firstName = prepare(req.getParameter("firstName"));
  43. this.lastName = prepare(req.getParameter("lastName"));
  44. this.email = prepare(req.getParameter("email"));
  45. this.password = prepareWithoutTrim(req.getParameter("password"));
  46. this.passwordConfirmation = prepareWithoutTrim(
  47. req.getParameter("passwordConfirmation"));
  48. this.streetName = prepare(req.getParameter("streetName"));
  49. this.streetNumber = prepare(req.getParameter("streetNumber"));
  50. this.city = prepare(req.getParameter("city"));
  51. this.country = prepare(req.getParameter("country"));
  52. this.phoneNumber = prepare(req.getParameter("phoneNumber"));
  53. }
  54. /**
  55. * Fills the given user with the data from this form.
  56. *
  57. * @param user
  58. * the user object to fill
  59. */
  60. public void fillUserData(User user) {
  61. user.setFirstName(firstName);
  62. user.setLastName(lastName);
  63. user.setEmail(email);
  64. if (!password.isEmpty()) {
  65. user.setPassword(password);
  66. }
  67. user.setCity(city);
  68. user.setCountry(country);
  69. user.setStreetName(streetName);
  70. user.setStreetNumber(Integer.parseInt(streetNumber));
  71. if (!phoneNumber.isEmpty()) {
  72. user.setPhoneNumber(phoneNumber);
  73. }
  74. user.setConfirmedEmail(false);
  75. }
  76. /**
  77. * Fills the fields of this form with the data from the given user.
  78. *
  79. * @param user
  80. * the user whose data will be filled in this
  81. */
  82. public void fillFromUserData(User user) {
  83. this.firstName = user.getFirstName();
  84. this.lastName = user.getLastName();
  85. this.email = user.getEmail();
  86. this.password = "";
  87. this.passwordConfirmation = "";
  88. this.streetName = user.getStreetName();
  89. this.streetNumber = user.getStreetNumber().toString();
  90. this.city = user.getCity();
  91. this.country = user.getCountry();
  92. this.phoneNumber = user.getPhoneNumber();
  93. }
  94. @Override
  95. public void validate() {
  96. errors.clear();
  97. if (email.isEmpty() || !isEmailValid(email)) {
  98. errors.put("email", "Krivi format e-mail adrese.");
  99. } else if (DAOProvider.getDAO().getUser(email) != null) {
  100. errors.put("email", "Već postoji korisnik s tom e-mail adresom.");
  101. }
  102. if (password.isEmpty()) {
  103. errors.put("password", "Morate upisati lozinku.");
  104. }
  105. if (password.length() < 6) {
  106. errors.put("password", "Lozinka mora imati barem 6 znakova.");
  107. }
  108. if (!password.equals(passwordConfirmation)) {
  109. errors.put("password", "Lozinke se ne podudaraju.");
  110. }
  111. if (firstName.isEmpty()) {
  112. errors.put("firstName", "Morate upisati ime.");
  113. }
  114. if (lastName.isEmpty()) {
  115. errors.put("lastName", "Morate upisati prezime.");
  116. }
  117. if (streetName.isEmpty()) {
  118. errors.put("streetName", "Morate upisati ime ulice.");
  119. }
  120. if (streetNumber.isEmpty()) {
  121. errors.put("streetNumber", "Morate upisati kućni broj.");
  122. }
  123. try {
  124. Integer.parseInt(streetNumber);
  125. } catch (NumberFormatException e) {
  126. errors.put("streetNumber", "Kućni broj mora biti broj.");
  127. }
  128. if (city.isEmpty()) {
  129. errors.put("city", "Morate upisati grad.");
  130. }
  131. if (country.isEmpty()) {
  132. errors.put("country", "Morate upisati državu.");
  133. }
  134. }
  135. public void validateEdit() {
  136. errors.clear();
  137. if (email.isEmpty() || !isEmailValid(email)) {
  138. errors.put("email", "Krivi format e-mail adrese.");
  139. }
  140. if (!password.isEmpty()) {
  141. if (password.length() < 6) {
  142. errors.put("password", "Lozinka mora imati barem 6 znakova.");
  143. }
  144. if (!password.equals(passwordConfirmation)) {
  145. errors.put("password", "Lozinke se ne podudaraju.");
  146. }
  147. }
  148. if (firstName.isEmpty()) {
  149. errors.put("firstName", "Morate upisati ime.");
  150. }
  151. if (lastName.isEmpty()) {
  152. errors.put("lastName", "Morate upisati prezime.");
  153. }
  154. if (streetName.isEmpty()) {
  155. errors.put("streetName", "Morate upisati ime ulice.");
  156. }
  157. if (streetNumber.isEmpty()) {
  158. errors.put("streetNumber", "Morate upisati kućni broj.");
  159. }
  160. try {
  161. Integer.parseInt(streetNumber);
  162. } catch (NumberFormatException e) {
  163. errors.put("streetNumber", "Kućni broj mora biti broj.");
  164. }
  165. if (city.isEmpty()) {
  166. errors.put("city", "Morate upisati grad.");
  167. }
  168. if (country.isEmpty()) {
  169. errors.put("country", "Morate upisati državu.");
  170. }
  171. }
  172. /**
  173. * Returns {@code true} if the given email is valid, {@code false}
  174. * otherwise.
  175. *
  176. * @param email
  177. * the email to check
  178. * @return {@code true} if the given mail is valid
  179. */
  180. public static boolean isEmailValid(String email) {
  181. Pattern pattern = Pattern.compile(".+@.+\\..+");
  182. Matcher matcher = pattern.matcher(email);
  183. return matcher.matches();
  184. }
  185. public String getFirstName() {
  186. return firstName;
  187. }
  188. public String getLastName() {
  189. return lastName;
  190. }
  191. public String getEmail() {
  192. return email;
  193. }
  194. public String getPassword() {
  195. return password;
  196. }
  197. public String getPasswordConfirmation() {
  198. return passwordConfirmation;
  199. }
  200. public String getStreetName() {
  201. return streetName;
  202. }
  203. public String getStreetNumber() {
  204. return streetNumber;
  205. }
  206. public String getCity() {
  207. return city;
  208. }
  209. public String getCountry() {
  210. return country;
  211. }
  212. public String getPhoneNumber() {
  213. return phoneNumber;
  214. }
  215. }