String buildMap(MapType maptype, String accessToken, String selection) { StoreHelper storeHelper = new StoreHelper(); try { // Start by getting user id DeezerClient dzClient = new DeezerClient(DEEZER_API_URL, accessToken); User me = dzClient.getMyProfile(); Collection<String> favIds = new HashSet<String>(); switch (maptype) { case ALBUM: Collection<Album> myFavAlbums = dzClient.getMyFavoriteAlbums(); for (Album favAlbum : myFavAlbums) { favIds.add(favAlbum.id); } addUserByAlbum(storeHelper, dzClient, me, favIds, 1); break; case ARTIST: Collection<Artist> myFavArtists = dzClient.getMyFavoriteArtists(); for (Artist favArtist : myFavArtists) { favIds.add(favArtist.id); } addUserByArtist(storeHelper, dzClient, me, favIds, 1); break; case RELARTIST: Collection<Artist> favArtists = dzClient.getMyFavoriteArtists(); for (Artist favArtist : favArtists) { favIds.add(favArtist.id); } int i = 0; Iterator<Artist> it = favArtists.iterator(); while (it.hasNext() && i < 10) { Artist a = it.next(); i++; LOG.debug("Add related artists for user favorite artist = {}", a); addRelatedArtists(storeHelper, dzClient, a, favIds, 1); } break; case RELARTISTSEL: String[] selectedIds = selection.split(","); Collections.addAll(favIds, selectedIds); for (String artistId : selectedIds) { Artist artist = dzClient.getArtistInformation(artistId); LOG.debug("Add related artists for user favorite artist = {}", artist); addRelatedArtists(storeHelper, dzClient, artist, favIds, 1); } break; } } catch (Exception e) { LOG.error(e.getMessage(), e); return StoreHelper.ErrorToJson(e); } return storeHelper.toJson(); }
public static void addUserByArtist( StoreHelper storeHelper, DeezerClient dzClient, User user, Collection<String> favIds, int level) throws JMIException, JsonProcessingException, IOException { // Add the user given in paramaters to the map entities list if (storeHelper.getEntity(user.id) != null) { LOG.debug("user {} was already added, skipping...", user); return; } LOG.info("Add user {} to the entities list", user); Entity ent = storeHelper.addEntity(user.id); ent.addProperty("name", user.name); ent.addProperty("url", user.link); // Get user favorite albums Collection<Artist> favArtists = dzClient.getUserFavoriteArtists(user.id, new NameValuePair("nb_items", "10")); // Iterate through artits and add them as user attributes. for (Artist favArtist : favArtists) { Attribute att = storeHelper.getAttribute(favArtist.id); // If the album was not already stored as attribute create it // And go through this album fans if (att == null) { att = storeHelper.addAttribute(favArtist.id); att.addProperty("name", favArtist.name); att.addProperty("image", favArtist.picture); att.addProperty("url", favArtist.link); // If this artist is in my favorite list set infavlist to true otherwise to false // It is usefull for the menu item state at the map initialization att.addProperty("infavlist", favIds.contains(favArtist.id)); if (level > 0) { // For each album get a list of fans Collection<User> fans = dzClient.getArtistFans(favArtist.id, new NameValuePair("nb_items", "50")); for (User fan : fans) { addUserByArtist(storeHelper, dzClient, fan, favIds, level - 1); } } } ent.addAttribute(att, 1); } }
public static void addRelatedArtists( StoreHelper storeHelper, DeezerClient dzClient, Artist artist, Collection<String> favIds, int level) throws JsonProcessingException, JMIException, IOException { /* if(storeHelper.getEntity(artist.id) != null) { LOG.debug("Artist {} was already added, skipping...", artist); return; } */ LOG.info("Add Artist {} to the attributes list", artist); Attribute att = storeHelper.addAttribute(artist.id); att.addProperty("name", artist.name); att.addProperty("image", artist.picture); att.addProperty("url", artist.link); att.addProperty("infavlist", favIds.contains(artist.id)); // Ask for this artist related artists Collection<Artist> relatedArtists = dzClient.getRelatedArtists(artist.id, new NameValuePair("nb_items", "20")); // Iterate through related artists and add them as the artist entities. for (Artist relatedArtist : relatedArtists) { Entity ent = storeHelper.getEntity(relatedArtist.id); // If the artist was not already stored as entity create it if (ent == null) { ent = storeHelper.addEntity(relatedArtist.id); ent.addProperty("name", relatedArtist.name); ent.addProperty("image", relatedArtist.picture); ent.addProperty("url", relatedArtist.link); // If this artist is in my favorite list set infavlist to true otherwise to false // It is usefull for the menu item state at the map initialization phase ent.addProperty("infavlist", favIds.contains(relatedArtist.id)); if (level > 0) { // Call that function again for that related artist addRelatedArtists(storeHelper, dzClient, relatedArtist, favIds, level - 1); } } ent.addAttribute(att, 1); } }