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 org.hibernate.annotations.QueryHints;
22  import org.mycore.datamodel.classifications2.MCRCategLinkReference;
23  import org.mycore.datamodel.classifications2.MCRCategory;
24  import org.mycore.datamodel.classifications2.MCRCategoryLink;
25  
26  import jakarta.persistence.Embedded;
27  import jakarta.persistence.Entity;
28  import jakarta.persistence.GeneratedValue;
29  import jakarta.persistence.GenerationType;
30  import jakarta.persistence.Id;
31  import jakarta.persistence.Index;
32  import jakarta.persistence.JoinColumn;
33  import jakarta.persistence.ManyToOne;
34  import jakarta.persistence.NamedQueries;
35  import jakarta.persistence.NamedQuery;
36  import jakarta.persistence.QueryHint;
37  import jakarta.persistence.Table;
38  import jakarta.persistence.UniqueConstraint;
39  
40  /**
41   * @author Thomas Scheffler (yagee)
42   *
43   * @version $Revision$ $Date$
44   * @since 2.0
45   */
46  @Entity
47  @Table(name = "MCRCategoryLink",
48      uniqueConstraints = { @UniqueConstraint(columnNames = { "category", "objectID", "objectType" }) },
49      indexes = { @Index(columnList = "objectID, objectType", name = "ObjectIDType") })
50  @NamedQueries({
51      @NamedQuery(name = "MCRCategoryLink.ObjectIDByCategory",
52          query = "SELECT objectReference.objectID FROM MCRCategoryLinkImpl WHERE category.id=:id"),
53      @NamedQuery(name = "MCRCategoryLink.deleteByObjectCollection",
54          query = "DELETE FROM MCRCategoryLinkImpl WHERE "
55              + "objectReference.objectID IN (:ids) and objectReference.type=:type"),
56      @NamedQuery(name = "MCRCategoryLink.NumberPerClassID",
57          query = "SELECT cat.id.id, count(distinct link.objectReference.objectID) as num"
58              + "  FROM MCRCategoryLinkImpl link, MCRCategoryImpl cat, MCRCategoryImpl cattree"
59              + "  WHERE cattree.internalID = link.category"
60              + "    AND cattree.id.rootID=:classID"
61              + "    AND cat.id.rootID=:classID"
62              + "    AND cattree.left BETWEEN cat.left AND cat.right"
63              + "  GROUP BY cat.id.id"),
64      @NamedQuery(name = "MCRCategoryLink.NumberPerChildOfParentID",
65          query = "SELECT cat.id.id, count(distinct link.objectReference.objectID) as num"
66              + "  FROM MCRCategoryLinkImpl link, MCRCategoryImpl cat, MCRCategoryImpl cattree"
67              + "  WHERE cattree.internalID = link.category"
68              + "    AND cattree.id.rootID=:classID"
69              + "    AND cat.parent.internalID=:parentID"
70              + "    AND cattree.left BETWEEN cat.left AND cat.right"
71              + "  GROUP BY cat.id.id"),
72      @NamedQuery(name = "MCRCategoryLink.categoriesByObjectID",
73          query = "SELECT category.id FROM MCRCategoryLinkImpl WHERE "
74              + "objectReference.objectID=:id and objectReference.type=:type"),
75      @NamedQuery(name = "MCRCategoryLink.ObjectIDByCategoryAndType",
76          query = "SELECT objectReference.objectID FROM MCRCategoryLinkImpl WHERE "
77              + "category.id=:id and objectReference.type=:type"),
78      @NamedQuery(name = "MCRCategoryLink.NumberByTypePerClassID",
79          query = "SELECT cat.id.id, count(distinct link.objectReference.objectID) as num"
80              + "  FROM MCRCategoryLinkImpl link, MCRCategoryImpl cat, MCRCategoryImpl cattree"
81              + "  WHERE cattree.internalID = link.category"
82              + "    AND link.objectReference.type=:type"
83              + "    AND cattree.id.rootID=:classID"
84              + "    AND cat.id.rootID=:classID"
85              + "    AND cattree.left BETWEEN cat.left AND cat.right"
86              + "  GROUP BY cat.id.id"),
87      @NamedQuery(name = "MCRCategoryLink.NumberByTypePerChildOfParentID",
88          query = "SELECT cat.id.id, count(distinct link.objectReference.objectID) as num"
89              + "  FROM MCRCategoryLinkImpl link, MCRCategoryImpl cat, MCRCategoryImpl cattree"
90              + "  WHERE cattree.internalID = link.category"
91              + "    AND link.objectReference.type=:type"
92              + "    AND cattree.id.rootID=:classID"
93              + "    AND cat.parent.internalID=:parentID"
94              + "    AND cattree.left BETWEEN cat.left AND cat.right"
95              + "  GROUP BY cat.id.id"),
96      @NamedQuery(name = "MCRCategoryLink.deleteByObjectID",
97          query = "DELETE FROM MCRCategoryLinkImpl WHERE objectReference.objectID=:id and objectReference.type=:type"),
98      @NamedQuery(name = "MCRCategoryLink.CategoryAndObjectID",
99          query = "SELECT link.objectReference.objectID"
100             + "  FROM MCRCategoryLinkImpl link, MCRCategoryImpl cat, MCRCategoryImpl cattree"
101             + "  WHERE cattree.internalID = link.category"
102             + "    AND link.objectReference.objectID=:objectID"
103             + "    AND link.objectReference.type=:type"
104             + "    AND cattree.id.rootID=:rootID"
105             + "    AND cat.id.rootID=:rootID"
106             + "    AND cat.id.id=:categID"
107             + "    AND cattree.left BETWEEN cat.left AND cat.right",
108         hints = { @QueryHint(name = QueryHints.READ_ONLY, value = "true") }),
109     @NamedQuery(name = "MCRCategoryLink.linkedClassifications",
110         query = "SELECT distinct node.id.rootID from MCRCategoryImpl as node, MCRCategoryLinkImpl as link "
111             + "where node.internalID = link.category"),
112     @NamedQuery(name = "MCRCategoryLink.types",
113         query = "SELECT DISTINCT(objectReference.type) FROM MCRCategoryLinkImpl"),
114     @NamedQuery(name = "MCRCategoryLink.links",
115         query = "FROM MCRCategoryLinkImpl WHERE objectReference.type=:type")
116 })
117 public class MCRCategoryLinkImpl implements MCRCategoryLink {
118 
119     @Id
120     @GeneratedValue(strategy = GenerationType.IDENTITY)
121     int id;
122 
123     @ManyToOne(targetEntity = MCRCategoryImpl.class)
124     @JoinColumn(name = "category")
125     private MCRCategory category;
126 
127     @Embedded
128     private MCRCategLinkReference objectReference;
129 
130     public MCRCategoryLinkImpl() {
131         this(null, null);
132     }
133 
134     MCRCategoryLinkImpl(MCRCategory category, MCRCategLinkReference objectReference) {
135         this.category = category;
136         this.objectReference = objectReference;
137     }
138 
139     @Override
140     public MCRCategory getCategory() {
141         return category;
142     }
143 
144     public void setCategory(MCRCategoryImpl category) {
145         this.category = category;
146     }
147 
148     @Override
149     public MCRCategLinkReference getObjectReference() {
150         return objectReference;
151     }
152 
153     public void setObjectReference(MCRCategLinkReference objectReference) {
154         this.objectReference = objectReference;
155     }
156 
157     @Override
158     public int hashCode() {
159         final int prime = 31;
160         int result = 1;
161         result = prime * result + (category == null ? 0 : category.hashCode());
162         result = prime * result + (objectReference == null ? 0 : objectReference.hashCode());
163         return result;
164     }
165 
166     @Override
167     public boolean equals(Object obj) {
168         if (this == obj) {
169             return true;
170         }
171         if (obj == null) {
172             return false;
173         }
174         if (!(obj instanceof MCRCategoryLinkImpl)) {
175             return false;
176         }
177         final MCRCategoryLinkImpl other = (MCRCategoryLinkImpl) obj;
178         if (category == null) {
179             if (other.category != null) {
180                 return false;
181             }
182         } else if (!category.equals(other.category)) {
183             return false;
184         }
185         if (objectReference == null) {
186             return other.objectReference == null;
187         } else {
188             return objectReference.equals(other.objectReference);
189         }
190     }
191 
192 }