diff --git a/src/main/java/musicplayer/ConfigManager.java b/src/main/java/musicplayer/ConfigManager.java index c5ba5b4..25e2d6a 100644 --- a/src/main/java/musicplayer/ConfigManager.java +++ b/src/main/java/musicplayer/ConfigManager.java @@ -1,6 +1,9 @@ package musicplayer; -import java.io.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; diff --git a/src/main/java/musicplayer/LibraryConfigGUI.java b/src/main/java/musicplayer/LibraryConfigGUI.java index e6f6436..b285d43 100644 --- a/src/main/java/musicplayer/LibraryConfigGUI.java +++ b/src/main/java/musicplayer/LibraryConfigGUI.java @@ -3,7 +3,7 @@ package musicplayer; import musicplayer.swingmodels.LibraryListModel; import javax.swing.*; -import java.awt.BorderLayout; +import java.awt.*; import java.io.File; import java.util.List; diff --git a/src/main/java/musicplayer/LibraryUtils.java b/src/main/java/musicplayer/LibraryUtils.java index 7945fc4..bfd6b42 100644 --- a/src/main/java/musicplayer/LibraryUtils.java +++ b/src/main/java/musicplayer/LibraryUtils.java @@ -10,7 +10,7 @@ import org.jaudiotagger.audio.exceptions.ReadOnlyFileException; import org.jaudiotagger.tag.Tag; import org.jaudiotagger.tag.TagException; -import java.io.*; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; diff --git a/src/main/java/musicplayer/PlayerGUI.java b/src/main/java/musicplayer/PlayerGUI.java index 7fffe9d..415197d 100644 --- a/src/main/java/musicplayer/PlayerGUI.java +++ b/src/main/java/musicplayer/PlayerGUI.java @@ -90,8 +90,8 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf * * @param libraryData Map of albums with a lists of associated songs. */ - private void populateLibrary(Map> libraryData) { - TreeMap> sortedData = new TreeMap<>(libraryData); + private void populateLibrary(Map> libraryData) { + TreeMap> sortedData = new TreeMap<>(libraryData); DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode()); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot(); sortedData.forEach((k, v) -> { @@ -236,8 +236,9 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf */ private Map createDisplayVariantMap() { Map value = new HashMap<>(); - value.put("Song", () -> Gateway.listAllSongs().ifPresent(this::populateLibrary)); + value.put("Song", () -> Gateway.listAllT(Song.class).ifPresent(this::populateLibrary)); value.put("Album/Song", () -> Gateway.listAllSongsGroupedByAlbum().ifPresent(this::populateLibrary)); + value.put("Artist/Song", () -> Gateway.listAllSongsGroupedByArtist().ifPresent(this::populateLibrary)); return value; } diff --git a/src/main/java/musicplayer/db/Gateway.java b/src/main/java/musicplayer/db/Gateway.java index 7a54952..43cc75c 100644 --- a/src/main/java/musicplayer/db/Gateway.java +++ b/src/main/java/musicplayer/db/Gateway.java @@ -43,13 +43,15 @@ public class Gateway { } /** - * @return List of all songs currently stored in the database. + * List all items in the database of a certain type. + * @param typeClass Class representing the type of items to find. + * @return List of all items in the database of class typeClass. */ - public static Optional> listAllSongs() { + public static Optional> listAllT(Class typeClass){ try (Session session = DatabaseManager.getInstance().getSession()) { @SuppressWarnings("unchecked") - List songs = session.createCriteria(Song.class).list(); - return (songs == null || songs.isEmpty()) ? Optional.empty() : Optional.of(songs); + List output = session.createCriteria(typeClass).list(); + return (output == null || output.isEmpty()) ? Optional.empty() : Optional.of(output); } } @@ -57,12 +59,22 @@ public class Gateway { * @return All songs currently in the database, grouped by album. */ public static Optional>> listAllSongsGroupedByAlbum() { - Optional> songList = listAllSongs(); + Optional> songList = listAllT(Song.class); return (songList.isPresent()) ? Optional.of(songList.get().stream().collect(Collectors.groupingBy(Song::getAlbum))) : Optional.empty(); } + /** + * @return All songs currently in the database, grouped by artist. + */ + public static Optional>> listAllSongsGroupedByArtist() { + Optional> songList = listAllT(Song.class); + return (songList.isPresent()) ? + Optional.of(songList.get().stream().collect(Collectors.groupingBy(Song::getArtist))) + : Optional.empty(); + } + /** * Add a new song to the database. * diff --git a/src/main/java/musicplayer/model/ExtractedMetadata.java b/src/main/java/musicplayer/model/ExtractedMetadata.java index 0a5c118..adac0e3 100644 --- a/src/main/java/musicplayer/model/ExtractedMetadata.java +++ b/src/main/java/musicplayer/model/ExtractedMetadata.java @@ -6,7 +6,10 @@ import org.jaudiotagger.tag.Tag; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; -import java.io.*; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Optional; diff --git a/src/main/java/musicplayer/swingmodels/PlaylistTableModel.java b/src/main/java/musicplayer/swingmodels/PlaylistTableModel.java index 351c8b3..2ba3696 100644 --- a/src/main/java/musicplayer/swingmodels/PlaylistTableModel.java +++ b/src/main/java/musicplayer/swingmodels/PlaylistTableModel.java @@ -3,7 +3,9 @@ package musicplayer.swingmodels; import musicplayer.model.Song; import javax.swing.table.AbstractTableModel; -import java.util.*; +import java.util.Collections; +import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.IntStream; diff --git a/src/test/java/musicplayer/db/GatewayTest.java b/src/test/java/musicplayer/db/GatewayTest.java index f402fd3..6441264 100644 --- a/src/test/java/musicplayer/db/GatewayTest.java +++ b/src/test/java/musicplayer/db/GatewayTest.java @@ -16,7 +16,8 @@ import java.io.File; import java.util.List; import java.util.Map; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; public class GatewayTest { @@ -63,6 +64,28 @@ public class GatewayTest { assertEquals(testArtist, retrievedArtist); } + @Test + public void testListAllSongsGroupedByArtist() throws Exception { + Artist artist1 = new Artist("Test 1"); + Artist artist2 = new Artist("Test 2"); + Song song1 = new Song("1", "1", "s1", artist1, new Album("a"), "", ""); + Song song2 = new Song("2", "1", "s2", artist1, new Album("b"), "", ""); + Song song3 = new Song("1", "1", "t1", artist2, new Album("c"), "", ""); + session.beginTransaction(); + session.save(song1); + session.save(song2); + session.save(song3); + session.getTransaction().commit(); + assertTrue(Gateway.listAllSongsGroupedByArtist().isPresent()); + Map> result = Gateway.listAllSongsGroupedByArtist().get(); + assertTrue(result.size() == 2); + assertTrue(result.get(artist1).size() == 2); + assertTrue(result.get(artist2).size() == 1); + assertTrue(result.get(artist1).contains(song1)); + assertTrue(result.get(artist1).contains(song2)); + assertTrue(result.get(artist2).contains(song3)); + } + @Test public void testGetOneArtistEmptyDB() throws Exception { assertTrue(!Gateway.getOneArtist("").isPresent()); @@ -78,8 +101,8 @@ public class GatewayTest { session.save(song2); session.save(song3); session.getTransaction().commit(); - assertTrue(Gateway.listAllSongs().isPresent()); - List result = Gateway.listAllSongs().get(); + assertTrue(Gateway.listAllT(Song.class).isPresent()); + List result = Gateway.listAllT(Song.class).get(); assertTrue(result.size() == 3); assertTrue(result.contains(song1)); assertTrue(result.contains(song2)); @@ -88,7 +111,7 @@ public class GatewayTest { @Test public void testListAllSongsEmptyDB() throws Exception { - assertTrue(!Gateway.listAllSongs().isPresent()); + assertTrue(!Gateway.listAllT(Song.class).isPresent()); } @Test