· 9 years ago · Feb 25, 2017, 03:48 PM
1package com.example.rayan.mydb;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.SQLException;
7import android.database.sqlite.SQLiteDatabase;
8import android.database.sqlite.SQLiteOpenHelper;
9import android.util.Log;
10
11import com.example.rayan.mydb.MyClasses.*;
12
13import java.util.ArrayList;
14
15public class RZDatabaseManager {
16 // the Activity or Application that is creating an object from this class.
17 Context context;
18
19 // a reference to the database used by this application/object
20 public SQLiteDatabase db;
21
22 // These constants are specific to the database. They should be
23 // changed to suit your needs.
24 private final String DB_NAME = "RZ_DB";
25 private final int DB_VERSION = 01;
26
27 private String[] user_fields = new String[] { "_id", "name", "username",
28 "password", "is_admin" };
29 private final String TABLE_USER = "user";
30
31 private String[] test_fields = new String[] { "_id", "test_name",
32 "type_id", "max_score", "level", "chapter_id" };
33 private final String TABLE_TEST = "test";
34
35 private String[] chapter_fields = new String[] { "_id", "name", "body" };
36 private final String TABLE_CHAPTER = "chapter";
37
38 private String[] question_fields = new String[] { "_id", "body", "test_id" };
39 private final String TABLE_QUESTION = "question";
40
41 private String[] options_fields = new String[] { "_id", "body",
42 "question_id", "is_correct" };
43 private final String TABLE_OPTIONS = "options";
44
45 private String[] test_type_fields = new String[] { "_id", "type" };
46 private final String TABLE_TEST_TYPE = "test_type";
47
48 private String[] test_taker_fields = new String[] { "_id", "student_id",
49 "test_id", "date", "score" };
50 private final String TABLE_TEST_TAKER = "test_taker";
51
52 public RZDatabaseManager(Context context) {
53 this.context = context;
54
55 // create or open the database
56 CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context);
57 this.db = helper.getWritableDatabase();
58
59 }
60 /**
61 * @author Rayan Zahab
62 * @param db
63 * handler
64 * @param tableName
65 * table to create
66 * @param fields
67 * fields of the table
68 */
69 public void createTable(SQLiteDatabase db, String tableName, String[] fields) {
70 String query = "create table " + tableName + " (";
71 for (int position = 0; position < fields.length; position++) {
72 String field = fields[position];
73 if (position == 0) {
74 query = query + field
75 + " integer primary key autoincrement not null, ";
76 } else if (position != fields.length - 1) {
77 query = query + field + " text,";
78 } else {
79 query = query + field + " text" + ");";
80 }
81 }
82 db.execSQL(query);
83 }
84
85 private class CustomSQLiteOpenHelper extends SQLiteOpenHelper {
86 public CustomSQLiteOpenHelper(Context context) {
87 super(context, DB_NAME, null, DB_VERSION);
88 }
89
90 @Override
91 public void onCreate(SQLiteDatabase db) {
92 createTable(db, TABLE_USER, user_fields);
93 createTable(db, TABLE_QUESTION, question_fields);
94 createTable(db, TABLE_OPTIONS, options_fields);
95 createTable(db, TABLE_CHAPTER, chapter_fields);
96 createTable(db, TABLE_TEST, test_fields);
97 createTable(db, TABLE_TEST_TYPE, test_type_fields);
98 createTable(db, TABLE_TEST_TAKER, test_taker_fields);
99
100 }
101
102 @Override
103 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
104 }
105 }
106
107 public void createAdmin() {
108 String name = "Admin";
109 String username = "admin";
110 String pass = "admin";
111 String pass2 = "admin";
112 User u = new User(name, username, pass, pass2);
113 u.setAdmin(true);
114
115 insertRow(u);
116
117 }
118
119 /**
120 * This function s rows: It knws to which table to save according to
121 * the type of the class sent
122 *
123 * @author Rayan Zahab <rayanzahab@gmail.com>
124 */
125 public int insertRow(Object o) {
126
127 String[] fields = new String[] {};
128 String[] values = new String[] {};
129 String tableName = "";
130 String cl_name = o.getClass().getSimpleName();
131 if (cl_name.equals("User")) {
132 fields = user_fields;
133 tableName = TABLE_USER;
134 User u = (User) o;
135 values = new String[] { u.getName(), u.getUsername(),
136 u.getPassword(), String.valueOf(u.isAdmin()) };
137 }
138
139 else if (cl_name.equals("Chapter")) {
140 fields = chapter_fields;
141 tableName = TABLE_CHAPTER;
142 Chapter c = (Chapter) o;
143 values = new String[] { c.getName(), c.getBody() };
144 }
145
146 else if (cl_name.equals("TestType")) {
147 fields = test_type_fields;
148 tableName = TABLE_TEST_TYPE;
149 TestType c = (TestType) o;
150 values = new String[] { c.getType() };
151 }
152
153 else if (cl_name.equals("Test")) {
154 fields = test_fields;
155 tableName = TABLE_TEST;
156 Test c = (Test) o;
157 values = new String[] { "" + c.getName(), "" + c.getType_id(),
158 "" + c.getMaxScore(), "" + c.getLevel(),
159 "" + c.getChapter_id() };
160 }
161
162 else if (cl_name.equals("Question")) {
163 fields = question_fields;
164 tableName = TABLE_QUESTION;
165 Question c = (Question) o;
166 values = new String[] { c.getBody(), "" + c.getTest_id() };
167 }
168
169 else if (cl_name.equals("Option")) {
170 fields = options_fields;
171 tableName = TABLE_OPTIONS;
172 Option c = (Option) o;
173 values = new String[] { c.getBody(), c.getQuestion_id() + "",
174 new Boolean(c.isCorrect()).toString() };
175 }
176 else if (cl_name.equals("TestTaker")) {
177 fields = test_taker_fields;
178 tableName = TABLE_TEST_TAKER;
179 TestTaker c = (TestTaker) o;
180
181 values = new String[] { c.getUser_id()+"", c.getTest_id() + "",c.getDate()+ "",c.getScore() + ""};
182
183 }
184 ContentValues content = new ContentValues();
185 for (int position = 1; position < fields.length; position++) {
186 String field = fields[position];
187 String value = values[position - 1];
188 content.put(field, value);
189 }
190 try {
191 return (int) db.insert(tableName, null, content);
192 } catch (Exception e) {
193 Log.e("DB ERROR", e.toString());
194 e.printStackTrace();
195 }
196 return 0;
197 }
198
199 /**
200 * UPDATING A ROW IN THE DATABASE TABLE
201 *
202 * @author Rayan Zahab & andreh abboud
203 * @param o
204 */
205 public void deleteRow(Object o) {
206 String tableName = "";
207 int id = 0;
208 String cl_name = o.getClass().getSimpleName();
209 /*
210 * if (cl_name.equals("User")) { tableName = "user"; User u = (User) o;
211 * id = u.getID(); try { db.delete(TABLE_RESERVATION, " user =" + id,
212 * null); } catch (Exception e) { Log.e("DB ERROR", e.toString());
213 * e.printStackTrace(); } } else if (cl_name.equals("Room")) { tableName
214 * = TABLE_ROOM; Room r = (Room) o; id = r.getId(); try {
215 * db.delete(TABLE_RESERVATION, " room =" + id, null); } catch
216 * (Exception e) { Log.e("DB ERROR", e.toString()); e.printStackTrace();
217 * } } else if (cl_name.equals("Reservation")) { tableName =
218 * TABLE_RESERVATION; Reservation res = (Reservation) o; id =
219 * res.getId(); }
220 */
221 // ask the database manager to delete the row of given id
222 try {
223 db.delete(tableName, " _id =" + id, null);
224 } catch (Exception e) {
225 Log.e("DB ERROR", e.toString());
226 e.printStackTrace();
227 }
228 }
229
230 /**
231 * UPDATING A ROW IN THE DATABASE TABLE
232 *
233 * @author Rayan Zahab
234 * @param o
235 * to edit having the old id, and new values
236 */
237 public void updateRow(Object o) {
238 String[] fields = new String[] {};
239 String[] values = new String[] {};
240 String tableName = "";
241 int id = 0;
242 String cl_name = o.getClass().getSimpleName();
243 /*
244 * if (cl_name.equals("User")) { fields = user_fields; tableName =
245 * "user"; User u = (User) o; id = u.getID(); values = new String[] {
246 * u.getFullName(), u.getEmail(), u.getCompany(), u.getPhoneNumber(),
247 * u.getPassword(), u.getIsAdmin() }; } else if (cl_name.equals("Room"))
248 * { fields = room_fields; tableName = "room"; Room r = (Room) o; id =
249 * r.getId(); values = new String[] { r.getName(), r.getAddress(),
250 * r.getCapacity(), r.getDescription() }; } else if
251 * (cl_name.equals("Reservation")) { fields = reservation_fields;
252 * tableName = "reservation"; Reservation res = (Reservation) o; Date
253 * date = res.getDate(); String date_str = date.toString(); values = new
254 * String[] { date_str, res.getFrom(), res.getTo(), "" +
255 * res.getRoomId(), "" + res.getUserId() }; }
256 */
257 ContentValues content = new ContentValues();
258 for (int position = 1; position < fields.length; position++) {
259 String field = fields[position];
260 String value = values[position - 1];
261 content.put(field, value);
262 }
263 // ask the database object to update the database row of given rowID
264 try {
265 db.update(tableName, content, "_id" + "=" + id, null);
266 } catch (Exception e) {
267 Log.e("DB Error", e.toString());
268 e.printStackTrace();
269 }
270 }
271
272 /**
273 * @author Rayan Zahab RETRIEVING A ROW FROM A DATABASE TABLE
274 * @param rowID
275 * : the id of the row to retrieve, tableName :the table from
276 * which to get the row, fields : the fields of the table
277 * @return an array containing the data from the row
278 */
279 public ArrayList<Object> getRowAsArray(long rowID, String tableName,
280 String[] fields) {
281 ArrayList<Object> rowArray = new ArrayList<Object>();
282 Cursor cursor;
283 try {
284 cursor = db.query(tableName, fields, fields[0] + "=" + rowID, null,
285 null, null, null);
286 cursor.moveToFirst();
287 if (!cursor.isAfterLast()) {
288 do {
289 for (int position = 0; position < fields.length; position++) {
290 rowArray.add(cursor.getString(position));
291 }
292 } while (cursor.moveToNext());
293 }
294 cursor.close();
295 } catch (SQLException e) {
296 Log.e("DB ERROR", e.toString());
297 e.printStackTrace();
298 }
299 return rowArray;
300 }
301 //andreh abboud
302 /**
303 *
304 * Check if the login info are right to allow login
305 *
306 * @author Rayan Zahab andreh abboud
307 * @param user
308 * @return
309 */
310 public User getRowAsArrayForLogin(User user) {
311 String[] fields = user_fields;
312 String tableName = "user";
313 ArrayList<String> rowArray = new ArrayList<String>();
314 User userReturn = null;
315 Cursor cursor;
316 try {
317 cursor = db.query(tableName, fields, fields[2] + " LIKE " + "'"
318 + user.getUsername() + "' AND " + fields[3] + " LIKE "
319 + "'" + user.getPassword() + "'", null, null, null, null);
320 cursor.moveToFirst();
321 if (!cursor.isAfterLast()) {
322 userReturn = new User();
323 do {
324 for (int position = 0; position < fields.length; position++) {
325 rowArray.add(cursor.getString(position));
326
327 }
328 } while (cursor.moveToNext());
329 userReturn.setId(Integer.parseInt(rowArray.get(0)));
330 userReturn.setName(rowArray.get(1));
331 userReturn.setUsername(rowArray.get(2));
332 userReturn.setAdmin(Boolean.parseBoolean(rowArray.get(4)));
333 userReturn.setPassword(rowArray.get(3));
334 }
335 cursor.close();
336 } catch (SQLException e) {
337 Log.e("DB ERROR", e.toString());
338 e.printStackTrace();
339 }
340
341 return userReturn;
342 }
343
344 /**
345 *
346 * Get rows having param=passed value in the paased table
347 *
348 * @author Rayan Zahab
349 * @param param
350 * : the position of the attribute to companre in the table
351 * fields
352 * @param tableName
353 * @param value
354 * is the value to search for
355 * @return
356 */
357 public ArrayList<ArrayList<Object>> getRowAsArrayByPosition(int param,
358 String tableName, String value) {
359 String[] fields = user_fields;
360 /*
361 * if (tableName.equals("User")) { fields = user_fields;
362 *
363 * } else if (tableName.equals("Room")) { fields = room_fields; } else
364 * if (tableName.equals("Reservation")) { fields = reservation_fields; }
365 */
366 ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
367 Cursor cursor;
368 try {
369 cursor = db.query(tableName, fields, fields[param] + " LIKE '"
370 + value + "'", null, null, null, null);
371 cursor.moveToFirst();
372 if (!cursor.isAfterLast()) {
373 do {
374 ArrayList<Object> dataList = new ArrayList<Object>();
375 for (int position = 0; position < fields.length; position++) {
376 dataList.add(cursor.getString(position));
377 }
378 dataArrays.add(dataList);
379 } while (cursor.moveToNext());
380 }
381 cursor.close();
382 } catch (SQLException e) {
383 Log.e("DB ERROR", e.toString());
384 e.printStackTrace();
385 }
386 return dataArrays;
387 }
388
389 /**
390 *
391 * Get attribute at position ATT having attribute at position param in
392 * tableName
393 *
394 * @author Rayan Zahab
395 * @param param
396 * : the position of the attribute to companre in the table
397 * fields
398 * @param param
399 * : the position of the attribute to get it value
400 * @param tableName
401 * @param value
402 * is the value to search for
403 * @return
404 */
405 public String getValueByParam(String tableName, int att, int param,
406 String value) {
407 String[] fields = user_fields;
408
409 if (tableName.equals("User")) {
410 fields = user_fields;
411 tableName = TABLE_USER;
412 } else if (tableName.equals("TestType")) {
413 fields = test_type_fields;
414 tableName = TABLE_TEST_TYPE;
415 } else if (tableName.equals("Chapter")) {
416 fields = chapter_fields;
417 tableName = TABLE_CHAPTER;
418 } else if (tableName.equals("Test")) {
419 fields = test_fields;
420 tableName = TABLE_TEST;
421 } else if (tableName.equals("Question")) {
422 fields = question_fields;
423 tableName = TABLE_QUESTION;
424 } else if (tableName.equals("Option")) {
425 fields = options_fields;
426 tableName = TABLE_OPTIONS;
427 }
428 Cursor cursor;
429 String toReturn = "";
430 try {
431 cursor = db.query(tableName, fields, fields[param] + " LIKE '"
432 + value + "'", null, null, null, null);
433 cursor.moveToFirst();
434
435 if (!cursor.isAfterLast()) {
436 do {
437 toReturn = cursor.getString(att);
438 cursor.close();
439 return toReturn;
440 } while (cursor.moveToNext());
441 }
442
443 cursor.close();
444 } catch (SQLException e) {
445 Log.e("DB ERROR", e.toString());
446 e.printStackTrace();
447 }
448 return toReturn;
449 }
450 public String getValueByTParam(String tableName, int att, int param1, int param2,
451 String value1,String value2) {
452 String[] fields = user_fields;
453
454 if (tableName.equals("User")) {
455 fields = user_fields;
456 tableName = TABLE_USER;
457 } else if (tableName.equals("TestType")) {
458 fields = test_type_fields;
459 tableName = TABLE_TEST_TYPE;
460 } else if (tableName.equals("Chapter")) {
461 fields = chapter_fields;
462 tableName = TABLE_CHAPTER;
463 } else if (tableName.equals("Test")) {
464 fields = test_fields;
465 tableName = TABLE_TEST;
466 } else if (tableName.equals("Question")) {
467 fields = question_fields;
468 tableName = TABLE_QUESTION;
469 } else if (tableName.equals("Option")) {
470 fields = options_fields;
471 tableName = TABLE_OPTIONS;
472 }
473 Cursor cursor;
474 String toReturn = "";
475 try {
476 cursor = db.query(tableName, fields, fields[param1] + " LIKE '"
477 + value1 + "' AND "+fields[param2]+" LIKE '"+value2+"'", null, null, null, null);
478
479 cursor.moveToFirst();
480
481 if (!cursor.isAfterLast()) {
482 do {
483 toReturn = cursor.getString(att);
484 cursor.close();
485 return toReturn;
486 } while (cursor.moveToNext());
487 }
488
489 cursor.close();
490 } catch (SQLException e) {
491 Log.e("DB ERROR", e.toString());
492 e.printStackTrace();
493 }
494 return toReturn;
495 }
496
497 /**
498 * Checks if a username (email) already exists in the db
499 *
500 * @author Rayan Zahab
501 * table fields
502 * @return
503 */
504 public ArrayList<String> getRowAsArrayByUser(Object u) {
505 ArrayList<String> rowArray = new ArrayList<String>();
506 Cursor cursor;
507 String[] fields = user_fields;
508 String tableName = "user";
509 String email = "";// u.getEmail();
510
511 try {
512 cursor = db.query(tableName, fields, fields[2] + " LIKE " + "'"
513 + email + "'", null, null, null, null);
514 cursor.moveToFirst();
515 if (!cursor.isAfterLast()) {
516 do {
517 for (int position = 0; position < fields.length; position++) {
518 rowArray.add(cursor.getString(position));
519 }
520 } while (cursor.moveToNext());
521 }
522 cursor.close();
523 } catch (SQLException e) {
524 Log.e("DB ERROR", e.toString());
525 e.printStackTrace();
526 }
527 return rowArray;
528 }
529
530 /**
531 * RETRIEVING ALL ROWS FROM A DATABASE TABLE
532 *
533 * @author Rayan Zahab
534 * the fields of the table
535 * @return an array containing the data of the table
536 *
537 */
538 // use this to list chapters!!!!
539 public ArrayList<ArrayList<Object>> getTablesRowsAsArrays(Object o) {
540 String tableName = "";
541 String[] fields = new String[] {};
542 String cl_name = o.getClass().getSimpleName();
543
544 if (cl_name.equals("User")) {
545 fields = user_fields;
546 tableName = "user";
547 } else if (cl_name.equals("Chapter")) {
548 fields = chapter_fields;
549 tableName = "chapter";
550 }
551
552
553 ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
554 Cursor cursor;
555 try {
556 cursor = db.query(tableName, fields, null, null, null, null, null);
557 cursor.moveToFirst();
558 if (!cursor.isAfterLast()) {
559 do {
560 ArrayList<Object> dataList = new ArrayList<Object>();
561 for (int position = 0; position < fields.length; position++) {
562 dataList.add(cursor.getString(position));
563 }
564 dataArrays.add(dataList);
565 } while (cursor.moveToNext());
566 }
567 } catch (SQLException e) {
568 Log.e("DB Error", e.toString());
569 e.printStackTrace();
570 }
571 return dataArrays;
572 }
573
574
575 public TestSet getChapterTestByType(int chapterId, int typeId) {
576 String tableName = "test";
577 String[] fields = test_fields;
578
579 Cursor cursor;
580 try {
581 cursor = db.query(tableName, fields, fields[5] + " LIKE "
582 + chapterId + " AND " + fields[2] + " LIKE " + typeId,
583 null, null, null, null);
584 cursor.moveToFirst();
585 if (!cursor.isAfterLast()) {
586
587 Test testTmp = new Test();
588 do {
589 ArrayList<String> dataList = new ArrayList<String>();
590 for (int position = 0; position < fields.length; position++) {
591 dataList.add(cursor.getString(position));
592 }
593
594 testTmp.setId(Integer.parseInt(dataList.get(0)));
595 testTmp.setType_id(Integer.parseInt(dataList.get(2)));
596 testTmp.setMaxScore(Integer.parseInt(dataList.get(3)));
597 testTmp.setLevel(Integer.parseInt(dataList.get(4)));
598 testTmp.setChapter_id(Integer.parseInt(dataList.get(5)));
599
600 TestSet setTmp = new TestSet();
601 setTmp.setTest(testTmp);
602 setTmp.setQuestions(getQuestions(testTmp.getId()));
603 return setTmp;
604 } while (cursor.moveToNext());
605 }
606 } catch (SQLException e) {
607 Log.e("DB Error", e.toString());
608 e.printStackTrace();
609 }
610 return null;
611 }
612 public ArrayList<Chapter> getChapters(int chId) {
613 String tableName = "chapter";
614 String[] fields = chapter_fields;
615
616 ArrayList<Chapter> myReturn= new ArrayList<Chapter>();
617 Cursor cursor;
618 try {
619 String where = "";
620 if(chId!=0)
621 {
622 where = fields[0]+" LIKE "+chId;
623 }
624 cursor = db.query(tableName, fields, where,
625 null, null, null, null);
626 cursor.moveToFirst();
627 if (!cursor.isAfterLast()) {
628
629 do {
630 Chapter chTmp= new Chapter();
631 ArrayList<String> dataList = new ArrayList<String>();
632 for (int position = 0; position < fields.length; position++) {
633 dataList.add(cursor.getString(position));
634 }
635
636 chTmp.setId(Integer.parseInt(dataList.get(0)));
637 chTmp.setName(dataList.get(1));
638 chTmp.setBody(dataList.get(2));
639 myReturn.add(chTmp);
640 } while (cursor.moveToNext());
641 }
642 } catch (SQLException e) {
643 Log.e("DB Error", e.toString());
644 e.printStackTrace();
645 }
646 return myReturn;
647 }
648
649 public ArrayList<Question> getQuestions(int testId) {
650 String tableName = "";
651 String[] fields = new String[] {};
652 ArrayList<Question> myQsts = new ArrayList<Question>();
653 fields = question_fields;
654 tableName = "question";
655 Cursor cursor;
656 try {
657 cursor = db.query(tableName, fields, fields[2] + " LIKE " + testId,
658 null, null, null, null);
659 cursor.moveToFirst();
660 if (!cursor.isAfterLast()) {
661 int i=0;
662 do {
663 Question qstTmp = new Question();
664 ArrayList<String> dataList = new ArrayList<String>();
665 for (int position = 0; position < fields.length; position++) {
666 dataList.add(cursor.getString(position));
667 }
668 qstTmp.setId(Integer.parseInt(dataList.get(0)));
669 qstTmp.setBody(dataList.get(1));
670 qstTmp.setTest_id(Integer.parseInt(dataList.get(2)));
671 qstTmp.setMyOptions(getOptions(qstTmp.getId()));
672 myQsts.add(i, qstTmp);
673 i++;
674 } while (cursor.moveToNext());
675 }
676 } catch (SQLException e) {
677 Log.e("DB Error", e.toString());
678 e.printStackTrace();
679 }
680 return myQsts;
681 }
682
683 public ArrayList<Option> getOptions(int questId) {
684 String tableName = "";
685 String[] fields = new String[] {};
686 ArrayList<Option> myOpts = new ArrayList<Option>();
687 fields = options_fields;
688 tableName = "options";
689 Cursor cursor;
690 try {
691 cursor = db.query(tableName, fields,
692 fields[2] + " LIKE " + questId, null, null, null, null);
693 cursor.moveToFirst();
694 if (!cursor.isAfterLast()) {
695 do {
696 Option optTmp = new Option();
697 ArrayList<String> dataList = new ArrayList<String>();
698 for (int position = 0; position < fields.length; position++) {
699 dataList.add(cursor.getString(position));
700 }
701 optTmp.setId(Integer.parseInt(dataList.get(0)));
702 optTmp.setBody(dataList.get(1));
703 optTmp.setQuestion_id(Integer.parseInt(dataList.get(2)));
704 optTmp.setCorrect(new Boolean(dataList.get(3)));
705
706 myOpts.add(optTmp);
707 } while (cursor.moveToNext());
708 }
709 } catch (SQLException e) {
710 Log.e("DB Error", e.toString());
711 e.printStackTrace();
712 }
713 return myOpts;
714 }
715
716 public ArrayList<ArrayList<Object>> getTest(String name) {
717 String[] fields = new String[] {};
718 String cl_name = "Test";
719 String tableName = "";
720
721 fields = test_fields;
722 tableName = "test";
723
724 ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
725 Cursor cursor;
726 try {
727 cursor = db.query(tableName, fields, fields[1] + " LIKE " + name,
728 null, null, null, null);
729 cursor.moveToFirst();
730 if (!cursor.isAfterLast()) {
731 do {
732 ArrayList<Object> dataList = new ArrayList<Object>();
733 for (int position = 0; position < fields.length; position++) {
734 dataList.add(cursor.getString(position));
735 }
736 dataArrays.add(dataList);
737 } while (cursor.moveToNext());
738 }
739 } catch (SQLException e) {
740 Log.e("DB Error", e.toString());
741 e.printStackTrace();
742 }
743 return dataArrays;
744 }
745 public ArrayList<TestTaker> getUserTests(int uId) {
746 String tableName = "test_taker";
747 String[] fields = test_taker_fields;
748
749 ArrayList<TestTaker> myReturn= new ArrayList<TestTaker>();
750 Cursor cursor;
751 try {
752 String where = "";
753 if(uId!=0)
754 {
755 where = fields[1]+" LIKE "+uId;
756 }
757 cursor = db.query(tableName, fields, where,
758 null, null, null, null);
759 cursor.moveToFirst();
760 if (!cursor.isAfterLast()) {
761
762 do {
763 TestTaker uTmp= new TestTaker();
764 ArrayList<String> dataList = new ArrayList<String>();
765 for (int position = 0; position < fields.length; position++) {
766 dataList.add(cursor.getString(position));
767 }
768 uTmp.setId(Integer.parseInt(dataList.get(0)));
769 uTmp.setUser_id(Integer.parseInt(dataList.get(1)));
770 uTmp.setTest_id(Integer.parseInt(dataList.get(2)));
771 uTmp.setScore(Integer.parseInt(dataList.get(4)));
772 uTmp.setDate(dataList.get(3));
773 myReturn.add(uTmp);
774 } while (cursor.moveToNext());
775 }
776 } catch (SQLException e) {
777 Log.e("DB Error", e.toString());
778 e.printStackTrace();
779 }
780 return myReturn;
781 }
782
783 public ArrayList<User> getUsers(int uId) {
784 String tableName = "user";
785 String[] fields = user_fields;
786
787 ArrayList<User> myReturn= new ArrayList<User>();
788 Cursor cursor;
789 try {
790 String where = "";
791 if(uId!=0)
792 {
793 where = fields[0]+" LIKE "+uId;
794 }
795 cursor = db.query(tableName, fields, where,
796 null, null, null, null);
797 cursor.moveToFirst();
798 if (!cursor.isAfterLast()) {
799
800 do {
801 User uTmp= new User();
802 ArrayList<String> dataList = new ArrayList<String>();
803 for (int position = 0; position < fields.length; position++) {
804 dataList.add(cursor.getString(position));
805 }
806
807 uTmp.setId(Integer.parseInt(dataList.get(0)));
808 uTmp.setName(dataList.get(1));
809 uTmp.setUsername(dataList.get(2));
810 uTmp.setPassword(dataList.get(3));
811 myReturn.add(uTmp);
812 } while (cursor.moveToNext());
813 }
814 } catch (SQLException e) {
815 Log.e("DB Error", e.toString());
816 e.printStackTrace();
817 }
818 return myReturn;
819 }
820 /**
821 * This function return the a db object based on its position
822 *
823 * @author Rayan Zahab
824 *
825 * @param position
826 * position of the element to get (usually given based on user
827 * selection)
828 * @param o
829 * o this will be from type user or room or reservation. And will
830 * be processed based on the type in the getTablesRowsAsArrays()
831 *
832 * @return ArrayList<Object>
833 */
834 public ArrayList<Object> getObjectFromPosition(int position, Object o) {
835 ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
836
837 // Get all data in the db by passing the object
838 dataArrays = this.getTablesRowsAsArrays(o);
839
840 // get the object at the needed position
841 ArrayList<Object> row = dataArrays.get(position);
842
843 return row;
844 }
845
846
847 public void testType() {
848 ArrayList<Object> tt = new ArrayList<Object>();
849 tt.add(new TestType("TF"));
850 tt.add(new TestType("MCQ"));
851 tt.add(new TestType("OP"));
852 for (int i = 0; i < tt.size(); i++) {
853 insertRow(tt.get(i));
854 }
855 }
856}