/src/java/eitex/sas/address/Address.java

https://github.com/amexboy/SAS · Java · 214 lines · 145 code · 36 blank · 33 comment · 20 complexity · 08320789b280ea733df44cc21ed54332 MD5 · raw file

  1. package eitex.sas.address;
  2. import eitex.sas.common.ModuleTemplate;
  3. import eitex.sas.common.NotFoundException;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6. /**
  7. * @author Amanu
  8. */
  9. public class Address extends ModuleTemplate{
  10. private int id;
  11. private String officeBuilding;
  12. private String officeNumber;
  13. private String mobilePhoneNumber;
  14. private String officePhoneNumber;
  15. private String email;
  16. /**
  17. * Constructs an object of Address. This constructor shall be used if values of all the fields
  18. * are known. If id is not needed or not known, 0 can be passed
  19. * @param id
  20. * @param officeBuilding
  21. * @param officeNumber
  22. * @param mobilePhoneNumber
  23. * @param officePhoneNumber
  24. * @param email
  25. */
  26. public Address(int id, String officeBuilding, String officeNumber,
  27. String mobilePhoneNumber, String officePhoneNumber, String email){
  28. this.id = id;
  29. this.officeBuilding = officeBuilding;
  30. this.officeNumber = officeNumber;
  31. this.mobilePhoneNumber = mobilePhoneNumber;
  32. this.officePhoneNumber = officePhoneNumber;
  33. this.email = email;
  34. this.setNew(true);
  35. }
  36. /**
  37. * Constructs an object of Address class getting the fields from database. This constructor should be
  38. * used when the id is known and to get the rest from database.
  39. * @param id
  40. * @throws eitex.sas.common.NotFoundException
  41. */
  42. public Address(int id) throws NotFoundException {
  43. this.id = id;
  44. Address address = AddressModel.getAddress(id);
  45. this.officeBuilding = address.officeBuilding;
  46. this.officeNumber = address.officeNumber;
  47. this.officePhoneNumber = address.officePhoneNumber;
  48. this.mobilePhoneNumber = address.mobilePhoneNumber;
  49. this.email = address.email;
  50. this.setNew(true);
  51. }
  52. /**
  53. * @param userName, the user that initiated the action
  54. * @throws eitex.sas.address.AddressFieldException
  55. * @return
  56. */
  57. @Override
  58. public boolean save(String userName) throws AddressFieldException {
  59. if(!isValidated()){
  60. this.validate();
  61. }
  62. if (this.isValid()) {
  63. if (isNew()) {
  64. this.id = AddressModel.saveToDataBase(this, userName);
  65. return this.id > 0;
  66. } else if (isModified()) {
  67. return AddressModel.updateDataBase(this, userName) > 0;
  68. }
  69. }
  70. return false;
  71. }
  72. /**
  73. * @throws eitex.sas.address.AddressFieldException
  74. * @return
  75. */
  76. @Override
  77. public final boolean validate() throws AddressFieldException {
  78. /**
  79. * RFC822 compliant adapted for java
  80. */
  81. Pattern VALID_EMAIL_ADDRESS_REGEX = Pattern.compile("(?:(?:\\r\\n)?[ \\t])*(?:(?:(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\"(?:[^\\\"\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\"(?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\"(?:[^\\\"\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\"(?:(?:\\r\\n)?[ \\t])*))*@(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*))*|(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\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\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*))*\\>(?:(?:\\r\\n)?[ \\t])*))*)?;\\s*)");
  82. Pattern VALID_PHONE_NUMBER_REGEX = Pattern.compile("^(0\\d{9})|([+]\\d{12})$");
  83. this.setValidated(true);
  84. this.setValid(false);
  85. boolean errorFound = false;
  86. AddressFieldException ex = new AddressFieldException();
  87. if (officeBuilding.matches(".*[><='!@#$%^&*].$")) {
  88. errorFound = true;
  89. ex.setOfficeBuildingError(true);
  90. }
  91. if (!officeNumber.matches("^\\d{1,10}$")) {
  92. errorFound = true;
  93. ex.setOfficeNumberError(true);
  94. }
  95. if (officePhoneNumber != null && !officePhoneNumber.isEmpty()) {
  96. Matcher officePhoneNumberMatcher = VALID_PHONE_NUMBER_REGEX.matcher(this.officePhoneNumber.replace("-", ""));
  97. if (!officePhoneNumberMatcher.matches()) {
  98. errorFound = true;
  99. ex.setOfficePhoneError(true);
  100. }
  101. }
  102. if (mobilePhoneNumber != null && !mobilePhoneNumber.isEmpty()) {
  103. Matcher mobilePhoneNumberMatcher = VALID_PHONE_NUMBER_REGEX.matcher(this.mobilePhoneNumber.replace("-", ""));
  104. if (!mobilePhoneNumberMatcher.matches()) {
  105. errorFound = true;
  106. ex.setMobileNumberError(true);
  107. }
  108. }
  109. if (email != null && !email.isEmpty()) {
  110. Matcher emailMatcher = VALID_EMAIL_ADDRESS_REGEX.matcher(this.email);
  111. if (!emailMatcher.matches()) {
  112. errorFound = true;
  113. ex.setEmailError(true);
  114. }
  115. }
  116. if(errorFound){
  117. throw ex;
  118. }
  119. this.setValid(true);
  120. return true;
  121. }
  122. @Override
  123. public boolean delete(String userName) {
  124. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  125. }
  126. @Override
  127. public boolean recover(String userName) {
  128. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  129. }
  130. //<editor-fold defaultstate="collapsed" desc="Setter & Getter">
  131. public int getId() {
  132. return this.id;
  133. }
  134. public void setId(int id) {
  135. this.id = id;
  136. }
  137. public String getOfficeBuilding() {
  138. return this.officeBuilding;
  139. }
  140. public void setOfficeBuilding(String officeBuilding) {
  141. this.officeBuilding = officeBuilding;
  142. this.setModified(true);
  143. this.setValidated(false);;
  144. }
  145. public String getOfficeNumber() {
  146. return this.officeNumber;
  147. }
  148. public void setOfficeNumber(String officeNumber) {
  149. this.officeNumber = officeNumber;
  150. this.setModified(true);
  151. this.setValidated(false);;
  152. }
  153. public String getMobilePhoneNumber() {
  154. return this.mobilePhoneNumber;
  155. }
  156. public void setMobilePhoneNumber(String mobilePhoneNumber) {
  157. this.mobilePhoneNumber = mobilePhoneNumber;
  158. this.setModified(true);
  159. this.setValidated(false);;
  160. }
  161. public String getOfficePhoneNumber() {
  162. return this.officePhoneNumber;
  163. }
  164. public void setOfficePhoneNumber(String officePhoneNumber) {
  165. this.officePhoneNumber = officePhoneNumber;
  166. this.setModified(true);
  167. this.setValidated(false);;
  168. }
  169. public String getEmail() {
  170. return this.email;
  171. }
  172. public void setEmail(String email) {
  173. this.email = email;
  174. this.setModified(true);
  175. this.setValidated(false);;
  176. }
  177. //</editor-fold>
  178. }