· 10 years ago · Sep 27, 2016, 06:34 PM
1package com.anita.libraryapplication.repo.booksystem.impl;
2
3
4import android.content.ContentValues;
5import android.content.Context;
6import android.database.Cursor;
7import android.database.SQLException;
8import android.database.sqlite.SQLiteDatabase;
9import android.database.sqlite.SQLiteOpenHelper;
10import android.util.Log;
11
12import com.anita.libraryapplication.conf.databases.DBConstants;
13import com.anita.libraryapplication.domain.booksystem.Books;
14import com.anita.libraryapplication.repo.booksystem.BookRepository;
15
16import java.util.HashSet;
17import java.util.Set;
18
19/**
20 * Created by Anita on 2016/04/24.
21 */
22public class BookRepositoryImpl extends SQLiteOpenHelper implements BookRepository
23{
24
25 public static final String TABLE_NAME = "book";
26 private SQLiteDatabase db;
27 public static final String COLUMN_ID = "id";
28 public static final String COLUMN_bookTitle = "bookTitle";
29 public static final String COLUMN_AUTHOR = "author";
30 public static final String COLUMN_PAGES = "pages";
31 public static final String COLUMN_PUBLISHER = "publisher";
32 public static final String COLUMN_ISBN = "iSBN";
33
34 // Database creation sql statement
35 private static final String DATABASE_CREATE = " CREATE TABLE "
36 + TABLE_NAME + "("
37 + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
38 + COLUMN_bookTitle + " TEXT NOT NULL , "
39 + COLUMN_AUTHOR + " TEXT NOT NULL , "
40 + COLUMN_PAGES + " TEXT NOT NULL , "
41 + COLUMN_PUBLISHER + " TEXT NOT NULL , "
42 + COLUMN_ISBN + " TEXT NOT NULL);" ;
43
44 public BookRepositoryImpl(Context context) {
45 super(context, DBConstants.DATABASE_NAME, null, DBConstants.DATABASE_VERSION);
46 }
47
48 public void open() throws SQLException {
49 db = this.getWritableDatabase();
50 }
51
52 public void close() {
53 this.close();
54 }
55
56 @Override
57 public Books findById(long id) {
58
59 SQLiteDatabase db = this.getReadableDatabase();
60 Cursor cursor = db.query(
61 TABLE_NAME,
62 new String[]{
63 COLUMN_ID,
64 COLUMN_bookTitle,
65 COLUMN_AUTHOR,
66 COLUMN_PAGES,
67 COLUMN_PUBLISHER,
68 COLUMN_ISBN},
69 COLUMN_ID + " =? ",
70 new String[]{String.valueOf(id)},
71 null,
72 null,
73 null,
74 null);
75 if (cursor.moveToFirst()) {
76 final Books book = new Books.Builder()
77 .id(cursor.getLong(cursor.getColumnIndex(COLUMN_ID)))
78 .bookTitle(cursor.getString(cursor.getColumnIndex(COLUMN_bookTitle)))
79 .author(cursor.getString(cursor.getColumnIndex(COLUMN_AUTHOR)))
80 .pages(cursor.getInt(cursor.getColumnIndex(COLUMN_PAGES)))
81 .publisher(cursor.getString(cursor.getColumnIndex(COLUMN_PUBLISHER)))
82 .iSBN(cursor.getString(cursor.getColumnIndex(COLUMN_ISBN)))
83 .build();
84 return book;
85 } else {
86 return null;
87 }
88 }
89 private long id;
90 private String bookTitle;
91 private String author;
92 private int pages;
93 private String publisher;
94 private String iSBN;
95 @Override
96 public Books save(Books entity) {
97 open();
98 ContentValues values = new ContentValues();
99 // values.put(COLUMN_ID, entity.getId());
100 values.put(COLUMN_bookTitle, entity.getBookTitle());
101 values.put(COLUMN_AUTHOR, entity.getAuthor());
102 values.put(COLUMN_PAGES, entity.getPages());
103 values.put(COLUMN_PUBLISHER, entity.getPublisher());
104 values.put(COLUMN_ISBN, entity.getiSBN());
105
106 System.out.println("This excecutes");
107 long id = db.insertOrThrow(TABLE_NAME, null, values);
108 Books insertedEntity = new Books.Builder()
109 .copy(entity)
110 .id(id)
111 .build();
112
113 return insertedEntity;
114 }
115
116 @Override
117 public Books update(Books entity) {
118 open();
119 ContentValues values = new ContentValues();
120 values.put(COLUMN_ID, entity.getId());
121 values.put(COLUMN_bookTitle, entity.getBookTitle());
122 values.put(COLUMN_AUTHOR, entity.getAuthor());
123 values.put(COLUMN_PAGES, entity.getPages());
124 values.put(COLUMN_PUBLISHER, entity.getPublisher());
125 values.put(COLUMN_ISBN, entity.getiSBN());
126
127
128 db.update(
129 TABLE_NAME,
130 values,
131 COLUMN_ID + " =? ",
132 new String[]{String.valueOf(entity.getId())}
133 );
134 return entity;
135 }
136
137 @Override
138 public Books delete(Books entity) {
139 open();
140 db.delete(
141 TABLE_NAME,
142 COLUMN_ID + " =? ",
143 new String[]{String.valueOf(entity.getId())});
144 return entity;
145 }
146
147 @Override
148 public Set<Books> findAll() {
149 SQLiteDatabase db = this.getReadableDatabase();
150 Set<Books> member = new HashSet<>();
151 open();
152 Cursor cursor = db.query(TABLE_NAME, null,null,null,null,null,null);
153 if (cursor.moveToFirst()) {
154 do {
155 final Books setting = new Books.Builder()
156
157 .id(cursor.getLong(cursor.getColumnIndex(COLUMN_ID)))
158 .bookTitle(cursor.getString(cursor.getColumnIndex(COLUMN_bookTitle)))
159 .author(cursor.getString(cursor.getColumnIndex(COLUMN_AUTHOR)))
160 .pages(cursor.getInt(cursor.getColumnIndex(COLUMN_PAGES)))
161 .publisher(cursor.getString(cursor.getColumnIndex(COLUMN_PUBLISHER)))
162 .iSBN(cursor.getString(cursor.getColumnIndex(COLUMN_ISBN)))
163 .build();
164 member.add(setting);
165 } while (cursor.moveToNext());
166 }
167 return member;
168 }
169
170 @Override
171 public int deleteAll() {
172 open();
173 int rowsDeleted = db.delete(TABLE_NAME, null, null);
174 // close();
175 return rowsDeleted;
176 }
177
178 public int findMaxPages() {
179 SQLiteDatabase db = this.getReadableDatabase();
180 int max = -5;
181 Set<Books> member = new HashSet<>();
182 open();
183 Cursor cursor = db.query(TABLE_NAME, null,null,null,null,null,null);
184 if (cursor.moveToFirst()) {
185 do {
186 final Books setting = new Books.Builder()
187 .id(cursor.getLong(cursor.getColumnIndex(COLUMN_ID)))
188 .pages(cursor.getInt(cursor.getColumnIndex(COLUMN_PAGES)))
189 .build();
190 member.add(setting);
191
192 if(setting.getPages()>max)
193 {
194 max = setting.getPages();
195 }
196
197
198 } while (cursor.moveToNext());
199 }
200 return max;
201 }
202
203
204 @Override
205 public void onCreate(SQLiteDatabase db) {
206 db.execSQL(DATABASE_CREATE);
207 }
208
209 @Override
210 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
211 Log.w(this.getClass().getName(),
212 "Upgrading database from version " + oldVersion + " to "
213 + newVersion + ", which will destroy all old data");
214 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
215 onCreate(db);
216 }
217
218}