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
|
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;
class SortByDate implements Comparator<Album> {
public int compare(Album a1, Album a2) {
return a1.getDate().compareTo(a2.getDate());
}
}
class SortByGenre implements Comparator<Song> {
public int compare(Song s1, Song s2) {
return s1.getGenre().compareTo(s2.getGenre());
}
}
class SortByAuthor implements Comparator<AudioElement> {
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<Album> albums;
private final List<PlayList> playlists;
private final List<AudioElement> 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<Album> albums() {
return albums.listIterator();
}
public Iterator<PlayList> playlists() {
return playlists.listIterator();
}
public Iterator<AudioElement> 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<AudioElement> 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<AudioElement> getAlbumSongs(String albumTitle) throws NoAlbumFoundException {
Album theAlbum = null;
ArrayList<AudioElement> 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<UUID> 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<Song> getAlbumSongsSortedByGenre(String albumTitle) throws NoAlbumFoundException {
Album theAlbum = null;
ArrayList<Song> 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<UUID> 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<Album> 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<PlayList> 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);
}
public void getAudioElement(List<AudioElement> audios, String elementTitle) throws NoAlbumFoundException, UnsupportedAudioFileException, IOException, LineUnavailableException {
for (AudioElement el : audios) {
if (el.getTitle().equalsIgnoreCase(elementTitle)) {
el.manageAudioElement();
}
}
}
}
|