summaryrefslogtreecommitdiffstats
path: root/src/ryuslash/org/dvdroid/DVDDataSource.java
blob: 7bfa92ac97690c42a304229029616cf448cffc98 (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
package ryuslash.org.dvdroid;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DVDDataSource
{
    private SQLiteDatabase database;
    private SQLiteHelper dbHelper;
    private String[] allColumns = { SQLiteHelper.COLUMN_ID,
                                    SQLiteHelper.COLUMN_TITLE,
                                    SQLiteHelper.COLUMN_WATCHED,
                                    SQLiteHelper.COLUMN_WISHLIST };

    public DVDDataSource(Context context)
    {
        dbHelper = new SQLiteHelper(context);
    }

    public void open() throws SQLException
    {
        database = dbHelper.getWritableDatabase();
    }

    public void close()
    {
        dbHelper.close();
    }

    private ContentValues createValues(String title, int watched,
                                       int wishlist)
    {
        ContentValues values = new ContentValues();

        values.put(SQLiteHelper.COLUMN_TITLE, title);
        values.put(SQLiteHelper.COLUMN_WATCHED, watched);
        values.put(SQLiteHelper.COLUMN_WISHLIST, wishlist);

        return values;
    }

    public DVD createDVD(String title, int watched, int wishlist)
    {
        ContentValues values = createValues(title, watched, wishlist);
        long insertId = database.insert(SQLiteHelper.TABLE_DVD, null,
                                        values);
        Cursor cursor = database.query(SQLiteHelper.TABLE_DVD,
                                       allColumns,
                                       SQLiteHelper.COLUMN_ID + " = "
                                       + insertId, null, null, null,
                                       null);
        cursor.moveToFirst();

        DVD newDVD = cursorToDVD(cursor);

        cursor.close();

        return newDVD;
    }

    public void updateDVD(long id, String title, int watched, int wishlist)
    {
        ContentValues values = createValues(title, watched, wishlist);
        String whereArgs[] = { "" + id };

        database.update(SQLiteHelper.TABLE_DVD, values, "id = ?",
                        whereArgs);
    }

    public void deleteDVD(DVD dvd)
    {
        deleteDVD(dvd.getId());
    }

    public void deleteDVD(long id)
    {
        database.delete(SQLiteHelper.TABLE_DVD,
                        SQLiteHelper.COLUMN_ID + " = " + id, null);
    }

    public DVD getById(long id)
    {
        Cursor cursor = database.query(SQLiteHelper.TABLE_DVD,
                                       allColumns,
                                       SQLiteHelper.COLUMN_ID + " = " + id,
                                       null, null, null, null);

        cursor.moveToFirst();

        DVD dvd = cursorToDVD(cursor);

        cursor.close();

        return dvd;
    }

    public List<DVD> getListFromCursor(Cursor cursor)
    {
        List<DVD> dvds = new ArrayList<DVD>();

        cursor.moveToFirst();

        while(!cursor.isAfterLast()) {
            DVD dvd = cursorToDVD(cursor);

            dvds.add(dvd);
            cursor.moveToNext();
        }

        cursor.close();

        return dvds;
    }

    public List<DVD> getWishList()
    {
        Cursor cursor = database.query(SQLiteHelper.TABLE_DVD,
                                       allColumns, "wishlist = 1", null,
                                       null, null, "title");
        return getListFromCursor(cursor);
    }

    public List<DVD> getWatchList()
    {
        Cursor cursor = database.query(SQLiteHelper.TABLE_DVD,
                                       allColumns, "watched = 0", null,
                                       null, null, "title");
        return getListFromCursor(cursor);
    }

    public List<DVD> getCollection()
    {
        Cursor cursor = database.query(SQLiteHelper.TABLE_DVD,
                                       allColumns, "wishlist = 0", null,
                                       null, null, "title");
        return getListFromCursor(cursor);
    }

    public List<DVD> getQuery(String query)
    {
        String params[] = { "%" + query + "%" };
        Cursor cursor = database.query(SQLiteHelper.TABLE_DVD,
                                       allColumns, "title LIKE ?",
                                       params, null, null, "title");
        return getListFromCursor(cursor);
    }

    private DVD cursorToDVD(Cursor cursor)
    {
        DVD dvd = new DVD();

        dvd.setId(cursor.getLong(0));
        dvd.setTitle(cursor.getString(1));
        dvd.setWatched(cursor.getInt(2));
        dvd.setWishlist(cursor.getInt(3));

        return dvd;
    }
}