Skip to content

Commit

Permalink
Song Database Implementation: songLibrary, songDsData, bits and pieces.
Browse files Browse the repository at this point in the history
Pull includes the implementation of everything relating to Song's database: mainly songLibrary and songDsData. Brief summary;

- **Uploaded the standard library of songs.** Currently located in the new folder songLib under src. Any test cases or other pieces of code that currently create new instances of Songs should likely be re-reviewed with this in mind. I already fixed a few, but I might've missed some.

- Updated build.gradle to include jaudiotagger

- Updated Song entity to include username of uploading user.

- Reading/saving from songs.csv, formatted as `ID, uploader, filepath`

- Changed artistList from List<String> to String[]

- Removed 'length' as Jaudiotagger cannot retrieve it. If we want to show it, it would be on the Jlayer side. Better suited this way, anyway.

- Removed isExplicit. Too much of a pain for too little gain.

- changed saveSong to return a boolean for a successful song addition.

- Created Test file for SongLibrary.

KNOWN ISSUES

- createFile() assumes the existence of a user admin, as it assigns all songs currently in songLib /to/ admin. (This was only important for creating songs.csv from scratch. It won't do this now that it exists.)

- Many files don't have album covers. I'll be creating default covers to put in the BufferedImage parameter later. I don't think anyone is at that stage (which is why I'm putting it off for a later PR), but please don't try accessing the BufferedImage parameter until I do.

- When parsing ID names, the 0s at the beginning of the names are omitted. Theoretically, this shouldn't create duplicate IDs anyway, as 1) what's being checked is the parsed ID, and 2) randInt will not create more IDs that start with 0. I'll go back and rename the files to omit the 0s (not that difficult), but I figure its lower priority due to the reasons I stated.

- Need to implement safeguard against improperly formatted mp3s (ex. missing genre). There currently aren't any, so it should be temporarily OK, but I have some code smells because of this.

Making this a PR despite this so you guys have a better SONG_LIBRARY to work with for now.
  • Loading branch information
clin1967 committed Nov 13, 2022
1 parent b67bac5 commit d2cd089
Show file tree
Hide file tree
Showing 271 changed files with 471 additions and 67 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repositories {
}

dependencies {
implementation 'junit:junit:4.13.1', 'javazoom:jlayer:1.0.1'
implementation 'junit:junit:4.13.2', 'javazoom:jlayer:1.0.1', 'net.jthink:jaudiotagger:3.0.1'
testImplementation('org.junit.jupiter:junit-jupiter:5.9.0')
}

Expand All @@ -16,4 +16,4 @@ test {
testLogging {
events "passed", "skipped", "failed"
}
}
}
5 changes: 3 additions & 2 deletions src/main/java/Database/songAccessInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ public interface songAccessInterface {
public Collection<songDsData> getLibrary();

/**
* @param song Song object to be saved to the database.
* @param song New Song object to be saved to the database.
* @return true iff save was successful.
*/
public void saveSong(songDsData song);
public boolean saveSong(songDsData song);

/**
* @param id the unique int ID of a given song.
Expand Down
29 changes: 11 additions & 18 deletions src/main/java/Database/songDsData.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
package Database;

import Entities.Song;

import java.awt.image.BufferedImage;
import java.io.File;

/**
* Data storage class between database class and entities.
*/
public class songDsData {
private final Song song;

public songDsData(int id) {
// TODO: remove once class is complete
this.song = new Song(id, null, null, 0, null, null, false, null);
}

public songDsData(Song song){
this.song = song;
}

public songDsData(String[] data){
//TODO: Implementation for use when reading in from csv.
// temporary constructor to avoid null exception in other tests
this.song = new Song(0, null, null, 0, null, null,
false, null);
}

public Song buildFromWrite(){
//TODO: Helper for String[] data constructor.
return null;
public songDsData(int id, String name, String[] artistList, String genre,
File file, BufferedImage cover, String uploader){
this.song = new Song(id, name, artistList, genre, file, cover, uploader);
}

public String buildToWrite(){
//TODO: Helper to turn into csv formatted line.
return null;
return this.song.getID() + "," + this.song.getUploader() + "," + this.song.getFile().getPath() + "\n";
}

public Song getSong() {
return this.song;
}

public int getID(){
return this.song.getID();
}
}
143 changes: 129 additions & 14 deletions src/main/java/Database/songLibrary.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
package Database;
import java.util.HashMap;

import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.TagException;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.Tag;

import java.awt.image.BufferedImage;
import java.util.Collection;
import java.util.Objects;
import java.util.HashMap;
import java.io.*;

/**
* Uses the Eager Instantiation version of the Singleton design pattern.
Expand All @@ -9,8 +21,7 @@
*/
public class songLibrary implements songAccessInterface{

// TODO: change "" param to filepath
private static final songLibrary SONG_LIBRARY = new songLibrary("");
private static final songLibrary SONG_LIBRARY = new songLibrary("./src/main/java/Database/songs.csv");
private final HashMap<Integer, songDsData> library;
private final String filepath;

Expand All @@ -24,36 +35,143 @@ public static songLibrary getInstance(){

private songLibrary(String filepath){
this.filepath = filepath;
library = readFile();
this.library = readFile();

}

/**
* Saves the current version of the songLibrary to songs.csv
*/
public void saveFile(){
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(filepath));
for(songDsData song: library.values()){
bw.write(song.buildToWrite());
}
bw.close();
}
catch(IOException e){
System.out.println("IOException: " + e);
}
}

/**
* Reads song.csv and returns a Hashmap of all songs.
* @return Hashmap mapping integer ID of a song to its respective songDsData.
*/
private HashMap<Integer, songDsData> readFile(){
//TODO: Implementation of reading csv of songs.
return new HashMap<>();
HashMap<Integer, songDsData> map = new HashMap<>();
try {
BufferedReader br = new BufferedReader(new FileReader(filepath));
String rawLine;
while ((rawLine = br.readLine()) != null) {

String[] songInfo = rawLine.split(",");
int id = Integer.parseInt(songInfo[0]);
String uploader = songInfo[1];

songDsData song = readSongFromMetadata(id, uploader, new MP3File(songInfo[2]));
map.put(id, song);

}
br.close();
} catch (FileNotFoundException e) {
System.out.println("FileNotFoundException: " + e + ". Creating new file.");
return createFile();
} catch (IOException e) {
System.out.println("IOException: " + e);
} catch (TagException | CannotReadException | InvalidAudioFrameException | ReadOnlyFileException e) {
throw new RuntimeException(e);
}
return map;
}

/**
* if songs.csv does not exist, createFile will 1) create it, and
* 2) write into both the csv and SONG_LIBRARY.
*/
private HashMap<Integer, songDsData> createFile(){
File csv = new File(filepath);
HashMap<Integer, songDsData> map = new HashMap<>();
try{
if(csv.createNewFile()){
BufferedWriter bw = new BufferedWriter(new FileWriter(filepath));
File rawLib = new File("src/songLib");
for(File rawSong: Objects.requireNonNull(rawLib.listFiles())){

String idStr = rawSong.getName();
int id = Integer.parseInt(idStr.substring(0, idStr.length() - 4));
String uploader = "admin";
songDsData song = readSongFromMetadata(id, uploader, new MP3File(rawSong));

map.put(id, song);
bw.write(song.buildToWrite());
}
bw.close();
}
} catch (TagException | CannotReadException | InvalidAudioFrameException | ReadOnlyFileException e){
throw new RuntimeException(e);
} catch (IOException e){
System.out.println("Failed to create songs.csv." + e);
} catch (NullPointerException e){
System.out.println("Failed to read folder." + e);
}
return map;
}

/**
* @param id The id of the song.
* @param uploader The username of the song uploader.
* @param rawSong The mp3 file of a given song.
* @return The songDsData entity representing rawSong.
*/
private songDsData readSongFromMetadata(int id, String uploader, MP3File rawSong){

Tag tag = rawSong.getTag();
BufferedImage cover = (BufferedImage) tag.getFirstArtwork();
//TODO: OR set as default cover
String name = format(tag.getFields(FieldKey.TITLE).toString());
String[] artistList = format(tag.getFields(FieldKey.ARTIST).toString()).split(";");
String genre = format(tag.getFields(FieldKey.GENRE).toString());

return new songDsData(id, name, artistList, genre, rawSong.getFile(), cover, uploader);
}

/**
* Corrects metadata read to remove extra text.
* @param line Line to be formatted.
* @return Correctly formatted line.
*/
private String format(String line){
return line.substring(7, line.length() - 5);
}

/**
* @return Collection of all songs.
*/
@Override
public Collection<songDsData> getLibrary() {
return library.values();
}

/**
* @param song Song object to be saved to the database.
* @param song New Song object to be saved to the database.
* @return true iff save was successful.
*/
public void saveSong(songDsData song) {
//TODO: 'Write' implementation for a single song.
@Override
public boolean saveSong(songDsData song) {
if(library.containsKey(song.getID())){
library.put(song.getID(), song);
return true;
}
return false;
}

/**
* @param id the unique int ID of a given song.
* @return true iff a song with the given ID exists.
*/
@Override
public boolean exists(int id) {
return library.containsKey(id);
}
Expand All @@ -62,11 +180,8 @@ public boolean exists(int id) {
* @param id the unique int ID of a given song.
* @return Song inside a songDsData with matching ID, or null if it does not exist.
*/
@Override
public songDsData getSong(int id){
if (library.containsKey(id))
return library.get(id);

// TODO: change once class is complete
return new songDsData(id);
return library.get(id);
}
}
Loading

0 comments on commit d2cd089

Please sign in to comment.