Merge lp:~karni/ubuntuone-android-music/added-missing-dao into lp:ubuntuone-android-music/v2

Proposed by Michał Karnicki
Status: Merged
Merged at revision: 31
Proposed branch: lp:~karni/ubuntuone-android-music/added-missing-dao
Merge into: lp:ubuntuone-android-music/v2
Diff against target: 267 lines (+110/-63)
4 files modified
src/com/ubuntuone/android/music/provider/dao/ArtistAlbumDao.java (+102/-0)
src/com/ubuntuone/android/music/provider/dao/ArtistDao.java (+0/-39)
src/com/ubuntuone/android/music/provider/dao/Dao.java (+0/-23)
src/com/ubuntuone/android/music/service/SyncService.java (+8/-1)
To merge this branch: bzr merge lp:~karni/ubuntuone-android-music/added-missing-dao
Reviewer Review Type Date Requested Status
Ubuntu One Client Engineering team Pending
Review via email: mp+136706@code.launchpad.net

Commit message

Added missing artist albums DAO.

Description of the change

Added missing artist albums DAO.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'src/com/ubuntuone/android/music/provider/dao/ArtistAlbumDao.java'
2--- src/com/ubuntuone/android/music/provider/dao/ArtistAlbumDao.java 1970-01-01 00:00:00 +0000
3+++ src/com/ubuntuone/android/music/provider/dao/ArtistAlbumDao.java 2012-11-28 16:23:32 +0000
4@@ -0,0 +1,102 @@
5+package com.ubuntuone.android.music.provider.dao;
6+
7+import java.util.ArrayList;
8+
9+import android.content.ContentResolver;
10+import android.content.ContentValues;
11+import android.database.sqlite.SQLiteDatabase;
12+import android.database.sqlite.SQLiteStatement;
13+import android.net.Uri;
14+
15+import com.ubuntuone.android.music.provider.MusicContentValues;
16+import com.ubuntuone.android.music.provider.MusicContract.Albums;
17+import com.ubuntuone.android.music.provider.MusicContract.ArtistAlbums;
18+import com.ubuntuone.android.music.provider.MusicContract.Artists;
19+import com.ubuntuone.android.music.provider.MusicDatabase;
20+import com.ubuntuone.android.music.provider.MusicDatabase.Tables;
21+import com.ubuntuone.android.music.provider.MusicProvider;
22+import com.ubuntuone.api.music.model.U1Artist;
23+
24+public class ArtistAlbumDao extends Dao
25+{
26+ public ArtistAlbumDao() {
27+ }
28+
29+ /**
30+ * Updates the artists albums. If the artists albums don't exist, inserts artists albums.
31+ * Backed by {@link MusicProvider}.
32+ *
33+ * @param resolver
34+ * The content resolver.
35+ * @param artist
36+ * The {@link U1Artist} whose albums to save.
37+ */
38+ public void updateOrInsert(ContentResolver resolver, U1Artist artist) {
39+ ArrayList<ContentValues> albumsValues = MusicContentValues.forArtistAlbums(artist);
40+ for (ContentValues albumValues : albumsValues) {
41+ Uri artistAlbumUri = Artists.buildAlbumUri(
42+ artist.getId(), albumValues.getAsString(Albums.ALBUM_ID));
43+ int count = resolver.update(artistAlbumUri, albumValues, null, null);
44+ if (count == 0) {
45+ resolver.insert(artistAlbumUri, albumValues);
46+ }
47+ }
48+ }
49+
50+ /**
51+ * Updates the artists albums. If the artists albums don't exist, inserts artists albums.
52+ * Backed by {@link MusicDatabase}.
53+ *
54+ * @param db
55+ * The {@link MusicDatabase} instance.
56+ * @param artist
57+ * The {@link U1Artist} whose albums to save.
58+ * @return True if update or insert succeeded, false otherwise.
59+ */
60+ public boolean updateOrInsert(SQLiteDatabase db, U1Artist artist) {
61+ boolean isSuccess = true;
62+ SQLiteStatement sqlUpdate = db.compileStatement(getUpdateSql());
63+ SQLiteStatement sqlInsert = db.compileStatement(getInsertSql());
64+
65+ final String artistId = artist.getId();
66+ final ArrayList<String> albumIds = artist.getAlbumIds();
67+ for (String albumId : albumIds) {
68+ int i = 1; // Index to the parameter to bind.
69+ sqlUpdate.clearBindings();
70+ sqlUpdate.bindLong(i++, System.currentTimeMillis());
71+ sqlUpdate.bindString(i++, artistId);
72+ sqlUpdate.bindString(i++, albumId);
73+ // Can't use executeUpdateDelete on API < 11
74+ sqlUpdate.execute();
75+ long rowsUpdated = getChanges(db);
76+ if (rowsUpdated == 0L) {
77+ i = 1;
78+ sqlInsert.clearBindings();
79+ sqlInsert.bindLong(i++, System.currentTimeMillis());
80+ sqlInsert.bindString(i++, artistId);
81+ sqlInsert.bindString(i++, albumId);
82+
83+ long rowId = sqlInsert.executeInsert();
84+ isSuccess = isSuccess && (rowId != -1);
85+ } else {
86+ isSuccess = isSuccess && (rowsUpdated == 1);
87+ }
88+ }
89+ return isSuccess;
90+ }
91+
92+ @Override
93+ public String onGetInsertSql() {
94+ return "INSERT INTO " + Tables.ARTISTS_ALBUMS + "(" +
95+ ArtistAlbums.UPDATED + ", " +
96+ ArtistAlbums.ARTIST_ID + ", " +
97+ ArtistAlbums.ALBUM_ID +
98+ ") VALUES (?, ?, ?)";
99+ }
100+
101+ @Override
102+ public String onGetUpdateSql() {
103+ return "UPDATE " + Tables.ARTISTS_ALBUMS + " SET " + ArtistAlbums.UPDATED + "=? " +
104+ "WHERE " + ArtistAlbums.ARTIST_ID + "=? AND " + ArtistAlbums.ALBUM_ID + "=?";
105+ }
106+}
107
108=== modified file 'src/com/ubuntuone/android/music/provider/dao/ArtistDao.java'
109--- src/com/ubuntuone/android/music/provider/dao/ArtistDao.java 2012-11-21 15:11:58 +0000
110+++ src/com/ubuntuone/android/music/provider/dao/ArtistDao.java 2012-11-28 16:23:32 +0000
111@@ -1,7 +1,5 @@
112 package com.ubuntuone.android.music.provider.dao;
113
114-import java.util.ArrayList;
115-
116 import android.content.ContentResolver;
117 import android.content.ContentValues;
118 import android.database.sqlite.SQLiteDatabase;
119@@ -9,8 +7,6 @@
120 import android.net.Uri;
121
122 import com.ubuntuone.android.music.provider.MusicContentValues;
123-import com.ubuntuone.android.music.provider.MusicContract.Albums;
124-import com.ubuntuone.android.music.provider.MusicContract.ArtistAlbums;
125 import com.ubuntuone.android.music.provider.MusicContract.Artists;
126 import com.ubuntuone.android.music.provider.MusicDatabase;
127 import com.ubuntuone.android.music.provider.MusicDatabase.Tables;
128@@ -39,16 +35,6 @@
129 if (count == 0) {
130 resolver.insert(Artists.CONTENT_URI, values);
131 }
132- // Insert artist albums.
133- ArrayList<ContentValues> albumsValues = MusicContentValues.forArtistAlbums(artist);
134- for (ContentValues albumValues : albumsValues) {
135- Uri artistAlbumUri = Artists.buildAlbumUri(
136- artist.getId(), albumValues.getAsString(Albums.ALBUM_ID));
137- int count2 = resolver.update(artistAlbumUri, albumValues, null, null);
138- if (count2 == 0) {
139- resolver.insert(artistAlbumUri, albumValues);
140- }
141- }
142 }
143
144 /**
145@@ -104,20 +90,6 @@
146 } else {
147 isSuccess = rowsUpdated == 1;
148 }
149-
150- // Insert artist albums.
151- sqlStmt = db.compileStatement(getSecondaryInsertSql());
152- final String artistId = artist.getId();
153- final ArrayList<String> albumIds = artist.getAlbumIds();
154- for (String albumId : albumIds) {
155- sqlStmt.clearBindings();
156- sqlStmt.bindLong(1, System.currentTimeMillis());
157- sqlStmt.bindString(2, artistId);
158- sqlStmt.bindString(3, albumId);
159- long rowId = sqlStmt.executeInsert();
160- isSuccess = isSuccess && (rowId != -1);
161- }
162-
163 return isSuccess;
164 }
165
166@@ -140,17 +112,6 @@
167 "?, ?" +
168 ")";
169 }
170-
171- @Override
172- public String onGetSecondaryInsertSql() {
173- return "INSERT INTO " + Tables.ARTISTS_ALBUMS + "(" +
174-
175- ArtistAlbums.UPDATED + ", " +
176- ArtistAlbums.ARTIST_ID + ", " +
177- ArtistAlbums.ALBUM_ID +
178-
179- ") VALUES (?, ?, ?)";
180- }
181
182 @Override
183 public String onGetUpdateSql() {
184
185=== modified file 'src/com/ubuntuone/android/music/provider/dao/Dao.java'
186--- src/com/ubuntuone/android/music/provider/dao/Dao.java 2012-11-21 15:11:58 +0000
187+++ src/com/ubuntuone/android/music/provider/dao/Dao.java 2012-11-28 16:23:32 +0000
188@@ -6,7 +6,6 @@
189 public abstract class Dao
190 {
191 private String mInsertSql;
192- private String mSecondaryInsertSql;
193 private String mUdpateSql;
194
195 /**
196@@ -22,18 +21,6 @@
197 }
198
199 /**
200- * Performs a lazy initialization with {@code Dao#onGetSecondaryInsertSql()}.
201- *
202- * @return The cached secondary SQL INSERT string.
203- */
204- public String getSecondaryInsertSql() {
205- if (mSecondaryInsertSql == null) {
206- mSecondaryInsertSql = onGetSecondaryInsertSql();
207- }
208- return mSecondaryInsertSql;
209- }
210-
211- /**
212 * Performs a lazy initialization with {@code Dao#onGetUpdateSql()}.
213 *
214 * @return The cached SQL UPDATE string.
215@@ -57,16 +44,6 @@
216 */
217 public abstract String onGetInsertSql();
218
219-
220- /**
221- * Override to return the secondary SQL INSERT string.
222- *
223- * @return The secondary SQL INSERT string.
224- */
225- public String onGetSecondaryInsertSql() {
226- return null;
227- }
228-
229 /**
230 * Override to return the SQL UPDATE string.
231 *
232
233=== modified file 'src/com/ubuntuone/android/music/service/SyncService.java'
234--- src/com/ubuntuone/android/music/service/SyncService.java 2012-11-22 11:17:42 +0000
235+++ src/com/ubuntuone/android/music/service/SyncService.java 2012-11-28 16:23:32 +0000
236@@ -42,6 +42,7 @@
237 import com.ubuntuone.android.music.provider.MusicDatabase;
238 import com.ubuntuone.android.music.provider.MusicProviderUtils;
239 import com.ubuntuone.android.music.provider.dao.AlbumDao;
240+import com.ubuntuone.android.music.provider.dao.ArtistAlbumDao;
241 import com.ubuntuone.android.music.provider.dao.ArtistDao;
242 import com.ubuntuone.android.music.provider.dao.GenreDao;
243 import com.ubuntuone.android.music.provider.dao.PlaylistDao;
244@@ -180,6 +181,7 @@
245 private U1ArtistListener mArtistListener = new U1ArtistListener() {
246 private SQLiteDatabase db;
247 private ArtistDao artistDao = new ArtistDao();
248+ private ArtistAlbumDao artistAlbumDao = new ArtistAlbumDao();
249 private Bundle progressBundle = new Bundle();
250 private Failure failure = null;
251 private int artistCount = 0;
252@@ -198,9 +200,14 @@
253 public void onSuccess(U1Artist artist) {
254 artistCount++;
255 if (DIRECT_SQL) {
256- artistDao.updateOrInsert(db, artist);
257+ if (artistDao.updateOrInsert(db, artist)) {
258+ if (!artistAlbumDao.updateOrInsert(db, artist)) {
259+ Log.w(TAG, "Artist album not inserted: " + artist.getId());
260+ }
261+ }
262 } else {
263 artistDao.updateOrInsert(mResolver, artist);
264+ artistAlbumDao.updateOrInsert(mResolver, artist);
265 }
266 mReceiver.send(SYNC_PROGRESS, progressBundle);
267 }

Subscribers

People subscribed via source and target branches

to all changes: