1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.datamodel.metadata;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.List;
25
26 import org.jdom2.Element;
27 import org.mycore.datamodel.classifications2.MCRCategoryID;
28
29
30
31
32
33
34
35
36 public class MCRFileMetadata implements Comparable<MCRFileMetadata> {
37
38 private Collection<MCRCategoryID> categories;
39
40 private String urn, handle;
41
42 private String name;
43
44 public MCRFileMetadata(Element file) {
45 this.name = file.getAttributeValue("name");
46 this.urn = file.getChildText("urn");
47 this.handle = file.getChildText("handle");
48
49 List<Element> categoryElements = file.getChildren("category");
50 this.categories = Collections.emptySet();
51 if (!categoryElements.isEmpty()) {
52 categories = new ArrayList<>(categoryElements.size());
53 for (Element categElement : categoryElements) {
54 MCRCategoryID categId = MCRCategoryID.fromString(categElement.getAttributeValue("id"));
55 categories.add(categId);
56 }
57 }
58 }
59
60 public MCRFileMetadata() {
61 this(null, null, null);
62 }
63
64 public MCRFileMetadata(String name, String urn, Collection<MCRCategoryID> categories) {
65 super();
66 setName(name);
67 setUrn(urn);
68 setHandle(null);
69 setCategories(categories);
70 }
71
72 public MCRFileMetadata(String name, String urn, String handle, Collection<MCRCategoryID> categories) {
73 this(name, urn, categories);
74 setHandle(handle);
75 }
76
77 public Element createXML() {
78 Element file = new Element("file");
79 file.setAttribute("name", name);
80 if (urn != null) {
81 Element urn = new Element("urn");
82 urn.setText(this.urn);
83 file.addContent(urn);
84 }
85 if (handle != null) {
86 Element handle = new Element("handle");
87 handle.setText(this.handle);
88 file.addContent(handle);
89 }
90 for (MCRCategoryID categid : categories) {
91 Element category = new Element("category");
92 category.setAttribute("id", categid.toString());
93 file.addContent(category);
94 }
95 return file;
96 }
97
98 public Collection<MCRCategoryID> getCategories() {
99 return Collections.unmodifiableCollection(categories);
100 }
101
102 public void setCategories(Collection<MCRCategoryID> categories) {
103 if (categories == null || categories.isEmpty()) {
104 this.categories = Collections.emptySet();
105 } else {
106 this.categories = new ArrayList<>(categories);
107 }
108 }
109
110 public String getUrn() {
111 return urn;
112 }
113
114 public void setUrn(String urn) {
115 this.urn = urn;
116 }
117
118
119
120
121 public String getHandle() {
122 return handle;
123 }
124
125
126
127
128 public void setHandle(String handle) {
129 this.handle = handle;
130 }
131
132 public String getName() {
133 return name;
134 }
135
136 public void setName(String name) {
137 this.name = name;
138 }
139
140 @Override
141 public int compareTo(MCRFileMetadata o) {
142 return this.name.compareTo(o.name);
143 }
144 }