1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.datamodel.classifications2.impl;
20
21 import java.net.URI;
22 import java.util.ArrayList;
23 import java.util.Locale;
24 import java.util.Optional;
25
26 import org.mycore.datamodel.classifications2.MCRCategory;
27 import org.mycore.datamodel.classifications2.MCRCategoryID;
28 import org.mycore.datamodel.classifications2.MCRLabel;
29
30
31
32
33
34
35 public class MCRCategoryDTO {
36 public static final String SELECT = "select new org.mycore.datamodel.classifications2.impl.MCRCategoryDTO"
37 + "(cat.internalID, cat.URI, cat.id, cat.left, cat.right, cat.level, labels.lang, labels.text,"
38 + " labels.description) from MCRCategoryImpl cat LEFT OUTER JOIN cat.labels labels";
39
40 public static final String CAT_SELECT = "select new org.mycore.datamodel.classifications2.impl.MCRCategoryDTO"
41 + "(cat.internalID, cat.URI, cat.id, cat.left, cat.right, cat.level) from MCRCategoryImpl cat";
42
43 public static final String LRL_SELECT = "select new org.mycore.datamodel.classifications2.impl.MCRCategoryDTO"
44 + "(cat.left, cat.right, cat.level) from MCRCategoryImpl cat";
45
46 int internalID;
47
48 URI uri;
49
50 MCRCategoryID id;
51
52 int leftValue, level, rightValue;
53
54 String lang, text, description;
55
56 public MCRCategoryDTO(int internalID, URI uri, MCRCategoryID id, int leftValue, int rightValue,
57 int level, String lang, String text, String description) {
58 this(internalID, uri, id, leftValue, rightValue, level);
59 this.lang = lang;
60 this.text = text;
61 this.description = description;
62 }
63
64 public MCRCategoryDTO(int internalID, URI uri, MCRCategoryID id, int leftValue, int rightValue,
65 int level) {
66 this(leftValue, rightValue, level);
67 this.internalID = internalID;
68 this.uri = uri;
69 this.id = id;
70 }
71
72 public MCRCategoryDTO(int leftValue, int rightValue, int level) {
73 this.leftValue = leftValue;
74 this.rightValue = rightValue;
75 this.level = level;
76 }
77
78 public MCRCategoryImpl merge(MCRCategoryImpl predecessor) {
79 if (predecessor == null) {
80 return toCategory();
81 }
82 if (predecessor.getInternalID() == internalID) {
83
84 return appendLabel(predecessor);
85 }
86 MCRCategoryImpl cat = toCategory();
87 MCRCategory parent = predecessor;
88 while (parent.getLevel() >= level) {
89 parent = parent.getParent();
90 }
91 parent.getChildren().add(cat);
92 cat.setLevel(level);
93 return cat;
94 }
95
96 private MCRCategoryImpl toCategory() {
97 MCRCategoryImpl cat = new MCRCategoryImpl();
98 cat.setInternalID(internalID);
99 cat.setURI(uri);
100 cat.setId(id);
101 if (cat.getId().isRootID()) {
102 cat.setRoot(cat);
103 }
104 cat.setLeft(leftValue);
105 cat.setRight(rightValue);
106 cat.setLevel(level);
107 cat.setChildren(new ArrayList<>());
108 return appendLabel(cat);
109 }
110
111 private MCRCategoryImpl appendLabel(MCRCategoryImpl cat) {
112 if (lang != null) {
113 MCRLabel label = new MCRLabel(lang, text,
114 Optional.ofNullable(description).filter(s -> !s.isEmpty()).orElse(null));
115 cat.getLabels().add(label);
116 }
117 return cat;
118 }
119
120 @Override
121 public String toString() {
122 return String.format(Locale.ROOT,
123 "MCRCategoryDTO [internalID=%s, id=%s, uri=%s, leftValue=%s, level=%s, "
124 + "rightValue=%s, lang=%s, text=%s, description=%s]",
125 internalID, id, uri, leftValue, level, rightValue, lang, text, description);
126 }
127
128 public int getInternalID() {
129 return internalID;
130 }
131
132 }