1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="fr"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>MusicHub.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">spoteezer</a> > <a href="index.source.html" class="el_package">musichub.business</a> > <span class="el_source">MusicHub.java</span></div><h1>MusicHub.java</h1><pre class="source lang-java linenums">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.io.IOException;
import java.util.*;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
<span class="nc" id="L15">class SortByDate implements Comparator<Album> {</span>
public int compare(Album a1, Album a2) {
<span class="nc" id="L17"> return a1.getDate().compareTo(a2.getDate());</span>
}
}
<span class="nc" id="L21">class SortByGenre implements Comparator<Song> {</span>
public int compare(Song s1, Song s2) {
<span class="nc" id="L23"> return s1.getGenre().compareTo(s2.getGenre());</span>
}
}
<span class="nc" id="L27">class SortByAuthor implements Comparator<AudioElement> {</span>
public int compare(AudioElement e1, AudioElement e2) {
<span class="nc" id="L29"> return e1.getArtist().compareTo(e2.getArtist());</span>
}
}
public class MusicHub {
<span class="nc" id="L34"> public static final String DIR = System.getProperty("user.dir");</span>
<span class="nc" id="L35"> public static final String ALBUMS_FILE_PATH = DIR + "/files/albums.xml";</span>
<span class="nc" id="L36"> public static final String PLAYLISTS_FILE_PATH = DIR + "/files/playlists.xml";</span>
<span class="nc" id="L37"> public static final String ELEMENTS_FILE_PATH = DIR + "/files/elements.xml";</span>
private final List<Album> albums;
private final List<PlayList> playlists;
private final List<AudioElement> elements;
<span class="nc" id="L41"> private final XMLHandler xmlHandler = new XMLHandler();</span>
<span class="nc" id="L43"> public MusicHub() {</span>
<span class="nc" id="L44"> albums = new LinkedList<>();</span>
<span class="nc" id="L45"> playlists = new LinkedList<>();</span>
<span class="nc" id="L46"> elements = new LinkedList<>();</span>
<span class="nc" id="L47"> this.loadElements();</span>
<span class="nc" id="L48"> this.loadAlbums();</span>
<span class="nc" id="L49"> this.loadPlaylists();</span>
<span class="nc" id="L50"> }</span>
public void addElement(AudioElement element) {
<span class="nc" id="L53"> elements.add(element);</span>
<span class="nc" id="L54"> }</span>
public void addAlbum(Album album) {
<span class="nc" id="L57"> albums.add(album);</span>
<span class="nc" id="L58"> }</span>
public void addPlaylist(PlayList playlist) {
<span class="nc" id="L61"> playlists.add(playlist);</span>
<span class="nc" id="L62"> }</span>
public void deletePlayList(String playListTitle) throws NoPlayListFoundException {
<span class="nc" id="L66"> PlayList thePlayList = null;</span>
<span class="nc" id="L67"> boolean result = false;</span>
<span class="nc bnc" id="L68" title="All 2 branches missed."> for (PlayList pl : playlists) {</span>
<span class="nc bnc" id="L69" title="All 2 branches missed."> if (pl.getTitle().equalsIgnoreCase(playListTitle)) {</span>
<span class="nc" id="L70"> thePlayList = pl;</span>
<span class="nc" id="L71"> break;</span>
}
<span class="nc" id="L73"> }</span>
<span class="nc bnc" id="L75" title="All 2 branches missed."> if (thePlayList != null)</span>
<span class="nc" id="L76"> result = playlists.remove(thePlayList);</span>
<span class="nc bnc" id="L77" title="All 2 branches missed."> if (!result) throw new NoPlayListFoundException("Playlist " + playListTitle + " not found!");</span>
<span class="nc" id="L78"> }</span>
public Iterator<Album> albums() {
<span class="nc" id="L81"> return albums.listIterator();</span>
}
public Iterator<PlayList> playlists() {
<span class="nc" id="L85"> return playlists.listIterator();</span>
}
public Iterator<AudioElement> elements() {
<span class="nc" id="L89"> return elements.listIterator();</span>
}
public String getAlbumsTitlesSortedByDate() {
<span class="nc" id="L93"> StringBuilder titleList = new StringBuilder();</span>
<span class="nc" id="L94"> albums.sort(new SortByDate());</span>
<span class="nc bnc" id="L95" title="All 2 branches missed."> for (Album al : albums)</span>
<span class="nc" id="L96"> titleList.append(al.getTitle()).append("\n");</span>
<span class="nc" id="L97"> return titleList.toString();</span>
}
public String getAudiobooksTitlesSortedByAuthor() {
<span class="nc" id="L101"> StringBuilder titleList = new StringBuilder();</span>
<span class="nc" id="L102"> List<AudioElement> audioBookList = new ArrayList<>();</span>
<span class="nc bnc" id="L103" title="All 2 branches missed."> for (AudioElement ae : elements)</span>
<span class="nc bnc" id="L104" title="All 2 branches missed."> if (ae instanceof AudioBook)</span>
<span class="nc" id="L105"> audioBookList.add(ae);</span>
<span class="nc" id="L106"> audioBookList.sort(new SortByAuthor());</span>
<span class="nc bnc" id="L107" title="All 2 branches missed."> for (AudioElement ab : audioBookList)</span>
<span class="nc" id="L108"> titleList.append(ab.getArtist()).append("\n");</span>
<span class="nc" id="L109"> return titleList.toString();</span>
}
public List<AudioElement> getAlbumSongs(String albumTitle) throws NoAlbumFoundException {
<span class="nc" id="L113"> Album theAlbum = null;</span>
<span class="nc" id="L114"> ArrayList<AudioElement> songsInAlbum = new ArrayList<>();</span>
<span class="nc bnc" id="L115" title="All 2 branches missed."> for (Album al : albums) {</span>
<span class="nc bnc" id="L116" title="All 2 branches missed."> if (al.getTitle().equalsIgnoreCase(albumTitle)) {</span>
<span class="nc" id="L117"> theAlbum = al;</span>
<span class="nc" id="L118"> break;</span>
}
<span class="nc" id="L120"> }</span>
<span class="nc bnc" id="L121" title="All 2 branches missed."> if (theAlbum == null) throw new NoAlbumFoundException("No album with this title in the MusicHub!");</span>
<span class="nc" id="L123"> List<UUID> songIDs = theAlbum.getSongs();</span>
<span class="nc bnc" id="L124" title="All 2 branches missed."> for (UUID id : songIDs)</span>
<span class="nc bnc" id="L125" title="All 2 branches missed."> for (AudioElement el : elements) {</span>
<span class="nc bnc" id="L126" title="All 2 branches missed."> if (el instanceof Song) {</span>
<span class="nc bnc" id="L127" title="All 2 branches missed."> if (el.getUUID().equals(id)) songsInAlbum.add(el);</span>
}
<span class="nc" id="L129"> }</span>
<span class="nc" id="L130"> return songsInAlbum;</span>
}
public List<Song> getAlbumSongsSortedByGenre(String albumTitle) throws NoAlbumFoundException {
<span class="nc" id="L135"> Album theAlbum = null;</span>
<span class="nc" id="L136"> ArrayList<Song> songsInAlbum = new ArrayList<>();</span>
<span class="nc bnc" id="L137" title="All 2 branches missed."> for (Album al : albums) {</span>
<span class="nc bnc" id="L138" title="All 2 branches missed."> if (al.getTitle().equalsIgnoreCase(albumTitle)) {</span>
<span class="nc" id="L139"> theAlbum = al;</span>
<span class="nc" id="L140"> break;</span>
}
<span class="nc" id="L142"> }</span>
<span class="nc bnc" id="L143" title="All 2 branches missed."> if (theAlbum == null) throw new NoAlbumFoundException("No album with this title in the MusicHub!");</span>
<span class="nc" id="L145"> List<UUID> songIDs = theAlbum.getSongs();</span>
<span class="nc bnc" id="L146" title="All 2 branches missed."> for (UUID id : songIDs)</span>
<span class="nc bnc" id="L147" title="All 2 branches missed."> for (AudioElement el : elements) {</span>
<span class="nc bnc" id="L148" title="All 2 branches missed."> if (el instanceof Song) {</span>
<span class="nc bnc" id="L149" title="All 2 branches missed."> if (el.getUUID().equals(id)) songsInAlbum.add((Song) el);</span>
}
<span class="nc" id="L151"> }</span>
<span class="nc" id="L152"> songsInAlbum.sort(new SortByGenre());</span>
<span class="nc" id="L153"> return songsInAlbum;</span>
}
public void addElementToAlbum(String elementTitle, String albumTitle) throws NoAlbumFoundException, NoElementFoundException {
<span class="nc" id="L158"> Album theAlbum = null;</span>
int i;
<span class="nc" id="L160"> boolean found = false;</span>
<span class="nc bnc" id="L161" title="All 2 branches missed."> for (i = 0; i < albums.size(); i++) {</span>
<span class="nc bnc" id="L162" title="All 2 branches missed."> if (albums.get(i).getTitle().equalsIgnoreCase(albumTitle)) {</span>
<span class="nc" id="L163"> theAlbum = albums.get(i);</span>
<span class="nc" id="L164"> found = true;</span>
<span class="nc" id="L165"> break;</span>
}
}
<span class="nc bnc" id="L169" title="All 2 branches missed."> if (found) {</span>
<span class="nc" id="L170"> AudioElement theElement = null;</span>
<span class="nc bnc" id="L171" title="All 2 branches missed."> for (AudioElement ae : elements) {</span>
<span class="nc bnc" id="L172" title="All 2 branches missed."> if (ae.getTitle().equalsIgnoreCase(elementTitle)) {</span>
<span class="nc" id="L173"> theElement = ae;</span>
<span class="nc" id="L174"> break;</span>
}
<span class="nc" id="L176"> }</span>
<span class="nc bnc" id="L177" title="All 2 branches missed."> if (theElement != null) {</span>
<span class="nc" id="L178"> theAlbum.addSong(theElement.getUUID());</span>
//replace the album in the list
<span class="nc" id="L180"> albums.set(i, theAlbum);</span>
<span class="nc" id="L181"> } else throw new NoElementFoundException("Element " + elementTitle + " not found!");</span>
<span class="nc" id="L182"> } else throw new NoAlbumFoundException("Album " + albumTitle + " not found!");</span>
<span class="nc" id="L184"> }</span>
public void addElementToPlayList(String elementTitle, String playListTitle) throws NoPlayListFoundException, NoElementFoundException {
<span class="nc" id="L187"> PlayList thePlaylist = null;</span>
int i;
<span class="nc" id="L189"> boolean found = false;</span>
<span class="nc bnc" id="L191" title="All 2 branches missed."> for (i = 0; i < playlists.size(); i++) {</span>
<span class="nc bnc" id="L192" title="All 2 branches missed."> if (playlists.get(i).getTitle().equalsIgnoreCase(playListTitle)) {</span>
<span class="nc" id="L193"> thePlaylist = playlists.get(i);</span>
<span class="nc" id="L194"> found = true;</span>
<span class="nc" id="L195"> break;</span>
}
}
<span class="nc bnc" id="L199" title="All 2 branches missed."> if (found) {</span>
<span class="nc" id="L200"> AudioElement theElement = null;</span>
<span class="nc bnc" id="L201" title="All 2 branches missed."> for (AudioElement ae : elements) {</span>
<span class="nc bnc" id="L202" title="All 2 branches missed."> if (ae.getTitle().equalsIgnoreCase(elementTitle)) {</span>
<span class="nc" id="L203"> theElement = ae;</span>
<span class="nc" id="L204"> break;</span>
}
<span class="nc" id="L206"> }</span>
<span class="nc bnc" id="L207" title="All 2 branches missed."> if (theElement != null) {</span>
<span class="nc" id="L208"> thePlaylist.addElement(theElement.getUUID());</span>
//replace the album in the list
<span class="nc" id="L210"> playlists.set(i, thePlaylist);</span>
<span class="nc" id="L211"> } else throw new NoElementFoundException("Element " + elementTitle + " not found!");</span>
<span class="nc" id="L213"> } else throw new NoPlayListFoundException("Playlist " + playListTitle + " not found!");</span>
<span class="nc" id="L215"> }</span>
private void loadAlbums() {
<span class="nc" id="L218"> NodeList albumNodes = xmlHandler.parseXMLFile(ALBUMS_FILE_PATH);</span>
<span class="nc bnc" id="L219" title="All 2 branches missed."> if (albumNodes == null) return;</span>
<span class="nc bnc" id="L221" title="All 2 branches missed."> for (int i = 0; i < albumNodes.getLength(); i++) {</span>
<span class="nc bnc" id="L222" title="All 2 branches missed."> if (albumNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {</span>
<span class="nc" id="L223"> Element albumElement = (Element) albumNodes.item(i);</span>
<span class="nc bnc" id="L224" title="All 2 branches missed."> if (albumElement.getNodeName().equals("album")) {</span>
try {
<span class="nc" id="L226"> this.addAlbum(new Album(albumElement));</span>
<span class="nc" id="L227"> } catch (Exception ex) {</span>
<span class="nc" id="L228"> System.out.println("Something is wrong with the XML album element");</span>
<span class="nc" id="L229"> }</span>
}
}
}
<span class="nc" id="L233"> }</span>
private void loadPlaylists() {
<span class="nc" id="L236"> NodeList playlistNodes = xmlHandler.parseXMLFile(PLAYLISTS_FILE_PATH);</span>
<span class="nc bnc" id="L237" title="All 2 branches missed."> if (playlistNodes == null) return;</span>
<span class="nc bnc" id="L239" title="All 2 branches missed."> for (int i = 0; i < playlistNodes.getLength(); i++) {</span>
<span class="nc bnc" id="L240" title="All 2 branches missed."> if (playlistNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {</span>
<span class="nc" id="L241"> Element playlistElement = (Element) playlistNodes.item(i);</span>
<span class="nc bnc" id="L242" title="All 2 branches missed."> if (playlistElement.getNodeName().equals("playlist")) {</span>
try {
<span class="nc" id="L244"> this.addPlaylist(new PlayList(playlistElement));</span>
<span class="nc" id="L245"> } catch (Exception ex) {</span>
<span class="nc" id="L246"> System.out.println("Something is wrong with the XML playlist element");</span>
<span class="nc" id="L247"> }</span>
}
}
}
<span class="nc" id="L251"> }</span>
private void loadElements() {
<span class="nc" id="L254"> NodeList audioelementsNodes = xmlHandler.parseXMLFile(ELEMENTS_FILE_PATH);</span>
<span class="nc bnc" id="L255" title="All 2 branches missed."> if (audioelementsNodes == null) return;</span>
<span class="nc bnc" id="L257" title="All 2 branches missed."> for (int i = 0; i < audioelementsNodes.getLength(); i++) {</span>
<span class="nc bnc" id="L258" title="All 2 branches missed."> if (audioelementsNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {</span>
<span class="nc" id="L259"> Element audioElement = (Element) audioelementsNodes.item(i);</span>
<span class="nc bnc" id="L260" title="All 2 branches missed."> if (audioElement.getNodeName().equals("song")) {</span>
try {
<span class="nc" id="L262"> AudioElement newSong = new Song(audioElement);</span>
<span class="nc" id="L263"> this.addElement(newSong);</span>
<span class="nc" id="L264"> } catch (Exception ex) {</span>
<span class="nc" id="L265"> System.out.println("Something is wrong with the XML song element");</span>
<span class="nc" id="L266"> }</span>
}
<span class="nc bnc" id="L268" title="All 2 branches missed."> if (audioElement.getNodeName().equals("audiobook")) {</span>
try {
<span class="nc" id="L270"> AudioElement newAudioBook = new AudioBook(audioElement);</span>
<span class="nc" id="L271"> this.addElement(newAudioBook);</span>
<span class="nc" id="L272"> } catch (Exception ex) {</span>
<span class="nc" id="L273"> System.out.println("Something is wrong with the XML audiobook element");</span>
<span class="nc" id="L274"> }</span>
}
}
}
<span class="nc" id="L278"> }</span>
public void saveAlbums() {
<span class="nc" id="L282"> Document document = xmlHandler.createXMLDocument();</span>
<span class="nc bnc" id="L283" title="All 2 branches missed."> if (document == null) return;</span>
// root element
<span class="nc" id="L286"> Element root = document.createElement("albums");</span>
<span class="nc" id="L287"> document.appendChild(root);</span>
//save all albums
<span class="nc bnc" id="L290" title="All 2 branches missed."> for (Iterator<Album> albumsIter = this.albums(); albumsIter.hasNext(); ) {</span>
<span class="nc" id="L291"> Album currentAlbum = albumsIter.next();</span>
<span class="nc" id="L292"> currentAlbum.createXMLElement(document, root);</span>
<span class="nc" id="L293"> }</span>
<span class="nc" id="L294"> xmlHandler.createXMLFile(document, ALBUMS_FILE_PATH);</span>
<span class="nc" id="L295"> }</span>
public void savePlayLists() {
<span class="nc" id="L298"> Document document = xmlHandler.createXMLDocument();</span>
<span class="nc bnc" id="L299" title="All 2 branches missed."> if (document == null) return;</span>
// root element
<span class="nc" id="L302"> Element root = document.createElement("playlists");</span>
<span class="nc" id="L303"> document.appendChild(root);</span>
//save all playlists
<span class="nc bnc" id="L306" title="All 2 branches missed."> for (Iterator<PlayList> playlistsIter = this.playlists(); playlistsIter.hasNext(); ) {</span>
<span class="nc" id="L307"> PlayList currentPlayList = playlistsIter.next();</span>
<span class="nc" id="L308"> currentPlayList.createXMLElement(document, root);</span>
<span class="nc" id="L309"> }</span>
<span class="nc" id="L310"> xmlHandler.createXMLFile(document, PLAYLISTS_FILE_PATH);</span>
<span class="nc" id="L311"> }</span>
public void saveElements() {
<span class="nc" id="L314"> Document document = xmlHandler.createXMLDocument();</span>
<span class="nc bnc" id="L315" title="All 2 branches missed."> if (document == null) return;</span>
// root element
<span class="nc" id="L318"> Element root = document.createElement("elements");</span>
<span class="nc" id="L319"> document.appendChild(root);</span>
//save all AudioElements
<span class="nc bnc" id="L322" title="All 2 branches missed."> for (AudioElement currentElement : elements) {</span>
<span class="nc bnc" id="L324" title="All 2 branches missed."> if (currentElement instanceof Song) {</span>
<span class="nc" id="L325"> currentElement.createXMLElement(document, root);</span>
}
<span class="nc bnc" id="L327" title="All 2 branches missed."> if (currentElement instanceof AudioBook) {</span>
<span class="nc" id="L328"> currentElement.createXMLElement(document, root);</span>
}
<span class="nc" id="L330"> }</span>
<span class="nc" id="L331"> xmlHandler.createXMLFile(document, ELEMENTS_FILE_PATH);</span>
<span class="nc" id="L332"> }</span>
public void getAudioElement(List<AudioElement> audios, String elementTitle) throws NoAlbumFoundException, UnsupportedAudioFileException, IOException, LineUnavailableException {
<span class="nc bnc" id="L335" title="All 2 branches missed."> for (AudioElement el : audios) {</span>
<span class="nc bnc" id="L336" title="All 2 branches missed."> if (el.getTitle().equalsIgnoreCase(elementTitle)) {</span>
<span class="nc" id="L337"> el.manageAudioElement();</span>
}
<span class="nc" id="L339"> }</span>
<span class="nc" id="L341"> }</span>
public void searchAudioElement() throws UnsupportedAudioFileException, NoAlbumFoundException, LineUnavailableException, IOException, NoElementFoundException {
<span class="nc" id="L344"> Scanner scanner = new Scanner(System.in);</span>
<span class="nc" id="L345"> System.out.println("Entrez le titre ou l'artiste de la musique que vous souhaitez chercher dans la base de données");</span>
<span class="nc" id="L346"> String word = scanner.next().toLowerCase(Locale.ROOT);</span>
<span class="nc" id="L347"> List<AudioElement> searchResult = new ArrayList<>();</span>
<span class="nc bnc" id="L348" title="All 2 branches missed."> for(AudioElement el : elements){</span>
<span class="nc bnc" id="L349" title="All 4 branches missed."> if(el.getTitle().toLowerCase(Locale.ROOT).contains(word) || el.getArtist().toLowerCase(Locale.ROOT).contains(word)){</span>
<span class="nc" id="L350"> searchResult.add(el);</span>
<span class="nc" id="L351"> System.out.println(el);</span>
}
<span class="nc" id="L353"> }</span>
<span class="nc bnc" id="L355" title="All 2 branches missed."> if(searchResult.isEmpty()){</span>
<span class="nc" id="L356"> throw new NoElementFoundException("Any result for your search");</span>
}
<span class="nc bnc" id="L358" title="All 2 branches missed."> if (searchResult.size()==1){</span>
<span class="nc" id="L359"> this.getAudioElement(searchResult, searchResult.get(0).getTitle());</span>
}
<span class="nc" id="L361"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>
|