Fixed updating existing song data not working properly.

This commit is contained in:
neviyn 2016-03-18 20:00:09 +00:00
parent e8633aa0e1
commit 7e1d9aac59
3 changed files with 112 additions and 20 deletions

View File

@ -102,6 +102,18 @@ class HibernateDatabase implements IDatabase{
}
}
/**
* @param target Data of song to find in database.
* @return Song found with metadata or Optional.empty()
*/
private Optional<Song> getOneSong(Song target){
try (Session session = getSession()) {
Song song = (Song)session.createCriteria(Song.class)
.add(Restrictions.eq("songFile", target.getSongFile().toString())).uniqueResult();
return (song == null) ? Optional.empty() : Optional.of(song);
}
}
/**
* List all items in the database of a certain type.
* @param typeClass Class representing the type of items to find.
@ -137,17 +149,32 @@ class HibernateDatabase implements IDatabase{
if(!songObj.isPresent())
session.save(new Song(metadata.getTrackNumber(), metadata.getDiscNumber(), metadata.getTitle(), artistObj.get(),
albumObj.get(), metadata.getGenre(), metadata.getSongFile()));
else
songObj.get().updateData(metadata.getTrackNumber(), metadata.getDiscNumber(), metadata.getTitle(), artistObj.get(),
else {
Song song = songObj.get();
song.updateData(metadata.getTrackNumber(), metadata.getDiscNumber(), metadata.getTitle(), artistObj.get(),
albumObj.get(), metadata.getGenre(), metadata.getSongFile());
session.update(song);
}
session.getTransaction().commit();
}
}
/**
* Add a new song to the database.
* If the song already exists it will be updated instead.
* @param song New song information.
*/
public void addSong(Song song){
try (Session session = getSession()) {
session.beginTransaction();
session.save(song);
Optional<Song> existsAlready = getOneSong(song);
if(existsAlready.isPresent()) {
Song updater = existsAlready.get();
updater.updateData(song);
session.update(updater);
}
else
session.saveOrUpdate(song);
session.getTransaction().commit();
}
}

View File

@ -27,7 +27,7 @@ public class Song implements Comparable<Song>, IDBType {
private String title;
@ManyToOne(cascade = CascadeType.ALL)
private Album album;
@Column(name = "songFile")
@Column(name = "songFile", unique = true)
private String songFile;
@Column(name = "trackNumber")
private int trackNumber;
@ -153,4 +153,8 @@ public class Song implements Comparable<Song>, IDBType {
this.album = album;
this.songFile = songFile;
}
public void updateData(Song other){
updateData(other.getTrackNumber().toString(), other.getDiscNumber().toString(), other.getTitle(), other.getArtist(), other.getAlbum(), other.getGenre(), other.songFile);
}
}

View File

@ -56,9 +56,9 @@ public class GatewayTest {
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"), "", "");
Song song1 = new Song("1", "1", "s1", artist1, new Album("a"), "", "1");
Song song2 = new Song("2", "1", "s2", artist1, new Album("b"), "", "2");
Song song3 = new Song("1", "1", "t1", artist2, new Album("c"), "", "3");
database.addSong(song1);
database.addSong(song2);
database.addSong(song3);
@ -79,9 +79,9 @@ public class GatewayTest {
@Test
public void testListAllSongs() throws Exception {
Song song1 = new Song("1", "1", "s1", new Artist("a"), new Album("a"), "", "");
Song song2 = new Song("2", "1","s2", new Artist("b"), new Album("a"), "", "");
Song song3 = new Song("1", "1","t1", new Artist("c"), new Album("b"), "", "");
Song song1 = new Song("1", "1", "s1", new Artist("a"), new Album("a"), "", "1");
Song song2 = new Song("2", "1","s2", new Artist("b"), new Album("a"), "", "2");
Song song3 = new Song("1", "1","t1", new Artist("c"), new Album("b"), "", "3");
database.addSong(song1);
database.addSong(song2);
database.addSong(song3);
@ -102,9 +102,9 @@ public class GatewayTest {
public void testListAllSongsGroupedByAlbum() throws Exception {
Album album1 = new Album("Test 1");
Album album2 = new Album("Test 2");
Song song1 = new Song("1", "1", "s1", new Artist("a"), album1, "", "");
Song song2 = new Song("2", "1", "s2", new Artist("b"), album1, "", "");
Song song3 = new Song("1", "1", "t1", new Artist("c"), album2, "", "");
Song song1 = new Song("1", "1", "s1", new Artist("a"), album1, "", "1");
Song song2 = new Song("2", "1", "s2", new Artist("b"), album1, "", "2");
Song song3 = new Song("1", "1", "t1", new Artist("c"), album2, "", "3");
database.addSong(song1);
database.addSong(song2);
database.addSong(song3);
@ -119,7 +119,7 @@ public class GatewayTest {
}
@Test
public void testAddSong() throws Exception {
public void testAddSongViaMetadata() throws Exception {
Tag tags = new ID3v23Tag();
tags.addField(FieldKey.ALBUM, "Test Album");
tags.addField(FieldKey.ARTIST, "Test Artist");
@ -130,11 +130,72 @@ public class GatewayTest {
ExtractedMetadata metadata = new ExtractedMetadata(tags, new File(""));
database.addSong(metadata);
Song song = database.getOneSong(metadata).get();
assertEquals(song.getAlbum().getName(), metadata.getAlbum());
assertEquals(song.getArtist().getName(), metadata.getArtist());
assertEquals(song.getTrackNumber(), Integer.valueOf(1));
assertEquals(song.getTitle(), metadata.getTitle());
assertEquals(song.getGenre(), metadata.getGenre());
assertEquals(song.getDiscNumber(), Integer.valueOf(100));
assertEquals(metadata.getAlbum(), song.getAlbum().getName());
assertEquals(metadata.getArtist(), song.getArtist().getName());
assertEquals(Integer.valueOf(1), song.getTrackNumber());
assertEquals(metadata.getTitle(), song.getTitle());
assertEquals(metadata.getGenre(), song.getGenre());
assertEquals(Integer.valueOf(100), song.getDiscNumber());
}
@Test
public void testAddSongDirectly() throws Exception {
Album album = new Album("Test 1");
Artist artist = new Artist("Test 2");
Song song = new Song("1", "1", "s1", artist, album, "g", "");
database.addSong(song);
Song retSong = database.listAllT(Song.class).get().get(0);
assertEquals(album, retSong.getAlbum());
assertEquals(artist, retSong.getArtist());
assertEquals(Integer.valueOf(1), retSong.getTrackNumber());
assertEquals("s1", retSong.getTitle());
assertEquals("g", retSong.getGenre());
assertEquals(Integer.valueOf(1), retSong.getDiscNumber());
}
@Test
public void testAddExistingSongViaMetadata() throws Exception {
// Add a song first
Tag tags = new ID3v23Tag();
tags.addField(FieldKey.ALBUM, "Test Album");
tags.addField(FieldKey.ARTIST, "Test Artist");
tags.addField(FieldKey.TRACK, "1");
tags.addField(FieldKey.TITLE, "Test Song");
tags.addField(FieldKey.GENRE, "Test Genre");
tags.addField(FieldKey.DISC_NO, "100");
ExtractedMetadata metadata = new ExtractedMetadata(tags, new File(""));
database.addSong(metadata);
// Add different tags but set the source file as the same
Tag newTags = new ID3v23Tag();
newTags.addField(FieldKey.ALBUM, "Test Album New");
newTags.addField(FieldKey.ARTIST, "Test Artist New");
newTags.addField(FieldKey.TRACK, "5");
newTags.addField(FieldKey.TITLE, "Test Song New");
newTags.addField(FieldKey.GENRE, "Test Genre New");
newTags.addField(FieldKey.DISC_NO, "200");
ExtractedMetadata newMetadata = new ExtractedMetadata(newTags, new File(""));
database.addSong(newMetadata);
// Only file technically matters so getting with old metadata should give our new data anyway.
Song song = database.getOneSong(metadata).get();
//
assertEquals(newMetadata.getAlbum(), song.getAlbum().getName());
assertEquals(newMetadata.getArtist(), song.getArtist().getName());
assertEquals(Integer.valueOf(5), song.getTrackNumber());
assertEquals(newMetadata.getTitle(), song.getTitle());
assertEquals(newMetadata.getGenre(), song.getGenre());
assertEquals(Integer.valueOf(200), song.getDiscNumber());
}
@Test
public void testAddExistingSongDirectly(){
Album album = new Album("Test 1");
Artist artist = new Artist("Test 2");
Song song = new Song("1", "1", "s1", artist, album, "g", "");
database.addSong(song);
Album newAlbum = new Album("Test 1");
Artist newArtist = new Artist("Test 2");
Song newSong = new Song("2", "2", "s2", newArtist, newAlbum, "g2", ""); // File same == Song same
database.addSong(newSong);
assertEquals(database.listAllT(Song.class).get().size(), 1);
}
}