View Javadoc
1   
2   /*
3    * This file is part of ***  M y C o R e  ***
4    * See http://www.mycore.de/ for details.
5    *
6    * MyCoRe is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * MyCoRe is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package org.mycore.datamodel.classifications2.model;
21  
22  import java.net.URI;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Optional;
26  import java.util.stream.Collectors;
27  
28  import org.mycore.datamodel.classifications2.MCRCategory;
29  import org.mycore.datamodel.classifications2.MCRCategoryID;
30  import org.mycore.datamodel.classifications2.MCRLabel;
31  import org.mycore.datamodel.classifications2.impl.MCRCategoryImpl;
32  
33  import com.fasterxml.jackson.annotation.JsonGetter;
34  import com.fasterxml.jackson.annotation.JsonInclude;
35  import com.fasterxml.jackson.annotation.JsonPropertyOrder;
36  
37  import jakarta.xml.bind.annotation.XmlAccessType;
38  import jakarta.xml.bind.annotation.XmlAccessorType;
39  import jakarta.xml.bind.annotation.XmlAttribute;
40  import jakarta.xml.bind.annotation.XmlElement;
41  import jakarta.xml.bind.annotation.XmlElementWrapper;
42  import jakarta.xml.bind.annotation.XmlRootElement;
43  import jakarta.xml.bind.annotation.XmlSeeAlso;
44  import jakarta.xml.bind.annotation.XmlType;
45  import jakarta.xml.bind.annotation.adapters.NormalizedStringAdapter;
46  import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
47  
48  @XmlRootElement(name = "mycoreclass")
49  @XmlAccessorType(XmlAccessType.FIELD)
50  @XmlType(name = "MCRClass",
51      propOrder = {
52          "label",
53          "url",
54          "categories"
55      })
56  @XmlSeeAlso({ MCRClassCategory.class, MCRClassURL.class, MCRLabel.class })
57  @JsonInclude(value = JsonInclude.Include.NON_EMPTY)
58  @JsonPropertyOrder({ "ID", "url", "labels", "categories" })
59  public class MCRClass {
60  
61      @XmlElement(required = true)
62      protected List<MCRLabel> label;
63  
64      protected MCRClassURL url;
65  
66      @XmlElementWrapper(name = "categories")
67      @XmlElement(name = "category")
68      protected List<MCRClassCategory> categories;
69  
70      @XmlAttribute(name = "ID", required = true)
71      @XmlJavaTypeAdapter(NormalizedStringAdapter.class)
72      protected String id;
73  
74      @JsonGetter("labels")
75      public List<MCRLabel> getLabel() {
76          if (label == null) {
77              label = new ArrayList<>();
78          }
79          return this.label;
80      }
81  
82      public MCRClassURL getUrl() {
83          return url;
84      }
85  
86      public void setUrl(MCRClassURL value) {
87          this.url = value;
88      }
89  
90      @JsonGetter("categories")
91      public List<MCRClassCategory> getCategories() {
92          return categories;
93      }
94  
95      public void setCategories(List<MCRClassCategory> value) {
96          this.categories = value;
97      }
98  
99      @JsonGetter
100     public String getID() {
101         return id;
102     }
103 
104     public void setID(String value) {
105         this.id = value;
106     }
107 
108     public static MCRClass getClassification(MCRCategory rootCategory) {
109         if (!rootCategory.getId().isRootID()) {
110             throw new IllegalArgumentException("Not a root category");
111         }
112         MCRClass mcrClass = new MCRClass();
113         mcrClass.setID(rootCategory.getId().getRootID());
114         mcrClass.setUrl(MCRClassURL.getInstance(rootCategory.getURI()));
115         mcrClass.getLabel()
116             .addAll(
117                 rootCategory.getLabels()
118                     .stream()
119                     .map(MCRLabel::clone)
120                     .collect(Collectors.toList()));
121         mcrClass.setCategories(MCRClassCategory.getInstance(rootCategory.getChildren()));
122         return mcrClass;
123     }
124 
125     public MCRCategory toCategory() {
126         MCRCategoryImpl category = new MCRCategoryImpl();
127         category.setId(MCRCategoryID.rootID(getID()));
128         category.setLevel(0);
129         URI uri = Optional.ofNullable(getUrl())
130             .map(MCRClassURL::getHref)
131             .map(URI::create)
132             .orElse(null);
133         category.setURI(uri);
134         category.getLabels()
135             .addAll(getLabel().stream()
136                 .map(MCRLabel::clone)
137                 .collect(Collectors.toList()));
138         return category;
139     }
140 
141     public static MCRCategory buildCategory(String classID, MCRClassCategory e, MCRCategory parent) {
142         MCRCategoryImpl category = new MCRCategoryImpl();
143         //setId must be called before setParent (info important)
144         category.setId(new MCRCategoryID(classID, e.getID()));
145         category.setRoot(parent.getRoot());
146         category.setChildren(new ArrayList<>());
147         category.setParent(parent);
148         category.getLabels()
149             .addAll(e.getLabel().stream()
150                 .map(MCRLabel::clone)
151                 .collect(Collectors.toList()));
152         category.setLevel(parent.getLevel() + 1);
153         URI uri = Optional.ofNullable(e.getUrl())
154             .map(MCRClassURL::getHref)
155             .map(URI::create)
156             .orElse(null);
157         category.setURI(uri);
158         for (MCRClassCategory child : e.getCategory()) {
159             buildCategory(classID, child, category);
160         }
161         return category;
162     }
163 
164 }