aboutsummaryrefslogtreecommitdiff
path: root/src/musichub/business/PlayList.java
blob: 4180a62a5eb5238c95272a7927e6b94ddd9d2974 (plain)
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
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<UUID> elementUUIDs;

    public PlayList(String title, String id, ArrayList<UUID> 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<UUID> 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);
    }

}