· 7 years ago · Jan 18, 2019, 11:40 PM
1public class CourseEbookDBHandler extends DbAdapter {
2
3private final String COURSE_EBOOKS_TABLE = "CourseEbooksTable";
4private final String ID = "id";
5private final String STUDENT_USERNAME = "username";
6private final String IS_ALLOWED = "isallowed";
7
8private final String TABLE_CREATE_COURSE_EBOOK = "CREATE TABLE IF NOT EXISTS "
9 + COURSE_EBOOKS_TABLE
10 + " ( "
11 + ID
12 + " TEXT PRIMARY KEY , "
13 + STUDENT_USERNAME + " TEXT, " + IS_ALLOWED + " INTEGER " + ");";
14
15private final String FIND_COURSE_EBOOK = "SELECT * FROM "
16 + COURSE_EBOOKS_TABLE;
17
18private static CourseEbookDBHandler instance;
19
20public synchronized static CourseEbookDBHandler getInstance(
21 Context appContext) {
22 if (instance == null) {
23 instance = new CourseEbookDBHandler(appContext, DATABASE_NAME,
24 DATABASE_VERSION);
25 }
26 return instance;
27}
28
29public CourseEbookDBHandler(Context context, String DatabaseName,
30 int version) {
31 super(context, DatabaseName, version);
32
33 createTable();
34}
35
36public synchronized void createTable() {
37 if (isDatabaseOpen()) {
38 try {
39 appDatabase.execSQL(TABLE_CREATE_COURSE_EBOOK);
40 } catch (SQLException e) {
41 Logger.e(this, "database table not created exception " + e);
42 }
43 }
44}
45
46public synchronized void insertValuesCourseEbook(
47 CourseEbookDBModel courseEbookModel) {
48
49 if (isDatabaseOpen()) {
50
51 try {
52
53 ContentValues initialValues = prepareContentValue(courseEbookModel);
54 long i=appDatabase.insert(COURSE_EBOOKS_TABLE, null, initialValues);
55 Logger.d(this,"insertion="+i);
56
57 } catch (SQLException e) {
58 Logger.d(this, "database sql exception" + e);
59
60 }
61
62 }
63}
64
65public ContentValues prepareContentValue(CourseEbookDBModel item) {
66 ContentValues initialValues = new ContentValues();
67 initialValues.put(STUDENT_USERNAME, item.getUserName());
68 initialValues.put(IS_ALLOWED, item.isAllowed());
69 Logger.d(this, "username:" + item.getUserName()+" "+item.isAllowed());
70
71 return initialValues;
72}
73
74public synchronized int getUserValues(CourseEbookDBModel courseEbookDBModel) {
75
76 int flag = 0;
77 if (isDatabaseOpen()) {
78 try {
79
80 Cursor cursor = appDatabase.rawQuery(FIND_COURSE_EBOOK, null);
81 if (cursor != null && cursor.moveToFirst()) {
82
83 int userNameIndex = cursor.getColumnIndex(STUDENT_USERNAME);
84 int isAllowedIndex = cursor.getColumnIndex(IS_ALLOWED);
85 Logger.d(this,"index="+isAllowedIndex);
86
87 do {
88 Logger.d(this,"User are:"+cursor.getString(userNameIndex));
89 if (cursor.getString(userNameIndex).equals(courseEbookDBModel.getUserName())) {
90
91 flag = cursor.getInt(isAllowedIndex);
92 Logger.d(this,"db user="+cursor.getString(userNameIndex));
93 break;
94 }
95
96 // String userName = cursor.getString(userNameIndex);
97
98 } while (cursor.moveToNext());
99 cursor.close();
100
101 }
102
103 } catch (Exception e) {
104 // TODO: handle exception
105 }
106 }
107
108 return flag;
109}
110}
111
112
113
114public class DbAdapter extends SQLiteOpenHelper {
115
116protected static final String DATABASE_NAME = "cec_todolist.db";
117protected static final int DATABASE_VERSION = 1;
118static SQLiteDatabase appDatabase = null;
119
120public DbAdapter(Context context, String DatabaseName, int version) {
121 super(context, DatabaseName, null, version);
122}
123
124@Override
125public synchronized void onCreate(SQLiteDatabase db) {
126// Logger.d(this, "oncreate of DbAdapter is called");
127
128}
129
130@Override
131public synchronized void onUpgrade(SQLiteDatabase db, int oldVersion,
132 int newVersion) {
133}
134
135public boolean isDatabaseOpen() {
136
137 try {
138
139 if (appDatabase != null && appDatabase.isOpen()) {
140 return true;
141 }
142 appDatabase = this.getWritableDatabase();
143 if (appDatabase == null) {
144 return false;
145 } else {
146 return true;
147 }
148 } catch (Exception e) {
149 close();
150 Logger.e(this, e.toString());
151 return false;
152
153 }
154
155}
156}