PageRenderTime 178ms CodeModel.GetById 40ms RepoModel.GetById 1ms app.codeStats 0ms

/samples-and-tests/unit-tests/app/models/DataEntry.java

https://github.com/greenlaw110/play-morphia
Java | 319 lines | 247 code | 64 blank | 8 comment | 52 complexity | 7b7b032d29674932f3f3ba5f13594fc1 MD5 | raw file
  1. package models;
  2. import com.google.gson.Gson;
  3. import org.bson.types.ObjectId;
  4. import org.joda.time.DateMidnight;
  5. import org.mongodb.morphia.annotations.Embedded;
  6. import org.mongodb.morphia.annotations.Entity;
  7. import play.modules.morphia.Model;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.regex.Pattern;
  13. /**
  14. * Created with IntelliJ IDEA.
  15. * User: luog
  16. * Date: 24/01/13
  17. * Time: 10:05 AM
  18. * To change this template use File | Settings | File Templates.
  19. */
  20. @Entity(noClassnameStored = true, value = "entry")
  21. public class DataEntry extends Model {
  22. @Embedded
  23. public static class KV {
  24. public String key;
  25. public String value;
  26. public KV(String key, String val) {
  27. this.key = key;
  28. this.value = val;
  29. }
  30. }
  31. public DataEntry() {
  32. }
  33. public DataEntry(boolean preview) {
  34. this.preview = preview;
  35. }
  36. @Embedded
  37. private List<KV> data = new ArrayList<KV>();
  38. public boolean moderated;
  39. public boolean approved;
  40. public int votes;
  41. public boolean preview;
  42. public String code;
  43. public boolean hasImage;
  44. @Loaded
  45. void initDataContainer() {
  46. if (null == data) data = new ArrayList<KV>();
  47. }
  48. public void addData(String key, String val) {
  49. data.add(new KV(key, val.replaceAll("\n", " ").replaceAll("\r", " ").replaceAll("\t", " ")));
  50. }
  51. public boolean hasData() {
  52. return data.size() > 0;
  53. }
  54. public boolean hasDataValue(String value) {
  55. for (KV kv : data) {
  56. if (kv.value.equals(value))
  57. return true;
  58. }
  59. return false;
  60. }
  61. public String get(String key) {
  62. for (KV kv : data) {
  63. if (kv.key.equals(key)) return kv.value;
  64. }
  65. return null;
  66. }
  67. public boolean set(String key, String value) {
  68. for (int i = 0; i < data.size(); i++) {
  69. if (data.get(i).key.equals(key)) {
  70. data.set(i, new KV(key, value));
  71. save();
  72. return true;
  73. }
  74. }
  75. return false;
  76. }
  77. public List<String> keys() {
  78. List<String> keys = new ArrayList<String>();
  79. for (KV kv : data) {
  80. keys.add(kv.key);
  81. }
  82. return keys;
  83. }
  84. public synchronized void incrVotes() {
  85. votes++;
  86. save();
  87. }
  88. public static enum Q {
  89. BY_VOTES("-votes"),
  90. BY_DATE("-_created");
  91. private String fld_;
  92. private Q(String fld) {
  93. fld_ = fld;
  94. }
  95. private MorphiaQuery q(boolean preModerate, boolean withImage) {
  96. MorphiaQuery q = DataEntry.q().order(fld_);
  97. if (withImage) {
  98. if (preModerate) {
  99. q.filter("approved", true);
  100. } else {
  101. q.or(q.criteria("moderated").equal(false), q.and(q.criteria("moderated").equal(true), q.criteria("approved").equal(true)));
  102. }
  103. q.filter("hasImage", true);
  104. }
  105. return q;
  106. }
  107. public List<DataEntry> find(boolean preModerate, boolean withImage) {
  108. return q(preModerate, withImage).asList();
  109. }
  110. public List<DataEntry> findLive(boolean preModerate, boolean withImage) {
  111. return q(preModerate, withImage).filter("preview", false).asList();
  112. }
  113. public List<DataEntry> findPreview(boolean preModerate, boolean withImage) {
  114. return q(preModerate, withImage).filter("preview", true).asList();
  115. }
  116. public List<DataEntry> find(boolean preModerate, int start, int limit, boolean withImage) {
  117. return q(preModerate, withImage).offset(start).limit(limit).asList();
  118. }
  119. public List<DataEntry> find(boolean preModerate, int start, int limit, String q, boolean withImage) {
  120. if (null == q || q.equals("all")) {
  121. return find(preModerate, start, limit, withImage);
  122. }
  123. return q(preModerate, withImage).filter("data.value", Pattern.compile(q, Pattern.CASE_INSENSITIVE)).offset(start).limit(limit).asList();
  124. }
  125. public List<DataEntry> find(boolean preModerate, int start, int limit, String q, String q2, boolean withImage) {
  126. if (null == q || q.equals("all")) {
  127. return find(preModerate, start, limit, q2, withImage);
  128. }
  129. if (null == q2 || q2.equals("")) {
  130. return find(preModerate, start, limit, q, withImage);
  131. }
  132. //workaround for double filter
  133. List<DataEntry> entriesFirstFilter = q(preModerate, withImage).filter("data.value", Pattern.compile(q, Pattern.CASE_INSENSITIVE)).asList();
  134. List<DataEntry> entriesSecondFilter = new ArrayList<DataEntry>();
  135. List<DataEntry> entriesSecondFilterWithOffsetAndLimit = new ArrayList<DataEntry>();
  136. for (DataEntry e : entriesFirstFilter) {
  137. if (e.hasDataValue(q2))
  138. entriesSecondFilter.add(e);
  139. }
  140. for (int i = start, j = 0; j < limit && i < entriesSecondFilter.size(); i++, j++) {
  141. entriesSecondFilterWithOffsetAndLimit.add(entriesSecondFilter.get(i));
  142. }
  143. return entriesSecondFilterWithOffsetAndLimit;
  144. }
  145. public MorphiaQuery filter(MorphiaQuery query, String key, Object value) {
  146. return query.filter(key, value);
  147. }
  148. public MorphiaQuery filterDate(MorphiaQuery query, long begin, long end) {
  149. return query.filter("_created >=", begin).filter("_created <=", end);
  150. }
  151. public MorphiaQuery filterDate(MorphiaQuery query, String date) {
  152. DateMidnight start = new DateMidnight();
  153. DateMidnight stop = new DateMidnight();
  154. if (date.equals("today"))
  155. stop = stop.plusDays(1);
  156. if (date.equals("yesterday"))
  157. start = start.minusDays(1);
  158. if (date.equals("thisWeek"))
  159. start = start.minusDays(start.getDayOfWeek() - 1);
  160. if (date.equals("lastWeek")) {
  161. start = start.minusDays(start.getDayOfWeek() + 6);
  162. stop = stop.minusDays(stop.getDayOfWeek() - 1);
  163. }
  164. if (date.equals("allTime"))
  165. return query;
  166. return filterDate(query, start.getMillis(), stop.getMillis());
  167. }
  168. public MorphiaQuery filterModeration(MorphiaQuery query, String moderation) {
  169. if (moderation.equals("pending"))
  170. return query.filter("moderated", false);
  171. if (moderation.equals("approved"))
  172. return query.filter("moderated", true).filter("approved", true);
  173. if (moderation.equals("rejected"))
  174. return query.filter("moderated", true).filter("approved", false);
  175. return query;
  176. }
  177. public MorphiaQuery search(MorphiaQuery query, String q) {
  178. return query.filter("data.value", Pattern.compile(q, Pattern.CASE_INSENSITIVE));
  179. }
  180. public MorphiaQuery segment(MorphiaQuery query, int start, int limit) {
  181. if (limit == -1)
  182. return query.offset(start);
  183. return query.offset(start).limit(limit);
  184. }
  185. public List<DataEntry> find(boolean preModerate, String target, int start, int limit, String preview, String moderated, String q, String begin, String end, String date, boolean withImage) {
  186. MorphiaQuery query = q(preModerate, withImage);
  187. if (date.equals("custom"))
  188. query = filterDate(query, Long.parseLong(begin), Long.parseLong(end));
  189. else
  190. query = filterDate(query, date);
  191. if (target != null && !target.equals("all"))
  192. query = filter(query, "target", target);
  193. query = filterModeration(query, moderated);
  194. if (q != null && !q.equals("") && !q.equals("undefined"))
  195. query = search(query, q);
  196. if (!preview.equals("all"))
  197. query = filter(query, "preview", Boolean.parseBoolean(preview));
  198. query = segment(query, start, limit);
  199. return query.asList();
  200. }
  201. public DataEntry moderate(boolean preModerate, String entryId, boolean approved, boolean withImage) {
  202. MorphiaQuery q = q(preModerate, withImage);
  203. DataEntry entry = q.filter("_id", new ObjectId(entryId)).get();
  204. if (null != entry) {
  205. entry.moderated = true;
  206. entry.approved = approved;
  207. }
  208. return entry;
  209. }
  210. }
  211. public String toJSON() {
  212. return new Gson().toJson(this);
  213. }
  214. public Map<String, Object> toMap() {
  215. Map<String, Object> map = new HashMap<String, Object>();
  216. for (KV kv : data) {
  217. map.put(kv.key, kv.value);
  218. }
  219. return map;
  220. }
  221. public static enum ModerateState {NM, AP, RJ}
  222. public static enum IMAGE_TYPE {FB, UP, NULL}
  223. private static DataEntry newEntry(ModerateState moderate, IMAGE_TYPE image) {
  224. DataEntry entry = new DataEntry();
  225. boolean approved = moderate == ModerateState.AP;
  226. switch (moderate) {
  227. case AP:
  228. case RJ:
  229. entry.moderated = true;
  230. entry.approved = approved;
  231. default:
  232. }
  233. entry.hasImage = true;
  234. switch (image) {
  235. case FB:
  236. entry.addData("facebookImage", "fb");
  237. break;
  238. case UP:
  239. entry.addData("upload", "up");
  240. break;
  241. default: // do nothing
  242. entry.hasImage = false;
  243. }
  244. entry.code = moderate + "-" + image;
  245. return entry;
  246. }
  247. public static void prepareData() {
  248. DataEntry.deleteAll();
  249. for (ModerateState ms: ModerateState.values()) {
  250. for (IMAGE_TYPE img: IMAGE_TYPE.values()) {
  251. newEntry(ms, img).save();
  252. }
  253. }
  254. }
  255. }