View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.datamodel.classifications2.model;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.stream.Collectors;
24  
25  import org.mycore.datamodel.classifications2.MCRCategory;
26  import org.mycore.datamodel.classifications2.MCRCategoryID;
27  import org.mycore.datamodel.classifications2.MCRLabel;
28  
29  import com.fasterxml.jackson.annotation.JsonGetter;
30  import com.fasterxml.jackson.annotation.JsonInclude;
31  import com.fasterxml.jackson.annotation.JsonPropertyOrder;
32  
33  import jakarta.xml.bind.annotation.XmlAccessType;
34  import jakarta.xml.bind.annotation.XmlAccessorType;
35  import jakarta.xml.bind.annotation.XmlAttribute;
36  import jakarta.xml.bind.annotation.XmlElement;
37  import jakarta.xml.bind.annotation.XmlRootElement;
38  import jakarta.xml.bind.annotation.XmlSeeAlso;
39  import jakarta.xml.bind.annotation.XmlType;
40  import jakarta.xml.bind.annotation.adapters.NormalizedStringAdapter;
41  import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
42  
43  @XmlAccessorType(XmlAccessType.FIELD)
44  @XmlType(name = "MCRClassCategory",
45      propOrder = {
46          "label",
47          "url",
48          "category"
49      })
50  @XmlSeeAlso({ MCRLabel.class, MCRClassURL.class })
51  @XmlRootElement(name = "category")
52  @JsonInclude(value = JsonInclude.Include.NON_EMPTY)
53  @JsonPropertyOrder({ "ID", "url", "labels", "categories" })
54  public class MCRClassCategory {
55  
56      @XmlElement(required = true)
57      protected List<MCRLabel> label;
58  
59      protected MCRClassURL url;
60  
61      protected List<MCRClassCategory> category;
62  
63      @XmlAttribute(name = "ID", required = true)
64      @XmlJavaTypeAdapter(NormalizedStringAdapter.class)
65      protected String id;
66  
67      @JsonGetter("labels")
68      public List<MCRLabel> getLabel() {
69          if (label == null) {
70              label = new ArrayList<>();
71          }
72          return this.label;
73      }
74  
75      public MCRClassURL getUrl() {
76          return url;
77      }
78  
79      public void setUrl(MCRClassURL value) {
80          this.url = value;
81      }
82  
83      @JsonGetter("categories")
84      public List<MCRClassCategory> getCategory() {
85          if (category == null) {
86              category = new ArrayList<>();
87          }
88          return this.category;
89      }
90  
91      @JsonGetter
92      public String getID() {
93          return id;
94      }
95  
96      public void setID(String value) {
97          this.id = value;
98      }
99  
100     public static MCRClassCategory getInstance(MCRCategory from) {
101         MCRClassCategory categ = new MCRClassCategory();
102         MCRCategoryID categoryID = from.getId();
103         categ.setID(categoryID.isRootID() ? categoryID.getRootID() : categoryID.getID());
104         categ.setUrl(MCRClassURL.getInstance(from.getURI()));
105         categ.getCategory()
106             .addAll(getInstance(from.getChildren()));
107         categ.getLabel()
108             .addAll(
109                 from.getLabels()
110                     .stream()
111                     .map(MCRLabel::clone)
112                     .collect(Collectors.toList()));
113         return categ;
114     }
115 
116     public static List<MCRClassCategory> getInstance(List<MCRCategory> children) {
117         return children
118             .stream()
119             .map(MCRClassCategory::getInstance)
120             .collect(Collectors.toList());
121     }
122 
123 }