PageRenderTime 145ms CodeModel.GetById 32ms RepoModel.GetById 7ms app.codeStats 0ms

/app/models/Contact.java

https://bitbucket.org/pepo77/solstice-api
Java | 200 lines | 160 code | 40 blank | 0 comment | 42 complexity | 53053f8fd53cc94e1d43b5697fae2c86 MD5 | raw file
  1. package models;
  2. import com.google.common.base.Strings;
  3. import com.google.gson.JsonObject;
  4. import exceptions.EmptyParametersException;
  5. import org.joda.time.format.DateTimeFormat;
  6. import org.joda.time.format.DateTimeFormatter;
  7. import org.mongodb.morphia.annotations.Entity;
  8. import play.data.validation.Email;
  9. import play.data.validation.Phone;
  10. import play.data.validation.Required;
  11. import play.modules.morphia.Model;
  12. import play.modules.morphia.Model.NoAutoTimestamp;
  13. import play.modules.morphia.validation.Unique;
  14. import java.security.InvalidParameterException;
  15. import java.util.Date;
  16. import java.util.List;
  17. import java.util.regex.Pattern;
  18. @NoAutoTimestamp
  19. @Entity
  20. public class Contact extends Model {
  21. @Required
  22. public String information;
  23. @Required
  24. public String name;
  25. public String company;
  26. public String profileImage;
  27. @Required
  28. @Unique
  29. @Email
  30. public String email;
  31. @Required
  32. public Date birthdate;
  33. @Phone
  34. public String workPhoneNumber;
  35. @Required
  36. @Unique
  37. @Phone
  38. public String personalPhoneNumber;
  39. @Required
  40. public Address address;
  41. public static class Address {
  42. @Required
  43. String direction;
  44. @Required
  45. String zipCode;
  46. @Required
  47. String city;
  48. @Required
  49. String state;
  50. @Required
  51. String country;
  52. }
  53. public Contact(JsonObject jsonObjectRequest) {
  54. address = new Address();
  55. setFieldsFrom(jsonObjectRequest);
  56. }
  57. public static Contact findByEmailOrPhone(String email, String phone) throws EmptyParametersException {
  58. MorphiaQuery query = Contact.q();
  59. if (Strings.isNullOrEmpty(email) && Strings.isNullOrEmpty(phone)) {
  60. throw new EmptyParametersException("You must define at least one parameter.");
  61. }
  62. if (email != null){
  63. query.and(
  64. query.criteria("email").equal(Pattern.compile(email, Pattern.CASE_INSENSITIVE))
  65. );
  66. }
  67. if (phone != null){
  68. query.and(
  69. query.criteria("personalPhoneNumber").equal(phone)
  70. );
  71. }
  72. return query.get();
  73. }
  74. public static List<Contact> findByStateOrCity(String state, String city) throws EmptyParametersException {
  75. MorphiaQuery query = Contact.q();
  76. if (Strings.isNullOrEmpty(state) && Strings.isNullOrEmpty(city)) {
  77. throw new EmptyParametersException("You must define at least one parameter.");
  78. }
  79. if (state != null){
  80. query.and(
  81. query.criteria("address.state").equal(Pattern.compile(state, Pattern.CASE_INSENSITIVE))
  82. );
  83. }
  84. if (city != null){
  85. query.and(
  86. query.criteria("address.city").equal(Pattern.compile(city, Pattern.CASE_INSENSITIVE))
  87. );
  88. }
  89. return query.asList();
  90. }
  91. public void setFieldsFrom(JsonObject jsonObjectRequest) {
  92. if (jsonObjectRequest.get("information") != null) {
  93. this.information = jsonObjectRequest.get("information").getAsString();
  94. }
  95. if (jsonObjectRequest.get("name") != null) {
  96. this.name = jsonObjectRequest.get("name").getAsString();
  97. }
  98. if (jsonObjectRequest.get("company") != null) {
  99. this.company = jsonObjectRequest.get("company").getAsString();
  100. }
  101. if (jsonObjectRequest.get("profileImage") != null) {
  102. this.profileImage = jsonObjectRequest.get("profileImage").getAsString();
  103. }
  104. if (jsonObjectRequest.get("email") != null) {
  105. this.email = jsonObjectRequest.get("email").getAsString();
  106. }
  107. if (jsonObjectRequest.get("birthdate") != null) {
  108. DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd");
  109. this.birthdate = dtf.parseDateTime(jsonObjectRequest.get("birthdate").getAsString()).toDate();
  110. }
  111. if (jsonObjectRequest.get("workPhoneNumber") != null) {
  112. this.workPhoneNumber = jsonObjectRequest.get("workPhoneNumber").getAsString();
  113. }
  114. if (jsonObjectRequest.get("personalPhoneNumber") != null) {
  115. this.personalPhoneNumber = jsonObjectRequest.get("personalPhoneNumber").getAsString();
  116. }
  117. if (jsonObjectRequest.get("address") != null) {
  118. loadAddress(jsonObjectRequest);
  119. }
  120. }
  121. private void loadAddress(JsonObject jsonObjectRequest) {
  122. if (jsonObjectRequest.get("address") != null) {
  123. JsonObject addressJsonObject = jsonObjectRequest.get("address").getAsJsonObject();
  124. if (addressJsonObject.get("direction") != null) {
  125. this.address.direction = addressJsonObject.get("direction").getAsString();
  126. }
  127. if (addressJsonObject.get("zipCode") != null) {
  128. this.address.zipCode = addressJsonObject.get("zipCode").getAsString();
  129. }
  130. if (addressJsonObject.get("city") != null) {
  131. this.address.city = addressJsonObject.get("city").getAsString();
  132. }
  133. if (addressJsonObject.get("state") != null) {
  134. this.address.state = addressJsonObject.get("state").getAsString();
  135. }
  136. if (addressJsonObject.get("country") != null) {
  137. this.address.country = addressJsonObject.get("country").getAsString();
  138. }
  139. }
  140. }
  141. public String toString() {
  142. return "{" +
  143. " \"information\":" + this.information +", " +
  144. " \"name\":" + this.name +", " +
  145. " \"company\":" + this.company +", " +
  146. " \"profileImage\":"+this.profileImage+", " +
  147. " \"email\":" + this.email +", " +
  148. " \"birthdate\":" + this.birthdate +", " +
  149. " \"workPhoneNumber\":" + this.workPhoneNumber +", " +
  150. " \"personalPhoneNumber\":" + this.personalPhoneNumber +", " +
  151. " \"address\":{ " +
  152. " \"direction\":" + this.address.direction +", " +
  153. " \"zipCode\":" + this.address.zipCode +", " +
  154. " \"city\":" + this.address.city +", " +
  155. " \"state\":" + this.address.state +", " +
  156. " \"country\":" + this.address.country +", " +
  157. " }" +
  158. "}";
  159. }
  160. }