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
=== added file 'src/com/ubuntuone/android/music/provider/dao/ArtistAlbumDao.java'
--- src/com/ubuntuone/android/music/provider/dao/ArtistAlbumDao.java 1970-01-01 00:00:00 +0000
+++ src/com/ubuntuone/android/music/provider/dao/ArtistAlbumDao.java 2012-11-28 16:23:32 +0000
@@ -0,0 +1,102 @@
1package com.ubuntuone.android.music.provider.dao;
2
3import java.util.ArrayList;
4
5import android.content.ContentResolver;
6import android.content.ContentValues;
7import android.database.sqlite.SQLiteDatabase;
8import android.database.sqlite.SQLiteStatement;
9import android.net.Uri;
10
11import com.ubuntuone.android.music.provider.MusicContentValues;
12import com.ubuntuone.android.music.provider.MusicContract.Albums;
13import com.ubuntuone.android.music.provider.MusicContract.ArtistAlbums;
14import com.ubuntuone.android.music.provider.MusicContract.Artists;
15import com.ubuntuone.android.music.provider.MusicDatabase;
16import com.ubuntuone.android.music.provider.MusicDatabase.Tables;
17import com.ubuntuone.android.music.provider.MusicProvider;
18import com.ubuntuone.api.music.model.U1Artist;
19
20public class ArtistAlbumDao extends Dao
21{
22 public ArtistAlbumDao() {
23 }
24
25 /**
26 * Updates the artists albums. If the artists albums don't exist, inserts artists albums.
27 * Backed by {@link MusicProvider}.
28 *
29 * @param resolver
30 * The content resolver.
31 * @param artist
32 * The {@link U1Artist} whose albums to save.
33 */
34 public void updateOrInsert(ContentResolver resolver, U1Artist artist) {
35 ArrayList<ContentValues> albumsValues = MusicContentValues.forArtistAlbums(artist);
36 for (ContentValues albumValues : albumsValues) {
37 Uri artistAlbumUri = Artists.buildAlbumUri(
38 artist.getId(), albumValues.getAsString(Albums.ALBUM_ID));
39 int count = resolver.update(artistAlbumUri, albumValues, null, null);
40 if (count == 0) {
41 resolver.insert(artistAlbumUri, albumValues);
42 }
43 }
44 }
45
46 /**
47 * Updates the artists albums. If the artists albums don't exist, inserts artists albums.
48 * Backed by {@link MusicDatabase}.
49 *
50 * @param db
51 * The {@link MusicDatabase} instance.
52 * @param artist
53 * The {@link U1Artist} whose albums to save.
54 * @return True if update or insert succeeded, false otherwise.
55 */
56 public boolean updateOrInsert(SQLiteDatabase db, U1Artist artist) {
57 boolean isSuccess = true;
58 SQLiteStatement sqlUpdate = db.compileStatement(getUpdateSql());
59 SQLiteStatement sqlInsert = db.compileStatement(getInsertSql());
60
61 final String artistId = artist.getId();
62 final ArrayList<String> albumIds = artist.getAlbumIds();
63 for (String albumId : albumIds) {
64 int i = 1; // Index to the parameter to bind.
65 sqlUpdate.clearBindings();
66 sqlUpdate.bindLong(i++, System.currentTimeMillis());
67 sqlUpdate.bindString(i++, artistId);
68 sqlUpdate.bindString(i++, albumId);
69 // Can't use executeUpdateDelete on API < 11
70 sqlUpdate.execute();
71 long rowsUpdated = getChanges(db);
72 if (rowsUpdated == 0L) {
73 i = 1;
74 sqlInsert.clearBindings();
75 sqlInsert.bindLong(i++, System.currentTimeMillis());
76 sqlInsert.bindString(i++, artistId);
77 sqlInsert.bindString(i++, albumId);
78
79 long rowId = sqlInsert.executeInsert();
80 isSuccess = isSuccess && (rowId != -1);
81 } else {
82 isSuccess = isSuccess && (rowsUpdated == 1);
83 }
84 }
85 return isSuccess;
86 }
87
88 @Override
89 public String onGetInsertSql() {
90 return "INSERT INTO " + Tables.ARTISTS_ALBUMS + "(" +
91 ArtistAlbums.UPDATED + ", " +
92 ArtistAlbums.ARTIST_ID + ", " +
93 ArtistAlbums.ALBUM_ID +
94 ") VALUES (?, ?, ?)";
95 }
96
97 @Override
98 public String onGetUpdateSql() {
99 return "UPDATE " + Tables.ARTISTS_ALBUMS + " SET " + ArtistAlbums.UPDATED + "=? " +
100 "WHERE " + ArtistAlbums.ARTIST_ID + "=? AND " + ArtistAlbums.ALBUM_ID + "=?";
101 }
102}
0103
=== modified file 'src/com/ubuntuone/android/music/provider/dao/ArtistDao.java'
--- src/com/ubuntuone/android/music/provider/dao/ArtistDao.java 2012-11-21 15:11:58 +0000
+++ src/com/ubuntuone/android/music/provider/dao/ArtistDao.java 2012-11-28 16:23:32 +0000
@@ -1,7 +1,5 @@
1package com.ubuntuone.android.music.provider.dao;1package com.ubuntuone.android.music.provider.dao;
22
3import java.util.ArrayList;
4
5import android.content.ContentResolver;3import android.content.ContentResolver;
6import android.content.ContentValues;4import android.content.ContentValues;
7import android.database.sqlite.SQLiteDatabase;5import android.database.sqlite.SQLiteDatabase;
@@ -9,8 +7,6 @@
9import android.net.Uri;7import android.net.Uri;
108
11import com.ubuntuone.android.music.provider.MusicContentValues;9import com.ubuntuone.android.music.provider.MusicContentValues;
12import com.ubuntuone.android.music.provider.MusicContract.Albums;
13import com.ubuntuone.android.music.provider.MusicContract.ArtistAlbums;
14import com.ubuntuone.android.music.provider.MusicContract.Artists;10import com.ubuntuone.android.music.provider.MusicContract.Artists;
15import com.ubuntuone.android.music.provider.MusicDatabase;11import com.ubuntuone.android.music.provider.MusicDatabase;
16import com.ubuntuone.android.music.provider.MusicDatabase.Tables;12import com.ubuntuone.android.music.provider.MusicDatabase.Tables;
@@ -39,16 +35,6 @@
39 if (count == 0) {35 if (count == 0) {
40 resolver.insert(Artists.CONTENT_URI, values);36 resolver.insert(Artists.CONTENT_URI, values);
41 }37 }
42 // Insert artist albums.
43 ArrayList<ContentValues> albumsValues = MusicContentValues.forArtistAlbums(artist);
44 for (ContentValues albumValues : albumsValues) {
45 Uri artistAlbumUri = Artists.buildAlbumUri(
46 artist.getId(), albumValues.getAsString(Albums.ALBUM_ID));
47 int count2 = resolver.update(artistAlbumUri, albumValues, null, null);
48 if (count2 == 0) {
49 resolver.insert(artistAlbumUri, albumValues);
50 }
51 }
52 }38 }
53 39
54 /**40 /**
@@ -104,20 +90,6 @@
104 } else {90 } else {
105 isSuccess = rowsUpdated == 1;91 isSuccess = rowsUpdated == 1;
106 }92 }
107
108 // Insert artist albums.
109 sqlStmt = db.compileStatement(getSecondaryInsertSql());
110 final String artistId = artist.getId();
111 final ArrayList<String> albumIds = artist.getAlbumIds();
112 for (String albumId : albumIds) {
113 sqlStmt.clearBindings();
114 sqlStmt.bindLong(1, System.currentTimeMillis());
115 sqlStmt.bindString(2, artistId);
116 sqlStmt.bindString(3, albumId);
117 long rowId = sqlStmt.executeInsert();
118 isSuccess = isSuccess && (rowId != -1);
119 }
120
121 return isSuccess;93 return isSuccess;
122 }94 }
12395
@@ -140,17 +112,6 @@
140 "?, ?" +112 "?, ?" +
141 ")";113 ")";
142 }114 }
143
144 @Override
145 public String onGetSecondaryInsertSql() {
146 return "INSERT INTO " + Tables.ARTISTS_ALBUMS + "(" +
147
148 ArtistAlbums.UPDATED + ", " +
149 ArtistAlbums.ARTIST_ID + ", " +
150 ArtistAlbums.ALBUM_ID +
151
152 ") VALUES (?, ?, ?)";
153 }
154115
155 @Override116 @Override
156 public String onGetUpdateSql() {117 public String onGetUpdateSql() {
157118
=== modified file 'src/com/ubuntuone/android/music/provider/dao/Dao.java'
--- src/com/ubuntuone/android/music/provider/dao/Dao.java 2012-11-21 15:11:58 +0000
+++ src/com/ubuntuone/android/music/provider/dao/Dao.java 2012-11-28 16:23:32 +0000
@@ -6,7 +6,6 @@
6public abstract class Dao6public abstract class Dao
7{7{
8 private String mInsertSql;8 private String mInsertSql;
9 private String mSecondaryInsertSql;
10 private String mUdpateSql;9 private String mUdpateSql;
11 10
12 /**11 /**
@@ -22,18 +21,6 @@
22 }21 }
23 22
24 /**23 /**
25 * Performs a lazy initialization with {@code Dao#onGetSecondaryInsertSql()}.
26 *
27 * @return The cached secondary SQL INSERT string.
28 */
29 public String getSecondaryInsertSql() {
30 if (mSecondaryInsertSql == null) {
31 mSecondaryInsertSql = onGetSecondaryInsertSql();
32 }
33 return mSecondaryInsertSql;
34 }
35
36 /**
37 * Performs a lazy initialization with {@code Dao#onGetUpdateSql()}.24 * Performs a lazy initialization with {@code Dao#onGetUpdateSql()}.
38 * 25 *
39 * @return The cached SQL UPDATE string.26 * @return The cached SQL UPDATE string.
@@ -57,16 +44,6 @@
57 */44 */
58 public abstract String onGetInsertSql();45 public abstract String onGetInsertSql();
59 46
60
61 /**
62 * Override to return the secondary SQL INSERT string.
63 *
64 * @return The secondary SQL INSERT string.
65 */
66 public String onGetSecondaryInsertSql() {
67 return null;
68 }
69
70 /**47 /**
71 * Override to return the SQL UPDATE string.48 * Override to return the SQL UPDATE string.
72 * 49 *
7350
=== modified file 'src/com/ubuntuone/android/music/service/SyncService.java'
--- src/com/ubuntuone/android/music/service/SyncService.java 2012-11-22 11:17:42 +0000
+++ src/com/ubuntuone/android/music/service/SyncService.java 2012-11-28 16:23:32 +0000
@@ -42,6 +42,7 @@
42import com.ubuntuone.android.music.provider.MusicDatabase;42import com.ubuntuone.android.music.provider.MusicDatabase;
43import com.ubuntuone.android.music.provider.MusicProviderUtils;43import com.ubuntuone.android.music.provider.MusicProviderUtils;
44import com.ubuntuone.android.music.provider.dao.AlbumDao;44import com.ubuntuone.android.music.provider.dao.AlbumDao;
45import com.ubuntuone.android.music.provider.dao.ArtistAlbumDao;
45import com.ubuntuone.android.music.provider.dao.ArtistDao;46import com.ubuntuone.android.music.provider.dao.ArtistDao;
46import com.ubuntuone.android.music.provider.dao.GenreDao;47import com.ubuntuone.android.music.provider.dao.GenreDao;
47import com.ubuntuone.android.music.provider.dao.PlaylistDao;48import com.ubuntuone.android.music.provider.dao.PlaylistDao;
@@ -180,6 +181,7 @@
180 private U1ArtistListener mArtistListener = new U1ArtistListener() {181 private U1ArtistListener mArtistListener = new U1ArtistListener() {
181 private SQLiteDatabase db;182 private SQLiteDatabase db;
182 private ArtistDao artistDao = new ArtistDao();183 private ArtistDao artistDao = new ArtistDao();
184 private ArtistAlbumDao artistAlbumDao = new ArtistAlbumDao();
183 private Bundle progressBundle = new Bundle();185 private Bundle progressBundle = new Bundle();
184 private Failure failure = null;186 private Failure failure = null;
185 private int artistCount = 0;187 private int artistCount = 0;
@@ -198,9 +200,14 @@
198 public void onSuccess(U1Artist artist) {200 public void onSuccess(U1Artist artist) {
199 artistCount++;201 artistCount++;
200 if (DIRECT_SQL) {202 if (DIRECT_SQL) {
201 artistDao.updateOrInsert(db, artist);203 if (artistDao.updateOrInsert(db, artist)) {
204 if (!artistAlbumDao.updateOrInsert(db, artist)) {
205 Log.w(TAG, "Artist album not inserted: " + artist.getId());
206 }
207 }
202 } else {208 } else {
203 artistDao.updateOrInsert(mResolver, artist);209 artistDao.updateOrInsert(mResolver, artist);
210 artistAlbumDao.updateOrInsert(mResolver, artist);
204 }211 }
205 mReceiver.send(SYNC_PROGRESS, progressBundle);212 mReceiver.send(SYNC_PROGRESS, progressBundle);
206 }213 }

Subscribers

People subscribed via source and target branches

to all changes: