summaryrefslogtreecommitdiff
path: root/src/de/podfetcher/storage/PodDBAdapter.java
blob: e9df7607eee6324ad50e1d32d5718454b8959d58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package de.podfetcher.storage;

import de.podfetcher.feed.Feed;
import de.podfetcher.feed.FeedCategory;
import de.podfetcher.feed.FeedImage;
import de.podfetcher.feed.FeedItem;
import de.podfetcher.feed.FeedMedia;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Implements methods for accessing the database
 * */
public class PodDBAdapter {

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

	// Key-constants
	public static final String KEY_ID = "id";
	public static final String KEY_TITLE = "title";
	public static final String KEY_NAME = "name";
	public static final String KEY_LINK = "link";
	public static final String KEY_DESCRIPTION = "description";
	public static final String KEY_FILE_URL = "file_url";
	public static final String KEY_DOWNLOAD_URL = "download_url";
	public static final String KEY_PUBDATE = "pubDate";
	public static final String KEY_READ = "read";
	public static final String KEY_LENGTH = "length";
	public static final String KEY_POSITION = "position";
	public static final String KEY_SIZE = "filesize";
	public static final String KEY_MIME_TYPE = "mime_type";
	public static final String KEY_IMAGE = "image";
	public static final String KEY_CATEGORY = "category";
	public static final String KEY_FEED = "feed";
	public static final String KEY_MEDIA = "media";

	// Table names
	public static final String TABLE_NAME_FEEDS = "Feeds";
	public static final String TABLE_NAME_FEED_ITEMS = "FeedItems";
	public static final String TABLE_NAME_FEED_CATEGORIES = "FeedCategories";
	public static final String TABLE_NAME_FEED_IMAGES = "FeedImages";
	public static final String TABLE_NAME_FEED_MEDIA = "FeedMedia";

	// SQL Statements for creating new tables
	private static final String TABLE_PRIMARY_KEY = KEY_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT ,";
	private static final String CREATE_TABLE_FEEDS = "CREATE TABLE "
			+ TABLE_NAME_FEEDS + " (" + TABLE_PRIMARY_KEY + KEY_TITLE
			+ " TEXT," + KEY_LINK + " TEXT," + KEY_DESCRIPTION
			+ " TEXT," + KEY_IMAGE + " INTEGER," + KEY_CATEGORY
			+ " INTEGER," + KEY_FILE_URL + " TEXT," + KEY_DOWNLOAD_URL
			+ " TEXT)";

	private static final String CREATE_TABLE_FEED_ITEMS = "CREATE TABLE "
			+ TABLE_NAME_FEED_ITEMS + " (" + TABLE_PRIMARY_KEY + KEY_TITLE
			+ " TEXT," + KEY_LINK + " TEXT," + KEY_DESCRIPTION
			+ " TEXT," + KEY_PUBDATE + " TEXT," + KEY_MEDIA
			+ " INTEGER," + KEY_FEED + " INTEGER," + KEY_READ
			+ " INTEGER)";

	private static final String CREATE_TABLE_FEED_CATEGORIES = "CREATE TABLE "
			+ TABLE_NAME_FEED_CATEGORIES + " (" + TABLE_PRIMARY_KEY + KEY_NAME
			+ " TEXT)";

	private static final String CREATE_TABLE_FEED_IMAGES = "CREATE TABLE "
			+ TABLE_NAME_FEED_IMAGES + " (" + TABLE_PRIMARY_KEY + KEY_TITLE
			+ " TEXT," + KEY_FILE_URL + " TEXT,"
			+ KEY_DOWNLOAD_URL + " TEXT)";

	private static final String CREATE_TABLE_FEED_MEDIA = "CREATE TABLE "
			+ TABLE_NAME_FEED_MEDIA + " (" + TABLE_PRIMARY_KEY + KEY_LENGTH
			+ " INTEGER," + KEY_POSITION + " INTEGER,"
			+ KEY_SIZE + " INTEGER," + KEY_MIME_TYPE + " TEXT,"
			+ KEY_FILE_URL + " TEXT," + KEY_DOWNLOAD_URL + " TEXT)";

	private SQLiteDatabase db;
	private final Context context;
	private PodDBHelper helper;

	public PodDBAdapter(Context c) {
		this.context = c;
		helper = new PodDBHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	public PodDBAdapter open() {
		try {
			db = helper.getWritableDatabase();
		} catch (SQLException ex) {
			db = helper.getReadableDatabase();
		}
		return this;
	}

	public void close() {
		db.close();
	}
	
	/** Inserts or updates a feed entry 
	 * @return the id of the entry
	 * */
	public long setFeed(Feed feed) {
	        open();
		ContentValues values = new ContentValues();
		values.put(KEY_TITLE, feed.title);
		values.put(KEY_LINK, feed.link);
		values.put(KEY_DESCRIPTION, feed.description);
		if (feed.image != null) {
			if (feed.image.id == 0) {
				setImage(feed.image);
			}
			values.put(KEY_IMAGE, feed.image.id);
		}
		if(feed.category != null) {
			if(feed.category.id == 0) {
				setCategory(feed.category);
			}
			values.put(KEY_CATEGORY, feed.category.id);
		}
		if(feed.file_url != null) {
			values.put(KEY_FILE_URL, feed.file_url);
		}
		values.put(KEY_DOWNLOAD_URL, feed.download_url);
		
		if(feed.id == 0) {
			// Create new entry
			feed.id = db.insert(TABLE_NAME_FEEDS, null, values);
		} else {
			db.update(TABLE_NAME_FEEDS, values, KEY_ID+"=?", new String[]{String.valueOf(feed.id)});
		}
		close();
		return feed.id;
	}

	/** Inserts or updates a category entry 
	 * @return the id of the entry
	 * */
	public long setCategory(FeedCategory category) {
		ContentValues values = new ContentValues();
		values.put(KEY_NAME, category.name);
		if(category.id == 0) {
			category.id = db.insert(TABLE_NAME_FEED_CATEGORIES, null, values);
		} else {
			db.update(TABLE_NAME_FEED_CATEGORIES, values, KEY_ID+"=?", new String[]{String.valueOf(category.id)});
			
		}
		return category.id;
	}

	/** 
	 * Inserts or updates an image entry 
	 * @return the id of the entry
	 * */
	public long setImage(FeedImage image) {
		ContentValues values = new ContentValues();
		values.put(KEY_TITLE, image.title);
		values.put(KEY_DOWNLOAD_URL, image.download_url);
		if(image.file_url != null) {
			values.put(KEY_FILE_URL, image.file_url);
		}
		if(image.id == 0) {
			image.id = db.insert(TABLE_NAME_FEED_IMAGES, null, values);
		} else {
			db.update(TABLE_NAME_FEED_IMAGES, values, KEY_ID+"=?", new String[]{String.valueOf(image.id)});
		}
		return image.id;
	}
	
	/**
	 * Inserts or updates an image entry 
	 * @return the id of the entry
	 */
	public long setMedia(FeedMedia media) {
		ContentValues values = new ContentValues();
		values.put(KEY_LENGTH, media.length);
		values.put(KEY_POSITION, media.position);
		values.put(KEY_SIZE, media.size);
		values.put(KEY_MIME_TYPE, media.mime_type);
		values.put(KEY_DOWNLOAD_URL, media.download_url);
		if(media.file_url != null) {
			values.put(KEY_FILE_URL, media.file_url);
		}
		if(media.id == 0) {
			media.id = db.insert(TABLE_NAME_FEED_MEDIA, null, values);
		} else {
			db.update(TABLE_NAME_FEED_MEDIA, values, KEY_ID+"=?", new String[]{String.valueOf(media.id)});
		}
		return media.id;
	}
	
	/**
	 * Inserts or updates a feeditem entry 
	 * @return the id of the entry
	 */
	public long setFeedItem(FeedItem item) {
		ContentValues values = new ContentValues();
		values.put(KEY_TITLE, item.title);
		values.put(KEY_LINK, item.link);
		values.put(KEY_DESCRIPTION, item.description);
		values.put(KEY_PUBDATE, item.pubDate);
		if(item.media != null) {
			if(item.media.id == 0) {
				setMedia(item.media);
			}
			values.put(KEY_MEDIA, item.media.id);
		}
		if(item.feed.id == 0) {
			setFeed(item.feed);
		}
		values.put(KEY_FEED, item.feed.id);
		values.put(KEY_READ, (item.read) ? 1 : 0);
		return item.id;
	}
	
	public Cursor getAllCategoriesCursor() {
		return db.query(TABLE_NAME_FEED_CATEGORIES, null, null, null, null, null, null);
	}
	
	public Cursor getAllFeedsCursor() {
		return db.query(TABLE_NAME_FEEDS, null, null, null, null, null, null);
	}
	
	public Cursor getAllItemsOfFeedCursor(Feed feed) {
		return db.query(TABLE_NAME_FEED_ITEMS, null, KEY_FEED+"=?", new String[]{String.valueOf(feed.id)}, null, null, null);
	}
	
	public Cursor getFeedMediaOfItemCursor(FeedItem item) {
		return db.query(TABLE_NAME_FEED_MEDIA, null, KEY_ID+"=?", new String[]{String.valueOf(item.media.id)}, null, null, null);
	}
	
	public Cursor getImageOfFeedCursor(Feed feed) {
		return db.query(TABLE_NAME_FEED_IMAGES, null, KEY_ID+"=?", new String[]{String.valueOf(feed.image.id)}, null, null, null);
	}
	
	public FeedMedia getFeedMedia(long row_index) throws SQLException{
		Cursor cursor = db.query(TABLE_NAME_FEED_MEDIA, null, KEY_ID+"=?", new String[]{String.valueOf(row_index)}, null, null, null);
		
		if((cursor.getCount() == 0) || !cursor.moveToFirst()) {
			throw new SQLException("No FeedMedia found at index: "+ row_index);
		}
		
		return new FeedMedia(row_index, 
				cursor.getLong(cursor.getColumnIndex(KEY_LENGTH)), 
				cursor.getLong(cursor.getColumnIndex(KEY_POSITION)), 
				cursor.getLong(cursor.getColumnIndex(KEY_SIZE)), 
				cursor.getString(cursor.getColumnIndex(KEY_MIME_TYPE)), 
				cursor.getString(cursor.getColumnIndex(KEY_FILE_URL)), 
				cursor.getString(cursor.getColumnIndex(KEY_DOWNLOAD_URL)));
		
	}
	
	public FeedImage getFeedImage(Feed feed) throws SQLException {
		Cursor cursor = this.getImageOfFeedCursor(feed);
		if((cursor.getCount() == 0) || !cursor.moveToFirst()) {
			throw new SQLException("No FeedImage found at index: "+ feed.image.id);
		}
		
		return new FeedImage(feed.image.id, cursor.getString(cursor.getColumnIndex(KEY_TITLE)),
				cursor.getString(cursor.getColumnIndex(KEY_FILE_URL)),
				cursor.getString(cursor.getColumnIndex(KEY_DOWNLOAD_URL)));
	}

	private static class PodDBHelper extends SQLiteOpenHelper {

		public PodDBHelper(Context context, String name, CursorFactory factory,
				int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(CREATE_TABLE_FEEDS);
			db.execSQL(CREATE_TABLE_FEED_ITEMS);
			db.execSQL(CREATE_TABLE_FEED_CATEGORIES);
			db.execSQL(CREATE_TABLE_FEED_IMAGES);
			db.execSQL(CREATE_TABLE_FEED_MEDIA);

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w("DBAdapter", "Upgrading from version " + oldVersion + " to "
					+ newVersion + ".");
			// TODO delete Database

		}

	}
}