PageRenderTime 1642ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/app/models/Venue.java

https://bitbucket.org/marcosflorez/mvp
Java | 228 lines | 202 code | 25 blank | 1 comment | 34 complexity | 03e1520cf1cde231fb83d51d7e1d39c2 MD5 | raw file
  1. package models;
  2. import com.google.gson.Gson;
  3. import models.Notifications.Publication;
  4. import org.bson.types.ObjectId;
  5. import play.Logger;
  6. import play.data.validation.Constraints.Required;
  7. import com.mongodb.BasicDBObject;
  8. import com.mongodb.DBObject;
  9. import org.apache.commons.lang3.text.WordUtils;
  10. import java.io.File;
  11. import java.util.ArrayList;
  12. import java.util.Arrays;
  13. public class Venue {
  14. @Required
  15. public String name;
  16. public String searchName="";
  17. public ArrayList<String> types = new ArrayList<String>(3);
  18. public ArrayList<String> followers;
  19. @Override
  20. public String toString() {
  21. return "Venue{" +
  22. "name='" + name + '\'' +
  23. ", types=" + types +
  24. ", address=" + address +
  25. '}';
  26. }
  27. public ArrayList<Publication> publications;
  28. public String description = "";
  29. public String phone = "";
  30. public String photoURLs[]= {"http://toptasting.s3.amazonaws.com/sinImagenVenue.png","",""};
  31. public ArrayList<String> menuSections=new ArrayList<String>(Arrays.asList("Otros"));
  32. public double coordinates[] = new double[]{0.0,0.0};
  33. public ObjectId oid;
  34. public Address address=new Address();
  35. public String date="";
  36. public String insertedBy="";
  37. public String modified="";//date
  38. public String modifiedBy="";
  39. public Venue() {
  40. }
  41. public ArrayList<Publication> getPublications() {
  42. return publications;
  43. }
  44. public void setPublications(ArrayList<Publication> publications) {
  45. this.publications = publications;
  46. }
  47. @SuppressWarnings("unchecked")
  48. public Venue(DBObject object) {
  49. if (object.containsField("name")) {
  50. this.name = object.get("name").toString();
  51. }
  52. if (object.containsField("searchName")) {
  53. this.searchName = object.get("searchName").toString();
  54. }
  55. Object o=object.get("date");
  56. if (o!=null) {
  57. this.date = o.toString();
  58. }
  59. o=object.get("insertBy");
  60. if (o!=null) {
  61. this.insertedBy = o.toString();
  62. }
  63. o=object.get("modified");
  64. if (o!=null) {
  65. this.modified = o.toString();
  66. }
  67. o=object.get("modifiedBy");
  68. if (o!=null) {
  69. this.modifiedBy = o.toString();
  70. }
  71. if (object.containsField("description")) {
  72. this.description = object.get("description").toString();
  73. }
  74. if (object.containsField("phone")) {
  75. this.phone = object.get("phone").toString();
  76. }
  77. ArrayList<Object> al = (ArrayList<Object>) object.get("coordinates");
  78. if (al!=null) {
  79. coordinates[0]=Double.parseDouble(al.get(0).toString());
  80. coordinates[1]=Double.parseDouble(al.get(1).toString());
  81. }
  82. if (object.containsField("address")) {
  83. DBObject address = (DBObject) object.get("address");
  84. if (address.containsField("street")) {
  85. this.address.street = address.get("street").toString();
  86. }
  87. if (address.containsField("city")) {
  88. this.address.city = WordUtils.capitalize( address.get("city").toString());
  89. }
  90. if (address.containsField("province")) {
  91. this.address.province = WordUtils.capitalize(address.get("province").toString());
  92. }
  93. if (address.containsField("streetNumber")) {
  94. this.address.streetNumber = Integer.parseInt(address.get("streetNumber")
  95. .toString());
  96. }
  97. if (address.containsField("postalCode")) {
  98. this.address.postalCode = address.get("postalCode").toString();
  99. }
  100. }
  101. if (object.containsField("_id")) {
  102. oid = (ObjectId) object.get("_id");
  103. }
  104. al = (ArrayList<Object>) object.get("menuSections");
  105. if (al != null) {
  106. this.menuSections = new ArrayList<String>(al.size());
  107. for (Object obj : al) {
  108. this.menuSections.add(obj.toString());
  109. }
  110. }
  111. al = (ArrayList<Object>) object.get("publications");
  112. if (al != null) {
  113. this.publications = new ArrayList<Publication>(al.size());
  114. for (Object obj : al) {
  115. this.publications.add(new Gson().fromJson(obj.toString(),Publication.class));
  116. }
  117. }
  118. al = (ArrayList<Object>) object.get("followers");
  119. if (al != null) {
  120. this.followers = new ArrayList<String>(al.size());
  121. for (Object obj : al) {
  122. followers.add(obj.toString());
  123. }
  124. }
  125. al = (ArrayList<Object>) object.get("types");
  126. if (al != null) {
  127. for (Object obj : al) {
  128. this.types.add(obj.toString());
  129. }
  130. al = (ArrayList<Object>) object.get("photoURLs");
  131. if (al != null) {
  132. this.photoURLs = new String[al.size()];
  133. int i = 0;
  134. for (Object obj : al) {
  135. this.photoURLs[i] = obj.toString();
  136. i++;
  137. }
  138. }
  139. // menuSections=(String[])object.get("menuSections")).toArray();
  140. }
  141. }
  142. public BasicDBObject toDBO(){
  143. BasicDBObject doc = new BasicDBObject("name", name);
  144. doc.append("description",description);
  145. doc.append("phone", phone);
  146. for(String type:types){
  147. type=type.toLowerCase();
  148. }
  149. doc.append("searchName",searchName);
  150. doc.append("types", types);
  151. doc.append("coordinates", coordinates);
  152. doc.append("date",date);
  153. doc.append("insertedBy",insertedBy);
  154. doc.append("modified",modified);
  155. doc.append("modifiedBy",modifiedBy);
  156. if(photoURLs[0]==null||photoURLs[0].equalsIgnoreCase("")||photoURLs[0].equalsIgnoreCase("undefined")){
  157. photoURLs[0]="http://toptasting.s3.amazonaws.com/sinImagenVenue.png";
  158. }
  159. doc.append("photoURLs", photoURLs);
  160. doc.append("menuSections", menuSections);
  161. if(address!=null){
  162. doc.append("address", address.toDBO());
  163. }
  164. return doc;
  165. }
  166. public class Address{
  167. public String street="";
  168. @Required
  169. public int streetNumber=0;
  170. public String postalCode="";
  171. public String province="asturias";
  172. public String city="";
  173. public Address(){
  174. }
  175. public Address(String city,String street, int streetNumber, String postalCode,
  176. String province) {
  177. super();
  178. this.street = street;
  179. this.streetNumber = streetNumber;
  180. this.postalCode = postalCode;
  181. this.province = province;
  182. this.city=city;
  183. }
  184. @Override
  185. public String toString() {
  186. return "Address{" +
  187. "street='" + street + '\'' +
  188. ", postalCode='" + postalCode + '\'' +
  189. ", province='" + province + '\'' +
  190. ", city='" + city + '\'' +
  191. '}';
  192. }
  193. public BasicDBObject toDBO(){
  194. BasicDBObject add=new BasicDBObject();
  195. add.append("street", address.street);
  196. add.append("city", address.city.toLowerCase());
  197. add.append("postalCode", address.postalCode);
  198. add.append("streetNumber", address.streetNumber);
  199. add.append("province", address.province.toLowerCase());
  200. return add;
  201. }
  202. }
  203. }