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
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Collection
;
15 import java
.util
.Enumeration
;
16 import java
.util
.Iterator
;
17 import java
.util
.List
;
18 import java
.util
.Locale
;
20 import java
.util
.UUID
;
22 import org
.apache
.commons
.lang
.StringUtils
;
23 import org
.apache
.log4j
.Logger
;
24 import org
.hibernate
.Criteria
;
25 import org
.hibernate
.Query
;
26 import org
.hibernate
.Session
;
27 import org
.hibernate
.criterion
.Criterion
;
28 import org
.hibernate
.criterion
.Disjunction
;
29 import org
.hibernate
.criterion
.Order
;
30 import org
.hibernate
.criterion
.Projections
;
31 import org
.hibernate
.criterion
.Restrictions
;
32 import org
.hibernate
.envers
.query
.AuditEntity
;
33 import org
.hibernate
.envers
.query
.AuditQuery
;
34 import org
.springframework
.stereotype
.Repository
;
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 List
<NamedArea
> listNamedArea(List
<TermVocabulary
> vocs
, Integer pageNumber
, Integer limit
, String pattern
, MatchMode matchmode
){
637 Session session
= getSession();
638 // Query query = null;
639 // if (pattern != null){
640 // if (vocs != null && !vocs.isEmpty()){
641 // query = session.createQuery("from NamedArea where titleCache like :pattern and vocabulary in :vocs ORDER BY titleCache");
642 // query.setParameterList("vocs", vocs);
644 // query = session.createQuery("from NamedArea where titleCache like :pattern ORDER BY titleCache");
646 // pattern = pattern.replace("*", "%");
647 // pattern = pattern.replace("?", "_");
648 // pattern = pattern + "%";
649 // query.setParameter("pattern", pattern);
652 // query = session.createQuery("FROM NamedArea WHERE vocabulary IN :vocs ORDER BY titleCache");
653 // query.setParameterList("vocs", vocs);
655 // if (limit != null && limit > 0){
656 // query.setMaxResults(limit);
659 Criteria crit
= getSession().createCriteria(type
, "namedArea");
660 if (!StringUtils
.isBlank(pattern
)){
661 if (matchmode
== MatchMode
.EXACT
) {
662 crit
.add(Restrictions
.eq("titleCache", matchmode
.queryStringFrom(pattern
)));
664 // crit.add(Restrictions.ilike("titleCache", matchmode.queryStringFrom(queryString)));
665 crit
.add(Restrictions
.like("titleCache", matchmode
.queryStringFrom(pattern
)));
668 if (limit
!= null && limit
>= 0) {
669 crit
.setMaxResults(limit
);
672 if (vocs
!= null &&!vocs
.isEmpty()){
673 crit
.createAlias("namedArea.vocabulary", "voc");
674 Disjunction or
= Restrictions
.disjunction();
675 for (TermVocabulary
<?
> voc
: vocs
){
676 Criterion criterion
= Restrictions
.eq("voc.id", voc
.getId());
682 crit
.addOrder(Order
.asc("titleCache"));
686 // int firstItem = (pageNumber - 1) * limit;
688 crit
.setFirstResult(0);
689 @SuppressWarnings("unchecked")
690 List
<NamedArea
> results
= crit
.list();
696 public List
<NamedArea
> listNamedAreaByAbbrev(List
<TermVocabulary
> vocs
, Integer pageNumber
, Integer limit
, String pattern
, MatchMode matchmode
, NamedAreaSearchField abbrevType
){
698 Criteria crit
= getSession().createCriteria(type
, "namedArea");
699 if (!StringUtils
.isBlank(pattern
)){
700 if (matchmode
== MatchMode
.EXACT
) {
701 crit
.add(Restrictions
.eq(abbrevType
.getKey(), matchmode
.queryStringFrom(pattern
)));
703 // crit.add(Restrictions.ilike("titleCache", matchmode.queryStringFrom(queryString)));
704 crit
.add(Restrictions
.like(abbrevType
.getKey(), matchmode
.queryStringFrom(pattern
)));
707 if (limit
!= null && limit
>= 0) {
708 crit
.setMaxResults(limit
);
711 if (vocs
!= null &&!vocs
.isEmpty()){
712 crit
.createAlias("namedArea.vocabulary", "voc");
713 Disjunction or
= Restrictions
.disjunction();
714 for (TermVocabulary
<?
> voc
: vocs
){
715 Criterion criterion
= Restrictions
.eq("voc.id", voc
.getId());
721 crit
.addOrder(Order
.asc(abbrevType
.getKey()));
725 // int firstItem = (pageNumber - 1) * limit;
727 crit
.setFirstResult(0);
728 @SuppressWarnings("unchecked")
729 List
<NamedArea
> results
= crit
.list();
734 public long count(List
<TermVocabulary
> vocs
, String pattern
){
735 Session session
= getSession();
737 if (pattern
!= null){
738 if (vocs
!= null && !vocs
.isEmpty()){
739 query
= session
.createQuery("SELECT COUNT(*) FROM NamedArea WHERE titleCache LIKE :pattern AND vocabulary IN :vocs");
740 query
.setParameterList("vocs", vocs
);
742 query
= session
.createQuery("SELECT COUNT(*) FROM NamedArea WHERE titleCache LIKE :pattern ");
744 pattern
= pattern
.replace("*", "%");
745 pattern
= pattern
.replace("?", "_");
746 pattern
= pattern
+ "%";
747 query
.setParameter("pattern", pattern
);
750 query
= session
.createQuery("SELECT COUNT(*) FROM NamedArea WHERE vocabulary IN :vocs");
751 query
.setParameterList("vocs", vocs
);
754 Long result
= (Long
) query
.uniqueResult();
759 public Collection
<TermDto
> getIncludesAsDto(
760 TermDto parentTerm
) {
762 if (parentTerm
.getTermType().equals(TermType
.NamedArea
)){
763 queryString
= TermDto
.getTermDtoSelectNamedArea();
765 queryString
= TermDto
.getTermDtoSelect();
767 queryString
= queryString
768 + "where a.partOf.uuid = :parentUuid";
769 Query query
= getSession().createQuery(queryString
);
770 query
.setParameter("parentUuid", parentTerm
.getUuid());
772 @SuppressWarnings("unchecked")
773 List
<Object
[]> result
= query
.list();
775 List
<TermDto
> list
= TermDto
.termDtoListFrom(result
);
780 public Collection
<TermDto
> getKindOfsAsDto(
781 TermDto parentTerm
) {
783 if (parentTerm
.getTermType().equals(TermType
.NamedArea
)){
784 queryString
= TermDto
.getTermDtoSelectNamedArea();
786 queryString
= TermDto
.getTermDtoSelect();
788 queryString
= queryString
+ "where a.kindOf.uuid = :parentUuid";
789 Query query
= getSession().createQuery(queryString
);
790 query
.setParameter("parentUuid", parentTerm
.getUuid());
792 @SuppressWarnings("unchecked")
793 List
<Object
[]> result
= query
.list();
795 List
<TermDto
> list
= TermDto
.termDtoListFrom(result
);
800 public Collection
<TermDto
> findByTitleAsDto(String title
, TermType termType
) {
801 String queryString
= TermDto
.getTermDtoSelect()
802 + " where a.titleCache like :title "
803 + (termType
!=null?
" and a.termType = :termType ":"");
805 title
= title
.replace("*", "%");
806 Query query
= getSession().createQuery(queryString
);
807 query
.setParameter("title", "%"+title
+"%");
809 query
.setParameter("termType", termType
);
812 @SuppressWarnings("unchecked")
813 List
<Object
[]> result
= query
.list();
815 List
<TermDto
> list
= TermDto
.termDtoListFrom(result
);
820 public Collection
<TermDto
> findByTypeAsDto(TermType termType
) {
821 if (termType
== null){
824 String queryString
= TermDto
.getTermDtoSelect()
825 + " where a.termType = :termType ";
826 Query query
= getSession().createQuery(queryString
);
829 query
.setParameter("termType", termType
);
832 @SuppressWarnings("unchecked")
833 List
<Object
[]> result
= query
.list();
835 List
<TermDto
> list
= TermDto
.termDtoListFrom(result
);
840 public Collection
<TermDto
> findByUriAsDto(URI uri
, String termLabel
, TermType termType
) {
841 String queryString
= TermDto
.getTermDtoSelect()
842 + " where a.uri like :uri "
843 + (termType
!=null?
" and a.termType = :termType ":"")
844 + (termLabel
!=null?
" and a.titleCache = :termLabel ":"")
846 Query query
= getSession().createQuery(queryString
);
847 query
.setParameter("uri", uri
.toString());
849 query
.setParameter("termLabel", "%"+termLabel
+"%");
852 query
.setParameter("termType", termType
);
855 @SuppressWarnings("unchecked")
856 List
<Object
[]> result
= query
.list();
858 List
<TermDto
> list
= TermDto
.termDtoListFrom(result
);
863 public List
<NamedArea
> listNamedArea(List
<TermVocabulary
> vocs
, Integer limit
, String pattern
) {
865 return listNamedArea(vocs
, 0, limit
, pattern
, MatchMode
.BEGINNING
);
869 public List
<NamedArea
> listNamedAreaByAbbrev(List
<TermVocabulary
> vocs
, Integer limit
, String pattern
, NamedAreaSearchField type
) {
871 return listNamedAreaByAbbrev(vocs
, 0, limit
, pattern
, MatchMode
.BEGINNING
, type
);
875 public List
<TermDto
> getSupportedStatesForFeature(UUID featureUuid
){
876 List
<TermDto
> list
= new ArrayList
<>();
877 String supportedCategoriesQueryString
= "SELECT cat.uuid "
878 + "from DefinedTermBase t "
879 + "join t.supportedCategoricalEnumerations as cat "
880 + "where t.uuid = :featureUuid";
881 Query supportedCategoriesQuery
= getSession().createQuery(supportedCategoriesQueryString
);
882 supportedCategoriesQuery
.setParameter("featureUuid", featureUuid
);
883 @SuppressWarnings("unchecked")
884 List
<UUID
> supportedCategories
= supportedCategoriesQuery
.list();
885 if(supportedCategories
.isEmpty()){
889 String queryString
= TermDto
.getTermDtoSelect()
890 + "where v.uuid in (:supportedCategories) "
891 + "order by a.titleCache";
892 Query query
= getSession().createQuery(queryString
);
893 query
.setParameterList("supportedCategories", supportedCategories
);
895 @SuppressWarnings("unchecked")
896 List
<Object
[]> result
= query
.list();
898 list
= TermDto
.termDtoListFrom(result
);
903 public Collection
<TermDto
> findByUUIDsAsDto(List
<UUID
> uuidList
) {
904 List
<TermDto
> list
= new ArrayList
<>();
905 if (uuidList
== null || uuidList
.isEmpty()){
909 String queryString
= TermDto
.getTermDtoSelect()
910 + "where a.uuid in :uuidList "
911 + "order by a.titleCache";
912 Query query
= getSession().createQuery(queryString
);
913 query
.setParameterList("uuidList", uuidList
);
915 @SuppressWarnings("unchecked")
916 List
<Object
[]> result
= query
.list();
918 list
= TermDto
.termDtoListFrom(result
);
923 public Collection
<TermDto
> findFeatureByUUIDsAsDto(List
<UUID
> uuidList
) {
924 List
<TermDto
> list
= new ArrayList
<>();
925 if (uuidList
== null || uuidList
.isEmpty()){
929 String queryString
= FeatureDto
.getTermDtoSelect()
930 + "where a.uuid in :uuidList "
931 + "order by a.titleCache";
932 Query query
= getSession().createQuery(queryString
);
933 query
.setParameterList("uuidList", uuidList
);
935 @SuppressWarnings("unchecked")
936 List
<Object
[]> result
= query
.list();
938 list
= FeatureDto
.termDtoListFrom(result
);
943 public Collection
<TermDto
> findFeatureByTitleAsDto(String pattern
) {
944 String queryString
= FeatureDto
.getTermDtoSelect()
945 + " where a.titleCache like :title "
946 + " and a.termType = :termType ";
948 pattern
= pattern
.replace("*", "%");
949 Query query
= getSession().createQuery(queryString
);
950 query
.setParameter("title", "%"+pattern
+"%");
951 query
.setParameter("termType", TermType
.Feature
);
954 @SuppressWarnings("unchecked")
955 List
<Object
[]> result
= query
.list();
957 List
<TermDto
> list
= FeatureDto
.termDtoListFrom(result
);