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

/app/models/Review.java

https://bitbucket.org/marcosflorez/mvp
Java | 229 lines | 208 code | 21 blank | 0 comment | 23 complexity | 4f8ae47a3728625deb679790ecd52807 MD5 | raw file
  1. package models;
  2. import java.awt.image.BufferedImage;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import javax.imageio.ImageIO;
  8. import com.google.gson.Gson;
  9. import com.google.gson.JsonObject;
  10. import org.bson.types.ObjectId;
  11. import com.fasterxml.jackson.databind.JsonNode;
  12. import play.Logger;
  13. import play.mvc.Http.MultipartFormData.FilePart;
  14. import Utils.ImageUtils;
  15. import com.mongodb.BasicDBObject;
  16. import com.mongodb.DBObject;
  17. public class Review {
  18. public ObjectId dishId;
  19. public ObjectId userId;
  20. public float points=0.f;
  21. public String user="";
  22. public String description="";
  23. public String tags[] = new String[] { "" };
  24. public String photoURL="http://toptasting.s3.amazonaws.com/sinImagenDish.png";
  25. public String userPhotoURL;
  26. public boolean firstTimeInVenue=false;
  27. public boolean firstReviewInVenue=false;
  28. public boolean firstReviewInDish=false;
  29. public boolean repeated=false;
  30. final int firstTIVpoints=1;
  31. final int firstRIVpoints=1;
  32. final int firstRIDpoints=1;
  33. final int hasPicturePoints=1;
  34. final int hasDescriptionPoints=1;
  35. public String getUserPhotoURL() {
  36. return userPhotoURL;
  37. }
  38. public void setUserPhotoURL(String userPhotoURL) {
  39. this.userPhotoURL = userPhotoURL;
  40. }
  41. public String getPhotoURL() {
  42. return photoURL;
  43. }
  44. public void setPhotoURL(String photoURL) {
  45. this.photoURL = photoURL;
  46. }
  47. public String date="";
  48. public String venueName;
  49. public Review() {
  50. }
  51. @SuppressWarnings("unchecked")
  52. public Review(DBObject object) {
  53. Object o;
  54. if (object.containsField("userId")) {
  55. this.userId = (ObjectId) object.get("userId");
  56. ;
  57. }
  58. if (object.containsField("user")) {
  59. this.user = object.get("user").toString();
  60. ;
  61. }
  62. o=object.get("date");
  63. if (o!=null) {
  64. this.date = o.toString();
  65. ;
  66. }
  67. if (object.containsField("points")) {
  68. this.points = Float.parseFloat(object.get("points").toString());
  69. }
  70. if (object.containsField("photoURL")) {
  71. this.photoURL = object.get("photoURL").toString();
  72. }
  73. if (object.containsField("description")) {
  74. this.description = object.get("description").toString();
  75. }
  76. if (object.get("firstIV")!=null) {
  77. this.firstTimeInVenue = (Boolean) object.get("firstIV");
  78. }if (object.get("firstRIV")!=null) {
  79. this.firstReviewInVenue = (Boolean) object.get("firstRIV");
  80. }if (object.get("firstRID")!=null) {
  81. this.firstReviewInDish= (Boolean) object.get("firstRID");
  82. }
  83. if (object.get("firstRID")!=null) {
  84. this.repeated= (Boolean) object.get("repeated");
  85. }
  86. ArrayList<Object> al = (ArrayList<Object>) object.get("tags");
  87. if (al != null) {
  88. this.tags = new String[al.size()];
  89. int i = 0;
  90. for (Object ob : al) {
  91. this.tags[i] = ob.toString();
  92. i++;
  93. }
  94. }
  95. }
  96. public Review(JsonNode json) {
  97. userId = new ObjectId(json.findPath("userId").textValue());
  98. Logger.info("USER ID (review): "+userId);
  99. points = json.findPath("points").intValue();
  100. user = json.findPath("user").asText();
  101. description = json.findPath("description").asText();
  102. photoURL = json.findPath("photoURL").asText();
  103. date=json.findPath("date").asText();
  104. dishId=new ObjectId(json.findPath("dishId").textValue());
  105. firstTimeInVenue=json.findPath("firstTIV").booleanValue();
  106. firstReviewInVenue=json.findPath("firstRIV").booleanValue();
  107. firstReviewInDish=json.findPath("firstRID").booleanValue();
  108. repeated=json.findPath("repeated").booleanValue();
  109. }
  110. public Review(JsonNode json, FilePart picture) {
  111. userId = new ObjectId(json.findPath("userId").asText());
  112. points = json.findPath("points").intValue();
  113. user = json.findPath("user").asText();
  114. date= json.findPath("date").asText();
  115. description = json.findPath("description").asText();
  116. firstTimeInVenue=json.findPath("firstTIV").booleanValue();
  117. firstReviewInVenue=json.findPath("firstRIV").booleanValue();
  118. firstReviewInDish=json.findPath("firstRID").booleanValue();
  119. repeated=json.findPath("repeated").booleanValue();
  120. photoURL = Utils.AWSuploader.insertImage(picture, "reviews");
  121. }
  122. public BasicDBObject toDBO() {
  123. BasicDBObject doc = new BasicDBObject();
  124. doc.append("description", description);
  125. doc.append("points", points);
  126. doc.append("photoURL", photoURL);
  127. doc.append("tags", tags);
  128. doc.append("user", user);
  129. doc.append("userId", userId);
  130. doc.append("date", date);
  131. doc.append("firstTIV", firstTimeInVenue);
  132. doc.append("firstRIV", firstReviewInVenue);
  133. doc.append("firstRID", firstReviewInDish);
  134. doc.append("repeated", repeated);
  135. return doc;
  136. }
  137. public String toJson(){
  138. JsonObject jsObject = new JsonObject();
  139. jsObject.addProperty("userId", userId.toString());
  140. jsObject.addProperty("dishId",dishId.toString());
  141. jsObject.addProperty("date",date);
  142. jsObject.addProperty("userName",user);
  143. jsObject.addProperty("venueName",venueName);
  144. jsObject.addProperty("points",points);
  145. jsObject.addProperty("firstTIV",firstTimeInVenue);
  146. jsObject.addProperty("firstRIV",firstReviewInVenue);
  147. jsObject.addProperty("firstRID",firstReviewInDish);
  148. jsObject.addProperty("photoURL", photoURL);
  149. jsObject.addProperty("repeated", repeated);
  150. return jsObject.toString();
  151. }
  152. public String getJsonPoints(){
  153. JsonObject jsObject = new JsonObject();
  154. if(!repeated){
  155. if(firstTimeInVenue){
  156. jsObject.addProperty("firstTIV",firstTIVpoints);
  157. }else{
  158. jsObject.addProperty("firstTIV",0);
  159. }
  160. if(firstReviewInVenue){
  161. jsObject.addProperty("firstRIV", firstRIVpoints);
  162. }else{
  163. jsObject.addProperty("firstRIV", 0);
  164. }
  165. if(firstReviewInDish){
  166. jsObject.addProperty("firstRID", firstRIDpoints);
  167. }else{
  168. jsObject.addProperty("firstRID", 0);
  169. }
  170. if(!photoURL.equalsIgnoreCase("http://toptasting.s3.amazonaws.com/sinImagenDish.png")){
  171. jsObject.addProperty("hasPicture", hasPicturePoints);
  172. }else{
  173. jsObject.addProperty("hasPicture", 0);
  174. }
  175. if(description!=null&&description.length()>10){
  176. jsObject.addProperty("hasDescription", hasDescriptionPoints);
  177. }else{
  178. jsObject.addProperty("hasDescription", 0);
  179. }
  180. }
  181. else{
  182. jsObject.addProperty("repeated", 1);
  183. }
  184. return jsObject.toString();
  185. }
  186. public int getUserPoints(){
  187. int points=1;
  188. if(repeated){
  189. return points;
  190. }
  191. if(firstTimeInVenue){
  192. points+=firstTIVpoints;
  193. }
  194. if(firstReviewInVenue){
  195. points+=firstRIVpoints;
  196. }
  197. if(firstReviewInDish){
  198. points+=firstRIDpoints;
  199. }
  200. if(!photoURL.equalsIgnoreCase("http://toptasting.s3.amazonaws.com/sinImagenDish.png")){
  201. points+=hasPicturePoints;
  202. }
  203. if(description!=null&&description.length()>10){
  204. points+= hasDescriptionPoints;
  205. }
  206. return points;
  207. }
  208. }