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.math.BigDecimal;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.Locale;
26 import java.util.stream.Collectors;
27
28 import org.jdom2.Element;
29 import org.mycore.common.MCRException;
30
31 import com.google.gson.JsonArray;
32 import com.google.gson.JsonObject;
33
34
35
36
37
38
39
40
41 public class MCRMetaSpatial extends MCRMetaDefault {
42
43 private List<BigDecimal> data;
44
45
46
47
48 public MCRMetaSpatial() {
49 super();
50 this.data = new ArrayList<>();
51 }
52
53 @Deprecated
54 public MCRMetaSpatial(String subtag, String defaultLanguage, String type, Integer inherited) throws MCRException {
55 super(subtag, defaultLanguage, type, inherited);
56 this.data = new ArrayList<>();
57 }
58
59
60
61
62
63
64
65
66
67 public MCRMetaSpatial(String subtag, String type, Integer inherited) throws MCRException {
68 super(subtag, null, type, inherited);
69 this.data = new ArrayList<>();
70 }
71
72
73
74
75
76
77
78 public List<BigDecimal> getData() {
79 return this.data;
80 }
81
82 public void setData(List<BigDecimal> data) {
83 this.data = data;
84 }
85
86
87
88
89
90
91
92 public void add(BigDecimal lat, BigDecimal lng) {
93 this.data.add(lat);
94 this.data.add(lng);
95 }
96
97 @Override
98 public void setFromDOM(Element element) throws MCRException {
99 super.setFromDOM(element);
100 String textData = element.getText();
101 String[] splitData = textData.split(",");
102 if (splitData.length % 2 != 0) {
103 throw new MCRException(String.format(Locale.ROOT,
104 "Unable to parse MCRMetaSpatial cause text data '%s' contains invalid content", textData));
105 }
106 try {
107 Arrays.stream(splitData).map(BigDecimal::new).forEach(this.data::add);
108 } catch (NumberFormatException nfe) {
109 throw new MCRException(String.format(Locale.ROOT,
110 "Unable to parse MCRMetaSpatial cause text data '%s' contains invalid content", textData), nfe);
111 }
112 }
113
114 @Override
115 public Element createXML() throws MCRException {
116 Element element = super.createXML();
117 return element.setText(this.data.stream().map(BigDecimal::toPlainString).collect(Collectors.joining(",")));
118 }
119
120
121
122
123
124
125
126
127
128
129
130
131 @Override
132 public JsonObject createJSON() {
133 JsonObject json = super.createJSON();
134 JsonArray dataArray = new JsonArray();
135 this.data.forEach(dataArray::add);
136 json.add("data", dataArray);
137 return json;
138 }
139
140 @Override
141 public void validate() throws MCRException {
142 super.validate();
143 if (this.data.isEmpty()) {
144 throw new MCRException("spatial list should contain content");
145 }
146 if (this.data.size() % 2 != 0) {
147 throw new MCRException(
148 String.format(Locale.ROOT, "spatial list content '%s' is uneven", this.data.toString()));
149 }
150 }
151
152
153
154
155
156
157
158
159 @Override
160 public MCRMetaSpatial clone() {
161 MCRMetaSpatial clone = (MCRMetaSpatial) super.clone();
162
163 clone.data = new ArrayList<>(this.data);
164
165 return clone;
166 }
167
168 }