static { mSelf.setName("Myself"); mSelf.setIsOffline(true); }
public List storeRecords( JsonObject object, int resultType, int requestType, boolean isBackgroundRequest, QueryParams params) throws IOException { List results = new ArrayList(); JsonElement elements = object.get("images"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); Image image = (Image) mCache.get(TYPE_IMAGES).get(id); if (image == null) { String url = getAsString(o, "url"); int width = getAsInt(o, "width"); int height = getAsInt(o, "height"); image = Image.get(url, true, width, height); mCache.get(TYPE_IMAGES).put(id, image); } if (resultType == TYPE_IMAGES) { results.add(image); } } } } elements = object.get("artists"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); Artist artist = (Artist) mCache.get(TYPE_ARTISTS).get(id); if (artist == null) { String name = getAsString(o, "name"); String wiki = getAsString(o, "wikiabstract"); artist = Artist.get(name); artist.setBio(new ListItemString(wiki)); JsonElement images = get(o, "images"); if (images instanceof JsonArray && ((JsonArray) images).size() > 0) { String imageId = ((JsonArray) images).get(0).getAsString(); Image image = (Image) findRecord(imageId, TYPE_IMAGES, isBackgroundRequest); artist.setImage(image); } mCache.get(TYPE_ARTISTS).put(id, artist); } if (requestType == InfoRequestData.INFOREQUESTDATA_TYPE_ARTISTS_ALBUMS) { JsonElement rawAlbums = get(o, "albums"); if (rawAlbums instanceof JsonObject) { results.addAll( storeRecords((JsonObject) rawAlbums, TYPE_ALBUMS, isBackgroundRequest)); } } else if (requestType == InfoRequestData.INFOREQUESTDATA_TYPE_ARTISTS_TOPHITS) { JsonElement rawTopHits = get(o, "topHits"); if (rawTopHits instanceof JsonObject) { results.addAll( storeRecords((JsonObject) rawTopHits, TYPE_TRACKS, isBackgroundRequest)); } } if (resultType == TYPE_ARTISTS) { results.add(artist); } } } } elements = object.get("albums"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); Album album = (Album) mCache.get(TYPE_ALBUMS).get(id); if (album == null) { String name = getAsString(o, "name"); String artistId = getAsString(o, "artist"); Artist artist = (Artist) findRecord(artistId, TYPE_ARTISTS, isBackgroundRequest); album = Album.get(name, artist); JsonElement images = get(o, "images"); if (images instanceof JsonArray && ((JsonArray) images).size() > 0) { String imageId = ((JsonArray) images).get(0).getAsString(); Image image = (Image) findRecord(imageId, TYPE_IMAGES, isBackgroundRequest); album.setImage(image); } mCache.get(TYPE_ALBUMS).put(id, album); } if (requestType == InfoRequestData.INFOREQUESTDATA_TYPE_ALBUMS_TRACKS) { JsonElement rawTracks = get(o, "tracks"); if (rawTracks instanceof JsonObject) { results.addAll( storeRecords((JsonObject) rawTracks, TYPE_TRACKS, isBackgroundRequest)); } } if (resultType == TYPE_ALBUMS) { results.add(album); } } } } elements = object.get("tracks"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); Query query = (Query) mCache.get(TYPE_TRACKS).get(id); if (query == null) { String name = getAsString(o, "name"); String artistId = getAsString(o, "artist"); Artist artist = (Artist) findRecord(artistId, TYPE_ARTISTS, isBackgroundRequest); query = Query.get(name, null, artist.getName(), false, true); mCache.get(TYPE_TRACKS).put(id, query); } if (resultType == TYPE_TRACKS) { results.add(query); } } } } elements = object.get("users"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); User user = User.get(id); String name = getAsString(o, "name"); user.setName(name); String about = getAsString(o, "about"); user.setAbout(about); int followersCount = getAsInt(o, "followersCount"); user.setFollowersCount(followersCount); int followCount = getAsInt(o, "followCount"); user.setFollowCount(followCount); String nowplayingId = getAsString(o, "nowplaying"); if (nowplayingId != null) { Query nowplaying = (Query) findRecord(nowplayingId, TYPE_TRACKS, isBackgroundRequest); user.setNowPlaying(nowplaying); } String nowplayingtimestamp = getAsString(o, "nowplayingtimestamp"); user.setNowPlayingTimeStamp(ISO8601Utils.parse(nowplayingtimestamp)); String avatar = getAsString(o, "avatar"); if (avatar != null) { Image image = (Image) findRecord(avatar, TYPE_IMAGES, isBackgroundRequest); user.setImage(image); } if (requestType == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_PLAYLISTS) { JsonElement rawPlaylists = get(o, "playlists"); if (rawPlaylists instanceof JsonObject) { List playlists = storeRecords((JsonObject) rawPlaylists, TYPE_PLAYLISTS, isBackgroundRequest); user.setPlaylists(playlists); } } else if (requestType == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_LOVEDITEMS) { JsonElement rawLovedItems = get(o, "lovedItems"); if (rawLovedItems instanceof JsonObject) { List playlists = storeRecords((JsonObject) rawLovedItems, TYPE_PLAYLISTS, isBackgroundRequest); if (playlists != null && playlists.size() > 0) { user.setFavorites((Playlist) playlists.get(0)); } } } else if (requestType == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_LOVEDALBUMS) { JsonElement rawLovedAlbums = get(o, "lovedAlbums"); if (rawLovedAlbums instanceof JsonObject) { List albums = storeRecords((JsonObject) rawLovedAlbums, TYPE_ALBUMS, isBackgroundRequest); user.setStarredAlbums(albums); } } else if (requestType == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_LOVEDARTISTS) { JsonElement rawLovedArtists = get(o, "lovedArtists"); if (rawLovedArtists instanceof JsonObject) { List artists = storeRecords((JsonObject) rawLovedArtists, TYPE_ARTISTS, isBackgroundRequest); user.setStarredArtists(artists); } } else if (requestType == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_PLAYBACKLOG) { JsonElement rawPlaybackLog = get(o, "playbacklog"); if (rawPlaybackLog instanceof JsonObject) { List playlists = storeRecords((JsonObject) rawPlaybackLog, TYPE_PLAYLISTS, isBackgroundRequest); if (playlists != null && playlists.size() > 0) { user.setPlaybackLog((Playlist) playlists.get(0)); } } } else if (requestType == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_FOLLOWS || requestType == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_FOLLOWERS) { boolean isFollows = requestType == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_FOLLOWS; JsonElement rawFollows = get(o, isFollows ? "follows" : "followers"); if (rawFollows instanceof JsonObject) { JsonObject follows = (JsonObject) rawFollows; storeRecords(follows, -1, isBackgroundRequest); JsonElement relationships = get(follows, "relationships"); if (relationships instanceof JsonArray) { TreeMap<User, String> followsMap = new TreeMap<>(new AlphaComparator()); for (JsonElement relationship : (JsonArray) relationships) { JsonObject relationshipObj = (JsonObject) relationship; String relationshipId = getAsString(relationshipObj, "id"); String userId = getAsString(relationshipObj, isFollows ? "targetUser" : "user"); User followedUser = (User) findRecord(userId, TYPE_USERS, isBackgroundRequest); followsMap.put(followedUser, relationshipId); } if (isFollows) { user.setFollowings(followsMap); } else { user.setFollowers(followsMap); } } } } else if (requestType == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_PLAYBACKLOG) { JsonElement rawPlaybackLog = get(o, "playbacklog"); if (rawPlaybackLog instanceof JsonObject) { List playlists = storeRecords((JsonObject) rawPlaybackLog, TYPE_PLAYLISTS, isBackgroundRequest); if (playlists != null && playlists.size() > 0) { user.setPlaybackLog((Playlist) playlists.get(0)); } } } mCache.get(TYPE_USERS).put(id, user); if (resultType == TYPE_USERS) { results.add(user); } } } } elements = object.get("playlistEntries"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); Query query = (Query) mCache.get(TYPE_PLAYLISTENTRIES).get(id); if (query == null) { String trackId = getAsString(o, "track"); query = (Query) findRecord(trackId, TYPE_TRACKS, isBackgroundRequest); mCache.get(TYPE_PLAYLISTENTRIES).put(id, query); } if (resultType == TYPE_PLAYLISTENTRIES) { results.add(query); } } } } elements = object.get("playlists"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); String title = getAsString(o, "title"); String currentrevision = getAsString(o, "currentrevision"); String localId = DatabaseHelper.get().getPlaylistLocalId(id); if (localId == null) { localId = id; } Playlist playlist = null; if (requestType == InfoRequestData.INFOREQUESTDATA_TYPE_PLAYLISTS_PLAYLISTENTRIES) { JsonElement rawEntries = get(o, "playlistEntries"); if (rawEntries instanceof JsonObject) { List<PlaylistEntry> entries = storeRecords((JsonObject) rawEntries, TYPE_PLAYLISTENTRIES, isBackgroundRequest); if (entries != null) { playlist = Playlist.fromEntriesList(localId, null, null, entries); playlist.setFilled(true); } } } else { JsonElement entryIds = o.get("playlistEntries"); if (entryIds instanceof JsonArray) { List<Query> queries = new ArrayList<>(); for (JsonElement entryId : (JsonArray) entryIds) { Query query = (Query) findRecord( entryId.getAsString(), TYPE_PLAYLISTENTRIES, isBackgroundRequest); queries.add(query); } playlist = Playlist.fromQueryList(localId, null, null, queries); playlist.setFilled(true); } } if (playlist == null) { playlist = Playlist.get(localId); } playlist.setName(title); playlist.setCurrentRevision(currentrevision); playlist.setHatchetId(id); JsonElement popularArtists = get(o, "popularArtists"); if (popularArtists instanceof JsonArray) { ArrayList<String> topArtistNames = new ArrayList<>(); for (JsonElement popularArtist : (JsonArray) popularArtists) { String artistId = popularArtist.getAsString(); Artist artist = (Artist) findRecord(artistId, TYPE_ARTISTS, isBackgroundRequest); if (artist != null) { topArtistNames.add(artist.getName()); } } playlist.setTopArtistNames(topArtistNames.toArray(new String[topArtistNames.size()])); } mCache.get(TYPE_PLAYLISTS).put(id, playlist); if (resultType == TYPE_PLAYLISTS) { results.add(playlist); } } } } elements = object.get("playbacklogEntries"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); Query query = (Query) mCache.get(TYPE_PLAYLISTENTRIES).get(id); if (query == null) { String trackId = getAsString(o, "track"); query = (Query) findRecord(trackId, TYPE_TRACKS, isBackgroundRequest); mCache.get(TYPE_PLAYLISTENTRIES).put(id, query); } if (resultType == TYPE_PLAYLISTENTRIES) { results.add(query); } } } } elements = object.get("playbacklogs"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); JsonArray playbacklogEntries = get(o, "playbacklogEntries").getAsJsonArray(); ArrayList<Query> queries = new ArrayList<>(); for (JsonElement entry : playbacklogEntries) { String entryId = entry.getAsString(); Query query = (Query) findRecord(entryId, TYPE_PLAYLISTENTRIES, isBackgroundRequest); queries.add(query); } Playlist playlist = Playlist.fromQueryList( TomahawkMainActivity.getLifetimeUniqueStringId(), "Playbacklog", null, queries); playlist.setHatchetId(id); playlist.setFilled(true); mCache.get(TYPE_PLAYLISTS).put(id, playlist); if (resultType == TYPE_PLAYLISTS) { results.add(playlist); } } } } elements = object.get("socialActions"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); SocialAction socialAction = (SocialAction) mCache.get(TYPE_SOCIALACTIONS).get(id); if (socialAction == null) { socialAction = SocialAction.get(id); String action = getAsString(o, "action"); socialAction.setAction(action); String date = getAsString(o, "date"); socialAction.setDate(ISO8601Utils.parse(date)); String actionType = getAsString(o, "type"); socialAction.setType(actionType); String trackId = getAsString(o, "track"); if (trackId != null) { Query query = (Query) findRecord(trackId, TYPE_TRACKS, isBackgroundRequest); socialAction.setQuery(query); } String artistId = getAsString(o, "artist"); if (artistId != null) { Artist artist = (Artist) findRecord(artistId, TYPE_ARTISTS, isBackgroundRequest); socialAction.setArtist(artist); } String albumId = getAsString(o, "album"); if (albumId != null) { Album album = (Album) findRecord(albumId, TYPE_ALBUMS, isBackgroundRequest); socialAction.setAlbum(album); } String userId = getAsString(o, "user"); if (userId != null) { User user = (User) findRecord(userId, TYPE_USERS, isBackgroundRequest); socialAction.setUser(user); } String targetId = getAsString(o, "target"); if (targetId != null) { User target = (User) findRecord(targetId, TYPE_USERS, isBackgroundRequest); socialAction.setTarget(target); } String playlistId = getAsString(o, "playlist"); if (playlistId != null) { Playlist playlist = (Playlist) findRecord(playlistId, TYPE_PLAYLISTS, isBackgroundRequest); socialAction.setPlaylist(playlist); } mCache.get(TYPE_SOCIALACTIONS).put(id, socialAction); } if (resultType == TYPE_SOCIALACTIONS) { results.add(socialAction); } } } if (params != null) { User user = (User) findRecord(params.userid, TYPE_USERS, false); if (user != null) { if (HatchetInfoPlugin.HATCHET_SOCIALACTION_PARAMTYPE_FRIENDSFEED.equals(params.type)) { user.setFriendsFeed(results, params.before_date); } else { user.setSocialActions(results, params.before_date); } } } } elements = object.get("searchResults"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); SearchResult searchResult = (SearchResult) mCache.get(TYPE_SEARCHRESULTS).get(id); if (searchResult == null) { float score = getAsFloat(o, "score"); String trackId = getAsString(o, "track"); if (trackId != null) { Query query = (Query) findRecord(trackId, TYPE_TRACKS, isBackgroundRequest); searchResult = new SearchResult(score, query); } String artistId = getAsString(o, "artist"); if (artistId != null) { Artist artist = (Artist) findRecord(artistId, TYPE_ARTISTS, isBackgroundRequest); searchResult = new SearchResult(score, artist); } String albumId = getAsString(o, "album"); if (albumId != null) { Album album = (Album) findRecord(albumId, TYPE_ALBUMS, isBackgroundRequest); searchResult = new SearchResult(score, album); } String userId = getAsString(o, "user"); if (userId != null) { User user = (User) findRecord(userId, TYPE_USERS, isBackgroundRequest); searchResult = new SearchResult(score, user); } String playlistId = getAsString(o, "playlist"); if (playlistId != null) { Playlist playlist = (Playlist) findRecord(playlistId, TYPE_PLAYLISTS, isBackgroundRequest); searchResult = new SearchResult(score, playlist); } if (searchResult == null) { throw new IOException("searchResult contained no actual result object!"); } mCache.get(TYPE_SEARCHRESULTS).put(id, searchResult); } if (resultType == TYPE_SEARCHRESULTS) { results.add(searchResult); } } } } elements = object.get("searches"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); JsonArray rawSearchResults = get(o, "searchResults").getAsJsonArray(); ArrayList<SearchResult> searchResults = new ArrayList<>(); for (JsonElement rawSearchResult : rawSearchResults) { String resultId = rawSearchResult.getAsString(); SearchResult searchResult = (SearchResult) findRecord(resultId, TYPE_SEARCHRESULTS, isBackgroundRequest); searchResults.add(searchResult); } Search search = new Search(searchResults); mCache.get(TYPE_SEARCHES).put(id, search); if (resultType == TYPE_SEARCHES) { results.add(search); } } } } elements = object.get("relationships"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); String type = getAsString(o, "type"); if (type.equals(HatchetInfoPlugin.HATCHET_RELATIONSHIPS_TYPE_LOVE)) { String userId = getAsString(o, "user"); User user = (User) findRecord(userId, TYPE_USERS, isBackgroundRequest); String trackId = getAsString(o, "targetTrack"); if (trackId != null) { Query query = (Query) findRecord(trackId, TYPE_TRACKS, isBackgroundRequest); user.putRelationShipId(query, id); } String albumId = getAsString(o, "targetAlbum"); if (albumId != null) { Album album = (Album) findRecord(albumId, TYPE_ALBUMS, isBackgroundRequest); user.putRelationShipId(album, id); } String artistId = getAsString(o, "targetArtist"); if (artistId != null) { Artist artist = (Artist) findRecord(artistId, TYPE_ARTISTS, isBackgroundRequest); user.putRelationShipId(artist, id); } } } } } return results; }