Changed library population to use song list fields contained in database rather than directly processed database queries.

This commit is contained in:
neviyn 2016-03-09 15:52:35 +00:00
parent ebc4852cfc
commit c7eb7fd024
7 changed files with 39 additions and 71 deletions

View File

@ -8,6 +8,8 @@ import musicplayer.callbacks.PlayerCallbackInterface;
import musicplayer.db.DatabaseManager; import musicplayer.db.DatabaseManager;
import musicplayer.db.Gateway; import musicplayer.db.Gateway;
import musicplayer.model.Album; import musicplayer.model.Album;
import musicplayer.model.Artist;
import musicplayer.model.HasSongs;
import musicplayer.model.Song; import musicplayer.model.Song;
import musicplayer.swingmodels.PlaylistTableModel; import musicplayer.swingmodels.PlaylistTableModel;
import musicplayer.util.LibraryUtils; import musicplayer.util.LibraryUtils;
@ -100,20 +102,14 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
libraryView.removeAll(); libraryView.removeAll();
} }
/** private <T extends HasSongs & Comparable<T>> void populateLibraryWithGroupedSongs(List<T> libraryData){
* Populate the library with songs grouped by album.
*
* @param libraryData Map of albums with a lists of associated songs.
*/
private <T> void populateLibrary(Map<T, List<Song>> libraryData) {
TreeMap<T, List<Song>> sortedData = new TreeMap<>(libraryData);
DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode()); DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode());
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot(); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot();
sortedData.forEach((k, v) -> { Collections.sort(libraryData);
DefaultMutableTreeNode albumNode = new DefaultMutableTreeNode(k); libraryData.forEach(x -> {
addNodeToTreeModel(model, parentNode, albumNode); DefaultMutableTreeNode outerNode = new DefaultMutableTreeNode(x);
Collections.sort(v); addNodeToTreeModel(model, parentNode, outerNode);
v.forEach(x -> addNodeToTreeModel(model, albumNode, new DefaultMutableTreeNode(x))); x.getSongs().forEach(y -> addNodeToTreeModel(model, outerNode, new DefaultMutableTreeNode(y)));
}); });
libraryView.setModel(model); libraryView.setModel(model);
} }
@ -123,7 +119,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
* *
* @param libraryData List of songs. * @param libraryData List of songs.
*/ */
private void populateLibrary(List<Song> libraryData) { private void populateLibraryWithSongsOnly(List<Song> libraryData) {
DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode()); DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode());
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot(); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot();
Collections.sort(libraryData); Collections.sort(libraryData);
@ -251,9 +247,9 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
*/ */
private Map<String, Runnable> createDisplayVariantMap() { private Map<String, Runnable> createDisplayVariantMap() {
Map<String, Runnable> value = new HashMap<>(); Map<String, Runnable> value = new HashMap<>();
value.put("Song", () -> Gateway.listAllT(Song.class).ifPresent(this::populateLibrary)); value.put("Song", () -> Gateway.listAllT(Song.class).ifPresent(this::populateLibraryWithSongsOnly));
value.put("Album/Song", () -> Gateway.listAllSongsGroupedByAlbum().ifPresent(this::populateLibrary)); value.put("Album/Song", () -> Gateway.listAllT(Album.class).ifPresent(this::populateLibraryWithGroupedSongs));
value.put("Artist/Song", () -> Gateway.listAllSongsGroupedByArtist().ifPresent(this::populateLibrary)); value.put("Artist/Song", () -> Gateway.listAllT(Artist.class).ifPresent(this::populateLibraryWithGroupedSongs));
return value; return value;
} }

View File

@ -1,17 +1,14 @@
package musicplayer.db; package musicplayer.db;
import musicplayer.util.AlbumArtExtractor;
import musicplayer.model.*; import musicplayer.model.*;
import musicplayer.util.AlbumArtExtractor;
import org.hibernate.Criteria; import org.hibernate.Criteria;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.criterion.Restrictions; import org.hibernate.criterion.Restrictions;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
/** /**
* Contains database queries. * Contains database queries.
@ -67,24 +64,6 @@ public class Gateway {
} }
} }
/**
* @return All songs currently in the database, grouped by album.
*/
public static Optional<Map<Album, List<Song>>> listAllSongsGroupedByAlbum() {
Optional<List<Album>> albumList = listAllT(Album.class);
return (albumList.isPresent()) ? Optional.of(albumList.get().stream().collect(Collectors.toMap(x -> x, x -> new ArrayList<>(x.getSongs()))))
: Optional.empty();
}
/**
* @return All songs currently in the database, grouped by artist.
*/
public static Optional<Map<Artist, List<Song>>> listAllSongsGroupedByArtist() {
Optional<List<Artist>> artistListList = listAllT(Artist.class);
return (artistListList.isPresent()) ? Optional.of(artistListList.get().stream().collect(Collectors.toMap(x -> x, x -> new ArrayList<>(x.getSongs()))))
: Optional.empty();
}
/** /**
* Add a new song to the database. * Add a new song to the database.
* If the song already exists it will be updated instead. * If the song already exists it will be updated instead.

View File

@ -10,7 +10,7 @@ import java.util.Optional;
import java.util.Set; import java.util.Set;
@Entity @Entity
public class Album implements Comparable<Album>, IDBType { public class Album implements Comparable<Album>, IDBType, HasSongs {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)

View File

@ -4,7 +4,7 @@ import javax.persistence.*;
import java.util.Set; import java.util.Set;
@Entity @Entity
public class Artist implements Comparable<Artist>, IDBType { public class Artist implements Comparable<Artist>, IDBType, HasSongs {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)

View File

@ -3,16 +3,7 @@ package musicplayer.model;
import org.jaudiotagger.tag.FieldKey; import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.Tag; import org.jaudiotagger.tag.Tag;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
/** /**
* Internal class representing metadata extracted from a music file. * Internal class representing metadata extracted from a music file.

View File

@ -0,0 +1,8 @@
package musicplayer.model;
import java.util.Set;
public interface HasSongs {
Set<Song> getSongs();
}

View File

@ -14,7 +14,6 @@ import org.junit.Test;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -76,14 +75,14 @@ public class GatewayTest {
session.save(song2); session.save(song2);
session.save(song3); session.save(song3);
session.getTransaction().commit(); session.getTransaction().commit();
assertTrue(Gateway.listAllSongsGroupedByArtist().isPresent()); List<Artist> retrievedArtists = Gateway.listAllT(Artist.class).get();
Map<Artist, List<Song>> result = Gateway.listAllSongsGroupedByArtist().get(); int index1 = retrievedArtists.indexOf(artist1);
assertTrue(result.size() == 2); int index2 = retrievedArtists.indexOf(artist2);
assertTrue(result.get(artist1).size() == 2); assertTrue(retrievedArtists.get(index1).getSongs().size() == 2);
assertTrue(result.get(artist2).size() == 1); assertTrue(retrievedArtists.get(index2).getSongs().size() == 1);
assertTrue(result.get(artist1).contains(song1)); assertTrue(retrievedArtists.get(index1).getSongs().contains(song1));
assertTrue(result.get(artist1).contains(song2)); assertTrue(retrievedArtists.get(index1).getSongs().contains(song2));
assertTrue(result.get(artist2).contains(song3)); assertTrue(retrievedArtists.get(index2).getSongs().contains(song3));
} }
@Test @Test
@ -126,19 +125,14 @@ public class GatewayTest {
session.save(song2); session.save(song2);
session.save(song3); session.save(song3);
session.getTransaction().commit(); session.getTransaction().commit();
assertTrue(Gateway.listAllSongsGroupedByAlbum().isPresent()); List<Album> retrievedAlbums = Gateway.listAllT(Album.class).get();
Map<Album, List<Song>> result = Gateway.listAllSongsGroupedByAlbum().get(); int index1 = retrievedAlbums.indexOf(album1);
assertTrue(result.size() == 2); int index2 = retrievedAlbums.indexOf(album2);
assertTrue(result.get(album1).size() == 2); assertTrue(retrievedAlbums.get(index1).getSongs().size() == 2);
assertTrue(result.get(album2).size() == 1); assertTrue(retrievedAlbums.get(index2).getSongs().size() == 1);
assertTrue(result.get(album1).contains(song1)); assertTrue(retrievedAlbums.get(index1).getSongs().contains(song1));
assertTrue(result.get(album1).contains(song2)); assertTrue(retrievedAlbums.get(index1).getSongs().contains(song2));
assertTrue(result.get(album2).contains(song3)); assertTrue(retrievedAlbums.get(index2).getSongs().contains(song3));
}
@Test
public void testListAllSongsGroupedByAlbumEmptyDB() throws Exception {
assertTrue(!Gateway.listAllSongsGroupedByAlbum().isPresent());
} }
@Test @Test