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.Date;
22 import java.util.Objects;
23
24 import org.apache.logging.log4j.LogManager;
25 import org.apache.logging.log4j.Logger;
26 import org.jdom2.Element;
27 import org.jdom2.Namespace;
28 import org.mycore.common.MCRException;
29 import org.mycore.datamodel.common.MCRISO8601Date;
30 import org.mycore.datamodel.common.MCRISO8601Format;
31
32 import com.google.gson.JsonObject;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public final class MCRMetaISO8601Date extends MCRMetaDefault {
48
49 private Element export;
50
51 private boolean changed = true;
52
53 private static final Namespace DEFAULT_NAMESPACE = Namespace.NO_NAMESPACE;
54
55 private MCRISO8601Date isoDate;
56
57 private static final Logger LOGGER = LogManager.getLogger();
58
59
60
61
62
63
64 public MCRMetaISO8601Date() {
65 super();
66 this.isoDate = new MCRISO8601Date();
67 }
68
69
70
71
72
73
74 public MCRMetaISO8601Date(String subtag, String type, int inherted) {
75 super(subtag, null, type, inherted);
76 this.isoDate = new MCRISO8601Date();
77 }
78
79
80
81
82
83
84 @Override
85 public Element createXML() throws MCRException {
86 if (!changed) {
87 return export.clone();
88 }
89 Element elm = super.createXML();
90 if (!(isoDate.getIsoFormat() == null || isoDate.getIsoFormat() == MCRISO8601Format.COMPLETE_HH_MM_SS_SSS)) {
91 elm.setAttribute("format", isoDate.getIsoFormat().toString());
92 }
93 elm.setText(getISOString());
94 export = elm;
95 changed = false;
96 return export.clone();
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110 @Override
111 public JsonObject createJSON() {
112 JsonObject obj = super.createJSON();
113 obj.addProperty("date", getISOString());
114 if (isoDate.getIsoFormat() != null) {
115 obj.addProperty("format", isoDate.getIsoFormat().toString());
116 }
117 return obj;
118 }
119
120 @Override
121 public void setFromDOM(Element element) {
122 super.setFromDOM(element);
123 setFormat(element.getAttributeValue("format"));
124 setDate(element.getTextTrim());
125 export = element.clone();
126 }
127
128
129
130
131
132
133 protected static Namespace getNs() {
134 return DEFAULT_NAMESPACE;
135 }
136
137
138
139
140
141
142
143 public void setDate(String isoString) {
144 isoDate.setDate(isoString);
145 }
146
147
148
149
150
151
152 public Date getDate() {
153 return isoDate.getDate();
154 }
155
156
157
158
159
160
161
162 public void setDate(Date dt) {
163 isoDate.setDate(dt);
164 }
165
166
167
168
169
170
171 public String getISOString() {
172 return isoDate.getISOString();
173 }
174
175
176
177
178
179
180
181
182
183
184
185
186
187 public void setFormat(String format) {
188 isoDate.setFormat(format);
189 }
190
191 public String getFormat() {
192 return isoDate == null || isoDate.getIsoFormat() == null ? null : isoDate.getIsoFormat().toString();
193 }
194
195
196
197
198
199
200 public MCRISO8601Date getMCRISO8601Date() {
201 return isoDate;
202 }
203
204
205
206
207 @Override
208 public void debug() {
209 if (LOGGER.isDebugEnabled()) {
210 super.debugDefault();
211 LOGGER.debug("Date={}", isoDate != null ? isoDate.getISOString() : "null");
212 if (isoDate != null) {
213 MCRISO8601Format isoFormat = isoDate.getIsoFormat();
214 LOGGER.debug("Format={}", isoFormat);
215 }
216 }
217 }
218
219
220
221
222
223
224
225
226 @Override
227 public MCRMetaISO8601Date clone() {
228 MCRMetaISO8601Date clone = (MCRMetaISO8601Date) super.clone();
229
230 clone.changed = this.changed;
231 clone.export = this.export.clone();
232 clone.isoDate = this.isoDate;
233
234 return clone;
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248 public void validate() throws MCRException {
249 super.validate();
250 if (isoDate == null || isoDate.getDt() == null) {
251 throw new MCRException(getSubTag() + ": date is invalid");
252 }
253 }
254
255 @Override
256 public boolean equals(Object obj) {
257 if (!super.equals(obj)) {
258 return false;
259 }
260 final MCRMetaISO8601Date other = (MCRMetaISO8601Date) obj;
261 return Objects.equals(this.isoDate, other.isoDate);
262 }
263
264 }