From e64ffae239b19871704b4ac1e9cd58275ed58622 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Sat, 26 Jun 2021 12:34:26 +0200 Subject: Changed the architecture to respect maven and generated an empty javadoc --- src/musichub/business/Album.java | 145 --------- src/musichub/business/AudioBook.java | 78 ----- src/musichub/business/AudioElement.java | 88 ------ src/musichub/business/Category.java | 14 - src/musichub/business/Genre.java | 14 - src/musichub/business/Language.java | 14 - src/musichub/business/MusicHub.java | 329 --------------------- src/musichub/business/NoAlbumFoundException.java | 8 - src/musichub/business/NoElementFoundException.java | 8 - .../business/NoPlayListFoundException.java | 8 - src/musichub/business/PlayList.java | 100 ------- src/musichub/business/Song.java | 56 ---- 12 files changed, 862 deletions(-) delete mode 100644 src/musichub/business/Album.java delete mode 100644 src/musichub/business/AudioBook.java delete mode 100644 src/musichub/business/AudioElement.java delete mode 100644 src/musichub/business/Category.java delete mode 100644 src/musichub/business/Genre.java delete mode 100644 src/musichub/business/Language.java delete mode 100644 src/musichub/business/MusicHub.java delete mode 100644 src/musichub/business/NoAlbumFoundException.java delete mode 100644 src/musichub/business/NoElementFoundException.java delete mode 100644 src/musichub/business/NoPlayListFoundException.java delete mode 100644 src/musichub/business/PlayList.java delete mode 100644 src/musichub/business/Song.java (limited to 'src/musichub/business') diff --git a/src/musichub/business/Album.java b/src/musichub/business/Album.java deleted file mode 100644 index 01fd179..0000000 --- a/src/musichub/business/Album.java +++ /dev/null @@ -1,145 +0,0 @@ -package musichub.business; - -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.*; - - -public class Album { - private final String title; - private final int lengthInSeconds; - private final UUID uuid; - private String artist; - private Date date; - private ArrayList songsUIDs; - - public Album(String title, String artist, int lengthInSeconds, String id, String date, ArrayList songsUIDs) { - this.title = title; - this.artist = artist; - this.lengthInSeconds = lengthInSeconds; - this.uuid = UUID.fromString(id); - try { - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); - this.date = sdf.parse(date); - } catch (ParseException ex) { - ex.printStackTrace(); - } - this.songsUIDs = songsUIDs; - } - - public Album(String title, String artist, int lengthInSeconds, String date) { - this.title = title; - this.artist = artist; - this.lengthInSeconds = lengthInSeconds; - this.uuid = UUID.randomUUID(); - try { - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); - this.date = sdf.parse(date); - } catch (ParseException ex) { - ex.printStackTrace(); - } - this.songsUIDs = new ArrayList<>(); - } - - public Album(Element xmlElement) throws Exception { - { - this.title = xmlElement.getElementsByTagName("title").item(0).getTextContent(); - this.lengthInSeconds = Integer.parseInt(xmlElement.getElementsByTagName("lengthInSeconds").item(0).getTextContent()); - String uuid = null; - try { - uuid = xmlElement.getElementsByTagName("UUID").item(0).getTextContent(); - } catch (Exception ex) { - System.out.println("Empty album UUID, will create a new one"); - } - if ((uuid == null) || (uuid.isEmpty())) - this.uuid = UUID.randomUUID(); - else this.uuid = UUID.fromString(uuid); - - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); - this.date = sdf.parse(xmlElement.getElementsByTagName("date").item(0).getTextContent()); - //parse list of songs: - Node songsElement = xmlElement.getElementsByTagName("songs").item(0); - NodeList songUUIDNodes = songsElement.getChildNodes(); - if (songUUIDNodes == null) return; - - this.songsUIDs = new ArrayList<>(); - - for (int i = 0; i < songUUIDNodes.getLength(); i++) { - if (songUUIDNodes.item(i).getNodeType() == Node.ELEMENT_NODE) { - Element songElement = (Element) songUUIDNodes.item(i); - if (songElement.getNodeName().equals("UUID")) { - try { - this.addSong(UUID.fromString(songElement.getTextContent())); - } catch (Exception ex) { - ex.printStackTrace(); - } - } - } - } - } - } - - - public void addSong(UUID song) { - songsUIDs.add(song); - } - - - public List getSongs() { - return songsUIDs; - } - - public ArrayList getSongsRandomly() { - ArrayList shuffledSongs = songsUIDs; - Collections.shuffle(shuffledSongs); - return shuffledSongs; - } - - public String getTitle() { - return title; - } - - public Date getDate() { - return date; - } - - public void createXMLElement(Document document, Element parentElement) { - Element albumElement = document.createElement("album"); - parentElement.appendChild(albumElement); - - Element nameElement = document.createElement("title"); - nameElement.appendChild(document.createTextNode(title)); - albumElement.appendChild(nameElement); - - Element artistElement = document.createElement("artist"); - artistElement.appendChild(document.createTextNode(artist)); - albumElement.appendChild(artistElement); - - Element lengthElement = document.createElement("lengthInSeconds"); - lengthElement.appendChild(document.createTextNode(Integer.valueOf(lengthInSeconds).toString())); - albumElement.appendChild(lengthElement); - - Element UUIDElement = document.createElement("UUID"); - UUIDElement.appendChild(document.createTextNode(uuid.toString())); - albumElement.appendChild(UUIDElement); - - Element dateElement = document.createElement("date"); - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); - dateElement.appendChild(document.createTextNode(sdf.format(date))); - albumElement.appendChild(dateElement); - - Element songsElement = document.createElement("songs"); - for (UUID currentUUID : this.songsUIDs) { - Element songUUIDElement = document.createElement("UUID"); - songUUIDElement.appendChild(document.createTextNode(currentUUID.toString())); - songsElement.appendChild(songUUIDElement); - } - albumElement.appendChild(songsElement); - - } -} \ No newline at end of file diff --git a/src/musichub/business/AudioBook.java b/src/musichub/business/AudioBook.java deleted file mode 100644 index 80392d6..0000000 --- a/src/musichub/business/AudioBook.java +++ /dev/null @@ -1,78 +0,0 @@ -package musichub.business; - -import org.w3c.dom.Document; -import org.w3c.dom.Element; - -public class AudioBook extends AudioElement { - private Language language; - private Category category; - - public AudioBook(String title, String artist, int lengthInSeconds, String uid, String content, String language, String category) { - super(title, artist, lengthInSeconds, uid, content); - this.setLanguage(language); - this.setCategory(category); - } - - public AudioBook(String title, String artist, int lengthInSeconds, String content, String language, String category) { - super(title, artist, lengthInSeconds, content); - this.setLanguage(language); - this.setCategory(category); - } - - public AudioBook(Element xmlElement) { - super(xmlElement); - this.setLanguage(xmlElement.getElementsByTagName("language").item(0).getTextContent()); - this.setCategory(xmlElement.getElementsByTagName("category").item(0).getTextContent()); - } - - public Language getLanguage() { - return this.language; - } - - public void setLanguage(String language) { - switch (language.toLowerCase()) { - default -> this.language = Language.ENGLISH; - case "french" -> this.language = Language.FRENCH; - case "german" -> this.language = Language.GERMAN; - case "spanish" -> this.language = Language.SPANISH; - case "italian" -> this.language = Language.ITALIAN; - } - } - - public Category getCategory() { - return this.category; - } - - public void setCategory(String category) { - switch (category.toLowerCase()) { - default -> this.category = Category.YOUTH; - case "novel" -> this.category = Category.NOVEL; - case "theater" -> this.category = Category.THEATER; - case "documentary" -> this.category = Category.DOCUMENTARY; - case "speech" -> this.category = Category.SPEECH; - } - } - - - public String toString() { - return super.toString() + ", Language = " + getLanguage() + ", Category = " + getCategory() + "\n"; - } - - - public void createXMLElement(Document document, Element parentElement) { - // audiobook element - Element audioBook = document.createElement("audiobook"); - - super.createXMLElement(document, audioBook); - - Element languageElement = document.createElement("language"); - languageElement.appendChild(document.createTextNode(language.getLanguage())); - audioBook.appendChild(languageElement); - - Element categoryElement = document.createElement("category"); - categoryElement.appendChild(document.createTextNode(category.getCategory())); - audioBook.appendChild(categoryElement); - - parentElement.appendChild(audioBook); - } -} \ No newline at end of file diff --git a/src/musichub/business/AudioElement.java b/src/musichub/business/AudioElement.java deleted file mode 100644 index e0a686b..0000000 --- a/src/musichub/business/AudioElement.java +++ /dev/null @@ -1,88 +0,0 @@ -package musichub.business; - -import org.w3c.dom.Document; -import org.w3c.dom.Element; - -import java.util.UUID; - -public abstract class AudioElement { - protected String title; - protected String artist; - protected int lengthInSeconds; - protected UUID uuid; - protected String content; - - public AudioElement(String title, String artist, int lengthInSeconds, String id, String content) { - this.title = title; - this.artist = artist; - this.lengthInSeconds = lengthInSeconds; - this.uuid = UUID.fromString(id); - this.content = content; - } - - public AudioElement(String title, String artist, int lengthInSeconds, String content) { - this.title = title; - this.artist = artist; - this.lengthInSeconds = lengthInSeconds; - this.content = content; - this.uuid = UUID.randomUUID(); - } - - public AudioElement(Element xmlElement) { - { - title = xmlElement.getElementsByTagName("title").item(0).getTextContent(); - artist = xmlElement.getElementsByTagName("artist").item(0).getTextContent(); - lengthInSeconds = Integer.parseInt(xmlElement.getElementsByTagName("length").item(0).getTextContent()); - content = xmlElement.getElementsByTagName("content").item(0).getTextContent(); - String uuid = null; - try { - uuid = xmlElement.getElementsByTagName("UUID").item(0).getTextContent(); - } catch (Exception ex) { - System.out.println("Empty element UUID, will create a new one"); - } - if ((uuid == null) || (uuid.isEmpty())) - this.uuid = UUID.randomUUID(); - else this.uuid = UUID.fromString(uuid); - } - } - - public UUID getUUID() { - return this.uuid; - } - - public String getArtist() { - return this.artist; - } - - public String getTitle() { - return this.title; - } - - public String toString() { - return "Title = " + this.title + ", Artist = " + this.artist + ", Length = " + this.lengthInSeconds + ", Content = " + this.content; - } - - public void createXMLElement(Document document, Element parentElement) { - Element nameElement = document.createElement("title"); - nameElement.appendChild(document.createTextNode(title)); - parentElement.appendChild(nameElement); - - Element artistElement = document.createElement("artist"); - artistElement.appendChild(document.createTextNode(artist)); - parentElement.appendChild(artistElement); - - Element lengthElement = document.createElement("length"); - lengthElement.appendChild(document.createTextNode(Integer.valueOf(lengthInSeconds).toString())); - parentElement.appendChild(lengthElement); - - Element UUIDElement = document.createElement("UUID"); - UUIDElement.appendChild(document.createTextNode(uuid.toString())); - parentElement.appendChild(UUIDElement); - - Element contentElement = document.createElement("content"); - contentElement.appendChild(document.createTextNode(content)); - parentElement.appendChild(contentElement); - - } - -} \ No newline at end of file diff --git a/src/musichub/business/Category.java b/src/musichub/business/Category.java deleted file mode 100644 index f676e51..0000000 --- a/src/musichub/business/Category.java +++ /dev/null @@ -1,14 +0,0 @@ -package musichub.business; - -public enum Category { - YOUTH("youth"), NOVEL("novel"), THEATER("theater"), DOCUMENTARY("documentary"), SPEECH("speech"); - private final String category; - - Category(String category) { - this.category = category; - } - - public String getCategory() { - return category; - } -} \ No newline at end of file diff --git a/src/musichub/business/Genre.java b/src/musichub/business/Genre.java deleted file mode 100644 index 18deca6..0000000 --- a/src/musichub/business/Genre.java +++ /dev/null @@ -1,14 +0,0 @@ -package musichub.business; - -public enum Genre { - JAZZ("jazz"), CLASSIC("classic"), HIPHOP("hiphop"), ROCK("rock"), POP("pop"), RAP("rap"); - private final String genre; - - Genre(String genre) { - this.genre = genre; - } - - public String getGenre() { - return genre; - } -} \ No newline at end of file diff --git a/src/musichub/business/Language.java b/src/musichub/business/Language.java deleted file mode 100644 index 679e586..0000000 --- a/src/musichub/business/Language.java +++ /dev/null @@ -1,14 +0,0 @@ -package musichub.business; - -public enum Language { - FRENCH("french"), ENGLISH("english"), ITALIAN("italian"), SPANISH("spanish"), GERMAN("german"); - private final String language; - - Language(String language) { - this.language = language; - } - - public String getLanguage() { - return language; - } -} \ No newline at end of file diff --git a/src/musichub/business/MusicHub.java b/src/musichub/business/MusicHub.java deleted file mode 100644 index 38e2214..0000000 --- a/src/musichub/business/MusicHub.java +++ /dev/null @@ -1,329 +0,0 @@ -package musichub.business; - -import musichub.util.XMLHandler; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; - -import java.util.*; - -class SortByDate implements Comparator { - public int compare(Album a1, Album a2) { - return a1.getDate().compareTo(a2.getDate()); - } -} - -class SortByGenre implements Comparator { - public int compare(Song s1, Song s2) { - return s1.getGenre().compareTo(s2.getGenre()); - } -} - -class SortByAuthor implements Comparator { - public int compare(AudioElement e1, AudioElement e2) { - return e1.getArtist().compareTo(e2.getArtist()); - } -} - -public class MusicHub { - public static final String DIR = System.getProperty("user.dir"); - public static final String ALBUMS_FILE_PATH = DIR + "/files/albums.xml"; - public static final String PLAYLISTS_FILE_PATH = DIR + "/files/playlists.xml"; - public static final String ELEMENTS_FILE_PATH = DIR + "/files/elements.xml"; - private final List albums; - private final List playlists; - private final List elements; - private final XMLHandler xmlHandler = new XMLHandler(); - - public MusicHub() { - albums = new LinkedList<>(); - playlists = new LinkedList<>(); - elements = new LinkedList<>(); - this.loadElements(); - this.loadAlbums(); - this.loadPlaylists(); - } - - public void addElement(AudioElement element) { - elements.add(element); - } - - public void addAlbum(Album album) { - albums.add(album); - } - - public void addPlaylist(PlayList playlist) { - playlists.add(playlist); - } - - public void deletePlayList(String playListTitle) throws NoPlayListFoundException { - - PlayList thePlayList = null; - boolean result = false; - for (PlayList pl : playlists) { - if (pl.getTitle().equalsIgnoreCase(playListTitle)) { - thePlayList = pl; - break; - } - } - - if (thePlayList != null) - result = playlists.remove(thePlayList); - if (!result) throw new NoPlayListFoundException("Playlist " + playListTitle + " not found!"); - } - - public Iterator albums() { - return albums.listIterator(); - } - - public Iterator playlists() { - return playlists.listIterator(); - } - - public Iterator elements() { - return elements.listIterator(); - } - - public String getAlbumsTitlesSortedByDate() { - StringBuilder titleList = new StringBuilder(); - albums.sort(new SortByDate()); - for (Album al : albums) - titleList.append(al.getTitle()).append("\n"); - return titleList.toString(); - } - - public String getAudiobooksTitlesSortedByAuthor() { - StringBuilder titleList = new StringBuilder(); - List audioBookList = new ArrayList<>(); - for (AudioElement ae : elements) - if (ae instanceof AudioBook) - audioBookList.add(ae); - audioBookList.sort(new SortByAuthor()); - for (AudioElement ab : audioBookList) - titleList.append(ab.getArtist()).append("\n"); - return titleList.toString(); - } - - public List getAlbumSongs(String albumTitle) throws NoAlbumFoundException { - Album theAlbum = null; - ArrayList songsInAlbum = new ArrayList<>(); - for (Album al : albums) { - if (al.getTitle().equalsIgnoreCase(albumTitle)) { - theAlbum = al; - break; - } - } - if (theAlbum == null) throw new NoAlbumFoundException("No album with this title in the MusicHub!"); - - List songIDs = theAlbum.getSongs(); - for (UUID id : songIDs) - for (AudioElement el : elements) { - if (el instanceof Song) { - if (el.getUUID().equals(id)) songsInAlbum.add(el); - } - } - return songsInAlbum; - - } - - public List getAlbumSongsSortedByGenre(String albumTitle) throws NoAlbumFoundException { - Album theAlbum = null; - ArrayList songsInAlbum = new ArrayList<>(); - for (Album al : albums) { - if (al.getTitle().equalsIgnoreCase(albumTitle)) { - theAlbum = al; - break; - } - } - if (theAlbum == null) throw new NoAlbumFoundException("No album with this title in the MusicHub!"); - - List songIDs = theAlbum.getSongs(); - for (UUID id : songIDs) - for (AudioElement el : elements) { - if (el instanceof Song) { - if (el.getUUID().equals(id)) songsInAlbum.add((Song) el); - } - } - songsInAlbum.sort(new SortByGenre()); - return songsInAlbum; - - } - - public void addElementToAlbum(String elementTitle, String albumTitle) throws NoAlbumFoundException, NoElementFoundException { - Album theAlbum = null; - int i; - boolean found = false; - for (i = 0; i < albums.size(); i++) { - if (albums.get(i).getTitle().equalsIgnoreCase(albumTitle)) { - theAlbum = albums.get(i); - found = true; - break; - } - } - - if (found) { - AudioElement theElement = null; - for (AudioElement ae : elements) { - if (ae.getTitle().equalsIgnoreCase(elementTitle)) { - theElement = ae; - break; - } - } - if (theElement != null) { - theAlbum.addSong(theElement.getUUID()); - //replace the album in the list - albums.set(i, theAlbum); - } else throw new NoElementFoundException("Element " + elementTitle + " not found!"); - } else throw new NoAlbumFoundException("Album " + albumTitle + " not found!"); - - } - - public void addElementToPlayList(String elementTitle, String playListTitle) throws NoPlayListFoundException, NoElementFoundException { - PlayList thePlaylist = null; - int i; - boolean found = false; - - for (i = 0; i < playlists.size(); i++) { - if (playlists.get(i).getTitle().equalsIgnoreCase(playListTitle)) { - thePlaylist = playlists.get(i); - found = true; - break; - } - } - - if (found) { - AudioElement theElement = null; - for (AudioElement ae : elements) { - if (ae.getTitle().equalsIgnoreCase(elementTitle)) { - theElement = ae; - break; - } - } - if (theElement != null) { - thePlaylist.addElement(theElement.getUUID()); - //replace the album in the list - playlists.set(i, thePlaylist); - } else throw new NoElementFoundException("Element " + elementTitle + " not found!"); - - } else throw new NoPlayListFoundException("Playlist " + playListTitle + " not found!"); - - } - - private void loadAlbums() { - NodeList albumNodes = xmlHandler.parseXMLFile(ALBUMS_FILE_PATH); - if (albumNodes == null) return; - - for (int i = 0; i < albumNodes.getLength(); i++) { - if (albumNodes.item(i).getNodeType() == Node.ELEMENT_NODE) { - Element albumElement = (Element) albumNodes.item(i); - if (albumElement.getNodeName().equals("album")) { - try { - this.addAlbum(new Album(albumElement)); - } catch (Exception ex) { - System.out.println("Something is wrong with the XML album element"); - } - } - } - } - } - - private void loadPlaylists() { - NodeList playlistNodes = xmlHandler.parseXMLFile(PLAYLISTS_FILE_PATH); - if (playlistNodes == null) return; - - for (int i = 0; i < playlistNodes.getLength(); i++) { - if (playlistNodes.item(i).getNodeType() == Node.ELEMENT_NODE) { - Element playlistElement = (Element) playlistNodes.item(i); - if (playlistElement.getNodeName().equals("playlist")) { - try { - this.addPlaylist(new PlayList(playlistElement)); - } catch (Exception ex) { - System.out.println("Something is wrong with the XML playlist element"); - } - } - } - } - } - - private void loadElements() { - NodeList audioelementsNodes = xmlHandler.parseXMLFile(ELEMENTS_FILE_PATH); - if (audioelementsNodes == null) return; - - for (int i = 0; i < audioelementsNodes.getLength(); i++) { - if (audioelementsNodes.item(i).getNodeType() == Node.ELEMENT_NODE) { - Element audioElement = (Element) audioelementsNodes.item(i); - if (audioElement.getNodeName().equals("song")) { - try { - AudioElement newSong = new Song(audioElement); - this.addElement(newSong); - } catch (Exception ex) { - System.out.println("Something is wrong with the XML song element"); - } - } - if (audioElement.getNodeName().equals("audiobook")) { - try { - AudioElement newAudioBook = new AudioBook(audioElement); - this.addElement(newAudioBook); - } catch (Exception ex) { - System.out.println("Something is wrong with the XML audiobook element"); - } - } - } - } - } - - - public void saveAlbums() { - Document document = xmlHandler.createXMLDocument(); - if (document == null) return; - - // root element - Element root = document.createElement("albums"); - document.appendChild(root); - - //save all albums - for (Iterator albumsIter = this.albums(); albumsIter.hasNext(); ) { - Album currentAlbum = albumsIter.next(); - currentAlbum.createXMLElement(document, root); - } - xmlHandler.createXMLFile(document, ALBUMS_FILE_PATH); - } - - public void savePlayLists() { - Document document = xmlHandler.createXMLDocument(); - if (document == null) return; - - // root element - Element root = document.createElement("playlists"); - document.appendChild(root); - - //save all playlists - for (Iterator playlistsIter = this.playlists(); playlistsIter.hasNext(); ) { - PlayList currentPlayList = playlistsIter.next(); - currentPlayList.createXMLElement(document, root); - } - xmlHandler.createXMLFile(document, PLAYLISTS_FILE_PATH); - } - - public void saveElements() { - Document document = xmlHandler.createXMLDocument(); - if (document == null) return; - - // root element - Element root = document.createElement("elements"); - document.appendChild(root); - - //save all AudioElements - for (AudioElement currentElement : elements) { - - if (currentElement instanceof Song) { - currentElement.createXMLElement(document, root); - } - if (currentElement instanceof AudioBook) { - currentElement.createXMLElement(document, root); - } - } - xmlHandler.createXMLFile(document, ELEMENTS_FILE_PATH); - } -} \ No newline at end of file diff --git a/src/musichub/business/NoAlbumFoundException.java b/src/musichub/business/NoAlbumFoundException.java deleted file mode 100644 index 04cbfcd..0000000 --- a/src/musichub/business/NoAlbumFoundException.java +++ /dev/null @@ -1,8 +0,0 @@ -package musichub.business; - -public class NoAlbumFoundException extends Exception { - - public NoAlbumFoundException(String msg) { - super(msg); - } -} \ No newline at end of file diff --git a/src/musichub/business/NoElementFoundException.java b/src/musichub/business/NoElementFoundException.java deleted file mode 100644 index a9b0d76..0000000 --- a/src/musichub/business/NoElementFoundException.java +++ /dev/null @@ -1,8 +0,0 @@ -package musichub.business; - -public class NoElementFoundException extends Exception { - - public NoElementFoundException(String msg) { - super(msg); - } -} \ No newline at end of file diff --git a/src/musichub/business/NoPlayListFoundException.java b/src/musichub/business/NoPlayListFoundException.java deleted file mode 100644 index c5eb413..0000000 --- a/src/musichub/business/NoPlayListFoundException.java +++ /dev/null @@ -1,8 +0,0 @@ -package musichub.business; - -public class NoPlayListFoundException extends Exception { - - public NoPlayListFoundException(String msg) { - super(msg); - } -} \ No newline at end of file diff --git a/src/musichub/business/PlayList.java b/src/musichub/business/PlayList.java deleted file mode 100644 index 4180a62..0000000 --- a/src/musichub/business/PlayList.java +++ /dev/null @@ -1,100 +0,0 @@ -package musichub.business; - -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; - -import java.util.ArrayList; -import java.util.UUID; - -public class PlayList { - private final String title; - private final UUID uuid; - private ArrayList elementUUIDs; - - public PlayList(String title, String id, ArrayList elementUUIDs) { - this.title = title; - this.uuid = UUID.fromString(id); - this.elementUUIDs = elementUUIDs; - } - - public PlayList(String title) { - this.title = title; - this.uuid = UUID.randomUUID(); - this.elementUUIDs = new ArrayList<>(); - } - - public PlayList(Element xmlElement) { - { - this.title = xmlElement.getElementsByTagName("title").item(0).getTextContent(); - - String uuid = null; - try { - uuid = xmlElement.getElementsByTagName("UUID").item(0).getTextContent(); - } catch (Exception ex) { - System.out.println("Empty playlist UUID, will create a new one"); - } - if ((uuid == null) || (uuid.isEmpty())) - this.uuid = UUID.randomUUID(); - else this.uuid = UUID.fromString(uuid); - - //parse list of elements: - Node elementsElement = xmlElement.getElementsByTagName("elements").item(0); - NodeList elementUUIDNodes = elementsElement.getChildNodes(); - if (elementUUIDNodes == null) return; - - this.elementUUIDs = new ArrayList<>(); - - - for (int i = 0; i < elementUUIDNodes.getLength(); i++) { - if (elementUUIDNodes.item(i).getNodeType() == Node.ELEMENT_NODE) { - Element elementElement = (Element) elementUUIDNodes.item(i); - if (elementElement.getNodeName().equals("UUID")) { - try { - this.addElement(UUID.fromString(elementElement.getTextContent())); - } catch (Exception ex) { - ex.printStackTrace(); - } - } - } - } - } - } - - public void addElement(UUID element) { - elementUUIDs.add(element); - } - - public ArrayList getElements() { - return elementUUIDs; - } - - public String getTitle() { - return title; - } - - public void createXMLElement(Document document, Element parentElement) { - Element playlistElement = document.createElement("playlist"); - parentElement.appendChild(playlistElement); - - Element nameElement = document.createElement("title"); - nameElement.appendChild(document.createTextNode(title)); - playlistElement.appendChild(nameElement); - - Element UUIDElement = document.createElement("UUID"); - UUIDElement.appendChild(document.createTextNode(uuid.toString())); - playlistElement.appendChild(UUIDElement); - - - Element elementsElement = document.createElement("elements"); - for (UUID currentUUID : this.elementUUIDs) { - - Element elementUUIDElement = document.createElement("UUID"); - elementUUIDElement.appendChild(document.createTextNode(currentUUID.toString())); - elementsElement.appendChild(elementUUIDElement); - } - playlistElement.appendChild(elementsElement); - } - -} \ No newline at end of file diff --git a/src/musichub/business/Song.java b/src/musichub/business/Song.java deleted file mode 100644 index 3e9011b..0000000 --- a/src/musichub/business/Song.java +++ /dev/null @@ -1,56 +0,0 @@ -package musichub.business; - -import org.w3c.dom.Document; -import org.w3c.dom.Element; - - -public class Song extends AudioElement { - private Genre genre; - - public Song(String title, String artist, int length, String uid, String content, String genre) { - super(title, artist, length, uid, content); - this.setGenre(genre); - } - - public Song(String title, String artist, int length, String content, String genre) { - super(title, artist, length, content); - this.setGenre(genre); - } - - public Song(Element xmlElement) { - super(xmlElement); - this.setGenre(xmlElement.getElementsByTagName("genre").item(0).getTextContent()); - } - - public String getGenre() { - return genre.getGenre(); - } - - public void setGenre(String genre) { - switch (genre.toLowerCase()) { - default -> this.genre = Genre.JAZZ; - case "classic" -> this.genre = Genre.CLASSIC; - case "hiphop" -> this.genre = Genre.HIPHOP; - case "rock" -> this.genre = Genre.ROCK; - case "pop" -> this.genre = Genre.POP; - case "rap" -> this.genre = Genre.RAP; - } - } - - public String toString() { - return super.toString() + ", Genre = " + getGenre() + "\n"; - } - - public void createXMLElement(Document document, Element parentElement) { - // song element - Element song = document.createElement("song"); - - super.createXMLElement(document, song); - - Element genreElement = document.createElement("genre"); - genreElement.appendChild(document.createTextNode(genre.getGenre())); - song.appendChild(genreElement); - - parentElement.appendChild(song); - } -} \ No newline at end of file -- cgit v1.2.3