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.Objects;
23 import java.util.stream.Collectors;
24
25 import org.apache.logging.log4j.LogManager;
26 import org.apache.logging.log4j.Logger;
27 import org.jdom2.Element;
28 import org.jdom2.Namespace;
29 import org.mycore.common.MCRCalendar;
30 import org.mycore.common.MCRException;
31
32 import com.ibm.icu.util.Calendar;
33 import com.ibm.icu.util.GregorianCalendar;
34
35
36
37
38
39
40
41
42
43
44
45 public class MCRMetaHistoryDate extends MCRMetaDefault {
46
47
48 private static final Logger LOGGER = LogManager.getLogger();
49
50
51 public static final int MCRHISTORYDATE_MAX_TEXT = 512;
52
53
54 private ArrayList<MCRMetaHistoryDateText> texts;
55
56 private Calendar von;
57
58 private Calendar bis;
59
60 private int ivon;
61
62 private int ibis;
63
64 private String calendar;
65
66
67
68
69
70
71
72
73 public MCRMetaHistoryDate() {
74 super();
75 texts = new ArrayList<>();
76 calendar = MCRCalendar.CALENDARS_LIST.get(0);
77 setDefaultVon();
78 setDefaultBis();
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 public MCRMetaHistoryDate(String subtag, String type, int inherted) throws MCRException {
98 super(subtag, null, type, inherted);
99 texts = new ArrayList<>();
100 calendar = MCRCalendar.CALENDARS_LIST.get(0);
101 setDefaultVon();
102 setDefaultBis();
103 }
104
105
106
107
108
109
110
111
112 @Deprecated
113 public final void setText(String text) {
114 setText(text, lang);
115 }
116
117
118
119
120
121
122
123
124
125
126 public final void setText(String text, String lang) {
127 if (text == null || text.length() == 0) {
128 LOGGER.warn("The text field of MCRMeataHistoryDate is empty.");
129 return;
130 }
131 if (text.length() <= MCRHISTORYDATE_MAX_TEXT) {
132 text = text.trim();
133 } else {
134 text = text.substring(0, MCRHISTORYDATE_MAX_TEXT);
135 }
136 if (lang == null || lang.length() == 0) {
137 addText(text, this.lang);
138 } else {
139 addText(text, lang);
140 }
141 }
142
143
144
145
146
147
148
149
150
151
152
153 public final void addText(String text, String lang) {
154 if (text == null || text.length() == 0) {
155 LOGGER.warn("The text field of MCRMeataHistoryDate is empty.");
156 return;
157 }
158 if (lang == null || lang.length() == 0) {
159 LOGGER.warn("The lang field of MCRMeataHistoryDate is empty.");
160 return;
161 }
162 for (int i = 0; i < texts.size(); i++) {
163 if (texts.get(i).getLang().equals(lang)) {
164 texts.remove(i);
165 break;
166 }
167 }
168 texts.add(new MCRMetaHistoryDateText(text, lang));
169 }
170
171
172
173
174
175
176
177
178
179 public final MCRMetaHistoryDateText getText(String lang) {
180 if (lang == null) {
181 return null;
182 }
183 return texts.stream()
184 .filter(text -> text.getLang().equals(lang))
185 .findFirst()
186 .orElse(null);
187 }
188
189
190
191
192
193
194
195
196
197 public final MCRMetaHistoryDateText getText(int index) {
198 if (index >= 0 && index < texts.size()) {
199 return texts.get(index);
200 }
201 return null;
202 }
203
204
205
206
207
208
209 public final ArrayList<MCRMetaHistoryDateText> getTexts() {
210 return texts;
211 }
212
213
214
215
216
217
218 public final int textSize() {
219 return texts.size();
220 }
221
222
223
224
225
226
227
228 public final void setCalendar(String calstr) {
229 if (calstr == null || calstr.trim().length() == 0 || (!MCRCalendar.CALENDARS_LIST.contains(calstr))) {
230 calendar = MCRCalendar.TAG_GREGORIAN;
231 LOGGER.warn("The calendar field of MCRMeataHistoryDate is set to default " + MCRCalendar.TAG_GREGORIAN
232 + ".");
233 return;
234 }
235 calendar = calstr;
236 }
237
238
239
240
241
242
243
244 public final void setCalendar(Calendar calendar) {
245 this.calendar = MCRCalendar.getCalendarTypeString(calendar);
246 }
247
248
249
250
251 public final void setDefaultVon() {
252 von = new GregorianCalendar();
253 von.set(Calendar.JULIAN_DAY, MCRCalendar.MIN_JULIAN_DAY_NUMBER);
254 ivon = MCRCalendar.MIN_JULIAN_DAY_NUMBER;
255 }
256
257
258
259
260 public final void setDefaultBis() {
261 bis = new GregorianCalendar();
262 bis.set(Calendar.JULIAN_DAY, MCRCalendar.MAX_JULIAN_DAY_NUMBER);
263 ibis = MCRCalendar.MAX_JULIAN_DAY_NUMBER;
264 }
265
266
267
268
269
270
271
272 public final void setVonDate(Calendar calendar) {
273 if (calendar == null) {
274 setDefaultVon();
275 LOGGER.warn("The calendar to set 'von' is null, default is set.");
276 } else {
277 von = calendar;
278 ivon = von.get(Calendar.JULIAN_DAY);
279 }
280 }
281
282
283
284
285
286
287
288
289
290 public final void setVonDate(String date, String calendar) {
291 try {
292 von = MCRCalendar.getHistoryDateAsCalendar(date, false, calendar);
293 ivon = von.get(Calendar.JULIAN_DAY);
294 } catch (Exception e) {
295 e.printStackTrace();
296 LOGGER.warn("The von date {} for calendar {} is false. Set to default!", date, calendar);
297 setDefaultVon();
298 }
299 }
300
301
302
303
304
305
306
307 public final void setBisDate(Calendar calendar) {
308 if (calendar == null) {
309 setDefaultBis();
310 LOGGER.warn("The calendar to set 'bis' is null, default is set.");
311 } else {
312 bis = calendar;
313 ibis = bis.get(Calendar.JULIAN_DAY);
314 }
315 }
316
317
318
319
320
321
322
323
324
325 public final void setBisDate(String date, String calendar) {
326 Calendar c = bis;
327 try {
328 c = MCRCalendar.getHistoryDateAsCalendar(date, true, calendar);
329 } catch (Exception e) {
330 LOGGER.warn("The bis date {} for calendar {} is false.", date, calendar);
331 c = null;
332 }
333 setBisDate(c);
334 }
335
336
337
338
339
340
341 public final String getCalendar() {
342 return calendar;
343 }
344
345
346
347
348
349
350 public final Calendar getVon() {
351 return von;
352 }
353
354
355
356
357
358
359 public final String getVonToString() {
360 return MCRCalendar.getCalendarDateToFormattedString(von);
361 }
362
363
364
365
366
367
368 public final int getIvon() {
369 return ivon;
370 }
371
372
373
374
375
376
377 public final Calendar getBis() {
378 return bis;
379 }
380
381
382
383
384
385
386 public final String getBisToString() {
387 return MCRCalendar.getCalendarDateToFormattedString(bis);
388 }
389
390
391
392
393
394
395 public final int getIbis() {
396 return ibis;
397 }
398
399
400
401
402
403
404
405
406 @Override
407 public void setFromDOM(Element element) {
408 super.setFromDOM(element);
409 texts.clear();
410
411 for (Element textElement : element.getChildren("text")) {
412 String text = textElement.getText();
413 String lang = textElement.getAttributeValue("lang", Namespace.XML_NAMESPACE);
414 if (lang != null) {
415 setText(text, lang);
416 } else {
417 setText(text);
418 }
419 }
420 setCalendar(element.getChildTextTrim("calendar"));
421 setVonDate(element.getChildTextTrim("von"), calendar);
422 setBisDate(element.getChildTextTrim("bis"), calendar);
423
424
425
426
427
428 if (calendar.equals(MCRCalendar.TAG_JULIAN)) {
429 calendar = MCRCalendar.TAG_GREGORIAN;
430 }
431 }
432
433
434
435
436
437
438
439
440
441 @Override
442 public Element createXML() throws MCRException {
443 Element elm = super.createXML();
444 for (MCRMetaHistoryDateText text : texts) {
445 Element elmt = new Element("text");
446 elmt.addContent(text.getText());
447 elmt.setAttribute("lang", text.getLang(), Namespace.XML_NAMESPACE);
448 elm.addContent(elmt);
449 }
450 elm.addContent(new Element("calendar").addContent(calendar));
451
452 elm.addContent(new Element("ivon").addContent(Integer.toString(ivon)));
453 elm.addContent(new Element("von").addContent(getVonToString()));
454
455 elm.addContent(new Element("ibis").addContent(Integer.toString(ibis)));
456 elm.addContent(new Element("bis").addContent(getBisToString()));
457 return elm;
458 }
459
460
461
462
463
464
465
466
467
468
469
470
471
472 @Override
473 public void validate() throws MCRException {
474 super.validate();
475 texts.removeIf(textItem -> !textItem.isValid());
476 if (texts.size() == 0) {
477 throw new MCRException(getSubTag() + ": no texts defined");
478 }
479 if (von == null || bis == null || calendar == null) {
480 throw new MCRException(getSubTag() + ": von,bis or calendar are null");
481 }
482 if (ibis < ivon) {
483 Calendar swp = (Calendar) von.clone();
484 setVonDate((Calendar) bis.clone());
485 setBisDate(swp);
486 }
487 }
488
489
490
491
492
493
494
495
496 @Override
497 public MCRMetaHistoryDate clone() {
498 MCRMetaHistoryDate clone = (MCRMetaHistoryDate) super.clone();
499
500 clone.texts = this.texts.stream().map(MCRMetaHistoryDateText::clone)
501 .collect(Collectors.toCollection(ArrayList::new));
502 clone.bis = (Calendar) this.bis.clone();
503 clone.von = (Calendar) this.von.clone();
504 clone.ibis = this.ibis;
505 clone.ivon = this.ivon;
506 clone.calendar = this.calendar;
507
508 return clone;
509 }
510
511
512
513
514 @Override
515 public void debug() {
516 if (LOGGER.isDebugEnabled()) {
517 super.debugDefault();
518 for (MCRMetaHistoryDateText text : texts) {
519 LOGGER.debug("Text / lang = {} / {}", text.getText(), text.getLang());
520 }
521 LOGGER.debug("Calendar = {}", calendar);
522 LOGGER.debug("Von (String) = {}", getVonToString());
523 LOGGER.debug("Von (JulianDay) = {}", ivon);
524 LOGGER.debug("Bis (String) = {}", getBisToString());
525 LOGGER.debug("Bis (JulianDay) = {}", ibis);
526 LOGGER.debug("");
527 }
528 }
529
530
531
532
533 @Override
534 public boolean equals(Object obj) {
535 if (!super.equals(obj)) {
536 return false;
537 }
538 final MCRMetaHistoryDate other = (MCRMetaHistoryDate) obj;
539 boolean fieldTest = Objects.equals(this.calendar, other.calendar) && Objects.equals(this.ivon, other.ivon);
540 boolean textTest = equalText(other.getTexts());
541 return fieldTest && textTest;
542 }
543
544 private boolean equalText(ArrayList<MCRMetaHistoryDateText> objtexts) {
545 boolean testflag = true;
546 int size = texts.size() < objtexts.size() ? texts.size() : objtexts.size();
547 for (int i = 0; i < size; i++) {
548 testflag &= texts.get(i).equals(objtexts.get(i));
549 }
550 return testflag;
551 }
552
553
554
555
556
557
558 public static class MCRMetaHistoryDateText implements Cloneable {
559 private String text;
560
561 private String lang;
562
563 public MCRMetaHistoryDateText() {
564 text = "";
565 lang = DEFAULT_LANGUAGE;
566 }
567
568 public MCRMetaHistoryDateText(String text, String lang) {
569 setText(text, lang);
570 }
571
572
573
574
575
576
577
578 public String getText() {
579 return text;
580 }
581
582
583
584
585
586
587 public String getLang() {
588 return lang;
589 }
590
591
592
593
594
595
596
597
598
599 public void setText(String text, String lang) {
600 if (lang == null || lang.length() == 0) {
601 this.lang = DEFAULT_LANGUAGE;
602 } else {
603 this.lang = lang;
604 }
605 if (text == null || text.length() == 0) {
606 this.text = "";
607 } else {
608 this.text = text;
609 }
610 }
611
612
613
614
615
616
617
618 @Deprecated
619 public void setText(String text) {
620 if (text == null || text.length() == 0) {
621 this.text = "";
622 } else {
623 this.text = text;
624 }
625 }
626
627
628
629
630
631
632
633 @Deprecated
634 public void setLang(String lang) {
635 if (lang == null || lang.length() == 0) {
636 this.lang = DEFAULT_LANGUAGE;
637 } else {
638 this.lang = lang;
639 }
640 }
641
642
643
644
645
646
647 public boolean isValid() {
648 return !(lang.length() == 0 || text.length() == 0);
649 }
650
651
652
653
654
655
656
657
658 public boolean equals(MCRMetaHistoryDateText obj) {
659 return lang.equals(obj.getLang()) && text.equals(obj.getText());
660 }
661
662 @Override
663 protected MCRMetaHistoryDateText clone() {
664 MCRMetaHistoryDateText clone = null;
665 try {
666 clone = (MCRMetaHistoryDateText) super.clone();
667 } catch (Exception e) {
668
669 }
670
671 clone.text = this.text;
672 clone.lang = this.lang;
673
674 return clone;
675 }
676 }
677 }