· 6 years ago · Mar 07, 2019, 03:10 PM
1public class Chapter {
2
3final SimpleDateFormat FORMATTER =
4 new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
5 private String title;
6 private URL link;
7 private String description;
8 private Date date;
9 private int id;
10
11 //Constructor for table includes ID.
12 public Chapter(int id, URL link, String title, String description, Date date) {
13 this.id = id;
14 this.link = link;
15 this.title = title;
16 this.description = description;
17 this.date = date;
18 }
19
20 //Empty Constructor
21 public Chapter() {
22 // TODO Auto-generated constructor stub
23 }
24
25 public String getTitle() {
26 return title;
27 }
28
29 public void setTitle(String title) {
30 this.title = title.trim();
31 }
32 // getters and setters omitted for brevity
33 public URL getLink() {
34 return link;
35 }
36
37 public void setLink(String link) {
38 try {
39 this.link = new URL(link);
40 } catch (MalformedURLException e) {
41 throw new RuntimeException(e);
42 }
43 }
44
45 public String getDescription() {
46 return description;
47 }
48
49 public void setDescription(String description) {
50 this.description = description.trim();
51 }
52
53 public String getDate() {
54 return FORMATTER.format(this.date);
55 }
56
57 public void setDate(String date) {
58 // pad the date if necessary
59 while (!date.endsWith("00")){
60 date += "0";
61 }
62 date = "";
63 try {
64 this.date = FORMATTER.parse(date.trim());
65 } catch (ParseException e) {
66 throw new RuntimeException(e);
67 }
68 }
69
70 public Chapter copy(){
71 Chapter copy = new Chapter();
72 copy.title = title;
73 copy.link = link;
74 copy.description = description;
75 copy.date = date;
76 return copy;
77 }
78
79 @Override
80 public String toString() {
81 StringBuilder sb = new StringBuilder();
82 sb.append("Title: ");
83 sb.append(title);
84 sb.append('n');
85 sb.append("Date: ");
86 sb.append(this.getDate());
87 sb.append('n');
88 sb.append("Link: ");
89 sb.append(link);
90 sb.append('n');
91 sb.append("Description: ");
92 sb.append(description);
93 return sb.toString();
94 }
95
96 @Override
97 public int hashCode() {
98 final int prime = 31;
99 int result = 1;
100 result = prime * result + ((date == null) ? 0 : date.hashCode());
101 result = prime * result
102 + ((description == null) ? 0 : description.hashCode());
103 result = prime * result + ((link == null) ? 0 : link.hashCode());
104 result = prime * result + ((title == null) ? 0 : title.hashCode());
105 return result;
106 }
107
108 @Override
109 public boolean equals(Object obj) {
110 if (this == obj)
111 return true;
112 if (obj == null)
113 return false;
114 if (getClass() != obj.getClass())
115 return false;
116 Chapter other = (Chapter) obj;
117 if (date == null) {
118 if (other.date != null)
119 return false;
120 } else if (!date.equals(other.date))
121 return false;
122 if (description == null) {
123 if (other.description != null)
124 return false;
125 } else if (!description.equals(other.description))
126 return false;
127 if (link == null) {
128 if (other.link != null)
129 return false;
130 } else if (!link.equals(other.link))
131 return false;
132 if (title == null) {
133 if (other.title != null)
134 return false;
135 } else if (!title.equals(other.title))
136 return false;
137 return true;
138 }
139
140 public int compareTo(Chapter another) {
141 if (another == null) return 1;
142 // sort descending, most recent first
143 return another.date.compareTo(date);
144 }
145
146 public int getId() {
147 // TODO Auto-generated method stub
148 return id;
149 }
150
151 public void setId(int parseInt) {
152 // TODO Auto-generated method stub
153 this.id = id;
154 }
155
156 public void setImage(String string) {
157 // TODO Auto-generated method stub
158
159 }
160
161public class ChapterSQLiteOpenHelper extends SQLiteOpenHelper {
162
163private static final String DATABASE_NAME = "chaptertable";
164 private static final int DATABASE_VERSION = 1;
165
166 // Database table
167 public static final String TABLE_CHAPTER = "Chapter";
168 public static final String COLUMN_ID = "_id";
169 public static final String COLUMN_LINK = "link";
170 public static final String COLUMN_TITLE = "title";
171 public static final String COLUMN_DESCRIPTION = "description";
172 public static final String COLUMN_PUBDATE = "pubdate";
173 public static final String COLUMN_IMAGEID = "imageid";
174
175 // Database creation SQL statement
176 private static final String DATABASE_CREATE = "create table "
177 + TABLE_CHAPTER
178 + "("
179 + COLUMN_ID + " integer primary key autoincrement, "
180 + COLUMN_LINK + " text not null, "
181 + COLUMN_TITLE + " text not null,"
182 + COLUMN_DESCRIPTION + " text not null"
183 + COLUMN_PUBDATE + " text not null,"
184 + COLUMN_IMAGEID + " text not null,"
185 + ");";
186
187
188
189 public ChapterSQLiteOpenHelper(Context context) {
190 super(context, DATABASE_NAME, null, DATABASE_VERSION);
191 }
192
193 // Method is called during creation of the database
194 @Override
195 public void onCreate(SQLiteDatabase database) {
196 database.execSQL(DATABASE_CREATE);
197 }
198
199 // Method is called during an upgrade of the database,
200 // e.g. if you increase the database version
201 // Upgrading database
202 @Override
203 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
204 // Drop older table if existed
205 db.execSQL("DROP TABLE IF EXISTS " + TABLE_CHAPTER);
206
207 // Create tables again
208 onCreate(db);
209 }
210
211
212 /**
213 * All CRUD(Create, Read, Update, Delete) Operations
214 */
215
216 // Adding new contact
217 void addContact(Chapter chapter) {
218 SQLiteDatabase db = this.getWritableDatabase();
219
220 ContentValues values = new ContentValues();
221 values.put(COLUMN_ID, chapter.getId()); // Chapter ID
222 values.put(COLUMN_LINK, chapter.getLink()); // Chapter Link
223 values.put(COLUMN_TITLE, chapter.getTitle()); // Chapter Title
224 values.put(COLUMN_DESCRIPTION, chapter.getDescription()); // Chapter Description
225 values.put(COLUMN_PUBDATE, chapter.getDate()); // Chapter Date
226 //values.put(COLUMN_IMAGEID, chapter.getImage()); // Chapter Image
227
228 // Inserting Row
229 db.insert(TABLE_CHAPTER, null, values);
230 db.close(); // Closing database connection
231 }
232
233 // Getting single contact
234 Chapter getChapter(int id) {
235 SQLiteDatabase db = this.getReadableDatabase();
236
237 Cursor cursor = db.query(TABLE_CHAPTER, new String[] { COLUMN_ID,
238 COLUMN_ID, COLUMN_LINK }, COLUMN_ID + "=?",
239 new String[] { String.valueOf(id) }, null, null, null, null);
240 if (cursor != null)
241 cursor.moveToFirst();
242
243 Chapter chapter = new Chapter(Integer.parseInt(cursor.getString(0)), cursor.getURL(1), cursor.getString(2), cursor.getString(3), cursor.getString(4));
244 // return contact
245 return chapter;
246 }
247
248 // Getting All Contacts
249 public List<Chapter> getAllContacts() {
250 List<Chapter> chapterList = new ArrayList<Chapter>();
251 // Select All Query
252 String selectQuery = "SELECT * FROM " + TABLE_CHAPTER;
253
254 SQLiteDatabase db = this.getWritableDatabase();
255 Cursor cursor = db.rawQuery(selectQuery, null);
256
257 // looping through all rows and adding to list
258 if (cursor.moveToFirst()) {
259 do {
260 Chapter chapter = new Chapter();
261 chapter.setId(Integer.parseInt(cursor.getString(0)));
262 chapter.setLink(cursor.getURL(1));
263 chapter.setTitle(cursor.getString(2));
264 chapter.setDescription(cursor.getString(3));
265 chapter.setDate(cursor.getString(4));
266 //chapter.setImage(cursor.getString(5));
267 // Adding chapter to list
268 chapterList.add(chapter);
269 } while (cursor.moveToNext());
270 }
271
272 // return contact list
273 return chapterList;
274 }
275
276 // Updating single contact
277 public int updateContact(Chapter chapter) {
278 SQLiteDatabase db = this.getWritableDatabase();
279
280 ContentValues values = new ContentValues();
281 values.put(COLUMN_ID, chapter.getId());
282 values.put(COLUMN_LINK, chapter.getLink());
283 values.put(COLUMN_TITLE, chapter.getTitle());
284 values.put(COLUMN_DESCRIPTION, chapter.getDescription());
285 values.put(COLUMN_PUBDATE, chapter.getDate());
286 //values.put(COLUMN_IMAGEID, chapter.getImage());
287
288 // updating row
289 return db.update(TABLE_CHAPTER, values, COLUMN_ID + " = ?",
290 new String[] { String.valueOf(chapter.getId()) });
291 }
292
293 // Deleting single contact
294 public void deleteContact(Chapter chapter) {
295 SQLiteDatabase db = this.getWritableDatabase();
296 db.delete(TABLE_CHAPTER, COLUMN_ID + " = ?",
297 new String[] { String.valueOf(chapter.getId()) });
298 db.close();
299 }
300
301 // Getting contacts Count
302 public int getChaptersCount() {
303 String countQuery = "SELECT * FROM " + TABLE_CHAPTER;
304 SQLiteDatabase db = this.getReadableDatabase();
305 Cursor cursor = db.rawQuery(countQuery, null);
306 cursor.close();
307
308 // return count
309 return cursor.getCount();
310 }
311
312public Chapter copy(){
313 Chapter copy = new Chapter();
314 copy.setTitle(this.title);
315 copy.setLink(this.link);
316 copy.setDescription(this.description);
317 copy.setDate(this.date);
318 return copy;
319}