package be.ecam.ecalendar;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import be.ecam.ecalendar.CalendarContract.*;


/**
 * Created by Sylvain on 21-03-17.
 */

public class CalendarDBHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "calendar.db";
    private static final int DATABASE_VERSION = 1;

    public CalendarDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        final String SQL_CREATE_SCHEDULE_TABLE =
                "CREATE TABLE " + ScheduleEntry.TABLE_NAME + " (" +
                        ScheduleEntry.SCHEDULE_ACTIVITY_ID + " TEXT PRIMARY KEY," +
                        ScheduleEntry.SCHEDULE_ACTIVITY_NAME + " TEXT NOT NULL," +
                        ScheduleEntry.SCHEDULE_START_TIME + " INTEGER NOT NULL," +
                        ScheduleEntry.SCHEDULE_END_TIME + " INTEGER NOT NULL," +
                        ScheduleEntry.SCHEDULE_GROUP + " TEXT NOT NULL" +
                        ScheduleEntry.SCHEDULE_TEACHER + " TEXT NOT NULL" +
                        ScheduleEntry.SCHEDULE_CLASS_ROOM + " TEXT NOT NULL" +
                        "); ";
        db.execSQL(SQL_CREATE_SCHEDULE_TABLE);

        final String SQL_CREATE_CALENDAR_TYPE_TABLE =
                "CREATE TABLE " + CalendarTypeEntry.TABLE_NAME + " (" +
                        CalendarTypeEntry.CALENDAR_TYPE_ID + " TEXT PRIMARY KEY," +
                        CalendarTypeEntry.CALENDAR_TYPE_DESCRIPTION + " TEXT NOT NULL," +
                        CalendarTypeEntry.CALENDAR_TYPE_TYPE + " TEXT NOT NULL," +
                        "); ";
        db.execSQL(SQL_CREATE_CALENDAR_TYPE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS " + ScheduleEntry.TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + CalendarTypeEntry.TABLE_NAME);

        onCreate(db);
    }
}

@@ -149,16 +149,17 @@
 Helper;%0A
+%0A
 import b
@@ -192,17 +192,90 @@
 ontract.
-*
+CalendarTypeEntry;%0Aimport be.ecam.ecalendar.CalendarContract.ScheduleEntry
 ;%0A%0A%0A/**%0A
@@ -845,36 +845,113 @@
 ID + %22 TEXT 
-PRIMARY KEY,
+NOT NULL, %22 +%0A                        ScheduleEntry.SCHEDULE_CALENDAR + %22 TEXT NOT NULL, 
 %22 +%0A        
@@ -1013,32 +1013,33 @@
 %22 TEXT NOT NULL,
+ 
 %22 +%0A            
@@ -1097,32 +1097,33 @@
 NTEGER NOT NULL,
+ 
 %22 +%0A            
@@ -1179,32 +1179,33 @@
 NTEGER NOT NULL,
+ 
 %22 +%0A            
@@ -1254,32 +1254,34 @@
  %22 TEXT NOT NULL
+, 
 %22 +%0A            
@@ -1332,32 +1332,34 @@
  %22 TEXT NOT NULL
+, 
 %22 +%0A            
@@ -1421,16 +1421,18 @@
 NOT NULL
+, 
 %22 +%0A    
@@ -1444,32 +1444,285 @@
                %22
+PRIMARY KEY (%22 + ScheduleEntry.SCHEDULE_ACTIVITY_ID + %22, %22 +%0A                        ScheduleEntry.SCHEDULE_START_TIME + %22, %22 +%0A                        ScheduleEntry.SCHEDULE_CALENDAR + %22, %22 +%0A                        ScheduleEntry.SCHEDULE_END_TIME + %22)
 ); %22;%0A        db
@@ -1957,20 +1957,18 @@
 EXT 
-PRIMARY KEY,
+NOT NULL, 
 %22 +%0A
@@ -2045,32 +2045,33 @@
 %22 TEXT NOT NULL,
+ 
 %22 +%0A            
@@ -2137,16 +2137,17 @@
 OT NULL,
+ 
 %22 +%0A    
@@ -2167,16 +2167,143 @@
        %22
+PRIMARY KEY (%22 + CalendarTypeEntry.CALENDAR_TYPE_ID + %22, %22 +%0A                        CalendarTypeEntry.CALENDAR_TYPE_TYPE  + %22)
 ); %22;%0A  

set multi column primary key to avoid collisions in DB