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.io.IOException;
22 import java.net.URI;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Optional;
27
28 import org.apache.logging.log4j.LogManager;
29 import org.apache.logging.log4j.Logger;
30 import org.jdom2.Document;
31 import org.jdom2.Element;
32 import org.jdom2.filter.Filters;
33 import org.jdom2.xpath.XPathExpression;
34 import org.jdom2.xpath.XPathFactory;
35 import org.mycore.common.MCRException;
36 import org.xml.sax.SAXParseException;
37
38 import com.google.gson.JsonObject;
39
40
41
42
43
44
45
46
47
48
49 public final class MCRDerivate extends MCRBase {
50
51 private static final Logger LOGGER = LogManager.getLogger();
52
53 public static final String ROOT_NAME = "mycorederivate";
54
55 public static final int MAX_LABEL_LENGTH = 256;
56
57
58 private MCRObjectDerivate mcrDerivate;
59
60 private int order;
61
62 protected String mcrLabel = null;
63
64
65
66
67
68
69
70
71 public MCRDerivate() throws MCRException {
72 super();
73 mcrDerivate = new MCRObjectDerivate(getId());
74 order = 1;
75 }
76
77 public MCRDerivate(byte[] bytes, boolean valid) throws SAXParseException {
78 this();
79 setFromXML(bytes, valid);
80 }
81
82 public MCRDerivate(Document doc) {
83 this();
84 setFromJDOM(doc);
85 }
86
87 public MCRDerivate(URI uri) throws SAXParseException, IOException {
88 this();
89 setFromURI(uri);
90 }
91
92
93
94
95
96
97
98 public MCRObjectDerivate getDerivate() {
99 return mcrDerivate;
100 }
101
102
103
104
105
106
107
108
109
110 @Override
111 protected void setUp() throws MCRException {
112 super.setUp();
113 setLabel(jdomDocument.getRootElement().getAttributeValue("label"));
114
115
116 Element derivateElement = jdomDocument.getRootElement().getChild("derivate");
117 mcrDerivate = new MCRObjectDerivate(mcrId, derivateElement);
118 }
119
120
121
122
123
124
125
126
127 @Override
128 public Document createXML() throws MCRException {
129 Document doc = super.createXML();
130 Element elm = doc.getRootElement();
131 elm.setAttribute("order", String.valueOf(order));
132 if (mcrLabel != null) {
133 elm.setAttribute("label", mcrLabel);
134 }
135 elm.addContent(mcrDerivate.createXML());
136 elm.addContent(mcrService.createXML());
137 return doc;
138 }
139
140 @Override
141 protected String getRootTagName() {
142 return ROOT_NAME;
143 }
144
145
146
147
148
149
150
151 public Map<String, String> getUrnMap() {
152 Map<String, String> fileUrnMap = new HashMap<>();
153
154 XPathExpression<Element> filesetPath = XPathFactory.instance().compile("./mycorederivate/derivate/fileset",
155 Filters.element());
156
157 Element result = filesetPath.evaluateFirst(this.createXML());
158 if (result == null) {
159 return fileUrnMap;
160 }
161 String urn = result.getAttributeValue("urn");
162
163 if (urn != null) {
164 XPathExpression<Element> filePath = XPathFactory
165 .instance()
166 .compile("./mycorederivate/derivate/fileset[@urn='" + urn + "']/file", Filters.element());
167 List<Element> files = filePath.evaluate(this.createXML());
168
169 for (Element currentFileElement : files) {
170 String currentUrn = currentFileElement.getChildText("urn");
171 String currentFile = currentFileElement.getAttributeValue("name");
172 fileUrnMap.put(currentFile, currentUrn);
173 }
174 }
175 return fileUrnMap;
176 }
177
178
179
180
181
182
183 public String getLabel() {
184 return mcrLabel;
185 }
186
187
188
189
190 public void debug() {
191 if (LOGGER.isDebugEnabled()) {
192 LOGGER.debug("MCRDerivate ID : {}", mcrId);
193 LOGGER.debug("MCRDerivate Schema : {}", mcrSchema);
194 LOGGER.debug("");
195 }
196 }
197
198
199
200
201
202
203
204
205
206
207
208
209 @Override
210 public void validate() throws MCRException {
211 super.validate();
212 MCRObjectDerivate derivate = getDerivate();
213 if (derivate == null) {
214 throw new MCRException("The <derivate> part of '" + getId() + "' is undefined.");
215 }
216 try {
217 derivate.validate();
218 } catch (Exception exc) {
219 throw new MCRException("The <derivate> part of '" + getId() + "' is invalid.", exc);
220 }
221 }
222
223
224
225
226 public MCRObjectID getOwnerID() {
227 return this.getDerivate().getMetaLink().getXLinkHrefID();
228 }
229
230 @Override
231 public void setId(MCRObjectID id) {
232 super.setId(id);
233 this.mcrDerivate.setDerivateID(id);
234 }
235
236
237
238
239
240
241 public void setLabel(String label) {
242 if (label == null) {
243 mcrLabel = label;
244 } else {
245 mcrLabel = label.trim();
246 if (mcrLabel.length() > MAX_LABEL_LENGTH) {
247 mcrLabel = mcrLabel.substring(0, MAX_LABEL_LENGTH);
248 }
249 }
250 }
251
252 public int getOrder() {
253 return order;
254 }
255
256 public void setOrder(int order) {
257 this.order = order;
258 }
259
260 @Override
261 protected void setFromJDOM(Document doc) {
262 super.setFromJDOM(doc);
263 this.order = Optional.ofNullable(doc.getRootElement().getAttributeValue("order"))
264 .map(Integer::valueOf)
265 .orElse(1);
266 }
267
268 @Override
269 public JsonObject createJSON() {
270 JsonObject base = super.createJSON();
271 if (mcrLabel != null) {
272 base.addProperty("label", mcrLabel);
273 }
274 return base;
275 }
276 }