Added new library display type. Generified populateLibrary and Gateway listAll functionality.

This commit is contained in:
neviyn 2016-02-29 04:06:18 +00:00
parent d9cfdc37dd
commit ccc1110dd8
8 changed files with 61 additions and 17 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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<Album, List<Song>> libraryData) {
TreeMap<Album, List<Song>> sortedData = new TreeMap<>(libraryData);
private <T> void populateLibrary(Map<T, List<Song>> libraryData) {
TreeMap<T, List<Song>> 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<String, Runnable> createDisplayVariantMap() {
Map<String, Runnable> 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;
}

View File

@ -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<List<Song>> listAllSongs() {
public static <T> Optional<List<T>> listAllT(Class<T> typeClass){
try (Session session = DatabaseManager.getInstance().getSession()) {
@SuppressWarnings("unchecked")
List<Song> songs = session.createCriteria(Song.class).list();
return (songs == null || songs.isEmpty()) ? Optional.empty() : Optional.of(songs);
List<T> 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<Map<Album, List<Song>>> listAllSongsGroupedByAlbum() {
Optional<List<Song>> songList = listAllSongs();
Optional<List<Song>> 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<Map<Artist, List<Song>>> listAllSongsGroupedByArtist() {
Optional<List<Song>> 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.
*

View File

@ -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;

View File

@ -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;

View File

@ -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<Artist, List<Song>> 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<Song> result = Gateway.listAllSongs().get();
assertTrue(Gateway.listAllT(Song.class).isPresent());
List<Song> 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