Updating the library will now to add songs with the same base file.

This commit is contained in:
neviyn 2016-03-08 16:41:25 +00:00
parent 148eb38bba
commit 2ae4ca1402
3 changed files with 49 additions and 14 deletions

View File

@ -154,4 +154,8 @@ class Player {
Gst.quit();
}
}
public boolean isPlaying(){
return playBin.isPlaying();
}
}

View File

@ -39,6 +39,18 @@ public class Gateway {
}
}
/**
* @param metadata Metadata of song to find.
* @return Song found with metadata or Optional.empty()
*/
public static Optional<Song> getOneSong(ExtractedMetadata metadata){
try (Session session = DatabaseManager.getInstance().getSession()) {
Song song = (Song)session.createCriteria(Song.class)
.add(Restrictions.eq("songFile", metadata.songFile)).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.
@ -74,7 +86,7 @@ public class Gateway {
/**
* Add a new song to the database.
*
* If the song already exists it will be updated instead.
* @param metadata New song information.
*/
public static void addSong(ExtractedMetadata metadata) {
@ -90,8 +102,13 @@ public class Gateway {
Artist artist = new Artist(metadata.artist);
artistObj = Optional.of(artist);
}
session.save(new Song(metadata.trackNumber, metadata.discNumber, metadata.title, artistObj.get(),
albumObj.get(), metadata.genre, metadata.songFile));
Optional<Song> songObj = getOneSong(metadata);
if(!songObj.isPresent())
session.save(new Song(metadata.trackNumber, metadata.discNumber, metadata.title, artistObj.get(),
albumObj.get(), metadata.genre, metadata.songFile));
else
songObj.get().updateData(metadata.trackNumber, metadata.discNumber, metadata.title, artistObj.get(),
albumObj.get(), metadata.genre, metadata.songFile);
session.getTransaction().commit();
}
}

View File

@ -41,17 +41,7 @@ public class Song implements Comparable<Song>, IDBType {
}
public Song(String trackNumber, String discNumber, String title, Artist artist, Album album, String genre, String songFile) {
try {
this.trackNumber = Integer.parseInt(trackNumber);
} catch (NumberFormatException e){ this.trackNumber = 0; }
try {
this.discNumber = Integer.parseInt(discNumber);
} catch (NumberFormatException e){ this.discNumber = 0; }
this.title = title;
this.artist = artist;
this.genre = genre;
this.album = album;
this.songFile = songFile;
updateData(trackNumber, discNumber, title, artist, album, genre, songFile);
}
@Override
@ -139,4 +129,28 @@ public class Song implements Comparable<Song>, IDBType {
public String getM3UFormatString(){
return String.format("#EXTINF:0,%s - %s\n%s\n", getArtist().getName(), getTitle(), getSongFile().getAbsolutePath());
}
/**
* Update the data stored in this Song object
* @param trackNumber Track Number
* @param discNumber Disc Number
* @param title Song Title
* @param artist Artist
* @param album Album
* @param genre Genre
* @param songFile Absolute path to song source file
*/
public void updateData(String trackNumber, String discNumber, String title, Artist artist, Album album, String genre, String songFile){
try {
this.trackNumber = Integer.parseInt(trackNumber);
} catch (NumberFormatException e){ this.trackNumber = 0; }
try {
this.discNumber = Integer.parseInt(discNumber);
} catch (NumberFormatException e){ this.discNumber = 0; }
this.title = title;
this.artist = artist;
this.genre = genre;
this.album = album;
this.songFile = songFile;
}
}