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
extends IdentifiableDaoBase
<DefinedTermBase
> implements IDefinedTermDao
{
81 private static final Logger logger
= Logger
.getLogger(DefinedTermDaoImpl
.class);
83 public DefinedTermDaoImpl() {
84 super(DefinedTermBase
.class);
85 indexedClasses
= new Class
[25];
86 indexedClasses
[0] = Rank
.class;
87 indexedClasses
[1] = AnnotationType
.class;
88 indexedClasses
[2] = ExtensionType
.class;
89 indexedClasses
[3] = Language
.class;
90 indexedClasses
[4] = MarkerType
.class;
91 indexedClasses
[5] = MeasurementUnit
.class;
92 indexedClasses
[6] = DefinedTerm
.class;
93 indexedClasses
[7] = PresenceAbsenceTerm
.class;
94 indexedClasses
[8] = State
.class;
95 indexedClasses
[9] = StatisticalMeasure
.class;
96 indexedClasses
[10] = TextFormat
.class;
97 indexedClasses
[11] = DerivationEventType
.class;
98 indexedClasses
[12] = NamedArea
.class;
99 indexedClasses
[13] = NamedAreaLevel
.class;
100 indexedClasses
[14] = NamedAreaType
.class;
101 indexedClasses
[15] = ReferenceSystem
.class;
102 indexedClasses
[16] = Country
.class;
103 indexedClasses
[17] = RightsType
.class;
104 indexedClasses
[18] = HybridRelationshipType
.class;
105 indexedClasses
[19] = NameRelationshipType
.class;
106 indexedClasses
[20] = NameTypeDesignationStatus
.class;
107 indexedClasses
[21] = NomenclaturalStatusType
.class;
108 indexedClasses
[22] = SpecimenTypeDesignationStatus
.class;
109 indexedClasses
[23] = SynonymType
.class;
110 indexedClasses
[24] = TaxonRelationshipType
.class;
117 public List
<DefinedTermBase
> findByTitle(String queryString
) {
118 return findByTitle(queryString
, null);
125 public List
<DefinedTermBase
> findByTitle(String queryString
, CdmBase sessionObject
) {
126 checkNotInPriorView("DefinedTermDaoImpl.findByTitle(String queryString, CdmBase sessionObject)");
127 Session session
= getSession();
128 if ( sessionObject
!= null ) {//attache the object to the session, TODO needed?
129 session
.update(sessionObject
);
131 Query query
= session
.createQuery("select term from DefinedTermBase term join fetch term.representations representation where representation.label = :label");
132 query
.setParameter("label", queryString
);
133 @SuppressWarnings({ "unchecked", "rawtypes" })
134 List
<DefinedTermBase
> result
= query
.list();
140 public List
<DefinedTermBase
> findByTitle(String queryString
, MatchMode matchMode
, int page
, int pagesize
, List
<Criterion
> criteria
) {
141 //FIXME is query parametrised?
142 checkNotInPriorView("DefinedTermDaoImpl.findByTitle(String queryString, ITitledDao.MATCH_MODE matchMode, int page, int pagesize, List<Criterion> criteria)");
143 Criteria crit
= getSession().createCriteria(type
);
144 crit
.add(Restrictions
.ilike("titleCache", matchMode
.queryStringFrom(queryString
)));
145 crit
.setMaxResults(pagesize
);
146 int firstItem
= (page
- 1) * pagesize
+ 1;
147 crit
.setFirstResult(firstItem
);
148 List
<DefinedTermBase
> results
= crit
.list();
153 public Country
getCountryByIso(String iso3166
) {
154 // If iso639 = "" query returns non-unique result. We prevent this here:
155 if (StringUtils
.isBlank(iso3166
) || iso3166
.length()<2 || iso3166
.length()>3) { return null; }
156 AuditEvent auditEvent
= getAuditEventFromContext();
157 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
158 Query query
= getSession().createQuery("from Country where iso3166_A2 = :isoCode OR idInVocabulary = :isoCode");
159 query
.setParameter("isoCode", iso3166
);
160 return (Country
) query
.uniqueResult();
162 AuditQuery query
= getAuditReader().createQuery().forEntitiesAtRevision(Country
.class,auditEvent
.getRevisionNumber());
163 query
.add(AuditEntity
.property("iso3166_A2").eq(iso3166
));
164 query
.add(AuditEntity
.property("idInVocabulary").eq(iso3166
));
165 return (Country
) query
.getSingleResult();
170 public <T
extends DefinedTermBase
> List
<T
> getDefinedTermByRepresentationText(String text
, Class
<T
> clazz
) {
171 return getDefinedTermByRepresentationText(text
,clazz
,null,null);
175 public <T
extends DefinedTermBase
> List
<T
> getDefinedTermByRepresentationText(String text
, Class
<T
> clazz
, Integer pageSize
,Integer pageNumber
) {
176 checkNotInPriorView("DefinedTermDaoImpl.getDefinedTermByRepresentationText(String text, Class<T> clazz, Integer pageSize,Integer pageNumber)");
178 Criteria criteria
= getCriteria(clazz
);
180 criteria
.createAlias("representations", "r").add(Restrictions
.like("r.text", text
));
182 addPageSizeAndNumber(criteria
, pageSize
, pageNumber
);
184 @SuppressWarnings("unchecked")
185 List
<T
> result
= criteria
.list();
190 public long countDefinedTermByRepresentationText(String text
, Class
<?
extends DefinedTermBase
> clazz
) {
191 checkNotInPriorView("DefinedTermDaoImpl.countDefinedTermByRepresentationText(String text, Class<? extends DefinedTermBase> clazz)");
192 Criteria criteria
= getCriteria(clazz
);
194 criteria
.createAlias("representations", "r").add(Restrictions
.like("r.text", text
));
196 criteria
.setProjection(Projections
.rowCount());
198 return (Long
)criteria
.uniqueResult();
202 public <T
extends DefinedTermBase
> List
<T
> getDefinedTermByIdInVocabulary(String label
, UUID vocUuid
, Class
<T
> clazz
, Integer pageSize
, Integer pageNumber
) {
203 checkNotInPriorView("DefinedTermDaoImpl.getDefinedTermByIdInVocabulary(String label, UUID vocUuid, Class<T> clazz, Integer pageSize, Integer pageNumber)");
205 Criteria criteria
= getCriteria(clazz
);
207 criteria
.createAlias("vocabulary", "voc")
208 .add(Restrictions
.like("voc.uuid", vocUuid
))
209 .add(Restrictions
.like("idInVocabulary", label
, org
.hibernate
.criterion
.MatchMode
.EXACT
));
211 addPageSizeAndNumber(criteria
, pageSize
, pageNumber
);
213 @SuppressWarnings("unchecked")
214 List
<T
> result
= criteria
.list();
219 public <T
extends DefinedTermBase
> List
<T
> getDefinedTermByRepresentationAbbrev(String text
, Class
<T
> clazz
, Integer pageSize
,Integer pageNumber
) {
220 checkNotInPriorView("DefinedTermDaoImpl.getDefinedTermByRepresentationAbbrev(String abbrev, Class<T> clazz, Integer pageSize,Integer pageNumber)");
222 Criteria criteria
= getCriteria(clazz
);
224 criteria
.createAlias("representations", "r").add(Restrictions
.like("r.abbreviatedLabel", text
));
226 addPageSizeAndNumber(criteria
, pageSize
, pageNumber
);
228 @SuppressWarnings("unchecked")
229 List
<T
> result
= criteria
.list();
234 public long countDefinedTermByRepresentationAbbrev(String text
, Class
<?
extends DefinedTermBase
> clazz
) {
235 checkNotInPriorView("DefinedTermDaoImpl.countDefinedTermByRepresentationAbbrev(String abbrev, Class<? extends DefinedTermBase> clazz)");
236 Criteria criteria
= getCriteria(clazz
);
238 criteria
.createAlias("representations", "r").add(Restrictions
.like("r.abbreviatedLabel", text
));
239 criteria
.setProjection(Projections
.rowCount());
241 return (Long
)criteria
.uniqueResult();
245 public Language
getLanguageByIso(String iso639
) {
246 if (iso639
.length() < 2 || iso639
.length() > 3) {
247 logger
.warn("Invalid length " + iso639
.length() + " of ISO code. Length must be 2 or 3.");
250 boolean isIso639_1
= iso639
.length() == 2;
254 queryStr
= "from Language where iso639_1 = :isoCode";
256 queryStr
= "from Language where idInVocabulary = :isoCode and vocabulary.uuid = :vocUuid";
258 AuditEvent auditEvent
= getAuditEventFromContext();
259 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
260 Query query
= getSession().createQuery(queryStr
);
261 query
.setParameter("isoCode", iso639
);
263 query
.setParameter("vocUuid", Language
.uuidLanguageVocabulary
);
265 return (Language
) query
.uniqueResult();
267 AuditQuery query
= getAuditReader().createQuery().forEntitiesAtRevision(Language
.class,auditEvent
.getRevisionNumber());
269 query
.add(AuditEntity
.property("iso639_1").eq(iso639
));
271 query
.add(AuditEntity
.property("iso639_2").eq(iso639
));
272 query
.add(AuditEntity
.property("vocabulary.uuid").eq(Language
.uuidLanguageVocabulary
));
275 return (Language
)query
.getSingleResult();
280 * FIXME this will result in a query per language - could you, given that iso codes
281 * are unique, use from Language where iso639_1 in (:isoCode) or iso639_2 in (:isoCode)
284 public List
<Language
> getLanguagesByIso(List
<String
> iso639List
) {
285 List
<Language
> languages
= new ArrayList
<Language
>(iso639List
.size());
286 for (String iso639
: iso639List
) {
287 languages
.add(getLanguageByIso(iso639
));
293 public List
<Language
> getLanguagesByLocale(Enumeration
<Locale
> locales
) {
294 List
<Language
> languages
= new ArrayList
<Language
>();
295 while(locales
.hasMoreElements()) {
296 Locale locale
= locales
.nextElement();
297 languages
.add(getLanguageByIso(locale
.getLanguage()));
303 public long count(NamedAreaLevel level
, NamedAreaType type
) {
304 AuditEvent auditEvent
= getAuditEventFromContext();
305 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
306 Criteria criteria
= getCriteria(NamedArea
.class);
309 criteria
.add(Restrictions
.eq("level",level
));
313 criteria
.add(Restrictions
.eq("type", type
));
316 criteria
.setProjection(Projections
.rowCount());
318 return (Long
)criteria
.uniqueResult();
320 AuditQuery query
= makeAuditQuery(NamedArea
.class, auditEvent
);
323 query
.add(AuditEntity
.relatedId("level").eq(level
.getId()));
327 query
.add(AuditEntity
.relatedId("type").eq(type
.getId()));
329 query
.addProjection(AuditEntity
.id().count());
330 return (Long
)query
.getSingleResult();
335 public long countMedia(DefinedTermBase definedTerm
) {
336 checkNotInPriorView("DefinedTermDaoImpl.countMedia(DefinedTermBase definedTerm)");
337 Query query
= getSession().createQuery("select count(media) from DefinedTermBase definedTerm join definedTerm.media media where definedTerm = :definedTerm");
338 query
.setParameter("definedTerm", definedTerm
);
340 return (Long
)query
.uniqueResult();
344 public List
<Media
> getMedia(DefinedTermBase definedTerm
, Integer pageSize
, Integer pageNumber
) {
345 checkNotInPriorView("DefinedTermDaoImpl.getMedia(DefinedTermBase definedTerm, Integer pageSize, Integer pageNumber)");
346 Query query
= getSession().createQuery("select media from DefinedTermBase definedTerm join definedTerm.media media where definedTerm = :definedTerm");
347 query
.setParameter("definedTerm", definedTerm
);
349 addPageSizeAndNumber(query
, pageSize
, pageNumber
);
351 @SuppressWarnings("unchecked")
352 List
<Media
> result
= query
.list();
357 public List
<NamedArea
> list(NamedAreaLevel level
, NamedAreaType type
, Integer pageSize
, Integer pageNumber
) {
358 AuditEvent auditEvent
= getAuditEventFromContext();
359 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
360 Criteria criteria
= getCriteria(NamedArea
.class);
363 criteria
.add(Restrictions
.eq("level",level
));
367 criteria
.add(Restrictions
.eq("type", type
));
370 addPageSizeAndNumber(criteria
, pageSize
, pageNumber
);
372 @SuppressWarnings("unchecked")
373 List
<NamedArea
> result
= criteria
.list();
376 AuditQuery query
= makeAuditQuery(NamedArea
.class, auditEvent
);
379 query
.add(AuditEntity
.relatedId("level").eq(level
.getId()));
383 query
.add(AuditEntity
.relatedId("type").eq(type
.getId()));
386 @SuppressWarnings("unchecked")
387 List
<NamedArea
> result
= query
.getResultList();
393 public List
<NamedArea
> list(NamedAreaLevel level
, NamedAreaType type
, Integer pageSize
, Integer pageNumber
,
394 List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
396 List
<NamedArea
> result
;
398 AuditEvent auditEvent
= getAuditEventFromContext();
399 if (auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
400 Criteria criteria
= getCriteria(NamedArea
.class);
403 criteria
.add(Restrictions
.eq("level", level
));
406 criteria
.add(Restrictions
.eq("type", type
));
408 addOrder(criteria
,orderHints
);
409 addPageSizeAndNumber(criteria
, pageSize
, pageNumber
);
411 result
= criteria
.list();
414 AuditQuery query
= getAuditReader().createQuery().forEntitiesAtRevision(NamedArea
.class,
415 auditEvent
.getRevisionNumber());
417 query
.add(AuditEntity
.relatedId("level").eq(level
.getId()));
420 query
.add(AuditEntity
.relatedId("type").eq(type
.getId()));
422 result
= query
.getResultList();
425 defaultBeanInitializer
.initializeAll(result
, propertyPaths
);
432 public <T
extends DefinedTermBase
> long countGeneralizationOf(T kindOf
) {
433 AuditEvent auditEvent
= getAuditEventFromContext();
434 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
435 Query query
= getSession().createQuery("select count(term) from DefinedTermBase term where term.kindOf = :kindOf");
436 query
.setParameter("kindOf", kindOf
);
437 return (Long
)query
.uniqueResult();
439 AuditQuery query
= makeAuditQuery(DefinedTermBase
.class,auditEvent
);
440 query
.add(AuditEntity
.relatedId("kindOf").eq(kindOf
.getId()));
441 query
.addProjection(AuditEntity
.id().count());
442 return (Long
)query
.getSingleResult();
447 public <T
extends DefinedTermBase
> long countIncludes(Collection
<T
> partOf
) {
448 if (partOf
== null || partOf
.isEmpty()){
451 AuditEvent auditEvent
= getAuditEventFromContext();
452 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
453 Query query
= getSession().createQuery("select count(term) from DefinedTermBase term where term.partOf in (:partOf)");
454 query
.setParameterList("partOf", partOf
);
455 return (Long
)query
.uniqueResult();
459 AuditQuery query
= makeAuditQuery(DefinedTermBase
.class, auditEvent
);
460 query
.add(AuditEntity
.relatedId("partOf").eq(t
.getId()));
461 query
.addProjection(AuditEntity
.id().count());
462 count
+= (Long
)query
.getSingleResult();
469 public <T
extends DefinedTermBase
> List
<T
> getGeneralizationOf(T kindOf
, Integer pageSize
, Integer pageNumber
) {
470 AuditEvent auditEvent
= getAuditEventFromContext();
471 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
472 Query query
= getSession().createQuery("select term from DefinedTermBase term where term.kindOf = :kindOf");
473 query
.setParameter("kindOf", kindOf
);
475 addPageSizeAndNumber(query
, pageSize
, pageNumber
);
477 @SuppressWarnings("unchecked")
478 List
<T
> result
= query
.list();
481 AuditQuery query
= makeAuditQuery(DefinedTermBase
.class, auditEvent
);
482 query
.add(AuditEntity
.relatedId("kindOf").eq(kindOf
.getId()));
484 addPageSizeAndNumber(query
, pageSize
, pageNumber
);
486 @SuppressWarnings("unchecked")
487 List
<T
> result
= query
.getResultList();
493 public <T
extends DefinedTermBase
> List
<T
> getIncludes(Collection
<T
> partOf
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
494 if (partOf
== null || partOf
.isEmpty()){
495 return new ArrayList
<>();
497 AuditEvent auditEvent
= getAuditEventFromContext();
498 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
499 Query query
= getSession().createQuery("select term from DefinedTermBase term where term.partOf in (:partOf)");
500 query
.setParameterList("partOf", partOf
);
502 addPageSizeAndNumber(query
, pageSize
, pageNumber
);
504 @SuppressWarnings("unchecked")
505 List
<T
> results
= query
.list();
506 defaultBeanInitializer
.initializeAll(results
, propertyPaths
);
509 List
<T
> result
= new ArrayList
<T
>();
511 AuditQuery query
= makeAuditQuery(DefinedTermBase
.class, auditEvent
);
512 query
.add(AuditEntity
.relatedId("partOf").eq(t
.getId()));
513 addPageSizeAndNumber(query
, pageSize
, pageNumber
);
515 result
.addAll(query
.getResultList());
517 defaultBeanInitializer
.initializeAll(result
, propertyPaths
);
523 public <T
extends DefinedTermBase
> long countPartOf(Set
<T
> definedTerms
) {
524 checkNotInPriorView("DefinedTermDaoImpl.countPartOf(Set<T> definedTerms)");
525 Query query
= getSession().createQuery("select count(distinct definedTerm) from DefinedTermBase definedTerm join definedTerm.includes included where included in (:definedTerms)");
526 query
.setParameterList("definedTerms", definedTerms
);
527 return (Long
)query
.uniqueResult();
531 public <T
extends DefinedTermBase
> List
<T
> getPartOf(Set
<T
> definedTerms
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
532 checkNotInPriorView("DefinedTermDaoImpl.getPartOf(Set<T> definedTerms, Integer pageSize, Integer pageNumber)");
533 Query query
= getSession().createQuery("select distinct definedTerm from DefinedTermBase definedTerm join definedTerm.includes included where included in (:definedTerms)");
534 query
.setParameterList("definedTerms", definedTerms
);
536 addPageSizeAndNumber(query
, pageSize
, pageNumber
);
538 @SuppressWarnings("unchecked")
539 List
<T
> r
= query
.list();
541 * For some weird reason, hibernate returns proxies (extending the superclass), not the actual class on this,
542 * despite querying the damn database and returning the discriminator along with the rest of the object properties!
544 * Probably a bug in hibernate, but we'll manually deproxy for now since the objects are initialized anyway, the
545 * performance implications are small (we're swapping one array of references for another, not hitting the db or
548 List
<T
> results
= new ArrayList
<>();
549 if(!definedTerms
.isEmpty()) {
551 T deproxied
= CdmBase
.deproxy(t
);
552 results
.add(deproxied
);
554 defaultBeanInitializer
.initializeAll(results
, propertyPaths
);
560 public DefinedTermBase
findByUri(URI uri
) {
561 AuditEvent auditEvent
= getAuditEventFromContext();
562 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
563 Query query
= getSession().createQuery("select term from DefinedTermBase term where term.uri = :uri");
564 query
.setParameter("uri", uri
);
565 return (DefinedTermBase
<?
>)query
.uniqueResult();
567 AuditQuery query
= makeAuditQuery(DefinedTermBase
.class, auditEvent
);
568 query
.add(AuditEntity
.property("uri").eq(uri
));
569 return (DefinedTermBase
<?
>)query
.getSingleResult();
574 public <T
extends DefinedTermBase
> List
<T
> listByTermType(TermType termType
, Integer limit
, Integer start
,
575 List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
576 Query query
= getSession().createQuery("SELECT term FROM DefinedTermBase term WHERE term.termType = :termType");
577 query
.setParameter("termType", termType
);
579 @SuppressWarnings("unchecked")
580 List
<T
> result
= query
.list();
582 defaultBeanInitializer
.initializeAll(result
, propertyPaths
);
587 public <TERM
extends DefinedTermBase
> List
<TERM
> listByTermClass(Class
<TERM
> clazz
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
588 // checkNotInPriorView("DefinedTermDaoImpl.listByTermClass(Class<TERM> clazz, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths)");
590 Query query
= getSession().createQuery("FROM " + clazz
.getSimpleName());
592 @SuppressWarnings("unchecked")
593 List
<TERM
> result
= query
.list();
595 defaultBeanInitializer
.initializeAll(result
, propertyPaths
);
601 public <S
extends DefinedTermBase
> List
<S
> list(Class
<S
> type
, Integer limit
, Integer start
,
602 List
<OrderHint
> orderHints
, List
<String
> propertyPath
) {
604 return deduplicateResult(super.list(type
, limit
, start
, orderHints
, propertyPath
));
608 * Workaround for http://dev.e-taxonomy.eu/trac/ticket/5871 and #5945
609 * Terms with multiple representations return identical duplicates
610 * due to eager representation loading. We expect these duplicates to appear
611 * in line wo we only compare one term with its predecessor. If it already
612 * exists we remove it from the result.
616 private <S
extends DefinedTermBase
<?
>> List
<S
> deduplicateResult(List
<S
> orginals
) {
617 List
<S
> result
= new ArrayList
<>();
618 Iterator
<S
> it
= orginals
.iterator();
620 while (it
.hasNext()){
623 if (!result
.contains(a
)){
633 public List
<NamedArea
> listNamedArea(List
<TermVocabulary
> vocs
, Integer pageNumber
, Integer limit
, String pattern
, MatchMode matchmode
){
634 Session session
= getSession();
635 // Query query = null;
636 // if (pattern != null){
637 // if (vocs != null && !vocs.isEmpty()){
638 // query = session.createQuery("from NamedArea where titleCache like :pattern and vocabulary in :vocs ORDER BY titleCache");
639 // query.setParameterList("vocs", vocs);
641 // query = session.createQuery("from NamedArea where titleCache like :pattern ORDER BY titleCache");
643 // pattern = pattern.replace("*", "%");
644 // pattern = pattern.replace("?", "_");
645 // pattern = pattern + "%";
646 // query.setParameter("pattern", pattern);
649 // query = session.createQuery("FROM NamedArea WHERE vocabulary IN :vocs ORDER BY titleCache");
650 // query.setParameterList("vocs", vocs);
652 // if (limit != null && limit > 0){
653 // query.setMaxResults(limit);
656 Criteria crit
= getSession().createCriteria(type
, "namedArea");
657 if (!StringUtils
.isBlank(pattern
)){
658 if (matchmode
== MatchMode
.EXACT
) {
659 crit
.add(Restrictions
.eq("titleCache", matchmode
.queryStringFrom(pattern
)));
661 // crit.add(Restrictions.ilike("titleCache", matchmode.queryStringFrom(queryString)));
662 crit
.add(Restrictions
.like("titleCache", matchmode
.queryStringFrom(pattern
)));
665 if (limit
!= null && limit
>= 0) {
666 crit
.setMaxResults(limit
);
669 if (vocs
!= null &&!vocs
.isEmpty()){
670 crit
.createAlias("namedArea.vocabulary", "voc");
671 Disjunction or
= Restrictions
.disjunction();
672 for (TermVocabulary
<?
> voc
: vocs
){
673 Criterion criterion
= Restrictions
.eq("voc.id", voc
.getId());
679 crit
.addOrder(Order
.asc("titleCache"));
683 // int firstItem = (pageNumber - 1) * limit;
685 crit
.setFirstResult(0);
686 @SuppressWarnings("unchecked")
687 List
<NamedArea
> results
= crit
.list();
693 public List
<NamedArea
> listNamedAreaByAbbrev(List
<TermVocabulary
> vocs
, Integer pageNumber
, Integer limit
, String pattern
, MatchMode matchmode
, NamedAreaSearchField abbrevType
){
695 Criteria crit
= getSession().createCriteria(type
, "namedArea");
696 if (!StringUtils
.isBlank(pattern
)){
697 if (matchmode
== MatchMode
.EXACT
) {
698 crit
.add(Restrictions
.eq(abbrevType
.getKey(), matchmode
.queryStringFrom(pattern
)));
700 // crit.add(Restrictions.ilike("titleCache", matchmode.queryStringFrom(queryString)));
701 crit
.add(Restrictions
.like(abbrevType
.getKey(), matchmode
.queryStringFrom(pattern
)));
704 if (limit
!= null && limit
>= 0) {
705 crit
.setMaxResults(limit
);
708 if (vocs
!= null &&!vocs
.isEmpty()){
709 crit
.createAlias("namedArea.vocabulary", "voc");
710 Disjunction or
= Restrictions
.disjunction();
711 for (TermVocabulary
<?
> voc
: vocs
){
712 Criterion criterion
= Restrictions
.eq("voc.id", voc
.getId());
718 crit
.addOrder(Order
.asc(abbrevType
.getKey()));
722 // int firstItem = (pageNumber - 1) * limit;
724 crit
.setFirstResult(0);
725 @SuppressWarnings("unchecked")
726 List
<NamedArea
> results
= crit
.list();
731 public long count(List
<TermVocabulary
> vocs
, String pattern
){
732 Session session
= getSession();
734 if (pattern
!= null){
735 if (vocs
!= null && !vocs
.isEmpty()){
736 query
= session
.createQuery("SELECT COUNT(*) FROM NamedArea WHERE titleCache LIKE :pattern AND vocabulary IN :vocs");
737 query
.setParameterList("vocs", vocs
);
739 query
= session
.createQuery("SELECT COUNT(*) FROM NamedArea WHERE titleCache LIKE :pattern ");
741 pattern
= pattern
.replace("*", "%");
742 pattern
= pattern
.replace("?", "_");
743 pattern
= pattern
+ "%";
744 query
.setParameter("pattern", pattern
);
747 query
= session
.createQuery("SELECT COUNT(*) FROM NamedArea WHERE vocabulary IN :vocs");
748 query
.setParameterList("vocs", vocs
);
751 Long result
= (Long
) query
.uniqueResult();
756 public Collection
<TermDto
> getIncludesAsDto(
757 TermDto parentTerm
) {
759 if (parentTerm
.getTermType().equals(TermType
.NamedArea
)){
760 queryString
= TermDto
.getTermDtoSelectNamedArea();
762 queryString
= TermDto
.getTermDtoSelect();
764 queryString
= queryString
765 + "where a.partOf.uuid = :parentUuid";
766 Query query
= getSession().createQuery(queryString
);
767 query
.setParameter("parentUuid", parentTerm
.getUuid());
769 @SuppressWarnings("unchecked")
770 List
<Object
[]> result
= query
.list();
772 List
<TermDto
> list
= TermDto
.termDtoListFrom(result
);
777 public Collection
<TermDto
> getKindOfsAsDto(
778 TermDto parentTerm
) {
780 if (parentTerm
.getTermType().equals(TermType
.NamedArea
)){
781 queryString
= TermDto
.getTermDtoSelectNamedArea();
783 queryString
= TermDto
.getTermDtoSelect();
785 queryString
= queryString
+ "where a.kindOf.uuid = :parentUuid";
786 Query query
= getSession().createQuery(queryString
);
787 query
.setParameter("parentUuid", parentTerm
.getUuid());
789 @SuppressWarnings("unchecked")
790 List
<Object
[]> result
= query
.list();
792 List
<TermDto
> list
= TermDto
.termDtoListFrom(result
);
797 public Collection
<TermDto
> findByTitleAsDto(String title
, TermType termType
) {
798 String queryString
= TermDto
.getTermDtoSelect()
799 + " where a.titleCache like :title "
800 + (termType
!=null?
" and a.termType = :termType ":"");
802 title
= title
.replace("*", "%");
803 Query query
= getSession().createQuery(queryString
);
804 query
.setParameter("title", "%"+title
+"%");
806 query
.setParameter("termType", termType
);
809 @SuppressWarnings("unchecked")
810 List
<Object
[]> result
= query
.list();
812 List
<TermDto
> list
= TermDto
.termDtoListFrom(result
);
817 public Collection
<TermDto
> findByTypeAsDto(TermType termType
) {
818 if (termType
== null){
821 String queryString
= TermDto
.getTermDtoSelect()
822 + " where a.termType = :termType ";
823 Query query
= getSession().createQuery(queryString
);
826 query
.setParameter("termType", termType
);
829 @SuppressWarnings("unchecked")
830 List
<Object
[]> result
= query
.list();
832 List
<TermDto
> list
= TermDto
.termDtoListFrom(result
);
837 public Collection
<TermDto
> findByUriAsDto(URI uri
, String termLabel
, TermType termType
) {
838 String queryString
= TermDto
.getTermDtoSelect()
839 + " where a.uri like :uri "
840 + (termType
!=null?
" and a.termType = :termType ":"")
841 + (termLabel
!=null?
" and a.titleCache = :termLabel ":"")
843 Query query
= getSession().createQuery(queryString
);
844 query
.setParameter("uri", uri
.toString());
846 query
.setParameter("termLabel", "%"+termLabel
+"%");
849 query
.setParameter("termType", termType
);
852 @SuppressWarnings("unchecked")
853 List
<Object
[]> result
= query
.list();
855 List
<TermDto
> list
= TermDto
.termDtoListFrom(result
);
860 public List
<NamedArea
> listNamedArea(List
<TermVocabulary
> vocs
, Integer limit
, String pattern
) {
862 return listNamedArea(vocs
, 0, limit
, pattern
, MatchMode
.BEGINNING
);
866 public List
<NamedArea
> listNamedAreaByAbbrev(List
<TermVocabulary
> vocs
, Integer limit
, String pattern
, NamedAreaSearchField type
) {
868 return listNamedAreaByAbbrev(vocs
, 0, limit
, pattern
, MatchMode
.BEGINNING
, type
);
872 public List
<TermDto
> getSupportedStatesForFeature(UUID featureUuid
){
873 List
<TermDto
> list
= new ArrayList
<>();
874 String supportedCategoriesQueryString
= "SELECT cat.uuid "
875 + "from DefinedTermBase t "
876 + "join t.supportedCategoricalEnumerations as cat "
877 + "where t.uuid = :featureUuid";
878 Query supportedCategoriesQuery
= getSession().createQuery(supportedCategoriesQueryString
);
879 supportedCategoriesQuery
.setParameter("featureUuid", featureUuid
);
880 @SuppressWarnings("unchecked")
881 List
<UUID
> supportedCategories
= supportedCategoriesQuery
.list();
882 if(supportedCategories
.isEmpty()){
886 String queryString
= TermDto
.getTermDtoSelect()
887 + "where v.uuid in (:supportedCategories) "
888 + "order by a.titleCache";
889 Query query
= getSession().createQuery(queryString
);
890 query
.setParameterList("supportedCategories", supportedCategories
);
892 @SuppressWarnings("unchecked")
893 List
<Object
[]> result
= query
.list();
895 list
= TermDto
.termDtoListFrom(result
);
900 public Collection
<TermDto
> findByUUIDsAsDto(List
<UUID
> uuidList
) {
901 List
<TermDto
> list
= new ArrayList
<>();
902 if (uuidList
== null || uuidList
.isEmpty()){
906 String queryString
= TermDto
.getTermDtoSelect()
907 + "where a.uuid in :uuidList "
908 + "order by a.titleCache";
909 Query query
= getSession().createQuery(queryString
);
910 query
.setParameterList("uuidList", uuidList
);
912 @SuppressWarnings("unchecked")
913 List
<Object
[]> result
= query
.list();
915 list
= TermDto
.termDtoListFrom(result
);
920 public Collection
<TermDto
> findFeatureByUUIDsAsDto(List
<UUID
> uuidList
) {
921 List
<TermDto
> list
= new ArrayList
<>();
922 if (uuidList
== null || uuidList
.isEmpty()){
926 String queryString
= FeatureDto
.getTermDtoSelect()
927 + "where a.uuid in :uuidList "
928 + "order by a.titleCache";
929 Query query
= getSession().createQuery(queryString
);
930 query
.setParameterList("uuidList", uuidList
);
932 @SuppressWarnings("unchecked")
933 List
<Object
[]> result
= query
.list();
935 list
= FeatureDto
.termDtoListFrom(result
);
940 public Collection
<TermDto
> findFeatureByTitleAsDto(String pattern
) {
941 String queryString
= FeatureDto
.getTermDtoSelect()
942 + " where a.titleCache like :title "
943 + " and a.termType = :termType ";
945 pattern
= pattern
.replace("*", "%");
946 Query query
= getSession().createQuery(queryString
);
947 query
.setParameter("title", "%"+pattern
+"%");
948 query
.setParameter("termType", TermType
.Feature
);
951 @SuppressWarnings("unchecked")
952 List
<Object
[]> result
= query
.list();
954 List
<TermDto
> list
= FeatureDto
.termDtoListFrom(result
);