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.Date;
23 import java.util.List;
24 import java.util.Optional;
25 import java.util.function.Consumer;
26 import java.util.stream.Collectors;
27 import java.util.stream.IntStream;
28
29 import org.apache.commons.lang3.StringUtils;
30 import org.apache.logging.log4j.LogManager;
31 import org.apache.logging.log4j.Logger;
32 import org.jdom2.Element;
33 import org.mycore.common.MCRException;
34 import org.mycore.common.MCRUtils;
35 import org.mycore.common.config.MCRConfiguration2;
36 import org.mycore.datamodel.classifications2.MCRCategoryDAOFactory;
37 import org.mycore.datamodel.classifications2.MCRCategoryID;
38 import org.mycore.datamodel.common.MCRISO8601Date;
39
40 import com.google.gson.JsonArray;
41 import com.google.gson.JsonObject;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public class MCRObjectService {
77
78 private static final Logger LOGGER = LogManager.getLogger(MCRObjectService.class);
79
80
81
82
83 public static final String DATE_TYPE_CREATEDATE = "createdate";
84
85
86
87
88 public static final String DATE_TYPE_MODIFYDATE = "modifydate";
89
90
91
92
93 public static final String FLAG_TYPE_CREATEDBY = "createdby";
94
95
96
97
98 public static final String FLAG_TYPE_MODIFIEDBY = "modifiedby";
99
100 private final ArrayList<MCRMetaISO8601Date> dates = new ArrayList<>();
101
102 private final ArrayList<MCRMetaAccessRule> rules = new ArrayList<>();
103
104 private final ArrayList<MCRMetaLangText> flags = new ArrayList<>();
105
106 private final ArrayList<MCRMetaDateLangText> messages = new ArrayList<>();
107
108 private final ArrayList<MCRMetaClassification> classifications = new ArrayList<>();
109
110 private MCRCategoryID state;
111
112
113
114
115
116 public MCRObjectService() {
117
118 Date now = new Date();
119
120 MCRMetaISO8601Date createDate = new MCRMetaISO8601Date("servdate", DATE_TYPE_CREATEDATE, 0);
121 createDate.setDate(now);
122 dates.add(createDate);
123
124 MCRMetaISO8601Date modifyDate = new MCRMetaISO8601Date("servdate", DATE_TYPE_MODIFYDATE, 0);
125 modifyDate.setDate(now);
126 dates.add(modifyDate);
127
128 }
129
130
131
132
133
134
135
136
137 public final void setFromDOM(Element service) {
138
139
140 Element datesElement = service.getChild("servdates");
141 dates.clear();
142
143 if (datesElement != null) {
144 List<Element> dateElements = datesElement.getChildren();
145 for (Element dateElement : dateElements) {
146 String dateElementName = dateElement.getName();
147 if (!"servdate".equals(dateElementName)) {
148 continue;
149 }
150 MCRMetaISO8601Date date = new MCRMetaISO8601Date();
151 date.setFromDOM(dateElement);
152 setDate(date);
153 }
154 }
155
156
157 Element rulesElement = service.getChild("servacls");
158 if (rulesElement != null) {
159 List<Element> ruleElements = rulesElement.getChildren();
160 for (Element ruleElement : ruleElements) {
161 if (!ruleElement.getName().equals("servacl")) {
162 continue;
163 }
164 MCRMetaAccessRule user = new MCRMetaAccessRule();
165 user.setFromDOM(ruleElement);
166 rules.add(user);
167 }
168 }
169
170
171 Element flagsElement = service.getChild("servflags");
172 if (flagsElement != null) {
173 List<Element> flagElements = flagsElement.getChildren();
174 for (Element flagElement : flagElements) {
175 if (!flagElement.getName().equals("servflag")) {
176 continue;
177 }
178 MCRMetaLangText flag = new MCRMetaLangText();
179 flag.setFromDOM(flagElement);
180 flags.add(flag);
181 }
182 }
183
184
185 Element classificationsElement = service.getChild("servclasses");
186 if (classificationsElement != null) {
187 List<Element> classificationElements = classificationsElement.getChildren();
188 for (Element classificationElement : classificationElements) {
189 if (!classificationElement.getName().equals("servclass")) {
190 continue;
191 }
192 MCRMetaClassification classification = new MCRMetaClassification();
193 classification.setFromDOM(classificationElement);
194 classifications.add(classification);
195 }
196 }
197
198
199 Element messagesElement = service.getChild("servmessages");
200 if (messagesElement != null) {
201 List<Element> messageElements = messagesElement.getChildren();
202 for (Element messageElement : messageElements) {
203 if (!messageElement.getName().equals("servmessage")) {
204 continue;
205 }
206 MCRMetaDateLangText message = new MCRMetaDateLangText();
207 message.setFromDOM(messageElement);
208 messages.add(message);
209 }
210 }
211
212
213 Element statesElement = service.getChild("servstates");
214 if (statesElement != null) {
215 List<Element> stateElements = statesElement.getChildren();
216 for (Element stateElement : stateElements) {
217 if (!stateElement.getName().equals("servstate")) {
218 continue;
219 }
220 MCRMetaClassification stateClass = new MCRMetaClassification();
221 stateClass.setFromDOM(stateElement);
222 state = new MCRCategoryID(stateClass.getClassId(), stateClass.getCategId());
223 }
224 }
225 }
226
227
228
229
230
231
232 public final int getDateSize() {
233 return dates.size();
234 }
235
236
237
238
239
240
241 protected ArrayList<MCRMetaISO8601Date> getDates() {
242 return dates;
243 }
244
245
246
247
248
249
250
251
252 public final MCRCategoryID getState() {
253 return state;
254 }
255
256
257
258
259
260
261
262
263
264
265
266
267 public final Date getDate(String type) {
268 MCRMetaISO8601Date isoDate = getISO8601Date(type);
269 if (isoDate == null) {
270 return null;
271 }
272 return isoDate.getDate();
273 }
274
275 private MCRMetaISO8601Date getISO8601Date(String type) {
276 if (type == null || type.length() == 0) {
277 return null;
278 }
279
280 return IntStream.range(0, dates.size())
281 .mapToObj(dates::get)
282 .filter(d -> d.getType().equals(type))
283 .findAny()
284 .orElse(null);
285 }
286
287
288
289
290
291
292
293
294 public final void setDate(String type) {
295 setDate(type, new Date());
296 }
297
298
299
300
301
302
303
304
305
306
307
308 public final void setDate(String type, Date date) {
309 MCRMetaISO8601Date d = getISO8601Date(type);
310 if (d == null) {
311 d = new MCRMetaISO8601Date("servdate", type, 0);
312 d.setDate(date);
313 dates.add(d);
314 } else {
315 d.setDate(date);
316 }
317 }
318
319
320
321
322
323
324
325
326 private void setDate(MCRMetaISO8601Date date) {
327 MCRMetaISO8601Date d = getISO8601Date(date.getType());
328
329 if (d == null) {
330 dates.add(date);
331 } else {
332 d.setDate(date.getDate());
333 }
334 }
335
336
337
338
339
340
341
342
343 public final void removeDate(String type) {
344 if (DATE_TYPE_CREATEDATE.equals(type) || DATE_TYPE_MODIFYDATE.equals(type)) {
345 LOGGER.error("Cannot delete built-in date: " + type);
346 } else {
347 MCRMetaISO8601Date d = getISO8601Date(type);
348 if (d != null) {
349 dates.remove(d);
350 }
351 }
352 }
353
354
355
356
357 public final void setState(MCRCategoryID state) {
358 if (state == null) {
359 this.state = state;
360 } else {
361 if (MCRCategoryDAOFactory.getInstance().exist(state)) {
362 this.state = state;
363 } else {
364 LOGGER.warn("Error at setting servstate classification.",
365 new MCRException("The category " + state + " does not exist."));
366 }
367 }
368 }
369
370
371
372
373
374 public final void setState(String state) {
375 if (state == null) {
376 this.state = null;
377 } else {
378 MCRCategoryID categState = new MCRCategoryID(
379 MCRConfiguration2.getString("MCR.Metadata.Service.State.Classification.ID").orElse("state"),
380 state);
381 setState(categState);
382 }
383 }
384
385
386
387
388 public final void removeState() {
389 this.state = null;
390 }
391
392
393
394
395
396
397
398 public final void addFlag(String value) {
399 addFlag(null, value);
400 }
401
402
403
404
405
406
407
408
409
410 public final void addFlag(String type, String value) {
411 String lType = StringUtils.trim(type);
412 MCRUtils.filterTrimmedNotEmpty(value)
413 .map(flagValue -> new MCRMetaLangText("servflag", null, lType, 0, null, flagValue))
414 .ifPresent(flags::add);
415 }
416
417
418
419
420
421
422 public final String getFlags() {
423 StringBuilder sb = new StringBuilder();
424
425 for (MCRMetaLangText flag : flags) {
426 sb.append(flag.getText()).append(" ");
427 }
428
429 return sb.toString();
430 }
431
432
433
434
435
436
437 protected final List<MCRMetaLangText> getFlagsAsList() {
438 return flags;
439 }
440
441
442
443
444
445
446
447
448 protected final ArrayList<MCRMetaLangText> getFlagsAsMCRMetaLangText(String type) {
449 return flags.stream()
450 .filter(metaLangText -> StringUtils.equals(type, metaLangText.getType()))
451 .collect(Collectors.toCollection(ArrayList::new));
452 }
453
454
455
456
457
458
459
460
461 public final ArrayList<String> getFlags(String type) {
462 return getFlagsAsMCRMetaLangText(type).stream()
463 .map(MCRMetaLangText::getText)
464 .collect(Collectors.toCollection(ArrayList::new));
465 }
466
467
468
469
470
471
472 public final int getFlagSize() {
473 return flags.size();
474 }
475
476
477
478
479
480
481
482
483 public final String getFlag(int index) throws IndexOutOfBoundsException {
484 if (index < 0 || index >= flags.size()) {
485 throw new IndexOutOfBoundsException("Index error in getFlag.");
486 }
487 return flags.get(index).getText();
488 }
489
490
491
492
493
494
495
496
497 public final String getFlagType(int index) throws IndexOutOfBoundsException {
498 if (index < 0 || index >= flags.size()) {
499 throw new IndexOutOfBoundsException("Index error in getFlagType.");
500 }
501 return flags.get(index).getType();
502 }
503
504
505
506
507
508
509
510
511 public final boolean isFlagSet(String value) {
512 return MCRUtils.filterTrimmedNotEmpty(value)
513 .map(flagValue -> flags.stream().anyMatch(flag -> flagValue.equals(flag.getText())))
514 .orElse(false);
515 }
516
517
518
519
520
521
522
523
524 public final boolean isFlagTypeSet(String type) {
525 return flags.stream().anyMatch(flag -> StringUtils.equals(type, flag.getType()));
526 }
527
528
529
530
531
532
533
534
535
536 public final void removeFlag(int index) throws IndexOutOfBoundsException {
537 if (index < 0 || index >= flags.size()) {
538 throw new IndexOutOfBoundsException("Index error in removeFlag.");
539 }
540
541 flags.remove(index);
542 }
543
544
545
546
547
548
549
550
551 public final void removeFlags(String type) {
552 flags.removeIf(f -> StringUtils.equals(type, f.getType()));
553 }
554
555
556
557
558
559
560
561
562
563
564
565 public final void replaceFlag(int index, String value) throws IndexOutOfBoundsException {
566 MCRUtils.filterTrimmedNotEmpty(value)
567 .ifPresent(flagValue -> updateFlag(index, flag -> flag.setText(value)));
568 }
569
570 private void updateFlag(int index, Consumer<MCRMetaLangText> flagUpdater) {
571 MCRMetaLangText flag = flags.get(index);
572 flagUpdater.accept(flag);
573 }
574
575
576
577
578
579
580
581
582
583
584
585 public final void replaceFlagType(int index, String type) throws IndexOutOfBoundsException {
586 String lType = StringUtils.trim(type);
587 updateFlag(index, flag -> flag.setType(lType));
588 }
589
590
591
592
593
594
595
596
597
598 public final void addRule(String permission, Element condition) {
599 if (condition == null) {
600 return;
601 }
602 MCRUtils.filterTrimmedNotEmpty(permission)
603 .filter(p -> getRuleIndex(p) == -1)
604 .map(p -> new MCRMetaAccessRule("servacl", null, 0, p, condition))
605 .ifPresent(rules::add);
606 }
607
608
609
610
611
612
613 public final int getRulesSize() {
614 return rules.size();
615 }
616
617
618
619
620
621
622 public final int getRuleIndex(String permission) {
623 int notFound = -1;
624 if (permission == null || permission.trim().length() == 0) {
625 return notFound;
626 }
627 return IntStream.range(0, rules.size())
628 .filter(i -> rules.get(i).getPermission().equals(permission))
629 .findAny()
630 .orElse(notFound);
631 }
632
633
634
635
636
637
638
639
640 public final MCRMetaAccessRule getRule(int index) throws IndexOutOfBoundsException {
641 if (index < 0 || index >= rules.size()) {
642 throw new IndexOutOfBoundsException("Index error in getRule.");
643 }
644 return rules.get(index);
645 }
646
647
648
649
650
651
652
653
654
655 public final String getRulePermission(int index) throws IndexOutOfBoundsException {
656 if (index < 0 || index >= rules.size()) {
657 throw new IndexOutOfBoundsException("Index error in getRulePermission.");
658 }
659 return rules.get(index).getPermission();
660 }
661
662
663
664
665
666
667
668
669
670 public final void removeRule(int index) throws IndexOutOfBoundsException {
671 if (index < 0 || index >= rules.size()) {
672 throw new IndexOutOfBoundsException("Index error in removeRule.");
673 }
674 rules.remove(index);
675 }
676
677
678
679
680
681
682 protected final ArrayList<MCRMetaAccessRule> getRules() {
683 return rules;
684 }
685
686
687
688
689
690
691
692
693 public final Element createXML() throws MCRException {
694 try {
695 validate();
696 } catch (MCRException exc) {
697 throw new MCRException("The content is not valid.", exc);
698 }
699 Element elm = new Element("service");
700
701 if (dates.size() != 0) {
702 Element elmm = new Element("servdates");
703 elmm.setAttribute("class", "MCRMetaISO8601Date");
704
705 for (MCRMetaISO8601Date date : dates) {
706 elmm.addContent(date.createXML());
707 }
708
709 elm.addContent(elmm);
710 }
711
712 if (rules.size() != 0) {
713 Element elmm = new Element("servacls");
714 elmm.setAttribute("class", "MCRMetaAccessRule");
715
716 for (MCRMetaAccessRule rule : rules) {
717 elmm.addContent(rule.createXML());
718 }
719
720 elm.addContent(elmm);
721 }
722
723 if (flags.size() != 0) {
724 Element elmm = new Element("servflags");
725 elmm.setAttribute("class", "MCRMetaLangText");
726
727 for (MCRMetaLangText flag : flags) {
728 elmm.addContent(flag.createXML());
729 }
730
731 elm.addContent(elmm);
732 }
733
734 if (messages.size() != 0) {
735 Element elmm = new Element("servmessages");
736 elmm.setAttribute("class", "MCRMetaDateLangText");
737
738 for (MCRMetaDateLangText message : messages) {
739 elmm.addContent(message.createXML());
740 }
741
742 elm.addContent(elmm);
743 }
744
745 if (classifications.size() != 0) {
746 Element elmm = new Element("servclasses");
747 elmm.setAttribute("class", "MCRMetaClassification");
748
749 for (MCRMetaClassification classification : classifications) {
750 elmm.addContent(classification.createXML());
751 }
752
753 elm.addContent(elmm);
754 }
755
756 if (state != null) {
757 Element elmm = new Element("servstates");
758 elmm.setAttribute("class", "MCRMetaClassification");
759 MCRMetaClassification stateClass = new MCRMetaClassification("servstate", 0, null, state);
760 elmm.addContent(stateClass.createXML());
761 elm.addContent(elmm);
762 }
763
764 return elm;
765 }
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800 public final JsonObject createJSON() {
801 JsonObject service = new JsonObject();
802
803
804 if (!getDates().isEmpty()) {
805 JsonObject dates = new JsonObject();
806 getDates()
807 .stream()
808 .forEachOrdered(date -> {
809 JsonObject jsonDate = date.createJSON();
810 jsonDate.remove("type");
811 dates.add(date.getType(), jsonDate);
812 });
813 service.add("dates", dates);
814 }
815
816
817 if (!getRules().isEmpty()) {
818 JsonArray rules = new JsonArray();
819 getRules()
820 .stream()
821 .map(MCRMetaAccessRule::createJSON)
822 .forEachOrdered(rules::add);
823 service.add("rules", rules);
824 }
825
826
827 if (!getFlags().isEmpty()) {
828 JsonArray flags = new JsonArray();
829 getFlagsAsList()
830 .stream()
831 .map(MCRMetaLangText::createJSON)
832 .forEachOrdered(flags::add);
833 service.add("flags", flags);
834 }
835
836
837 if (!getMessages().isEmpty()) {
838 JsonArray messages = new JsonArray();
839 getMessagesAsList()
840 .stream()
841 .map(MCRMetaDateLangText::createJSON)
842 .forEachOrdered(messages::add);
843 service.add("messages", messages);
844 }
845
846
847 if (!getClassifications().isEmpty()) {
848 JsonArray classifications = new JsonArray();
849 getClassificationsAsList()
850 .stream()
851 .map(MCRMetaClassification::createJSON)
852 .forEachOrdered(classifications::add);
853 service.add("classifications", classifications);
854 }
855
856
857 Optional.ofNullable(getState()).ifPresent(stateId -> {
858 JsonObject state = new JsonObject();
859 if (stateId.getID() != null) {
860 state.addProperty("id", stateId.getID());
861 }
862 state.addProperty("rootId", stateId.getRootID());
863 });
864
865 return service;
866 }
867
868
869
870
871
872
873
874
875
876
877
878
879 public final boolean isValid() {
880 try {
881 validate();
882 return true;
883 } catch (MCRException exc) {
884 LOGGER.warn("The <service> part is invalid.");
885 }
886 return false;
887 }
888
889
890
891
892
893
894
895
896
897
898 public void validate() {
899
900 if (getISO8601Date(DATE_TYPE_CREATEDATE) == null) {
901 setDate(DATE_TYPE_CREATEDATE);
902 }
903 if (getISO8601Date(DATE_TYPE_MODIFYDATE) == null) {
904 setDate(DATE_TYPE_MODIFYDATE);
905 }
906 }
907
908
909
910
911
912
913
914
915 public final int getFlagIndex(String value) {
916 return MCRUtils.filterTrimmedNotEmpty(value)
917 .map(v -> {
918 for (int i = 0; i < flags.size(); i++) {
919 if (flags.get(i).getText().equals(v)) {
920 return i;
921 }
922 }
923 return -1;
924 })
925 .orElse(-1);
926 }
927
928
929
930
931
932
933 public final int getMessagesSize() {
934 return messages.size();
935 }
936
937
938
939
940
941
942 protected final ArrayList<MCRMetaDateLangText> getMessagesAsList() {
943 return messages;
944 }
945
946
947
948
949
950
951
952
953 protected final ArrayList<MCRMetaDateLangText> getMessagesAsMCRMetaDateLangText(String type) {
954 return messages.stream()
955 .filter(metaLangText -> type.equals(metaLangText.getType()))
956 .collect(Collectors.toCollection(ArrayList::new));
957 }
958
959
960
961
962
963
964 public final ArrayList<String> getMessages() {
965 return messages.stream()
966 .map(MCRMetaDateLangText::getText)
967 .collect(Collectors.toCollection(ArrayList::new));
968 }
969
970
971
972
973
974
975
976
977 public final ArrayList<String> getMessages(String type) {
978 return getMessagesAsMCRMetaDateLangText(type).stream()
979 .map(MCRMetaDateLangText::getText)
980 .collect(Collectors.toCollection(ArrayList::new));
981 }
982
983
984
985
986
987
988
989
990 public final String getMessage(int index) throws IndexOutOfBoundsException {
991 if (index < 0 || index >= messages.size()) {
992 throw new IndexOutOfBoundsException("Index error in getMessage.");
993 }
994 return messages.get(index).getText();
995 }
996
997
998
999
1000
1001
1002
1003
1004 public final String getMessageType(int index) throws IndexOutOfBoundsException {
1005 if (index < 0 || index >= messages.size()) {
1006 throw new IndexOutOfBoundsException("Index error in getMessageType.");
1007 }
1008 return messages.get(index).getType();
1009 }
1010
1011
1012
1013
1014
1015
1016
1017
1018 public final MCRISO8601Date getMessageDate(int index) throws IndexOutOfBoundsException {
1019 if (index < 0 || index >= messages.size()) {
1020 throw new IndexOutOfBoundsException("Index error in getMessageMCRMetaLangText.");
1021 }
1022 return messages.get(index).getDate();
1023 }
1024
1025
1026
1027
1028
1029
1030
1031 public final void addMessage(String value) {
1032 addMessage(null, value);
1033 }
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043 public final void addMessage(String type, String value) {
1044 addMessage(type, value, null);
1045 }
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057 public final void addMessage(String type, String value, String form) {
1058 String lType = StringUtils.trim(type);
1059 MCRUtils.filterTrimmedNotEmpty(value)
1060 .map(messageValue -> {
1061 MCRMetaDateLangText message
1062 = new MCRMetaDateLangText("servmessage", null, lType, 0, form, messageValue);
1063 message.setDate(MCRISO8601Date.now());
1064 return message;
1065 })
1066 .ifPresent(messages::add);
1067 }
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079 public final void replaceMessage(int index, String value) throws IndexOutOfBoundsException {
1080 MCRUtils.filterTrimmedNotEmpty(value)
1081 .ifPresent(messageValue -> updateMessage(index, message -> message.setText(value)));
1082 }
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094 public final void replaceMessageType(int index, String type) throws IndexOutOfBoundsException {
1095 String lType = StringUtils.trim(type);
1096 updateMessage(index, message -> message.setType(lType));
1097 }
1098
1099 private void updateMessage(int index, Consumer<MCRMetaDateLangText> messageUpdater) {
1100 MCRMetaDateLangText message = messages.get(index);
1101 messageUpdater.accept(message);
1102 }
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112 public final void removeMessage(int index) throws IndexOutOfBoundsException {
1113 if (index < 0 || index >= messages.size()) {
1114 throw new IndexOutOfBoundsException("Index error in removeMessage.");
1115 }
1116 messages.remove(index);
1117 }
1118
1119
1120
1121
1122
1123
1124
1125
1126 public final void removeMessages(String type) {
1127 List<MCRMetaDateLangText> internalList = getMessagesAsMCRMetaDateLangText(type);
1128 messages.removeAll(internalList);
1129 }
1130
1131
1132
1133
1134
1135
1136 public final int getClassificationsSize() {
1137 return this.classifications.size();
1138 }
1139
1140
1141
1142
1143
1144
1145 protected final ArrayList<MCRMetaClassification> getClassificationsAsList() {
1146 return classifications;
1147 }
1148
1149
1150
1151
1152
1153
1154
1155
1156 protected final ArrayList<MCRMetaClassification> getClassificationsAsMCRMetaClassification(String type) {
1157 return classifications.stream()
1158 .filter(metaLangText -> type.equals(metaLangText.getType()))
1159 .collect(Collectors.toCollection(ArrayList::new));
1160 }
1161
1162
1163
1164
1165
1166
1167 public final ArrayList<MCRCategoryID> getClassifications() {
1168 return classifications.stream()
1169 .map(c -> new MCRCategoryID(c.getClassId(), c.getCategId()))
1170 .collect(Collectors.toCollection(ArrayList::new));
1171 }
1172
1173
1174
1175
1176
1177
1178
1179
1180 public final ArrayList<MCRCategoryID> getClassifications(String type) {
1181 return getClassificationsAsMCRMetaClassification(type).stream()
1182 .map(c -> new MCRCategoryID(c.getClassId(), c.getCategId()))
1183 .collect(Collectors.toCollection(ArrayList::new));
1184 }
1185
1186
1187
1188
1189
1190
1191
1192
1193 public final MCRCategoryID getClassification(int index) throws IndexOutOfBoundsException {
1194 if (index < 0 || index >= classifications.size()) {
1195 throw new IndexOutOfBoundsException("Index error in getClassification.");
1196 }
1197 MCRMetaClassification classification = classifications.get(index);
1198 return new MCRCategoryID(classification.getCategId(), classification.getClassId());
1199 }
1200
1201
1202
1203
1204
1205
1206
1207
1208 public final String getClassificationType(int index) throws IndexOutOfBoundsException {
1209 if (index < 0 || index >= classifications.size()) {
1210 throw new IndexOutOfBoundsException("Index error in getClassification.");
1211 }
1212 return classifications.get(index).getType();
1213 }
1214
1215
1216
1217
1218
1219
1220
1221
1222 public final MCRMetaClassification getClassificationAsMCRMetaClassification(int index)
1223 throws IndexOutOfBoundsException {
1224 if (index < 0 || index >= classifications.size()) {
1225 throw new IndexOutOfBoundsException("Index error in getClassificationAsMCRMetaClassification.");
1226 }
1227 return classifications.get(index);
1228 }
1229
1230
1231
1232
1233
1234
1235
1236 public final void addClassification(MCRCategoryID value) {
1237 addClassification(null, value);
1238 }
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248 public final void addClassification(String type, MCRCategoryID value) {
1249 String lType = StringUtils.trim(type);
1250 classifications.add(new MCRMetaClassification("servclass", 0, lType, value));
1251 }
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263 public final void replaceClassification(int index, MCRCategoryID value) throws IndexOutOfBoundsException {
1264 updateClassification(index,
1265 classificationValue -> classificationValue.setValue(value.getRootID(), value.getID()));
1266 }
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278 public final void replaceClassificationType(int index, String type) throws IndexOutOfBoundsException {
1279 String lType = StringUtils.trim(type);
1280 updateClassification(index, classification -> classification.setType(lType));
1281 }
1282
1283 private void updateClassification(int index, Consumer<MCRMetaClassification> classificationUpdater) {
1284 MCRMetaClassification classification = classifications.get(index);
1285 classificationUpdater.accept(classification);
1286 }
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296 public final void removeClassification(int index) throws IndexOutOfBoundsException {
1297 if (index < 0 || index >= classifications.size()) {
1298 throw new IndexOutOfBoundsException("Index error in removeClassification.");
1299 }
1300 classifications.remove(index);
1301 }
1302
1303
1304
1305
1306
1307
1308
1309
1310 public final void removeClassifications(String type) {
1311 String lType = StringUtils.trim(type);
1312 classifications.removeIf(c -> StringUtils.equals(lType, c.getType()));
1313 }
1314
1315 }