2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.term
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Collection
;
14 import java
.util
.Enumeration
;
15 import java
.util
.Iterator
;
16 import java
.util
.List
;
17 import java
.util
.Locale
;
19 import java
.util
.UUID
;
21 import org
.apache
.commons
.lang
.StringUtils
;
22 import org
.apache
.log4j
.Logger
;
23 import org
.hibernate
.Criteria
;
24 import org
.hibernate
.Query
;
25 import org
.hibernate
.Session
;
26 import org
.hibernate
.criterion
.Criterion
;
27 import org
.hibernate
.criterion
.Disjunction
;
28 import org
.hibernate
.criterion
.Order
;
29 import org
.hibernate
.criterion
.Projections
;
30 import org
.hibernate
.criterion
.Restrictions
;
31 import org
.hibernate
.envers
.query
.AuditEntity
;
32 import org
.hibernate
.envers
.query
.AuditQuery
;
33 import org
.springframework
.stereotype
.Repository
;
35 import eu
.etaxonomy
.cdm
.common
.URI
;
36 import eu
.etaxonomy
.cdm
.model
.common
.AnnotationType
;
37 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
38 import eu
.etaxonomy
.cdm
.model
.common
.ExtensionType
;
39 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
40 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
41 import eu
.etaxonomy
.cdm
.model
.description
.MeasurementUnit
;
42 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTerm
;
43 import eu
.etaxonomy
.cdm
.model
.description
.State
;
44 import eu
.etaxonomy
.cdm
.model
.description
.StatisticalMeasure
;
45 import eu
.etaxonomy
.cdm
.model
.description
.TextFormat
;
46 import eu
.etaxonomy
.cdm
.model
.location
.Country
;
47 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
48 import eu
.etaxonomy
.cdm
.model
.location
.NamedAreaLevel
;
49 import eu
.etaxonomy
.cdm
.model
.location
.NamedAreaType
;
50 import eu
.etaxonomy
.cdm
.model
.location
.ReferenceSystem
;
51 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
52 import eu
.etaxonomy
.cdm
.model
.media
.RightsType
;
53 import eu
.etaxonomy
.cdm
.model
.metadata
.NamedAreaSearchField
;
54 import eu
.etaxonomy
.cdm
.model
.name
.HybridRelationshipType
;
55 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationshipType
;
56 import eu
.etaxonomy
.cdm
.model
.name
.NameTypeDesignationStatus
;
57 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatusType
;
58 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
59 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignationStatus
;
60 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivationEventType
;
61 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymType
;
62 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
63 import eu
.etaxonomy
.cdm
.model
.term
.DefinedTerm
;
64 import eu
.etaxonomy
.cdm
.model
.term
.DefinedTermBase
;
65 import eu
.etaxonomy
.cdm
.model
.term
.TermType
;
66 import eu
.etaxonomy
.cdm
.model
.term
.TermVocabulary
;
67 import eu
.etaxonomy
.cdm
.model
.view
.AuditEvent
;
68 import eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.common
.IdentifiableDaoBase
;
69 import eu
.etaxonomy
.cdm
.persistence
.dao
.term
.IDefinedTermDao
;
70 import eu
.etaxonomy
.cdm
.persistence
.dto
.FeatureDto
;
71 import eu
.etaxonomy
.cdm
.persistence
.dto
.TermDto
;
72 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
73 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
76 * @author a.kohlbecker
80 public class DefinedTermDaoImpl
81 extends IdentifiableDaoBase
<DefinedTermBase
>
82 implements IDefinedTermDao
{
84 private static final Logger logger
= Logger
.getLogger(DefinedTermDaoImpl
.class);
86 public DefinedTermDaoImpl() {
87 super(DefinedTermBase
.class);
88 indexedClasses
= new Class
[25];
89 indexedClasses
[0] = Rank
.class;
90 indexedClasses
[1] = AnnotationType
.class;
91 indexedClasses
[2] = ExtensionType
.class;
92 indexedClasses
[3] = Language
.class;
93 indexedClasses
[4] = MarkerType
.class;
94 indexedClasses
[5] = MeasurementUnit
.class;
95 indexedClasses
[6] = DefinedTerm
.class;
96 indexedClasses
[7] = PresenceAbsenceTerm
.class;
97 indexedClasses
[8] = State
.class;
98 indexedClasses
[9] = StatisticalMeasure
.class;
99 indexedClasses
[10] = TextFormat
.class;
100 indexedClasses
[11] = DerivationEventType
.class;
101 indexedClasses
[12] = NamedArea
.class;
102 indexedClasses
[13] = NamedAreaLevel
.class;
103 indexedClasses
[14] = NamedAreaType
.class;
104 indexedClasses
[15] = ReferenceSystem
.class;
105 indexedClasses
[16] = Country
.class;
106 indexedClasses
[17] = RightsType
.class;
107 indexedClasses
[18] = HybridRelationshipType
.class;
108 indexedClasses
[19] = NameRelationshipType
.class;
109 indexedClasses
[20] = NameTypeDesignationStatus
.class;
110 indexedClasses
[21] = NomenclaturalStatusType
.class;
111 indexedClasses
[22] = SpecimenTypeDesignationStatus
.class;
112 indexedClasses
[23] = SynonymType
.class;
113 indexedClasses
[24] = TaxonRelationshipType
.class;
120 public List
<DefinedTermBase
> findByTitle(String queryString
) {
121 return findByTitle(queryString
, null);
128 public List
<DefinedTermBase
> findByTitle(String queryString
, CdmBase sessionObject
) {
129 checkNotInPriorView("DefinedTermDaoImpl.findByTitle(String queryString, CdmBase sessionObject)");
130 Session session
= getSession();
131 if ( sessionObject
!= null ) {//attache the object to the session, TODO needed?
132 session
.update(sessionObject
);
134 Query query
= session
.createQuery("select term from DefinedTermBase term join fetch term.representations representation where representation.label = :label");
135 query
.setParameter("label", queryString
);
136 @SuppressWarnings({ "unchecked", "rawtypes" })
137 List
<DefinedTermBase
> result
= query
.list();
143 public List
<DefinedTermBase
> findByTitle(String queryString
, MatchMode matchMode
, int page
, int pagesize
, List
<Criterion
> criteria
) {
144 //FIXME is query parametrised?
145 checkNotInPriorView("DefinedTermDaoImpl.findByTitle(String queryString, ITitledDao.MATCH_MODE matchMode, int page, int pagesize, List<Criterion> criteria)");
146 Criteria crit
= getSession().createCriteria(type
);
147 crit
.add(Restrictions
.ilike("titleCache", matchMode
.queryStringFrom(queryString
)));
148 crit
.setMaxResults(pagesize
);
149 int firstItem
= (page
- 1) * pagesize
+ 1;
150 crit
.setFirstResult(firstItem
);
151 List
<DefinedTermBase
> results
= crit
.list();
156 public Country
getCountryByIso(String iso3166
) {
157 // If iso639 = "" query returns non-unique result. We prevent this here:
158 if (StringUtils
.isBlank(iso3166
) || iso3166
.length()<2 || iso3166
.length()>3) { return null; }
159 AuditEvent auditEvent
= getAuditEventFromContext();
160 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
161 Query query
= getSession().createQuery("from Country where iso3166_A2 = :isoCode OR idInVocabulary = :isoCode");
162 query
.setParameter("isoCode", iso3166
);
163 return (Country
) query
.uniqueResult();
165 AuditQuery query
= getAuditReader().createQuery().forEntitiesAtRevision(Country
.class,auditEvent
.getRevisionNumber());
166 query
.add(AuditEntity
.property("iso3166_A2").eq(iso3166
));
167 query
.add(AuditEntity
.property("idInVocabulary").eq(iso3166
));
168 return (Country
) query
.getSingleResult();
173 public <T
extends DefinedTermBase
> List
<T
> getDefinedTermByRepresentationText(String text
, Class
<T
> clazz
) {
174 return getDefinedTermByRepresentationText(text
,clazz
,null,null);
178 public <T
extends DefinedTermBase
> List
<T
> getDefinedTermByRepresentationText(String text
, Class
<T
> clazz
, Integer pageSize
,Integer pageNumber
) {
179 checkNotInPriorView("DefinedTermDaoImpl.getDefinedTermByRepresentationText(String text, Class<T> clazz, Integer pageSize,Integer pageNumber)");
181 Criteria criteria
= getCriteria(clazz
);
183 criteria
.createAlias("representations", "r").add(Restrictions
.like("r.text", text
));
185 addPageSizeAndNumber(criteria
, pageSize
, pageNumber
);
187 @SuppressWarnings("unchecked")
188 List
<T
> result
= criteria
.list();
193 public long countDefinedTermByRepresentationText(String text
, Class
<?
extends DefinedTermBase
> clazz
) {
194 checkNotInPriorView("DefinedTermDaoImpl.countDefinedTermByRepresentationText(String text, Class<? extends DefinedTermBase> clazz)");
195 Criteria criteria
= getCriteria(clazz
);
197 criteria
.createAlias("representations", "r").add(Restrictions
.like("r.text", text
));
199 criteria
.setProjection(Projections
.rowCount());
201 return (Long
)criteria
.uniqueResult();
205 public <T
extends DefinedTermBase
> List
<T
> getDefinedTermByIdInVocabulary(String label
, UUID vocUuid
, Class
<T
> clazz
, Integer pageSize
, Integer pageNumber
) {
206 checkNotInPriorView("DefinedTermDaoImpl.getDefinedTermByIdInVocabulary(String label, UUID vocUuid, Class<T> clazz, Integer pageSize, Integer pageNumber)");
208 Criteria criteria
= getCriteria(clazz
);
210 criteria
.createAlias("vocabulary", "voc")
211 .add(Restrictions
.like("voc.uuid", vocUuid
))
212 .add(Restrictions
.like("idInVocabulary", label
, org
.hibernate
.criterion
.MatchMode
.EXACT
));
214 addPageSizeAndNumber(criteria
, pageSize
, pageNumber
);
216 @SuppressWarnings("unchecked")
217 List
<T
> result
= criteria
.list();
222 public <T
extends DefinedTermBase
> List
<T
> getDefinedTermByRepresentationAbbrev(String text
, Class
<T
> clazz
, Integer pageSize
,Integer pageNumber
) {
223 checkNotInPriorView("DefinedTermDaoImpl.getDefinedTermByRepresentationAbbrev(String abbrev, Class<T> clazz, Integer pageSize,Integer pageNumber)");
225 Criteria criteria
= getCriteria(clazz
);
227 criteria
.createAlias("representations", "r").add(Restrictions
.like("r.abbreviatedLabel", text
));
229 addPageSizeAndNumber(criteria
, pageSize
, pageNumber
);
231 @SuppressWarnings("unchecked")
232 List
<T
> result
= criteria
.list();
237 public long countDefinedTermByRepresentationAbbrev(String text
, Class
<?
extends DefinedTermBase
> clazz
) {
238 checkNotInPriorView("DefinedTermDaoImpl.countDefinedTermByRepresentationAbbrev(String abbrev, Class<? extends DefinedTermBase> clazz)");
239 Criteria criteria
= getCriteria(clazz
);
241 criteria
.createAlias("representations", "r").add(Restrictions
.like("r.abbreviatedLabel", text
));
242 criteria
.setProjection(Projections
.rowCount());
244 return (Long
)criteria
.uniqueResult();
248 public Language
getLanguageByIso(String iso639
) {
249 if (iso639
.length() < 2 || iso639
.length() > 3) {
250 logger
.warn("Invalid length " + iso639
.length() + " of ISO code. Length must be 2 or 3.");
253 boolean isIso639_1
= iso639
.length() == 2;
257 queryStr
= "from Language where iso639_1 = :isoCode";
259 queryStr
= "from Language where idInVocabulary = :isoCode and vocabulary.uuid = :vocUuid";
261 AuditEvent auditEvent
= getAuditEventFromContext();
262 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
263 Query query
= getSession().createQuery(queryStr
);
264 query
.setParameter("isoCode", iso639
);
266 query
.setParameter("vocUuid", Language
.uuidLanguageVocabulary
);
268 return (Language
) query
.uniqueResult();
270 AuditQuery query
= getAuditReader().createQuery().forEntitiesAtRevision(Language
.class,auditEvent
.getRevisionNumber());
272 query
.add(AuditEntity
.property("iso639_1").eq(iso639
));
274 query
.add(AuditEntity
.property("iso639_2").eq(iso639
));
275 query
.add(AuditEntity
.property("vocabulary.uuid").eq(Language
.uuidLanguageVocabulary
));
278 return (Language
)query
.getSingleResult();
283 * FIXME this will result in a query per language - could you, given that iso codes
284 * are unique, use from Language where iso639_1 in (:isoCode) or iso639_2 in (:isoCode)
287 public List
<Language
> getLanguagesByIso(List
<String
> iso639List
) {
288 List
<Language
> languages
= new ArrayList
<Language
>(iso639List
.size());
289 for (String iso639
: iso639List
) {
290 languages
.add(getLanguageByIso(iso639
));
296 public List
<Language
> getLanguagesByLocale(Enumeration
<Locale
> locales
) {
297 List
<Language
> languages
= new ArrayList
<Language
>();
298 while(locales
.hasMoreElements()) {
299 Locale locale
= locales
.nextElement();
300 languages
.add(getLanguageByIso(locale
.getLanguage()));
306 public long count(NamedAreaLevel level
, NamedAreaType type
) {
307 AuditEvent auditEvent
= getAuditEventFromContext();
308 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
309 Criteria criteria
= getCriteria(NamedArea
.class);
312 criteria
.add(Restrictions
.eq("level",level
));
316 criteria
.add(Restrictions
.eq("type", type
));
319 criteria
.setProjection(Projections
.rowCount());
321 return (Long
)criteria
.uniqueResult();
323 AuditQuery query
= makeAuditQuery(NamedArea
.class, auditEvent
);
326 query
.add(AuditEntity
.relatedId("level").eq(level
.getId()));
330 query
.add(AuditEntity
.relatedId("type").eq(type
.getId()));
332 query
.addProjection(AuditEntity
.id().count());
333 return (Long
)query
.getSingleResult();
338 public long countMedia(DefinedTermBase definedTerm
) {
339 checkNotInPriorView("DefinedTermDaoImpl.countMedia(DefinedTermBase definedTerm)");
340 Query query
= getSession().createQuery("select count(media) from DefinedTermBase definedTerm join definedTerm.media media where definedTerm = :definedTerm");
341 query
.setParameter("definedTerm", definedTerm
);
343 return (Long
)query
.uniqueResult();
347 public List
<Media
> getMedia(DefinedTermBase definedTerm
, Integer pageSize
, Integer pageNumber
) {
348 checkNotInPriorView("DefinedTermDaoImpl.getMedia(DefinedTermBase definedTerm, Integer pageSize, Integer pageNumber)");
349 Query query
= getSession().createQuery("select media from DefinedTermBase definedTerm join definedTerm.media media where definedTerm = :definedTerm");
350 query
.setParameter("definedTerm", definedTerm
);
352 addPageSizeAndNumber(query
, pageSize
, pageNumber
);
354 @SuppressWarnings("unchecked")
355 List
<Media
> result
= query
.list();
360 public List
<NamedArea
> list(NamedAreaLevel level
, NamedAreaType type
, Integer pageSize
, Integer pageNumber
) {
361 AuditEvent auditEvent
= getAuditEventFromContext();
362 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
363 Criteria criteria
= getCriteria(NamedArea
.class);
366 criteria
.add(Restrictions
.eq("level",level
));
370 criteria
.add(Restrictions
.eq("type", type
));
373 addPageSizeAndNumber(criteria
, pageSize
, pageNumber
);
375 @SuppressWarnings("unchecked")
376 List
<NamedArea
> result
= criteria
.list();
379 AuditQuery query
= makeAuditQuery(NamedArea
.class, auditEvent
);
382 query
.add(AuditEntity
.relatedId("level").eq(level
.getId()));
386 query
.add(AuditEntity
.relatedId("type").eq(type
.getId()));
389 @SuppressWarnings("unchecked")
390 List
<NamedArea
> result
= query
.getResultList();
396 public List
<NamedArea
> list(NamedAreaLevel level
, NamedAreaType type
, Integer pageSize
, Integer pageNumber
,
397 List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
399 List
<NamedArea
> result
;
401 AuditEvent auditEvent
= getAuditEventFromContext();
402 if (auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
403 Criteria criteria
= getCriteria(NamedArea
.class);
406 criteria
.add(Restrictions
.eq("level", level
));
409 criteria
.add(Restrictions
.eq("type", type
));
411 addOrder(criteria
,orderHints
);
412 addPageSizeAndNumber(criteria
, pageSize
, pageNumber
);
414 result
= criteria
.list();
417 AuditQuery query
= getAuditReader().createQuery().forEntitiesAtRevision(NamedArea
.class,
418 auditEvent
.getRevisionNumber());
420 query
.add(AuditEntity
.relatedId("level").eq(level
.getId()));
423 query
.add(AuditEntity
.relatedId("type").eq(type
.getId()));
425 result
= query
.getResultList();
428 defaultBeanInitializer
.initializeAll(result
, propertyPaths
);
435 public <T
extends DefinedTermBase
> long countGeneralizationOf(T kindOf
) {
436 AuditEvent auditEvent
= getAuditEventFromContext();
437 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
438 Query query
= getSession().createQuery("select count(term) from DefinedTermBase term where term.kindOf = :kindOf");
439 query
.setParameter("kindOf", kindOf
);
440 return (Long
)query
.uniqueResult();
442 AuditQuery query
= makeAuditQuery(DefinedTermBase
.class,auditEvent
);
443 query
.add(AuditEntity
.relatedId("kindOf").eq(kindOf
.getId()));
444 query
.addProjection(AuditEntity
.id().count());
445 return (Long
)query
.getSingleResult();
450 public <T
extends DefinedTermBase
> long countIncludes(Collection
<T
> partOf
) {
451 if (partOf
== null || partOf
.isEmpty()){
454 AuditEvent auditEvent
= getAuditEventFromContext();
455 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
456 Query query
= getSession().createQuery("select count(term) from DefinedTermBase term where term.partOf in (:partOf)");
457 query
.setParameterList("partOf", partOf
);
458 return (Long
)query
.uniqueResult();
462 AuditQuery query
= makeAuditQuery(DefinedTermBase
.class, auditEvent
);
463 query
.add(AuditEntity
.relatedId("partOf").eq(t
.getId()));
464 query
.addProjection(AuditEntity
.id().count());
465 count
+= (Long
)query
.getSingleResult();
472 public <T
extends DefinedTermBase
> List
<T
> getGeneralizationOf(T kindOf
, Integer pageSize
, Integer pageNumber
) {
473 AuditEvent auditEvent
= getAuditEventFromContext();
474 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
475 Query query
= getSession().createQuery("select term from DefinedTermBase term where term.kindOf = :kindOf");
476 query
.setParameter("kindOf", kindOf
);
478 addPageSizeAndNumber(query
, pageSize
, pageNumber
);
480 @SuppressWarnings("unchecked")
481 List
<T
> result
= query
.list();
484 AuditQuery query
= makeAuditQuery(DefinedTermBase
.class, auditEvent
);
485 query
.add(AuditEntity
.relatedId("kindOf").eq(kindOf
.getId()));
487 addPageSizeAndNumber(query
, pageSize
, pageNumber
);
489 @SuppressWarnings("unchecked")
490 List
<T
> result
= query
.getResultList();
496 public <T
extends DefinedTermBase
> List
<T
> getIncludes(Collection
<T
> partOf
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
497 if (partOf
== null || partOf
.isEmpty()){
498 return new ArrayList
<>();
500 AuditEvent auditEvent
= getAuditEventFromContext();
501 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
502 Query query
= getSession().createQuery("select term from DefinedTermBase term where term.partOf in (:partOf)");
503 query
.setParameterList("partOf", partOf
);
505 addPageSizeAndNumber(query
, pageSize
, pageNumber
);
507 @SuppressWarnings("unchecked")
508 List
<T
> results
= query
.list();
509 defaultBeanInitializer
.initializeAll(results
, propertyPaths
);
512 List
<T
> result
= new ArrayList
<T
>();
514 AuditQuery query
= makeAuditQuery(DefinedTermBase
.class, auditEvent
);
515 query
.add(AuditEntity
.relatedId("partOf").eq(t
.getId()));
516 addPageSizeAndNumber(query
, pageSize
, pageNumber
);
518 result
.addAll(query
.getResultList());
520 defaultBeanInitializer
.initializeAll(result
, propertyPaths
);
526 public <T
extends DefinedTermBase
> long countPartOf(Set
<T
> definedTerms
) {
527 checkNotInPriorView("DefinedTermDaoImpl.countPartOf(Set<T> definedTerms)");
528 Query query
= getSession().createQuery("select count(distinct definedTerm) from DefinedTermBase definedTerm join definedTerm.includes included where included in (:definedTerms)");
529 query
.setParameterList("definedTerms", definedTerms
);
530 return (Long
)query
.uniqueResult();
534 public <T
extends DefinedTermBase
> List
<T
> getPartOf(Set
<T
> definedTerms
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
535 checkNotInPriorView("DefinedTermDaoImpl.getPartOf(Set<T> definedTerms, Integer pageSize, Integer pageNumber)");
536 Query query
= getSession().createQuery("select distinct definedTerm from DefinedTermBase definedTerm join definedTerm.includes included where included in (:definedTerms)");
537 query
.setParameterList("definedTerms", definedTerms
);
539 addPageSizeAndNumber(query
, pageSize
, pageNumber
);
541 @SuppressWarnings("unchecked")
542 List
<T
> r
= query
.list();
544 * For some weird reason, hibernate returns proxies (extending the superclass), not the actual class on this,
545 * despite querying the damn database and returning the discriminator along with the rest of the object properties!
547 * Probably a bug in hibernate, but we'll manually deproxy for now since the objects are initialized anyway, the
548 * performance implications are small (we're swapping one array of references for another, not hitting the db or
551 List
<T
> results
= new ArrayList
<>();
552 if(!definedTerms
.isEmpty()) {
554 T deproxied
= CdmBase
.deproxy(t
);
555 results
.add(deproxied
);
557 defaultBeanInitializer
.initializeAll(results
, propertyPaths
);
563 public DefinedTermBase
findByUri(URI uri
) {
564 AuditEvent auditEvent
= getAuditEventFromContext();
565 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
566 Query query
= getSession().createQuery("select term from DefinedTermBase term where term.uri = :uri");
567 query
.setParameter("uri", uri
);
568 return (DefinedTermBase
<?
>)query
.uniqueResult();
570 AuditQuery query
= makeAuditQuery(DefinedTermBase
.class, auditEvent
);
571 query
.add(AuditEntity
.property("uri").eq(uri
));
572 return (DefinedTermBase
<?
>)query
.getSingleResult();
577 public <T
extends DefinedTermBase
> List
<T
> listByTermType(TermType termType
, Integer limit
, Integer start
,
578 List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
579 Query query
= getSession().createQuery("SELECT term FROM DefinedTermBase term WHERE term.termType = :termType");
580 query
.setParameter("termType", termType
);
582 @SuppressWarnings("unchecked")
583 List
<T
> result
= query
.list();
585 defaultBeanInitializer
.initializeAll(result
, propertyPaths
);
590 public <TERM
extends DefinedTermBase
> List
<TERM
> listByTermClass(Class
<TERM
> clazz
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
591 // checkNotInPriorView("DefinedTermDaoImpl.listByTermClass(Class<TERM> clazz, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths)");
593 Query query
= getSession().createQuery("FROM " + clazz
.getSimpleName());
595 @SuppressWarnings("unchecked")
596 List
<TERM
> result
= query
.list();
598 defaultBeanInitializer
.initializeAll(result
, propertyPaths
);
604 public <S
extends DefinedTermBase
> List
<S
> list(Class
<S
> type
, Integer limit
, Integer start
,
605 List
<OrderHint
> orderHints
, List
<String
> propertyPath
) {
607 return deduplicateResult(super.list(type
, limit
, start
, orderHints
, propertyPath
));
611 * Workaround for https://dev.e-taxonomy.eu/redmine/issues/5871 and #5945
612 * Terms with multiple representations return identical duplicates
613 * due to eager representation loading. We expect these duplicates to appear
614 * in line wo we only compare one term with its predecessor. If it already
615 * exists we remove it from the result.
619 private <S
extends DefinedTermBase
<?
>> List
<S
> deduplicateResult(List
<S
> orginals
) {
620 List
<S
> result
= new ArrayList
<>();
621 Iterator
<S
> it
= orginals
.iterator();
623 while (it
.hasNext()){
626 if (!result
.contains(a
)){
636 public <S
extends DefinedTermBase
> List
<S
> list(Class
<S
> clazz
, List
<TermVocabulary
> vocs
, Integer limit
, String pattern
) {
637 return list(clazz
, vocs
, 0, limit
, pattern
, MatchMode
.BEGINNING
);
641 public <S
extends DefinedTermBase
> List
<S
> list(Class
<S
> clazz
, List
<TermVocabulary
> vocs
, Integer pageNumber
, Integer limit
, String pattern
, MatchMode matchmode
){
642 Session session
= getSession();
643 // Query query = null;
644 // if (pattern != null){
645 // if (vocs != null && !vocs.isEmpty()){
646 // query = session.createQuery("from NamedArea where titleCache like :pattern and vocabulary in :vocs ORDER BY titleCache");
647 // query.setParameterList("vocs", vocs);
649 // query = session.createQuery("from NamedArea where titleCache like :pattern ORDER BY titleCache");
651 // pattern = pattern.replace("*", "%");
652 // pattern = pattern.replace("?", "_");
653 // pattern = pattern + "%";
654 // query.setParameter("pattern", pattern);
657 // query = session.createQuery("FROM NamedArea WHERE vocabulary IN :vocs ORDER BY titleCache");
658 // query.setParameterList("vocs", vocs);
660 // if (limit != null && limit > 0){
661 // query.setMaxResults(limit);
667 Criteria crit
= getSession().createCriteria(clazz
, "term");
668 if (!StringUtils
.isBlank(pattern
)){
669 if (matchmode
== MatchMode
.EXACT
) {
670 crit
.add(Restrictions
.eq("titleCache", matchmode
.queryStringFrom(pattern
)));
672 crit
.add(Restrictions
.like("titleCache", matchmode
.queryStringFrom(pattern
)));
675 if (limit
!= null && limit
>= 0) {
676 crit
.setMaxResults(limit
);
679 if (vocs
!= null &&!vocs
.isEmpty()){
680 crit
.createAlias("term.vocabulary", "voc");
681 Disjunction or
= Restrictions
.disjunction();
682 for (TermVocabulary
<?
> voc
: vocs
){
683 Criterion criterion
= Restrictions
.eq("voc.id", voc
.getId());
689 crit
.addOrder(Order
.asc("titleCache"));
693 // int firstItem = (pageNumber - 1) * limit;
695 crit
.setFirstResult(0);
696 @SuppressWarnings("unchecked")
697 List
<S
> results
= deduplicateResult(crit
.list());
702 public <S
extends DefinedTermBase
> List
<S
> listByAbbrev(Class
<S
> clazz
, List
<TermVocabulary
> vocs
, Integer limit
, String pattern
, NamedAreaSearchField type
) {
703 return listByAbbrev(clazz
, vocs
, 0, limit
, pattern
, MatchMode
.BEGINNING
, type
);
707 public <S
extends DefinedTermBase
> List
<S
> listByAbbrev(Class
<S
> clazz
, List
<TermVocabulary
> vocs
, Integer pageNumber
, Integer limit
, String pattern
, MatchMode matchmode
, NamedAreaSearchField abbrevType
){
712 Criteria crit
= getSession().createCriteria(clazz
, "type");
713 if (!StringUtils
.isBlank(pattern
)){
714 if (matchmode
== MatchMode
.EXACT
) {
715 crit
.add(Restrictions
.eq(abbrevType
.getKey(), matchmode
.queryStringFrom(pattern
)));
717 crit
.add(Restrictions
.like(abbrevType
.getKey(), matchmode
.queryStringFrom(pattern
)));
720 if (limit
!= null && limit
>= 0) {
721 crit
.setMaxResults(limit
);
724 if (vocs
!= null &&!vocs
.isEmpty()){
725 crit
.createAlias("type.vocabulary", "voc");
726 Disjunction or
= Restrictions
.disjunction();
727 for (TermVocabulary
<?
> voc
: vocs
){
728 Criterion criterion
= Restrictions
.eq("voc.id", voc
.getId());
734 crit
.addOrder(Order
.asc(abbrevType
.getKey()));
738 // int firstItem = (pageNumber - 1) * limit;
740 crit
.setFirstResult(0);
741 @SuppressWarnings("unchecked")
742 List
<S
> results
= deduplicateResult(crit
.list());
746 //FIXME AM: this returns only NamedArea counts though not mentioned in method name, fortunately it is currently not in use
748 public long count(List
<TermVocabulary
> vocs
, String pattern
){
749 Session session
= getSession();
751 if (pattern
!= null){
752 if (vocs
!= null && !vocs
.isEmpty()){
753 query
= session
.createQuery("SELECT COUNT(*) FROM NamedArea WHERE titleCache LIKE :pattern AND vocabulary IN :vocs");
754 query
.setParameterList("vocs", vocs
);
756 query
= session
.createQuery("SELECT COUNT(*) FROM NamedArea WHERE titleCache LIKE :pattern ");
758 pattern
= pattern
.replace("*", "%");
759 pattern
= pattern
.replace("?", "_");
760 pattern
= pattern
+ "%";
761 query
.setParameter("pattern", pattern
);
764 query
= session
.createQuery("SELECT COUNT(*) FROM NamedArea WHERE vocabulary IN :vocs");
765 query
.setParameterList("vocs", vocs
);
768 Long result
= (Long
) query
.uniqueResult();
773 public Collection
<TermDto
> getIncludesAsDto(
774 TermDto parentTerm
) {
776 if (parentTerm
.getTermType().equals(TermType
.NamedArea
)){
777 queryString
= TermDto
.getTermDtoSelectNamedArea();
779 queryString
= TermDto
.getTermDtoSelect();
781 queryString
= queryString
782 + "where a.partOf.uuid = :parentUuid";
783 Query query
= getSession().createQuery(queryString
);
784 query
.setParameter("parentUuid", parentTerm
.getUuid());
786 @SuppressWarnings("unchecked")
787 List
<Object
[]> result
= query
.list();
789 List
<TermDto
> list
= TermDto
.termDtoListFrom(result
);
794 public Collection
<TermDto
> getKindOfsAsDto(
795 TermDto parentTerm
) {
797 if (parentTerm
.getTermType().equals(TermType
.NamedArea
)){
798 queryString
= TermDto
.getTermDtoSelectNamedArea();
800 queryString
= TermDto
.getTermDtoSelect();
802 queryString
= queryString
+ "where a.kindOf.uuid = :parentUuid";
803 Query query
= getSession().createQuery(queryString
);
804 query
.setParameter("parentUuid", parentTerm
.getUuid());
806 @SuppressWarnings("unchecked")
807 List
<Object
[]> result
= query
.list();
809 List
<TermDto
> list
= TermDto
.termDtoListFrom(result
);
814 public Collection
<TermDto
> findByTitleAsDto(String title
, TermType termType
) {
815 String queryString
= TermDto
.getTermDtoSelect()
816 + " where a.titleCache like :title "
817 + (termType
!=null?
" and a.termType = :termType ":"");
819 title
= title
.replace("*", "%");
820 Query query
= getSession().createQuery(queryString
);
821 query
.setParameter("title", "%"+title
+"%");
823 query
.setParameter("termType", termType
);
826 @SuppressWarnings("unchecked")
827 List
<Object
[]> result
= query
.list();
829 List
<TermDto
> list
= TermDto
.termDtoListFrom(result
);
834 public Collection
<TermDto
> findByTypeAsDto(TermType termType
) {
835 if (termType
== null){
838 String queryString
= TermDto
.getTermDtoSelect()
839 + " where a.termType = :termType ";
840 Query query
= getSession().createQuery(queryString
);
843 query
.setParameter("termType", termType
);
846 @SuppressWarnings("unchecked")
847 List
<Object
[]> result
= query
.list();
849 List
<TermDto
> list
= TermDto
.termDtoListFrom(result
);
854 public Collection
<TermDto
> findByUriAsDto(URI uri
, String termLabel
, TermType termType
) {
855 String queryString
= TermDto
.getTermDtoSelect()
856 + " where a.uri like :uri "
857 + (termType
!=null?
" and a.termType = :termType ":"")
858 + (termLabel
!=null?
" and a.titleCache = :termLabel ":"")
860 Query query
= getSession().createQuery(queryString
);
861 query
.setParameter("uri", uri
.toString());
863 query
.setParameter("termLabel", "%"+termLabel
+"%");
866 query
.setParameter("termType", termType
);
869 @SuppressWarnings("unchecked")
870 List
<Object
[]> result
= query
.list();
872 List
<TermDto
> list
= TermDto
.termDtoListFrom(result
);
877 public List
<TermDto
> getSupportedStatesForFeature(UUID featureUuid
){
878 List
<TermDto
> list
= new ArrayList
<>();
879 String supportedCategoriesQueryString
= "SELECT cat.uuid "
880 + "from DefinedTermBase t "
881 + "join t.supportedCategoricalEnumerations as cat "
882 + "where t.uuid = :featureUuid";
883 Query supportedCategoriesQuery
= getSession().createQuery(supportedCategoriesQueryString
);
884 supportedCategoriesQuery
.setParameter("featureUuid", featureUuid
);
885 @SuppressWarnings("unchecked")
886 List
<UUID
> supportedCategories
= supportedCategoriesQuery
.list();
887 if(supportedCategories
.isEmpty()){
891 String queryString
= TermDto
.getTermDtoSelect()
892 + "where v.uuid in (:supportedCategories) "
893 + "order by a.titleCache";
894 Query query
= getSession().createQuery(queryString
);
895 query
.setParameterList("supportedCategories", supportedCategories
);
897 @SuppressWarnings("unchecked")
898 List
<Object
[]> result
= query
.list();
900 list
= TermDto
.termDtoListFrom(result
);
905 public Collection
<TermDto
> findByUUIDsAsDto(List
<UUID
> uuidList
) {
906 List
<TermDto
> list
= new ArrayList
<>();
907 if (uuidList
== null || uuidList
.isEmpty()){
911 String queryString
= TermDto
.getTermDtoSelect()
912 + "where a.uuid in :uuidList "
913 + "order by a.titleCache";
914 Query query
= getSession().createQuery(queryString
);
915 query
.setParameterList("uuidList", uuidList
);
917 @SuppressWarnings("unchecked")
918 List
<Object
[]> result
= query
.list();
920 list
= TermDto
.termDtoListFrom(result
);
925 public Collection
<TermDto
> findFeatureByUUIDsAsDto(List
<UUID
> uuidList
) {
926 List
<TermDto
> list
= new ArrayList
<>();
927 if (uuidList
== null || uuidList
.isEmpty()){
931 String queryString
= FeatureDto
.getTermDtoSelect()
932 + "where a.uuid in :uuidList "
933 + "order by a.titleCache";
934 Query query
= getSession().createQuery(queryString
);
935 query
.setParameterList("uuidList", uuidList
);
937 @SuppressWarnings("unchecked")
938 List
<Object
[]> result
= query
.list();
940 list
= FeatureDto
.termDtoListFrom(result
);
945 public Collection
<TermDto
> findFeatureByTitleAsDto(String pattern
) {
946 String queryString
= FeatureDto
.getTermDtoSelect()
947 + " where a.titleCache like :title "
948 + " and a.termType = :termType ";
950 pattern
= pattern
.replace("*", "%");
951 Query query
= getSession().createQuery(queryString
);
952 query
.setParameter("title", "%"+pattern
+"%");
953 query
.setParameter("termType", TermType
.Feature
);
956 @SuppressWarnings("unchecked")
957 List
<Object
[]> result
= query
.list();
959 List
<TermDto
> list
= FeatureDto
.termDtoListFrom(result
);