From 2ae4ca1402ff706e54252a8dbdae3761df78ffec Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Tue, 8 Mar 2016 16:41:25 +0000 Subject: [PATCH] Updating the library will now to add songs with the same base file. --- src/main/java/musicplayer/Player.java | 4 +++ src/main/java/musicplayer/db/Gateway.java | 23 +++++++++++++-- src/main/java/musicplayer/model/Song.java | 36 ++++++++++++++++------- 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/src/main/java/musicplayer/Player.java b/src/main/java/musicplayer/Player.java index 3b5ad74..1570b0b 100644 --- a/src/main/java/musicplayer/Player.java +++ b/src/main/java/musicplayer/Player.java @@ -154,4 +154,8 @@ class Player { Gst.quit(); } } + + public boolean isPlaying(){ + return playBin.isPlaying(); + } } diff --git a/src/main/java/musicplayer/db/Gateway.java b/src/main/java/musicplayer/db/Gateway.java index 6e892d3..ab19ccc 100644 --- a/src/main/java/musicplayer/db/Gateway.java +++ b/src/main/java/musicplayer/db/Gateway.java @@ -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 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 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(); } } diff --git a/src/main/java/musicplayer/model/Song.java b/src/main/java/musicplayer/model/Song.java index 5ff216c..490ccf3 100644 --- a/src/main/java/musicplayer/model/Song.java +++ b/src/main/java/musicplayer/model/Song.java @@ -41,17 +41,7 @@ public class Song implements Comparable, 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, 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; + } }