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.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   * Used for specific JPA queries only. Do not use directly!
32   * @author Thomas Scheffler (yagee)
33   * @since 2016.04
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              //only add label
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); //is reset to parent.level+1 in step before
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 }