PageRenderTime 43ms CodeModel.GetById 32ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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