PageRenderTime 84ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/src/edu/mit/meet/meetag/connectors/DAO.java

https://bitbucket.org/simplexproject/simplex-development
Java | 353 lines | 133 code | 41 blank | 179 comment | 13 complexity | 352eb41975ecfeac264142c9ff669fd0 MD5 | raw file
  1. /*
  2. *
  3. * SIMPLEx CONFIDENTIAL
  4. * __________________
  5. *
  6. * [2012] - [2013] SIMPLEx
  7. * All Rights Reserved.
  8. *
  9. * NOTICE: All information contained herein is, and remains
  10. * the property of SIMPLEx and its suppliers,
  11. * if any. The intellectual and technical concepts contained
  12. * herein are proprietary to SIMPLEx
  13. * and is protected by trade secret or copyright law.
  14. * Dissemination of this information or reproduction of this material
  15. * is strictly forbidden unless prior written permission is obtained
  16. * from SIMPLEx.
  17. */
  18. package edu.mit.meet.meetag.connectors;
  19. import java.util.ArrayList;
  20. import com.googlecode.objectify.Objectify;
  21. import com.googlecode.objectify.ObjectifyService;
  22. import com.googlecode.objectify.Query;
  23. import com.googlecode.objectify.util.DAOBase;
  24. import edu.mit.meet.meetag.entities.Course;
  25. import edu.mit.meet.meetag.entities.Group;
  26. import edu.mit.meet.meetag.entities.Lesson;
  27. import edu.mit.meet.meetag.entities.MailBox;
  28. import edu.mit.meet.meetag.entities.Organization;
  29. import edu.mit.meet.meetag.entities.Settings;
  30. import edu.mit.meet.meetag.entities.User;
  31. /**
  32. * This class is the DAO class - a connector to the database. This is a
  33. * singleton.
  34. *
  35. * @author George Khoury
  36. *
  37. */
  38. public class DAO extends DAOBase {
  39. /**
  40. * the singleton itself
  41. */
  42. private static final DAO instance = new DAO();
  43. private DAO() {
  44. }
  45. /**
  46. * Static block of code that register all the entities to the database. All
  47. * the entities that are stored in the database should be register here.
  48. */
  49. static {
  50. ObjectifyService.register(edu.mit.meet.meetag.entities.Message.class);
  51. ObjectifyService.register(MailBox.class);
  52. ObjectifyService.register(User.class);
  53. ObjectifyService.register(Group.class);
  54. ObjectifyService.register(Lesson.class);
  55. ObjectifyService.register(Course.class);
  56. ObjectifyService.register(Organization.class);
  57. // ObjectifyService.register(Component.class);
  58. // ObjectifyService.register(TextQuestion.class);
  59. // ObjectifyService.register(Settings.class);
  60. }
  61. /**
  62. * A static getInstance method that returns the singleton
  63. *
  64. * @return
  65. */
  66. public static DAO getDAO() {
  67. return instance;
  68. }
  69. /*
  70. *
  71. * ======= User section =======
  72. */
  73. /**
  74. * Returns the user by its id, if not found return null
  75. *
  76. * @param email
  77. * the id of the wanted user
  78. * @return a user object if the user was found, null otherwise
  79. */
  80. public User getUser(String email) {
  81. Objectify objectify = ObjectifyService.begin();
  82. return objectify.find(User.class, email);
  83. }
  84. /**
  85. * This method takes a User. If it already exists in the database it updates
  86. * it, otherwise it adds it to the databas Assumes that the input in not
  87. * null.
  88. *
  89. * @param lesson
  90. * The user to add/update
  91. */
  92. public void addAndUpdateUser(User user) {
  93. Objectify objectify = ObjectifyService.begin();
  94. objectify.put(user);
  95. }
  96. /**
  97. * @brief This method delete user from the database. Assumes that the input
  98. * is valid (i.e. not null).
  99. * @param user
  100. * - the user to delete.
  101. */
  102. public void deleteUser(User user) {
  103. Objectify objectify = ObjectifyService.begin();
  104. objectify.delete(user);
  105. }
  106. /**
  107. * @brief Deletes the user by its email.
  108. * @param email
  109. * - the id of the wanted user.
  110. */
  111. public void deleteUser(String email) {
  112. Objectify objectify = ObjectifyService.begin();
  113. objectify.delete(objectify.find(User.class, email));
  114. }
  115. /**
  116. * @brief Validate user finds the user in the databse (by it's email) and
  117. * validates. That the given password is like the one iin the
  118. * database.
  119. * @param email
  120. * - the email of the user.
  121. * @param password
  122. * - the password of the user.
  123. * @return the user id the given password matches the one in the database
  124. * (if the user exists), Otherwise null.
  125. */
  126. public User validateUser(String email, String password) {
  127. Objectify objectify = ObjectifyService.begin();
  128. if (email != null && !email.equals("") && password != null
  129. && !password.equals("")) { // Checks for valid parameters, and
  130. // then search for the user in the
  131. // database:
  132. User usr = objectify.find(User.class, email.toLowerCase().trim());
  133. if ((usr != null)
  134. && (usr.getPassword().equals(User.getEncryptedPassword(
  135. password, email)))) { // Checks validation of
  136. // (encrypted) password:
  137. System.out.println("Successful validation");
  138. return usr;
  139. }
  140. }
  141. System.out.println("Failed login attempt:\n" + "email: " + email
  142. + " Password:" + password);
  143. return null;
  144. }
  145. /**
  146. * @brief This method returns an ArrayList of radix users in the database,
  147. * starting from location index.
  148. * @param index
  149. * - which location to start from.
  150. * @param radix
  151. * - amount of users to get.
  152. * @return ArrayList of radix users, starting from the given index.
  153. */
  154. public ArrayList<User> getUsers(int index, int radix) {
  155. ArrayList<User> users = new ArrayList<User>();
  156. Objectify objectify = ObjectifyService.begin();
  157. Query<User> objectifyUsers = objectify.query(User.class).offset(index)
  158. .limit(radix);
  159. for (User u : objectifyUsers) {
  160. users.add(u);
  161. }
  162. return users;
  163. }
  164. /*
  165. * ======= Lesson section =======
  166. */
  167. /**
  168. * @brief Returns a lesson by the given id.
  169. * @param id
  170. * - the lesson to get.
  171. * @return A lesson in the database with the diven id, or null if does not
  172. * exist
  173. */
  174. public Lesson getLesson(Long id) {
  175. Objectify objectify = ObjectifyService.begin();
  176. return objectify.find(Lesson.class, id);
  177. }
  178. /**
  179. * @brief This method takes a lesson. If it already exists in the database
  180. * it updates it, Otherwise it adds it to the databas Assumes that
  181. * the input in not null.
  182. * @param lesson
  183. * - The lesson to add/update.
  184. */
  185. public void addAndUpdateLesson(Lesson lesson) {
  186. Objectify objectify = ObjectifyService.begin();
  187. objectify.put(lesson);
  188. }
  189. /*
  190. * ======= Course section =======
  191. */
  192. /**
  193. * @brief Returns the course by its id, if not found return null.
  194. * @param id
  195. * - the id of the wanted course.
  196. * @return The course if was found in the database, null otherwise.
  197. */
  198. public Course getCourse(long id) {
  199. Objectify objectify = ObjectifyService.begin();
  200. return objectify.find(Course.class, id);
  201. }
  202. /**
  203. * @brief This method takes a course. If it already exists in the database
  204. * it updates it, Otherwise it adds it to the databas Assumes that
  205. * the input in not null.
  206. * @param course
  207. * - The course to add/update.
  208. */
  209. public void addAndUpdateCourse(Course course) {
  210. Objectify objectify = ObjectifyService.begin();
  211. objectify.put(course);
  212. }
  213. /**
  214. * @brief This method returns an ArrayList of radix courses in the database,
  215. * starting from location index.
  216. * @param index
  217. * - which location to start from.
  218. * @param radix
  219. * - amount of users to get.
  220. * @return ArrayList of radix courses, starting from the given index
  221. */
  222. public ArrayList<Course> getCourses(int index, int radix) {
  223. ArrayList<Course> courses = new ArrayList<Course>();
  224. Objectify objectify = ObjectifyService.begin();
  225. Query<Course> crs = objectify.query(Course.class).offset(index)
  226. .limit(radix);
  227. for (Course course : crs) {
  228. courses.add(course);
  229. }
  230. return courses;
  231. }
  232. /**
  233. * @return ArrayList of all the courses in the database
  234. */
  235. public ArrayList<Course> getAllCourses() {
  236. ArrayList<Course> courses = new ArrayList<Course>();
  237. Objectify objectify = ObjectifyService.begin();
  238. Query<Course> crs = objectify.query(Course.class).offset(0);
  239. for (Course course : crs) {
  240. courses.add(course);
  241. }
  242. return courses;
  243. }
  244. /*
  245. * ======= Settings section =======
  246. */
  247. /**
  248. * @brief This method takes a setting set. If it already exists in the
  249. * database it updates it, Otherwise it adds it to the databas
  250. * Assumes that the input in not null.
  251. * @param settings
  252. * - The settings set to add/update.
  253. * @param type
  254. * - The type of the given setting set.
  255. */
  256. public void addAndUpdateSettingsSet(Settings settings) {
  257. Objectify objectify = ObjectifyService.begin();
  258. objectify.put(settings);
  259. }
  260. /*
  261. * ======= Organization section =======
  262. */
  263. /**
  264. * @brief Deletes the organization by its id.
  265. * @param id
  266. * - The id of the organization you want to delete.
  267. */
  268. public void deleteOrganization(Long id) {
  269. Objectify objectify = ObjectifyService.begin();
  270. objectify.delete(objectify.find(Organization.class, id));
  271. }
  272. /**
  273. * @brief Returns the organization by its id, if not found return null.
  274. * @param id
  275. * - the id of the wanted organization.
  276. * @return The organization if was found in the database, null otherwise.
  277. */
  278. public Organization getOrganization(Long id) {
  279. Objectify objectify = ObjectifyService.begin();
  280. return objectify.find(Organization.class, id);
  281. }
  282. /**
  283. * @brief This method takes a organization. If it already exists in the
  284. * database it updates it, otherwise it adds it to the databas
  285. * Assumes that the input is not null.
  286. * @param orginization
  287. * - The organization to add/update.
  288. */
  289. public void addAndUpdateOrganization(Organization orginization) {
  290. Objectify objectify = ObjectifyService.begin();
  291. objectify.put(orginization);
  292. }
  293. /**
  294. * @brief This method returns an ArrayList of radix organizations in the
  295. * database, starting from location index.
  296. * @param index
  297. * - which location to start from.
  298. * @param radix
  299. * - amount of users to get.
  300. * @return ArrayList of radix organizations, starting from the given index.
  301. */
  302. public ArrayList<Organization> getOrganizations(int index, int radix) {
  303. ArrayList<Organization> organizations = new ArrayList<Organization>();
  304. Objectify objectify = ObjectifyService.begin();
  305. Query<Organization> orgs = objectify.query(Organization.class)
  306. .offset(index).limit(radix);
  307. for (Organization org : orgs) {
  308. organizations.add(org);
  309. }
  310. return organizations;
  311. }
  312. }