Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 51.56 KB | None | 0 0
  1. package com.example.dariabut.myapplication_3.database;
  2.  
  3. import android.database.sqlite.SQLiteDatabase;
  4. import android.database.sqlite.SQLiteOpenHelper;
  5. import android.content.Context;
  6. import android.content.ContentValues;
  7. import android.database.Cursor;
  8. import android.util.Log;
  9.  
  10. import com.example.dariabut.myapplication_3.model.Attendance;
  11. import com.example.dariabut.myapplication_3.model.Course;
  12. import com.example.dariabut.myapplication_3.model.Group;
  13. import com.example.dariabut.myapplication_3.model.Hometask;
  14. import com.example.dariabut.myapplication_3.model.Lesson;
  15. import com.example.dariabut.myapplication_3.model.Mark;
  16. import com.example.dariabut.myapplication_3.model.Student;
  17. import com.example.dariabut.myapplication_3.model.Teacher;
  18. import com.example.dariabut.myapplication_3.model.User;
  19.  
  20. import java.util.ArrayList;
  21. import java.util.List;
  22.  
  23. public class DatabaseHandler extends SQLiteOpenHelper {
  24.  
  25.     //information of database
  26.     private static final int DATABASE_VERSION = 4;
  27.     private static final String DATABASE_NAME = "studdeediary";
  28.     private static DatabaseHandler sInstance;//возмодно добавить .db
  29.  
  30.     // Table names
  31.     private static final String
  32.             TABLE_USER = "User",
  33.             TABLE_TEACHER = "Teacher",
  34.             TABLE_TEACHER_COURSE = "TeacherCourse",
  35.             TABLE_STUDENT = "Student",
  36.             TABLE_COURSE = "Course",
  37.             TABLE_STUDY_PLAN = "StudyPlan",
  38.             TABLE_LESSON = "Lesson",
  39.             TABLE_GROUP = "Groupp",
  40.             TABLE_MARK = "Mark",
  41.             TABLE_ATTENDANCE = "Attendance",
  42.             TABLE_HOMETASK = "Hometask";
  43.  
  44.     // Common column names
  45.     private static final String
  46.             KEY_USER_ID = "userID",
  47.             KEY_TEACHER_ID = "teacherID",
  48.             KEY_STUDENT_ID = "studentID",
  49.             KEY_GROUP_ID = "groupID",
  50.             KEY_COURSE_NAME_ID = "courseNameID",
  51.             KEY_ATTENDANCE_ID = "attendanceID",
  52.             KEY_HOMETASK_ID = "hometaskID",
  53.             KEY_MARK_ID = "markID",
  54.             KEY_LESSON_ID = "lessonID";
  55.  
  56.     // TABLE_USER - column names
  57.     private static final String
  58.             KEY_FULL_NAME = "fullName",
  59.             KEY_EMAIL = "email",
  60.             KEY_PASSWORD = "password",
  61.             KEY_TYPE = "type";
  62.  
  63.     // TABLE_TEACHER - column names
  64.     private static final String
  65.             KEY_ROLE = "role";
  66.  
  67.     // TABLE_COURSE - column names
  68.     private static final String
  69.             KEY_AMOUNT = "amount",
  70.             KEY_CLASS_NUMBER = "classNumber";
  71.  
  72.     // TABLE_STUDENT - column names
  73.     private static final String
  74.             KEY_ADRESS = "adress";
  75.  
  76.     //TABLE_GROUP - column names
  77.     private static final String
  78.             KEY_GROUP_NUMBER_ID = "groupNumberID",
  79.             KEY_GROUP_LABEL = "groupLabel";
  80.  
  81.     //TABLE_LESSON
  82.     private static final String
  83.             KEY_TOPIC = "lessonTopic",
  84.             KEY_ROOM = "room",
  85.             KEY_LESSON_DATE = "lessonDate",
  86.             KEY_LESSON_TIME = "lessonTime",
  87.             KEY_LESSON_TYPE = "lessonType";
  88.  
  89.     //TABLE_HOMETASK
  90.     private static final String
  91.             KEY_DEADLINE = "deadline",
  92.             KEY_HOMETASK_STATUS = "hometaskStatus";
  93.  
  94.     //TABLE_ATTENDANCE
  95.     private static final String
  96.             KEY_ATTENDANCE_STATUS = "attendanceStatus",
  97.             KEY_REASON = "reason";
  98.  
  99.     //TABLE_MARK
  100.     private static final String
  101.             KEY_MARK_VALUE = "markValue",
  102.             KEY_COMMENT = "comment";
  103.  
  104.     public static synchronized DatabaseHandler getInstance(Context context) {
  105.         if (sInstance == null) {
  106.             sInstance = new DatabaseHandler(context.getApplicationContext());
  107.         }
  108.         return sInstance;
  109.     }
  110.  
  111.     public DatabaseHandler(Context context) {
  112.         super(context, DATABASE_NAME, null, DATABASE_VERSION);
  113.     }
  114.  
  115.     @Override
  116.     public void onCreate(SQLiteDatabase db) {
  117.  
  118.         final String createTableUser = "CREATE TABLE " + TABLE_USER + "(" +
  119.                 KEY_USER_ID + " INTEGER PRIMARY KEY, " +
  120.                 KEY_EMAIL + " TEXT, " +
  121.                 KEY_PASSWORD + " TEXT, " +
  122.                 KEY_FULL_NAME + " TEXT, " +
  123.                 KEY_TYPE + " TEXT " + ")";
  124.  
  125.         final String createTableTeacher = "CREATE TABLE " + TABLE_TEACHER + "(" +
  126.                 KEY_TEACHER_ID + " INTEGER PRIMARY KEY, " +
  127.                 // KEY_EMAIL + " TEXT, " +
  128.                 KEY_USER_ID + " INTEGER, " +
  129.                 KEY_ROLE + " TEXT, " +
  130.                 " FOREIGN KEY (" + KEY_USER_ID + ") REFERENCES " +
  131.                 TABLE_USER + "(" + KEY_USER_ID + ") ON DELETE CASCADE )";
  132.  
  133.         final String createTableCourse = "CREATE TABLE " + TABLE_COURSE + "(" +
  134.                 KEY_COURSE_NAME_ID + " TEXT PRIMARY KEY, " +
  135.                 // KEY_EMAIL + " TEXT, " +
  136.                 KEY_AMOUNT + " INTEGER, " +
  137.                 KEY_CLASS_NUMBER + " TEXT " + ")";
  138.  
  139.         final String createTableTeacherCourse = "CREATE TABLE " + TABLE_TEACHER_COURSE + "(" +
  140.                 KEY_TEACHER_ID + " INTEGER, " +
  141.                 KEY_COURSE_NAME_ID + " TEXT, " +
  142.                 KEY_CLASS_NUMBER + " INTEGER, " +
  143.                 " FOREIGN KEY (" + KEY_TEACHER_ID + ") REFERENCES " +
  144.                 TABLE_TEACHER + "(" + KEY_TEACHER_ID + ")," +
  145.                 " FOREIGN KEY (" + KEY_COURSE_NAME_ID + ") REFERENCES " +
  146.                 TABLE_COURSE + "(" + KEY_COURSE_NAME_ID + "))";
  147.  
  148.         final String createTableStudent = "CREATE TABLE " + TABLE_STUDENT + "(" +
  149.                 KEY_STUDENT_ID + " INTEGER PRIMARY KEY, " +
  150.                 KEY_USER_ID + " INTEGER, " +
  151.                 KEY_ADRESS + " TEXT, " +
  152.                 KEY_GROUP_ID + " INTEGER, " +
  153.                 " FOREIGN KEY (" + KEY_USER_ID + ") REFERENCES " +
  154.                 TABLE_USER + "(" + KEY_USER_ID + ")  ON DELETE CASCADE," +
  155.                 " FOREIGN KEY (" + KEY_GROUP_ID + ") REFERENCES " +
  156.                 TABLE_GROUP + "(" + KEY_GROUP_ID +"))";
  157.  
  158.         final String createTableGroup = "CREATE TABLE " + TABLE_GROUP + "(" +
  159.                 KEY_GROUP_ID + " INTEGER PRIMARY KEY, " +
  160.                 KEY_GROUP_NUMBER_ID + " INTEGER, " +
  161.                 KEY_GROUP_LABEL + " TEXT" + ")";
  162.  
  163.         final String createTableStudyPlan ="CREATE TABLE " + TABLE_STUDY_PLAN + "(" +
  164.                 KEY_GROUP_ID + " INTEGER, " +
  165.                 KEY_COURSE_NAME_ID + " TEXT, " +
  166.                 " FOREIGN KEY (" +  KEY_GROUP_ID +  ") REFERENCES " +
  167.                 TABLE_GROUP + "(" +  KEY_GROUP_ID + ")," +
  168.                 " FOREIGN KEY (" + KEY_COURSE_NAME_ID + ") REFERENCES " +
  169.                 TABLE_COURSE + "(" + KEY_COURSE_NAME_ID + "))";
  170.  
  171.         final String createTableLesson = "CREATE TABLE " + TABLE_LESSON +"(" +
  172.                 KEY_LESSON_ID + " INTEGER PRIMARY KEY, " +
  173.                 KEY_TOPIC + " TEXT, " +
  174.                 KEY_LESSON_DATE + " TEXT, " +
  175.                 KEY_LESSON_TIME + " TEXT, " +
  176.                 KEY_LESSON_TYPE + " TEXT, " +
  177.                 KEY_ROOM + " TEXT, " +
  178.                 KEY_COURSE_NAME_ID + " TEXT, " +
  179.                 KEY_GROUP_ID + " INTEGER, " +
  180.                 " FOREIGN KEY (" + KEY_GROUP_ID + ") REFERENCES " +
  181.                 TABLE_GROUP + "(" + KEY_GROUP_ID + "), " +
  182.                 " FOREIGN KEY (" + KEY_COURSE_NAME_ID + ") REFERENCES " +
  183.                 TABLE_COURSE + "(" + KEY_COURSE_NAME_ID + ")  ON DELETE CASCADE)";
  184.  
  185.         final String createTableMark = "CREATE TABLE " + TABLE_MARK + "(" +
  186.                 KEY_MARK_ID + " INTEGER PRIMARY KEY, " +
  187.                 KEY_STUDENT_ID + " INTEGER, " +
  188.                 KEY_LESSON_ID + " INTEGER, " +
  189.                 KEY_MARK_VALUE + " TEXT, " +
  190.                 KEY_COMMENT + " TEXT, " +
  191.                 " FOREIGN KEY (" + KEY_STUDENT_ID + ") REFERENCES " +
  192.                 TABLE_STUDENT + "(" + KEY_STUDENT_ID + ")  ON DELETE CASCADE," +
  193.                 " FOREIGN KEY (" + KEY_LESSON_ID + ") REFERENCES " +
  194.                 TABLE_LESSON + "(" + KEY_LESSON_ID + ")  ON DELETE CASCADE" + ")";
  195.  
  196.         final String createTableHometask = "CREATE TABLE " + TABLE_HOMETASK + "(" +
  197.                 KEY_HOMETASK_ID + " INTEGER PRIMARY KEY, " +
  198.                 KEY_DEADLINE + " TEXT, " +
  199.                 KEY_HOMETASK_STATUS + " TEXT, " +
  200.                 KEY_STUDENT_ID + " INTEGER, " +
  201.                 KEY_LESSON_ID + " INTEGER, " +
  202.                 " FOREIGN KEY (" + KEY_STUDENT_ID + ") REFERENCES " +
  203.                 TABLE_STUDENT + "(" + KEY_STUDENT_ID + ")  ON DELETE CASCADE," +
  204.                 " FOREIGN KEY (" + KEY_LESSON_ID + ") REFERENCES " +
  205.                 TABLE_LESSON + "(" + KEY_LESSON_ID + ")  ON DELETE CASCADE" + ")";
  206.  
  207.         final String createTableAttendance = "CREATE TABLE " + TABLE_ATTENDANCE + "(" +
  208.                 KEY_ATTENDANCE_ID + " INTEGER PRIMARY KEY, " +
  209.                 KEY_ATTENDANCE_STATUS + " TEXT, " +
  210.                 KEY_REASON + " TEXT, " +
  211.                 KEY_STUDENT_ID + " INTEGER, " +
  212.                 KEY_LESSON_ID + " INTEGER, " +
  213.                 " FOREIGN KEY (" + KEY_STUDENT_ID + ") REFERENCES " +
  214.                 TABLE_STUDENT + "(" + KEY_STUDENT_ID + ")  ON DELETE CASCADE," +
  215.                 " FOREIGN KEY (" + KEY_LESSON_ID + ") REFERENCES " +
  216.                 TABLE_LESSON + "(" + KEY_LESSON_ID + ")  ON DELETE CASCADE" + ")";
  217.  
  218.  
  219.  
  220.         db.execSQL(createTableUser);
  221.         db.execSQL(createTableStudent);
  222.         db.execSQL(createTableTeacher);
  223.         db.execSQL(createTableTeacherCourse);
  224.         db.execSQL(createTableCourse);
  225.         db.execSQL(createTableStudyPlan);
  226.         db.execSQL(createTableGroup);
  227.         db.execSQL(createTableLesson);
  228.         db.execSQL(createTableMark);
  229.         db.execSQL(createTableHometask);
  230.         db.execSQL(createTableAttendance);
  231.     }
  232.  
  233.     @Override
  234.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  235.         db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
  236.         db.execSQL("DROP TABLE IF EXISTS " + TABLE_STUDENT);
  237.         db.execSQL("DROP TABLE IF EXISTS " + TABLE_TEACHER);
  238.         db.execSQL("DROP TABLE IF EXISTS " + TABLE_TEACHER_COURSE);
  239.         db.execSQL("DROP TABLE IF EXISTS " + TABLE_COURSE);
  240.         db.execSQL("DROP TABLE IF EXISTS " + TABLE_STUDY_PLAN);
  241.         db.execSQL("DROP TABLE IF EXISTS " + TABLE_GROUP);
  242.         db.execSQL("DROP TABLE IF EXISTS " + TABLE_LESSON);
  243.         db.execSQL("DROP TABLE IF EXISTS " + TABLE_ATTENDANCE);
  244.         db.execSQL("DROP TABLE IF EXISTS " + TABLE_HOMETASK);
  245.         db.execSQL("DROP TABLE IF EXISTS " + TABLE_MARK);
  246.  
  247.         onCreate(db);
  248.     }
  249.  
  250.  
  251.  
  252.     //                                                      User
  253.     //======================================================================================================================================
  254.     public User getUser(int id) {
  255.         SQLiteDatabase db = this.getReadableDatabase();
  256.  
  257.         Cursor cursor = db.query(TABLE_USER, new String[]{
  258.                         KEY_USER_ID, KEY_EMAIL, KEY_PASSWORD, KEY_FULL_NAME, KEY_TYPE}, KEY_USER_ID + "=?",
  259.                 new String[]{String.valueOf(id)}, null, null, null, null);
  260.         if (cursor != null) {
  261.             cursor.moveToFirst();
  262.  
  263.             //Integer userID, String email, String password, String fullName, String type
  264.             User user = new User(
  265.                     cursor.getInt(0),       //KEY_USER_ID
  266.                     cursor.getString(1),       // KEY_EMAIL
  267.                     cursor.getString(2),    // KET_PASSWORD
  268.                     cursor.getString(3),    // KEY_FUL_NAME
  269.                     cursor.getString(4));   // KEY_TYPE
  270.             cursor.close();
  271.             db.close();
  272.             return user;
  273.         }
  274.         db.close();
  275.         return null;
  276.     }
  277.  
  278.     public List<User> getAllUsers(){
  279.         List<User> users = new ArrayList<>();
  280.         String selectQuery = "SELECT " +
  281.                 KEY_USER_ID + ", " +
  282.                 KEY_FULL_NAME +
  283.                 " FROM " + TABLE_USER;
  284.         SQLiteDatabase db = this.getWritableDatabase();
  285.         Cursor cursor = db.rawQuery(selectQuery, null);
  286.  
  287.         if (cursor.moveToFirst()) {
  288.             do {
  289.                 User user = new User();
  290.                 user.setUserID(cursor.getInt(0));
  291.                 user.setFullName(cursor.getString(1));
  292.                 //     user.setEmail(cursor.getString(2));
  293.                 users.add(user);
  294.             } while (cursor.moveToNext());
  295.         }
  296.         cursor.close();
  297.         db.close();
  298.         return users;
  299.     }
  300.  
  301.     public Integer addUser(User user, SQLiteDatabase db) {
  302.         ContentValues userValues = new ContentValues();
  303.         userValues.put(KEY_FULL_NAME, user.getFullName());
  304.         userValues.put(KEY_PASSWORD, user.getPassword());
  305.         userValues.put(KEY_EMAIL, user.getEmail());
  306.         userValues.put(KEY_TYPE, user.getType());
  307.  
  308.         Integer userID = (int) db.insert(TABLE_USER, null, userValues);
  309.         Log.d("DB Add new user", user.toString());
  310.         Log.d("UserID=", String.valueOf(userID));
  311.  
  312.         return userID;
  313.     }
  314.  
  315.     //======================================================================================================================================
  316.  
  317.  
  318.     //                                                      Teacher
  319.     //======================================================================================================================================
  320.  
  321.     public Integer addTeacher(User user, Teacher teacher) {
  322.         SQLiteDatabase db = this.getWritableDatabase();
  323.  
  324.         Integer userID = addUser(user, db);
  325.  
  326.         ContentValues teacherValues = new ContentValues();
  327.         teacherValues.put(KEY_USER_ID, userID);
  328.         teacherValues.put(KEY_ROLE, teacher.getRole());
  329.  
  330.         Integer teacherId = (int) db.insert(TABLE_TEACHER, null, teacherValues);
  331.         db.close();
  332.         Log.d("Add new teacher", teacher.toString());
  333.         Log.d("teacherID", String.valueOf(teacherId));
  334.  
  335.         return teacherId;
  336.     }
  337.  
  338.     public Teacher getTeacher(int id) {
  339.         SQLiteDatabase db = this.getReadableDatabase();
  340.  
  341.         Cursor cursor = db.query(TABLE_TEACHER, new String[]{
  342.                         KEY_TEACHER_ID, KEY_USER_ID, KEY_ROLE}, KEY_TEACHER_ID + "=?",
  343.                 new String[]{String.valueOf(id)}, null, null, null, null);
  344.         if (cursor != null) {
  345.             cursor.moveToFirst();
  346.  
  347.             //(Integer teacherID, Integer userID, String role)
  348.             Teacher teacher = new Teacher(
  349.                     cursor.getInt(0),       // KEY_TEACHER_ID
  350.                     cursor.getInt(1),       // KEY_USER_ID
  351.                     cursor.getString(2));    // KET_ROLE
  352.             cursor.close();
  353.             db.close();
  354.             return teacher;
  355.         }
  356.         db.close();
  357.         return null;
  358.     }
  359.  
  360.     //======================================================================================================================================
  361.  
  362.  
  363.     //                                                      Teacher_Course
  364.     //======================================================================================================================================
  365.     public void addCourseForTeacher(Integer teacherID, String courseName) {
  366.         SQLiteDatabase db = this.getWritableDatabase();
  367.  
  368.         ContentValues values = new ContentValues();
  369.         values.put(KEY_TEACHER_ID, teacherID);
  370.         values.put(KEY_COURSE_NAME_ID, courseName);
  371.  
  372.         db.insertWithOnConflict(TABLE_TEACHER_COURSE, null, values, SQLiteDatabase.CONFLICT_IGNORE);
  373.         db.close();
  374.  
  375.         Log.d("Add course for teacher", courseName + " " + teacherID);
  376.     }
  377.  
  378.     public List<Course> getCoursesForTeacher(Teacher teacher) {
  379.         List<Course> coursesList = new ArrayList<>();
  380.         SQLiteDatabase db = this.getReadableDatabase();
  381.  
  382.         Cursor cursor = db.query(TABLE_TEACHER_COURSE, new String[]{
  383.                         KEY_TEACHER_ID, KEY_COURSE_NAME_ID}, KEY_TEACHER_ID + "=?",
  384.                 new String[]{String.valueOf(teacher.getTeacherID())}, null, null, null, null);
  385.  
  386.         if (cursor.moveToFirst()) {
  387.             do {
  388.                 Course course = getCourse(cursor.getString(1));
  389.                 coursesList.add(course);
  390.             } while (cursor.moveToNext());
  391.         }
  392.  
  393.         cursor.close();
  394.         db.close();
  395.  
  396.         return coursesList;
  397.     }
  398.     //======================================================================================================================================
  399.  
  400.  
  401.     //                                                      Course
  402.     //======================================================================================================================================
  403.     public Course getCourse(String nameID) {
  404.         SQLiteDatabase db = this.getReadableDatabase();
  405.  
  406.         Cursor cursor = db.query(TABLE_COURSE, new String[]{
  407.                         KEY_COURSE_NAME_ID, KEY_AMOUNT, KEY_CLASS_NUMBER}, KEY_COURSE_NAME_ID + "=?",
  408.                 new String[]{nameID}, null, null, null, null);
  409.  
  410.         Course course = null;
  411.         if (cursor != null && cursor.moveToFirst()) {
  412.             course = new Course(
  413.                     cursor.getString(0),
  414.                     cursor.getInt(1),
  415.                     cursor.getInt(2));
  416.  
  417.             cursor.close();
  418.         }
  419.         db.close();
  420.         return course;
  421.     }
  422.  
  423.     public void addCourse(Course course){
  424.         SQLiteDatabase db = this.getWritableDatabase();
  425.  
  426.         ContentValues values = new ContentValues();
  427.  
  428.         values.put(KEY_COURSE_NAME_ID, course.getCourseNameID());
  429.         values.put(KEY_AMOUNT, course.getAmountOfLessons());
  430.         values.put(KEY_CLASS_NUMBER, course.getGroupNumber());
  431.  
  432.         db.insertWithOnConflict(TABLE_COURSE, null, values, SQLiteDatabase.CONFLICT_IGNORE);
  433.         db.close();
  434.  
  435.         Log.d("Add new course", course.toString());
  436.     }
  437.     //======================================================================================================================================
  438.  
  439.  
  440.     //                                                      StudyPlan - Course_Group
  441.     //======================================================================================================================================
  442.  
  443.     public void addCourseForGroup(Integer groupID, String courseName) {
  444.         SQLiteDatabase db = this.getWritableDatabase();
  445.  
  446.         ContentValues values = new ContentValues();
  447.         values.put(KEY_GROUP_ID, groupID);
  448.         values.put(KEY_COURSE_NAME_ID, courseName);
  449.  
  450.         db.insertWithOnConflict(TABLE_STUDY_PLAN, null, values, SQLiteDatabase.CONFLICT_IGNORE);
  451.         db.close();
  452.  
  453.         Log.d("Add course for teacher", courseName + " " + groupID);
  454.     }
  455.     //======================================================================================================================================
  456.  
  457.     //                                                      Group
  458.     //======================================================================================================================================
  459.     public List<Group> getGroupsForTeacher(Integer teacherID){
  460.         List<Group> groups = new ArrayList<>();
  461.         String selectQuery = "SELECT " +
  462.                 TABLE_GROUP + "." + KEY_GROUP_NUMBER_ID +
  463.                 TABLE_GROUP + "." + KEY_GROUP_LABEL +
  464.                 " FROM " + TABLE_GROUP +
  465.                 " INNER JOIN " + TABLE_STUDY_PLAN +
  466.                 " ON " + TABLE_GROUP + "." + KEY_GROUP_ID + "=" +
  467.                 TABLE_STUDY_PLAN + "." + KEY_GROUP_ID +
  468.                 " INNER JOIN " + TABLE_COURSE +
  469.                 " ON " + TABLE_COURSE + "." + KEY_COURSE_NAME_ID + "=" +
  470.                 TABLE_STUDY_PLAN + "." + KEY_COURSE_NAME_ID +
  471.                 " INNER JOIN " + TABLE_TEACHER_COURSE +
  472.                 " ON " + TABLE_TEACHER_COURSE + "." + KEY_COURSE_NAME_ID + "=" +
  473.                 TABLE_STUDY_PLAN + "." + KEY_COURSE_NAME_ID +
  474.                 " INNER JOIN " + TABLE_TEACHER +
  475.                 " ON " + TABLE_TEACHER_COURSE + "." + KEY_TEACHER_ID + "=" +
  476.                 TABLE_TEACHER + "." + KEY_TEACHER_ID +
  477.                 " WHERE (" + TABLE_TEACHER + "." + KEY_TEACHER_ID +  " LIKE \'" + teacherID + "%\'" + ")";
  478.  
  479.         SQLiteDatabase db = this.getWritableDatabase();
  480.         Cursor cursor = db.rawQuery(selectQuery, null);
  481.  
  482.         if (cursor.moveToFirst()) {
  483.             do {
  484.                 Group group = new Group();
  485.              //   group.setGroupID(cursor.getInt(0));
  486.                 group.setGroupNumberID(cursor.getInt(0));
  487.                 group.setGroupLabel(cursor.getString(1));
  488.                 groups.add(group);
  489.             } while (cursor.moveToNext());
  490.         }
  491.         cursor.close();
  492.         db.close();
  493.         return groups;
  494.     }
  495.  
  496.     public Integer getGroupID (Integer groupNum, String groupLab){
  497.         SQLiteDatabase db = this.getReadableDatabase();
  498.         String selectQuery = "SELECT " + TABLE_GROUP +"." + KEY_GROUP_ID +
  499.                 " FROM " + TABLE_GROUP +
  500.                 " WHERE (" +
  501.                 TABLE_GROUP + "." + KEY_GROUP_NUMBER_ID + "=?" + " AND " +
  502.                 TABLE_GROUP + "." + KEY_GROUP_LABEL + "=?" + ")";
  503.         Cursor cursor = db.rawQuery(selectQuery, new String[]{Integer.toString(groupNum), groupLab});
  504.         Integer groupID = cursor.getInt(0);
  505.         cursor.close();
  506.         db.close();
  507.         return groupID;
  508.     }
  509.  
  510.     public Group getGroup(Integer groupID){
  511.         SQLiteDatabase db = this.getReadableDatabase();
  512.  
  513.         String selectQuery  = "SELECT * FROM " + TABLE_GROUP +
  514.                 " WHERE( " + KEY_GROUP_ID + "=?" + ")";
  515.  
  516.         Cursor cursor = db.rawQuery(selectQuery, new String[]{String.valueOf(groupID)});
  517.         if (cursor != null) {
  518.             cursor.moveToFirst();
  519.  
  520.             Group group = new Group(
  521.                     cursor.getInt(0),        // KEY_GROUP_ID
  522.                     cursor.getInt(1),        // KEY_GROUP_NAME_ID
  523.                     cursor.getString(2));    // KEY_GROUP_LABEL
  524.             cursor.close();
  525.             db.close();
  526.             return group;
  527.         }
  528.         db.close();
  529.         return null;
  530.     }
  531.  
  532.     public Integer addGroup(Group group, SQLiteDatabase db) {
  533.         ContentValues groupValues = new ContentValues();
  534.         //  groupValues.put(KEY_GROUP_ID, group.getGroupID());
  535.         groupValues.put(KEY_GROUP_LABEL, group.getGroupLabel());
  536.         groupValues.put(KEY_GROUP_NUMBER_ID, group.getGroupNumberID());
  537.  
  538.         Integer groupId = (int) db.insert(TABLE_GROUP, null, groupValues);
  539.         db.close();
  540.         Log.d("Add new group", group.toString());
  541.         return groupId;
  542.     }
  543.  
  544.     public Integer addGroup(Group group){
  545.         SQLiteDatabase db = this.getWritableDatabase();
  546.  
  547.         ContentValues groupValues = new ContentValues();
  548.         //  groupValues.put(KEY_GROUP_ID, group.getGroupID());
  549.         groupValues.put(KEY_GROUP_LABEL, group.getGroupLabel());
  550.         groupValues.put(KEY_GROUP_NUMBER_ID, group.getGroupNumberID());
  551.  
  552.         Integer groupId = (int) db.insert(TABLE_GROUP, null, groupValues);
  553.         db.close();
  554.         Log.d("Add new group", group.toString());
  555.         return groupId;
  556.     }
  557.  
  558.     //======================================================================================================================================
  559.  
  560.  
  561.     //                                                      Lesson
  562.     //======================================================================================================================================
  563.  
  564.     public Integer addLessonForCourse(Lesson lesson){
  565.         SQLiteDatabase db = this.getReadableDatabase();
  566.  
  567.         String courseName = lesson.getCourseID();
  568.         Integer groupID = lesson.getGroupID();
  569.  
  570.         ContentValues values = new ContentValues();
  571.         values.put(KEY_LESSON_DATE, lesson.getLessonDate());
  572.         values.put(KEY_LESSON_TIME, lesson.getLessonTime());
  573.         values.put(KEY_LESSON_TYPE, lesson.getLessonType());
  574.         values.put(KEY_ROOM, lesson.getRoom());
  575.         values.put(KEY_TOPIC, lesson.getTopic());
  576.         values.put(KEY_COURSE_NAME_ID, courseName);
  577.         values.put(KEY_GROUP_ID, groupID);
  578.  
  579.         Integer lessonID = (int) db.insert(TABLE_LESSON, null, values);
  580.         db.close();
  581.         Log.d("Add new lesson", lesson.toString());
  582.         return lessonID;
  583.     }
  584.  
  585.     public Lesson getLesson(int id) {
  586.         SQLiteDatabase db = this.getReadableDatabase();
  587.  
  588.         Cursor cursor = db.query(TABLE_LESSON, new String[] {
  589.                         KEY_LESSON_ID, KEY_COURSE_NAME_ID,  KEY_LESSON_DATE, KEY_LESSON_TIME, KEY_TOPIC, KEY_ROOM, KEY_LESSON_TYPE}, KEY_ATTENDANCE_ID + "=?",
  590.                 new String[] { String.valueOf(id) }, null, null, null, null);
  591.         if (cursor != null) {
  592.             cursor.moveToFirst();
  593.  
  594.  
  595.             //Integer lessonID, String courseID, String lessonDate, String lessonTime, String topic, String room, String lessonType
  596.             Lesson lesson = new Lesson(
  597.                     cursor.getInt(0),
  598.                     cursor.getString(1),
  599.                     cursor.getInt(2),
  600.                     cursor.getString(3),
  601.                     cursor.getString(4),
  602.                     cursor.getString(5),
  603.                     cursor.getString(6),
  604.                     cursor.getString(7));
  605.  
  606.             cursor.close();
  607.             db.close();
  608.             return lesson;
  609.         }
  610.         db.close();
  611.         return null;
  612.     }
  613.  
  614.     public void deleteLessonById(Integer lessonID){
  615.         SQLiteDatabase db = this.getWritableDatabase();
  616.  
  617.         String courseID = getLesson(lessonID).getCourseID();
  618.         Integer groupID = getLesson(lessonID).getGroupID();
  619.  
  620.         this.getWritableDatabase()
  621.                 .delete(TABLE_COURSE, KEY_COURSE_NAME_ID + " =?", new String[] {courseID});
  622.         this.getWritableDatabase()
  623.                 .delete(TABLE_LESSON, KEY_LESSON_ID + " =?", new String[]{Integer.toString(lessonID)});
  624.         this.getWritableDatabase()
  625.                 .delete(TABLE_GROUP, KEY_LESSON_ID + " =?", new String[]{Integer.toString(groupID)});
  626.         db.close();
  627.     }
  628.  
  629.     public void addLessonForCourseGroup(String courseName, Lesson lesson, Integer groupID) {
  630.         SQLiteDatabase db = this.getWritableDatabase();
  631.  
  632.         ContentValues values = new ContentValues();
  633.         //values.put(KEY_TEACHER_ID, teacherID);
  634.         values.put(KEY_COURSE_NAME_ID, courseName);
  635.         values.put(KEY_LESSON_TYPE, lesson.getLessonType());
  636.         values.put(KEY_LESSON_TIME, lesson.getLessonTime());
  637.         values.put(KEY_LESSON_DATE, lesson.getLessonDate());
  638.         values.put(KEY_ROOM, lesson.getRoom());
  639.         values.put(KEY_TOPIC, lesson.getTopic());
  640.         values.put(KEY_GROUP_ID, groupID);
  641.  
  642.         Integer lessonID = (int) db.insertWithOnConflict(TABLE_LESSON, null, values, SQLiteDatabase.CONFLICT_IGNORE);
  643.         db.close();
  644.  
  645.         Log.d("Add lesson for course", courseName + " " + lessonID);
  646.     }
  647.  
  648.     public Lesson getLessonForTeacherOnCourse(Integer teacherID, Integer lessonID, String courseNameID) {
  649.         //  List<Lesson> lessons = new ArrayList<>();
  650.         SQLiteDatabase db = this.getWritableDatabase();
  651.  
  652.         String selectQuery = "SELECT " +
  653.                 TABLE_LESSON + "." + KEY_TOPIC +
  654.                 " FROM " + TABLE_LESSON +
  655.                 " INNER JOIN " + TABLE_COURSE +
  656.                 " ON " + TABLE_LESSON + "." + KEY_COURSE_NAME_ID + "=" +
  657.                 TABLE_COURSE+ "." + KEY_COURSE_NAME_ID +
  658.                 " INNER JOIN " + TABLE_TEACHER_COURSE +
  659.                 " ON " + TABLE_TEACHER_COURSE + "." + KEY_COURSE_NAME_ID + "=" +
  660.                 TABLE_COURSE + "." + KEY_COURSE_NAME_ID +
  661.                 " INNER JOIN " + TABLE_TEACHER +
  662.                 " ON " + TABLE_TEACHER + "." + KEY_TEACHER_ID + "=" +
  663.                 TABLE_TEACHER + "." + KEY_TEACHER_ID +
  664.                 " WHERE (" +
  665.                 KEY_COURSE_NAME_ID +  " LIKE \'" + courseNameID + "%\'" +
  666.                 KEY_LESSON_ID +  " LIKE \'" + lessonID + "%\'" +
  667.                 KEY_TEACHER_ID + " LIKE \'" + teacherID + "%\')";
  668.  
  669.  
  670.         Cursor cursor = db.rawQuery(selectQuery, null);
  671.         if (cursor != null) {
  672.             cursor.moveToFirst();
  673.  
  674.             Lesson lesson = new Lesson();
  675.             lesson.setTopic(cursor.getString(0));
  676.             cursor.close();
  677.             db.close();
  678.             return lesson;
  679.         }
  680.         db.close();
  681.         return null;
  682.     }
  683.  
  684.     public List<Lesson> getLessonsForTeacher(Integer teacherID) {
  685.  
  686.         List<Lesson> lessons = new ArrayList<>();
  687.         SQLiteDatabase db = this.getWritableDatabase();
  688.  
  689.         String selectQuery = "SELECT " +
  690.                 TABLE_LESSON + "." + KEY_TOPIC +
  691.                 " FROM " + TABLE_LESSON +
  692.                 " INNER JOIN " + TABLE_COURSE +
  693.                 " ON " + TABLE_LESSON + "." + KEY_COURSE_NAME_ID + "=" +
  694.                 TABLE_COURSE + "." + KEY_COURSE_NAME_ID +
  695.                 " INNER JOIN " + TABLE_TEACHER_COURSE +
  696.                 " ON " + TABLE_TEACHER_COURSE + "." + KEY_COURSE_NAME_ID + "=" +
  697.                 TABLE_COURSE + "." + KEY_COURSE_NAME_ID +
  698.                 " INNER JOIN " + TABLE_TEACHER +
  699.                 " ON " + TABLE_TEACHER + "." + KEY_TEACHER_ID + "=" +
  700.                 TABLE_TEACHER + "." + KEY_TEACHER_ID +
  701.                 " WHERE (" +
  702.                 TABLE_TEACHER + "." + KEY_TEACHER_ID + " LIKE \'" + teacherID + "%\')";
  703.  
  704.  
  705.         Cursor cursor = db.rawQuery(selectQuery, null);
  706.         if (cursor != null) {
  707.             do {
  708.                 Lesson lesson = new Lesson(
  709.                         cursor.getInt(0),
  710.                         cursor.getString(1),
  711.                         cursor.getInt(2),
  712.                         cursor.getString(3),
  713.                         cursor.getString(4),
  714.                         cursor.getString(5),
  715.                         cursor.getString(6),
  716.                         cursor.getString(7));
  717.                 lessons.add(lesson);
  718.             } while (cursor.moveToNext());
  719.         }
  720.         cursor.close();
  721.         db.close();
  722.         return lessons;
  723.     }
  724.  
  725.     public Integer getLessonID (String lessonTopic){
  726.         SQLiteDatabase db = this.getReadableDatabase();
  727.         String selectQuery = "SELECT " + TABLE_LESSON +"." + KEY_LESSON_ID +
  728.                 " FROM " + TABLE_LESSON +
  729.                 " WHERE (" +
  730.                 TABLE_LESSON +"." + KEY_LESSON_ID+ "=?" + ")";
  731.         Cursor cursor = db.rawQuery(selectQuery, new String[]{lessonTopic});
  732.         Integer currentUserID = cursor.getInt(1);
  733.         cursor.close();
  734.         db.close();
  735.         return currentUserID;
  736.     }
  737.  
  738.     //======================================================================================================================================
  739.  
  740.  
  741.     //                                                      Mark
  742.     //======================================================================================================================================
  743.  
  744.     public List<Mark> getMarksForStudentOnSubject(Integer studentID, String courseName){
  745.         List<Mark> marks = new ArrayList<>();
  746.         String selectQuery = "SELECT " +
  747.                 TABLE_LESSON + "." + KEY_TOPIC + ", " +
  748.                 TABLE_MARK + "." + KEY_MARK_VALUE +
  749.                 " FROM "+ TABLE_MARK +
  750.                 " INNER JOIN " + TABLE_LESSON + " ON " +
  751.                 TABLE_LESSON + "." + KEY_LESSON_ID + "=" +
  752.                 TABLE_MARK + "." + KEY_LESSON_ID +
  753.                 " WHERE (" + KEY_STUDENT_ID +  " LIKE \'" + studentID + "%\'"+ " AND " +
  754.                 TABLE_LESSON + "." + KEY_COURSE_NAME_ID  +  " LIKE \'" + courseName + "%\'" + ")";
  755.  
  756.         SQLiteDatabase db = this.getWritableDatabase();
  757.         Cursor cursor = db.rawQuery(selectQuery, null);
  758.  
  759.         if (cursor.moveToFirst()) {
  760.             do {
  761.                 Mark mark = new Mark();
  762.                 mark.setMarkID(cursor.getInt(0));
  763.                 mark.setMarkValue(cursor.getString(1));
  764.                 mark.setLessonID(cursor.getInt(2));
  765.                 //     user.setEmail(cursor.getString(2));
  766.                 marks.add(mark);
  767.             } while (cursor.moveToNext());
  768.         }
  769.         cursor.close();
  770.         db.close();
  771.         return marks;
  772.     }
  773.  
  774.     public List<Mark> getAllMarks(Integer studentID){
  775.         List<Mark> marks = new ArrayList<>();
  776.         String selectQuery = "SELECT " +
  777.                 TABLE_LESSON + "." + KEY_TOPIC + ", " +
  778.                 TABLE_MARK + "." + KEY_MARK_VALUE +
  779.                 " FROM "+ TABLE_MARK +
  780.                 " INNER JOIN " + TABLE_LESSON + " ON " +
  781.                 TABLE_LESSON + "." + KEY_LESSON_ID + "=" +
  782.                 TABLE_MARK + "." + KEY_LESSON_ID +
  783.                 " WHERE (" + KEY_STUDENT_ID +  " LIKE \'" + studentID + "%\')";
  784.  
  785.         SQLiteDatabase db = this.getWritableDatabase();
  786.         Cursor cursor = db.rawQuery(selectQuery, null);
  787.  
  788.         if (cursor.moveToFirst()) {
  789.             do {
  790.                 Mark mark = new Mark();
  791.                 mark.setMarkID(cursor.getInt(0));
  792.                 mark.setMarkValue(cursor.getString(1));
  793.                 mark.setLessonID(cursor.getInt(2));
  794.                 //     user.setEmail(cursor.getString(2));
  795.                 marks.add(mark);
  796.             } while (cursor.moveToNext());
  797.         }
  798.         cursor.close();
  799.         db.close();
  800.         return marks;
  801.     }
  802.  
  803.     public void addMark(Mark mark) {
  804.         SQLiteDatabase db = this.getWritableDatabase();
  805.  
  806.         ContentValues values = new ContentValues();
  807.         values.put(KEY_MARK_VALUE, mark.getMarkValue());
  808.         values.put(KEY_MARK_ID, mark.getMarkID());
  809.         values.put(KEY_STUDENT_ID, mark.getStudentID());
  810.         values.put(KEY_LESSON_ID, mark.getLessonID());
  811.         values.put(KEY_COMMENT, mark.getComment());
  812.  
  813.         db.insertWithOnConflict(TABLE_MARK, null, values, SQLiteDatabase.CONFLICT_IGNORE);
  814.         db.close();
  815.  
  816.         Log.d("Add side mark", mark.toString());
  817.     }
  818.  
  819.     public Mark getMark(int id) {
  820.         SQLiteDatabase db = this.getReadableDatabase();
  821.  
  822.         Cursor cursor = db.query(TABLE_MARK, new String[] {
  823.                         KEY_MARK_ID, KEY_LESSON_ID, KEY_STUDENT_ID, KEY_MARK_VALUE, KEY_COMMENT}, KEY_MARK_ID + "=?",
  824.                 new String[] { String.valueOf(id) }, null, null, null, null);
  825.         if (cursor != null) {
  826.             cursor.moveToFirst();
  827.         }
  828.  
  829.         //(Integer markID, Integer lessonID, Integer studentID, String markValue, String comment)
  830.         Mark mark = new Mark(
  831.                 cursor.getInt(0),       // KEY_MARK_ID
  832.                 cursor.getInt(1),       // KEY_LESSON_ID
  833.                 cursor.getInt(2),       // KEY_STUDENT_ID
  834.                 cursor.getString(3),       // KEY_MARK_VALUE
  835.                 cursor.getString(4));   // KEY_COMMENT
  836.  
  837.         cursor.close();
  838.         db.close();
  839.         return mark;
  840.     }
  841.  
  842.     public void deleteMarkById(Integer markID){
  843.         SQLiteDatabase db = this.getWritableDatabase();
  844.  
  845.       //  int studentID = getMark(markID).getStudentID();
  846.         //int lessonID = getMark(markID).getLessonID();
  847.  
  848.         this.getWritableDatabase()
  849.                 .delete(TABLE_MARK, KEY_MARK_ID + " =?", new String[]{Integer.toString(markID)});
  850.         /*this.getWritableDatabase()
  851.                 .delete(TABLE_STUDENT, KEY_STUDENT_ID + " =?", new String[] {Integer.toString(studentID)});
  852.         this.getWritableDatabase()
  853.                 .delete(TABLE_LESSON, KEY_LESSON_ID + " =?", new String[]{Integer.toString(lessonID)});*/
  854.         db.close();
  855.     }
  856.  
  857.     //======================================================================================================================================
  858.  
  859.  
  860.     //                                                      Hometask
  861.     //======================================================================================================================================
  862.     public Hometask getHometask(int id) {
  863.         SQLiteDatabase db = this.getReadableDatabase();
  864.  
  865.         Cursor cursor = db.query(TABLE_HOMETASK, new String[] {
  866.                         KEY_HOMETASK_ID, KEY_LESSON_ID, KEY_STUDENT_ID, KEY_HOMETASK_STATUS, KEY_DEADLINE}, KEY_HOMETASK_ID + "=?",
  867.                 new String[] { String.valueOf(id) }, null, null, null, null);
  868.         if (cursor != null) {
  869.             cursor.moveToFirst();
  870.  
  871.  
  872.             //((Integer hometaskID, Integer lessonID, Integer studentID, String status, String deadline)
  873.             Hometask hometask = new Hometask(
  874.                     cursor.getInt(0),       // KEY_HOMETASK_ID
  875.                     cursor.getInt(1),       // KEY_LESSON_ID
  876.                     cursor.getInt(2),       // KEY_STUDENT_ID
  877.                     cursor.getString(3),       // KEY_HOMETASK_STATUS
  878.                     cursor.getString(4));   // KEY_DEADLINE
  879.  
  880.             cursor.close();
  881.             db.close();
  882.             return hometask;
  883.         }
  884.         db.close();
  885.         return null;
  886.     }
  887.  
  888.     //отсортированное ДЗ
  889.     public List<Hometask> getAllHometasks(Integer studentID){
  890.         List<Hometask> hometasks = new ArrayList<>();
  891.         String selectQuery = "SELECT " +
  892.                 TABLE_LESSON + "." + KEY_TOPIC + ", " +
  893.                 TABLE_HOMETASK + "." + KEY_HOMETASK_STATUS +
  894.                 TABLE_HOMETASK + "." + KEY_DEADLINE +
  895.                 " FROM "+ TABLE_HOMETASK +
  896.                 " INNER JOIN " + TABLE_LESSON + " ON " +
  897.                 TABLE_LESSON + "." + KEY_LESSON_ID + "=" +
  898.                 TABLE_MARK + "." + KEY_LESSON_ID +
  899.                 " WHERE " + KEY_STUDENT_ID +  " LIKE \'" + studentID + "%\'" +
  900.                 " ORDER BY " + KEY_HOMETASK_STATUS + " DESC, " + KEY_DEADLINE + " DESC" ;
  901.  
  902.         SQLiteDatabase db = this.getWritableDatabase();
  903.         Cursor cursor = db.rawQuery(selectQuery, null);
  904.  
  905.         if (cursor.moveToFirst()) {
  906.             do {
  907.                 Hometask hometask = new Hometask();
  908.                 hometask.setStatus(cursor.getString(0));
  909.                 hometask.setDeadline(cursor.getString(1));
  910.                 //     user.setEmail(cursor.getString(2));
  911.                 hometasks.add(hometask);
  912.             } while (cursor.moveToNext());
  913.         }
  914.         cursor.close();
  915.         db.close();
  916.         return hometasks;
  917.     }
  918.  
  919.     public void deleteHometaskById(Integer hometaskID){
  920.         SQLiteDatabase db = this.getWritableDatabase();
  921.  
  922.      //   int studentID = getHometask(hometaskID).getStudentID();
  923.        // int lessonID = getHometask(hometaskID).getLessonID();
  924.  
  925.         this.getWritableDatabase().delete(TABLE_HOMETASK, KEY_HOMETASK_ID +
  926.                 " =?", new String[]{Integer.toString(hometaskID)});
  927.    /*     this.getWritableDatabase()
  928.                 .delete(TABLE_STUDENT, KEY_STUDENT_ID + " =?", new String[] {Integer.toString(studentID)});
  929.         this.getWritableDatabase()
  930.                 .delete(TABLE_LESSON, KEY_LESSON_ID + " =?", new String[]{Integer.toString(lessonID)});*/
  931.         db.close();
  932.     }
  933.  
  934.     //======================================================================================================================================
  935.  
  936.  
  937.  
  938.     //                                                      Attendance
  939.     //======================================================================================================================================
  940.  
  941.     public Attendance getAttendance(int id) {
  942.         SQLiteDatabase db = this.getReadableDatabase();
  943.  
  944.         Cursor cursor = db.query(TABLE_ATTENDANCE, new String[] {
  945.                         KEY_ATTENDANCE_ID, KEY_STUDENT_ID, KEY_LESSON_ID, KEY_ATTENDANCE_STATUS, KEY_ATTENDANCE_STATUS, KEY_REASON}, KEY_ATTENDANCE_ID + "=?",
  946.                 new String[] { String.valueOf(id) }, null, null, null, null);
  947.         if (cursor != null) {
  948.             cursor.moveToFirst();
  949.  
  950.  
  951.             //(Integer attendanceID, Integer lessonID, Integer studentID, Integer status, String reason)
  952.             Attendance attendance = new Attendance(
  953.                     cursor.getInt(0),       // KEY_ATTENDANCE_ID
  954.                     cursor.getInt(1),       // KEY_LESSON_ID
  955.                     cursor.getInt(2),       // KEY_STUDENT_ID
  956.                     cursor.getInt(3),       // KEY_ATTENDANCE_STATUS
  957.                     cursor.getString(4));   // KEY_REASON
  958.  
  959.             cursor.close();
  960.             db.close();
  961.             return attendance;
  962.         }
  963.         db.close();
  964.         return null;
  965.     }
  966.  
  967.     public List<Attendance> getAllAttendance(Integer studentID){
  968.         List<Attendance> attendances = new ArrayList<>();
  969.         String selectQuery = "SELECT " +
  970.                 TABLE_LESSON + "." + KEY_TOPIC + ", " +
  971.                 TABLE_ATTENDANCE + "." + KEY_ATTENDANCE_STATUS +
  972.                 " FROM "+ TABLE_ATTENDANCE +
  973.                 " INNER JOIN " + TABLE_LESSON + " ON " +
  974.                 TABLE_LESSON + "." + KEY_LESSON_ID + "=" +
  975.                 TABLE_ATTENDANCE + "." + KEY_ATTENDANCE_STATUS +
  976.                 " WHERE (" + KEY_STUDENT_ID +  " LIKE \'" + studentID + "%\')";
  977.  
  978.         SQLiteDatabase db = this.getWritableDatabase();
  979.         Cursor cursor = db.rawQuery(selectQuery, null);
  980.  
  981.         if (cursor.moveToFirst()) {
  982.             do {
  983.                 Attendance attendance = new Attendance();
  984.                 attendance.setAttendanceID(cursor.getInt(0));
  985.                 attendance.setStatus(cursor.getInt(1));
  986.                 attendance.setLessonID(cursor.getInt(2));
  987.                 //     user.setEmail(cursor.getString(2));
  988.                 attendances.add(attendance);
  989.             } while (cursor.moveToNext());
  990.         }
  991.         cursor.close();
  992.         db.close();
  993.         return attendances;
  994.     }
  995.  
  996.     public void deleteAttendenceById(Integer attendenceID){
  997.         SQLiteDatabase db = this.getWritableDatabase();
  998.  
  999.      //   int studentID = getAttendance(attendenceID).getStudentID();
  1000.        // int lessonID = getAttendance(attendenceID).getLessonID();
  1001.  
  1002.         this.getWritableDatabase().delete(TABLE_ATTENDANCE, KEY_ATTENDANCE_ID +
  1003.                 " =?", new String[]{Integer.toString(attendenceID)});
  1004.    /*     this.getWritableDatabase()
  1005.                 .delete(TABLE_STUDENT, KEY_STUDENT_ID + " =?", new String[] {Integer.toString(studentID)});
  1006.         this.getWritableDatabase()
  1007.                 .delete(TABLE_LESSON, KEY_LESSON_ID + " =?", new String[]{Integer.toString(lessonID)});*/
  1008.         db.close();
  1009.     }
  1010.     //======================================================================================================================================
  1011.  
  1012.  
  1013.     //                                                      Student
  1014.     //======================================================================================================================================
  1015.     public Student getStudent(int id) {
  1016.         SQLiteDatabase db = this.getReadableDatabase();
  1017.  
  1018.         Cursor cursor = db.query(TABLE_STUDENT, new String[]{
  1019.                         KEY_STUDENT_ID, KEY_USER_ID, KEY_GROUP_NUMBER_ID, KEY_GROUP_LABEL, KEY_ADRESS}, KEY_STUDENT_ID + "=?",
  1020.                 new String[]{String.valueOf(id)}, null, null, null, null);
  1021.         if (cursor != null) {
  1022.             cursor.moveToFirst();
  1023.  
  1024.             Student student = new Student(
  1025.                     cursor.getInt(0),       // KEY_STUDENT_ID
  1026.                     cursor.getInt(1),       // KEY_USER_ID
  1027.                     cursor.getString(2),   // KEY_ADRESS
  1028.                     cursor.getInt(3));      // KEY_GROUP_ID
  1029.             cursor.close();
  1030.             db.close();
  1031.             return student;
  1032.         }
  1033.         db.close();
  1034.         return null;
  1035.     }
  1036.  
  1037.     public List<Student> getStudentsForTeacher(Integer teacherID){
  1038.         List<Student> students = new ArrayList<>();
  1039.         String selectQuery = "SELECT " +
  1040.                 TABLE_USER + "." + KEY_FULL_NAME + ", " +
  1041.                 TABLE_GROUP + "." + KEY_GROUP_NUMBER_ID +
  1042.                 TABLE_GROUP + "." + KEY_GROUP_LABEL +
  1043.                 " FROM "+ TABLE_USER +
  1044.                 " INNER JOIN " + TABLE_STUDENT +
  1045.                 " ON " + TABLE_USER + "." + KEY_USER_ID + "=" +
  1046.                 TABLE_STUDENT + "." + KEY_USER_ID +
  1047.                 " INNER JOIN " + TABLE_GROUP +
  1048.                 " ON " + TABLE_GROUP + "." + KEY_GROUP_ID + "=" +
  1049.                 TABLE_STUDENT + "." + KEY_GROUP_ID +
  1050.                 " INNER JOIN " + TABLE_STUDY_PLAN +
  1051.                 " ON " + TABLE_GROUP + "." + KEY_GROUP_ID + "=" +
  1052.                 TABLE_STUDY_PLAN + "." + KEY_GROUP_ID +
  1053.                 " INNER JOIN " + TABLE_COURSE +
  1054.                 " ON " + TABLE_COURSE + "." + KEY_COURSE_NAME_ID + "=" +
  1055.                 TABLE_STUDY_PLAN + "." + KEY_COURSE_NAME_ID +
  1056.                 " INNER JOIN " + TABLE_TEACHER_COURSE +
  1057.                 " ON " + TABLE_TEACHER_COURSE + "." + KEY_COURSE_NAME_ID + "=" +
  1058.                 TABLE_STUDY_PLAN + "." + KEY_COURSE_NAME_ID +
  1059.                 " INNER JOIN " + TABLE_TEACHER +
  1060.                 " ON " + TABLE_TEACHER_COURSE + "." + KEY_TEACHER_ID + "=" +
  1061.                 TABLE_TEACHER + "." + KEY_TEACHER_ID +
  1062.                 " WHERE (" + TABLE_TEACHER + "." + KEY_TEACHER_ID +  " LIKE \'" + teacherID + "%\'" + ")";
  1063.  
  1064.         SQLiteDatabase db = this.getWritableDatabase();
  1065.         Cursor cursor = db.rawQuery(selectQuery, null);
  1066.  
  1067.         if (cursor.moveToFirst()) {
  1068.             do {
  1069.                 Student student = new Student();
  1070.                 student.setStudentID(cursor.getInt(0));
  1071.                 student.setGroupID(cursor.getInt(1));
  1072.                 student.setUserID(cursor.getInt(2));
  1073.                 student.setAdress(cursor.getString(3));
  1074.                 //     user.setEmail(cursor.getString(2));
  1075.                 students.add(student);
  1076.             } while (cursor.moveToNext());
  1077.         }
  1078.         cursor.close();
  1079.         db.close();
  1080.         return students;
  1081.     }
  1082.  
  1083.     public Integer addStudent(User user, Student student) {
  1084.         SQLiteDatabase db = this.getWritableDatabase();
  1085.  
  1086.         Integer userID = addUser(user, db);
  1087.  
  1088.         ContentValues studentValues = new ContentValues();
  1089.         studentValues.put(KEY_USER_ID, userID);
  1090.         studentValues.put(KEY_ADRESS, student.getAdress());
  1091.  
  1092.         Integer studentID = (int) db.insert(TABLE_STUDENT, null, studentValues);
  1093.         db.close();
  1094.         Log.d("Add new student", student.toString());
  1095.         Log.d("studentID", String.valueOf(studentID));
  1096.  
  1097.         return studentID;
  1098.     }
  1099.  
  1100.     public Integer addStudentForGroup(User user, Integer groupID, Student student) {
  1101.         SQLiteDatabase db = this.getWritableDatabase();
  1102.  
  1103.         Integer userID = addUser(user, db);
  1104.  
  1105.         ContentValues studentValues = new ContentValues();
  1106.         studentValues.put(KEY_USER_ID, userID);
  1107.         studentValues.put(KEY_GROUP_ID, groupID);
  1108.         studentValues.put(KEY_ADRESS, student.getAdress());
  1109.  
  1110.         Integer studentID = (int) db.insert(TABLE_STUDENT, null, studentValues);
  1111.         db.close();
  1112.         Log.d("Add new student", student.toString());
  1113.         Log.d("studentID", String.valueOf(studentID));
  1114.  
  1115.         return studentID;
  1116.     }
  1117.  
  1118.     public Integer getStudentID (String fullName){
  1119.         SQLiteDatabase db = this.getReadableDatabase();
  1120.         String selectQuery = "SELECT " + TABLE_STUDENT +"." + KEY_STUDENT_ID +
  1121.                 " FROM " + TABLE_STUDENT +
  1122.                 " INNER JOIN " + TABLE_USER +
  1123.                 " ON " + TABLE_STUDENT + "." + KEY_USER_ID +
  1124.                 " = " + TABLE_USER + "." + KEY_USER_ID +
  1125.                 " WHERE (" +
  1126.                 TABLE_USER + "." + KEY_FULL_NAME + "=?" + ")";
  1127.         Cursor cursor = db.rawQuery(selectQuery, new String[]{fullName});
  1128.         Integer currentUserID = cursor.getInt(1);
  1129.         cursor.close();
  1130.         db.close();
  1131.         return currentUserID;
  1132.     }
  1133.     //======================================================================================================================================
  1134.  
  1135.  
  1136.  
  1137.     //======================================================================================================================================
  1138.     public Boolean checkEmailAndPasswordTeacher(String email, String password) {
  1139.         SQLiteDatabase db = this.getReadableDatabase();
  1140.         String selectQuery = "SELECT * FROM " + TABLE_TEACHER +
  1141.                 " INNER JOIN " + TABLE_USER +
  1142.                 " ON " + TABLE_TEACHER + "." + KEY_USER_ID +
  1143.                 " = " + TABLE_USER + "." + KEY_USER_ID +
  1144.                 " WHERE (" +
  1145.                 TABLE_USER + "." + KEY_EMAIL + "=?" +  " and " +
  1146.                 TABLE_USER + "." + KEY_PASSWORD + "=?" + ")";
  1147.  
  1148.         String selectQuery1 = "SELECT * FROM " + TABLE_USER +
  1149.                 " WHERE (" +
  1150.                 TABLE_USER + "." + KEY_EMAIL + "=?" +  " and " +
  1151.                 TABLE_USER + "." + KEY_PASSWORD + "=?" + ")";
  1152.         Cursor cursor = db.rawQuery(selectQuery, new String[]{email, password});
  1153.         //Log.d("User-teacher", "1");
  1154.         int count = cursor.getCount();
  1155.         cursor.close();
  1156.         return count > 0;
  1157.     }
  1158.     public Boolean checkEmailAndPasswordStudent(String email, String password) {
  1159.         SQLiteDatabase db = this.getReadableDatabase();
  1160.         String selectQuery = "SELECT * FROM " + TABLE_STUDENT +
  1161.                 " INNER JOIN " + TABLE_USER +
  1162.                 " ON " + TABLE_STUDENT + "." + KEY_USER_ID +
  1163.                 " = " + TABLE_USER + "." + KEY_USER_ID +
  1164.                 " WHERE (" +
  1165.                 TABLE_USER + "." + KEY_EMAIL + "=?" +  " and " +
  1166.                 TABLE_USER + "." + KEY_PASSWORD + "=?" + ")";
  1167.         Cursor cursor = db.rawQuery(selectQuery, new String[]{email, password});
  1168.         int count = cursor.getCount();
  1169.         cursor.close();
  1170.         db.close();
  1171.         return count > 0;
  1172.     }
  1173.  
  1174.     public Integer CurrentUserID(String email, String password){
  1175.         SQLiteDatabase db = this.getReadableDatabase();
  1176.         String selectQuery = "SELECT " + TABLE_USER + "." + KEY_USER_ID +
  1177.                 " FROM " + TABLE_USER +
  1178.                 " WHERE (" +
  1179.                 TABLE_USER + "." + KEY_EMAIL + "=?" +  " and " +
  1180.                 TABLE_USER + "." + KEY_PASSWORD + "=?" + ")";
  1181.         Cursor cursor = db.rawQuery(selectQuery, new String[]{email, password});
  1182.         Integer currentUserID = cursor.getInt(0);
  1183.         cursor.close();
  1184.         db.close();
  1185.         return currentUserID;
  1186.     }
  1187.  
  1188.     public Integer CurrentTeacherID(String email, String password){
  1189.         SQLiteDatabase db = this.getReadableDatabase();
  1190.         String selectQuery = "SELECT " + TABLE_TEACHER + "." + KEY_TEACHER_ID +
  1191.                 " FROM " + TABLE_TEACHER +
  1192.                 " INNER JOIN " + TABLE_USER +
  1193.                 " ON " + TABLE_TEACHER + "." + KEY_USER_ID +
  1194.                 " = " + TABLE_USER + "." + KEY_USER_ID +
  1195.                 " WHERE (" +
  1196.                 TABLE_USER + "." + KEY_EMAIL + "=?" +  " and " +
  1197.                 TABLE_USER + "." + KEY_PASSWORD + "=?" + ")";
  1198.         Cursor cursor = db.rawQuery(selectQuery, new String[]{email, password});
  1199.         Integer currentUserID = cursor.getInt(0);
  1200.         cursor.close();
  1201.         db.close();
  1202.         return currentUserID;
  1203.     }
  1204.  
  1205.     public Integer CurrentStudentID(String email, String password){
  1206.         SQLiteDatabase db = this.getReadableDatabase();
  1207.         String selectQuery = "SELECT " + TABLE_STUDENT +"." + KEY_STUDENT_ID +
  1208.                 " FROM " + TABLE_STUDENT +
  1209.                 " INNER JOIN " + TABLE_USER +
  1210.                 " ON " + TABLE_STUDENT + "." + KEY_USER_ID +
  1211.                 " = " + TABLE_USER + "." + KEY_USER_ID +
  1212.                 " WHERE (" +
  1213.                 TABLE_USER + "." + KEY_EMAIL + "=?" +  " and " +
  1214.                 TABLE_USER + "." + KEY_PASSWORD + "=?" + ")";
  1215.         Cursor cursor = db.rawQuery(selectQuery, new String[]{email, password});
  1216.         Integer currentUserID = cursor.getInt(0);
  1217.         cursor.close();
  1218.         db.close();
  1219.         return currentUserID;
  1220.     }
  1221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement