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
.TermDto
;
71 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
72 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
76 * @author a.kohlbecker
81 public class DefinedTermDaoImpl
extends IdentifiableDaoBase
<DefinedTermBase
> implements IDefinedTermDao
{
82 private static final Logger logger
= Logger
.getLogger(DefinedTermDaoImpl
.class);
84 public DefinedTermDaoImpl() {
85 super(DefinedTermBase
.class);
86 indexedClasses
= new Class
[25];
87 indexedClasses
[0] = Rank
.class;
88 indexedClasses
[1] = AnnotationType
.class;
89 indexedClasses
[2] = ExtensionType
.class;
90 indexedClasses
[3] = Language
.class;
91 indexedClasses
[4] = MarkerType
.class;
92 indexedClasses
[5] = MeasurementUnit
.class;
93 indexedClasses
[6] = DefinedTerm
.class;
94 indexedClasses
[7] = PresenceAbsenceTerm
.class;
95 indexedClasses
[8] = State
.class;
96 indexedClasses
[9] = StatisticalMeasure
.class;
97 indexedClasses
[10] = TextFormat
.class;
98 indexedClasses
[11] = DerivationEventType
.class;
99 indexedClasses
[12] = NamedArea
.class;
100 indexedClasses
[13] = NamedAreaLevel
.class;
101 indexedClasses
[14] = NamedAreaType
.class;
102 indexedClasses
[15] = ReferenceSystem
.class;
103 indexedClasses
[16] = Country
.class;
104 indexedClasses
[17] = RightsType
.class;
105 indexedClasses
[18] = HybridRelationshipType
.class;
106 indexedClasses
[19] = NameRelationshipType
.class;
107 indexedClasses
[20] = NameTypeDesignationStatus
.class;
108 indexedClasses
[21] = NomenclaturalStatusType
.class;
109 indexedClasses
[22] = SpecimenTypeDesignationStatus
.class;
110 indexedClasses
[23] = SynonymType
.class;
111 indexedClasses
[24] = TaxonRelationshipType
.class;
116 * @see eu.etaxonomy.cdm.persistence.dao.common.ITitledDao#findByTitle(java.lang.String)
119 public List
<DefinedTermBase
> findByTitle(String queryString
) {
120 return findByTitle(queryString
, null);
126 * @see eu.etaxonomy.cdm.persistence.dao.common.ITitledDao#findByTitle(java.lang.String, eu.etaxonomy.cdm.model.common.CdmBase)
129 public List
<DefinedTermBase
> findByTitle(String queryString
, CdmBase sessionObject
) {
130 checkNotInPriorView("DefinedTermDaoImpl.findByTitle(String queryString, CdmBase sessionObject)");
131 Session session
= getSession();
132 if ( sessionObject
!= null ) {//attache the object to the session, TODO needed?
133 session
.update(sessionObject
);
135 Query query
= session
.createQuery("select term from DefinedTermBase term join fetch term.representations representation where representation.label = :label");
136 query
.setParameter("label", queryString
);
142 public List
<DefinedTermBase
> findByTitleAndClass(String queryString
, Class
<DefinedTermBase
> clazz
) {
143 checkNotInPriorView("DefinedTermDaoImpl.findByTitleAndClass(String queryString, Class<DefinedTermBase> clazz)");
144 Session session
= getSession();
145 Criteria crit
= session
.createCriteria(clazz
);
146 crit
.add(Restrictions
.ilike("persistentTitleCache", queryString
));
147 List
<DefinedTermBase
> results
= crit
.list();
152 public List
<DefinedTermBase
> findByTitle(String queryString
, MatchMode matchMode
, int page
, int pagesize
, List
<Criterion
> criteria
) {
153 //FIXME is query parametrised?
154 checkNotInPriorView("DefinedTermDaoImpl.findByTitle(String queryString, ITitledDao.MATCH_MODE matchMode, int page, int pagesize, List<Criterion> criteria)");
155 Criteria crit
= getSession().createCriteria(type
);
156 crit
.add(Restrictions
.ilike("titleCache", matchMode
.queryStringFrom(queryString
)));
157 crit
.setMaxResults(pagesize
);
158 int firstItem
= (page
- 1) * pagesize
+ 1;
159 crit
.setFirstResult(firstItem
);
160 List
<DefinedTermBase
> results
= crit
.list();
166 public Country
getCountryByIso(String iso3166
) {
167 // If iso639 = "" query returns non-unique result. We prevent this here:
168 if (StringUtils
.isBlank(iso3166
) || iso3166
.length()<2 || iso3166
.length()>3) { return null; }
169 AuditEvent auditEvent
= getAuditEventFromContext();
170 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
171 Query query
= getSession().createQuery("from Country where iso3166_A2 = :isoCode OR idInVocabulary = :isoCode");
172 query
.setParameter("isoCode", iso3166
);
173 return (Country
) query
.uniqueResult();
175 AuditQuery query
= getAuditReader().createQuery().forEntitiesAtRevision(Country
.class,auditEvent
.getRevisionNumber());
176 query
.add(AuditEntity
.property("iso3166_A2").eq(iso3166
));
177 query
.add(AuditEntity
.property("idInVocabulary").eq(iso3166
));
178 return (Country
) query
.getSingleResult();
183 public <T
extends DefinedTermBase
> List
<T
> getDefinedTermByRepresentationText(String text
, Class
<T
> clazz
) {
184 return getDefinedTermByRepresentationText(text
,clazz
,null,null);
188 public <T
extends DefinedTermBase
> List
<T
> getDefinedTermByRepresentationText(String text
, Class
<T
> clazz
, Integer pageSize
,Integer pageNumber
) {
189 checkNotInPriorView("DefinedTermDaoImpl.getDefinedTermByRepresentationText(String text, Class<T> clazz, Integer pageSize,Integer pageNumber)");
191 Criteria criteria
= getCriteria(clazz
);
193 criteria
.createAlias("representations", "r").add(Restrictions
.like("r.text", text
));
195 addPageSizeAndNumber(criteria
, pageSize
, pageNumber
);
197 @SuppressWarnings("unchecked")
198 List
<T
> result
= criteria
.list();
203 public long countDefinedTermByRepresentationText(String text
, Class
<?
extends DefinedTermBase
> clazz
) {
204 checkNotInPriorView("DefinedTermDaoImpl.countDefinedTermByRepresentationText(String text, Class<? extends DefinedTermBase> clazz)");
205 Criteria criteria
= getCriteria(clazz
);
207 criteria
.createAlias("representations", "r").add(Restrictions
.like("r.text", text
));
209 criteria
.setProjection(Projections
.rowCount());
211 return (Long
)criteria
.uniqueResult();
215 public <T
extends DefinedTermBase
> List
<T
> getDefinedTermByIdInVocabulary(String label
, UUID vocUuid
, Class
<T
> clazz
, Integer pageSize
, Integer pageNumber
) {
216 checkNotInPriorView("DefinedTermDaoImpl.getDefinedTermByIdInVocabulary(String label, UUID vocUuid, Class<T> clazz, Integer pageSize, Integer pageNumber)");
218 Criteria criteria
= getCriteria(clazz
);
220 criteria
.createAlias("vocabulary", "voc")
221 .add(Restrictions
.like("voc.uuid", vocUuid
))
222 .add(Restrictions
.like("idInVocabulary", label
, org
.hibernate
.criterion
.MatchMode
.EXACT
));
224 addPageSizeAndNumber(criteria
, pageSize
, pageNumber
);
226 @SuppressWarnings("unchecked")
227 List
<T
> result
= criteria
.list();
232 public <T
extends DefinedTermBase
> List
<T
> getDefinedTermByRepresentationAbbrev(String text
, Class
<T
> clazz
, Integer pageSize
,Integer pageNumber
) {
233 checkNotInPriorView("DefinedTermDaoImpl.getDefinedTermByRepresentationAbbrev(String abbrev, Class<T> clazz, Integer pageSize,Integer pageNumber)");
235 Criteria criteria
= getCriteria(clazz
);
237 criteria
.createAlias("representations", "r").add(Restrictions
.like("r.abbreviatedLabel", text
));
239 addPageSizeAndNumber(criteria
, pageSize
, pageNumber
);
241 @SuppressWarnings("unchecked")
242 List
<T
> result
= criteria
.list();
247 public long countDefinedTermByRepresentationAbbrev(String text
, Class
<?
extends DefinedTermBase
> clazz
) {
248 checkNotInPriorView("DefinedTermDaoImpl.countDefinedTermByRepresentationAbbrev(String abbrev, Class<? extends DefinedTermBase> clazz)");
249 Criteria criteria
= getCriteria(clazz
);
251 criteria
.createAlias("representations", "r").add(Restrictions
.like("r.abbreviatedLabel", text
));
252 criteria
.setProjection(Projections
.rowCount());
254 return (Long
)criteria
.uniqueResult();
258 public Language
getLanguageByIso(String iso639
) {
259 if (iso639
.length() < 2 || iso639
.length() > 3) {
260 logger
.warn("Invalid length " + iso639
.length() + " of ISO code. Length must be 2 or 3.");
263 boolean isIso639_1
= iso639
.length() == 2;
267 queryStr
= "from Language where iso639_1 = :isoCode";
269 queryStr
= "from Language where idInVocabulary = :isoCode and vocabulary.uuid = :vocUuid";
271 AuditEvent auditEvent
= getAuditEventFromContext();
272 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
273 Query query
= getSession().createQuery(queryStr
);
274 query
.setParameter("isoCode", iso639
);
276 query
.setParameter("vocUuid", Language
.uuidLanguageVocabulary
);
278 return (Language
) query
.uniqueResult();
280 AuditQuery query
= getAuditReader().createQuery().forEntitiesAtRevision(Language
.class,auditEvent
.getRevisionNumber());
282 query
.add(AuditEntity
.property("iso639_1").eq(iso639
));
284 query
.add(AuditEntity
.property("iso639_2").eq(iso639
));
285 query
.add(AuditEntity
.property("vocabulary.uuid").eq(Language
.uuidLanguageVocabulary
));
288 return (Language
)query
.getSingleResult();
293 * FIXME this will result in a query per language - could you, given that iso codes
294 * are unique, use from Language where iso639_1 in (:isoCode) or iso639_2 in (:isoCode)
297 public List
<Language
> getLanguagesByIso(List
<String
> iso639List
) {
298 List
<Language
> languages
= new ArrayList
<Language
>(iso639List
.size());
299 for (String iso639
: iso639List
) {
300 languages
.add(getLanguageByIso(iso639
));
306 public List
<Language
> getLanguagesByLocale(Enumeration
<Locale
> locales
) {
307 List
<Language
> languages
= new ArrayList
<Language
>();
308 while(locales
.hasMoreElements()) {
309 Locale locale
= locales
.nextElement();
310 languages
.add(getLanguageByIso(locale
.getLanguage()));
316 public long count(NamedAreaLevel level
, NamedAreaType type
) {
317 AuditEvent auditEvent
= getAuditEventFromContext();
318 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
319 Criteria criteria
= getCriteria(NamedArea
.class);
322 criteria
.add(Restrictions
.eq("level",level
));
326 criteria
.add(Restrictions
.eq("type", type
));
329 criteria
.setProjection(Projections
.rowCount());
331 return (Long
)criteria
.uniqueResult();
333 AuditQuery query
= makeAuditQuery(NamedArea
.class, auditEvent
);
336 query
.add(AuditEntity
.relatedId("level").eq(level
.getId()));
340 query
.add(AuditEntity
.relatedId("type").eq(type
.getId()));
342 query
.addProjection(AuditEntity
.id().count());
343 return (Long
)query
.getSingleResult();
348 public long countMedia(DefinedTermBase definedTerm
) {
349 checkNotInPriorView("DefinedTermDaoImpl.countMedia(DefinedTermBase definedTerm)");
350 Query query
= getSession().createQuery("select count(media) from DefinedTermBase definedTerm join definedTerm.media media where definedTerm = :definedTerm");
351 query
.setParameter("definedTerm", definedTerm
);
353 return (Long
)query
.uniqueResult();
357 public List
<Media
> getMedia(DefinedTermBase definedTerm
, Integer pageSize
, Integer pageNumber
) {
358 checkNotInPriorView("DefinedTermDaoImpl.getMedia(DefinedTermBase definedTerm, Integer pageSize, Integer pageNumber)");
359 Query query
= getSession().createQuery("select media from DefinedTermBase definedTerm join definedTerm.media media where definedTerm = :definedTerm");
360 query
.setParameter("definedTerm", definedTerm
);
362 addPageSizeAndNumber(query
, pageSize
, pageNumber
);
364 @SuppressWarnings("unchecked")
365 List
<Media
> result
= query
.list();
370 public List
<NamedArea
> list(NamedAreaLevel level
, NamedAreaType type
, Integer pageSize
, Integer pageNumber
) {
371 AuditEvent auditEvent
= getAuditEventFromContext();
372 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
373 Criteria criteria
= getCriteria(NamedArea
.class);
376 criteria
.add(Restrictions
.eq("level",level
));
380 criteria
.add(Restrictions
.eq("type", type
));
383 addPageSizeAndNumber(criteria
, pageSize
, pageNumber
);
385 @SuppressWarnings("unchecked")
386 List
<NamedArea
> result
= criteria
.list();
389 AuditQuery query
= makeAuditQuery(NamedArea
.class, auditEvent
);
392 query
.add(AuditEntity
.relatedId("level").eq(level
.getId()));
396 query
.add(AuditEntity
.relatedId("type").eq(type
.getId()));
399 @SuppressWarnings("unchecked")
400 List
<NamedArea
> result
= query
.getResultList();
406 public List
<NamedArea
> list(NamedAreaLevel level
, NamedAreaType type
, Integer pageSize
, Integer pageNumber
,
407 List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
409 List
<NamedArea
> result
;
411 AuditEvent auditEvent
= getAuditEventFromContext();
412 if (auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
413 Criteria criteria
= getCriteria(NamedArea
.class);
416 criteria
.add(Restrictions
.eq("level", level
));
419 criteria
.add(Restrictions
.eq("type", type
));
421 addOrder(criteria
,orderHints
);
422 addPageSizeAndNumber(criteria
, pageSize
, pageNumber
);
424 result
= criteria
.list();
427 AuditQuery query
= getAuditReader().createQuery().forEntitiesAtRevision(NamedArea
.class,
428 auditEvent
.getRevisionNumber());
430 query
.add(AuditEntity
.relatedId("level").eq(level
.getId()));
433 query
.add(AuditEntity
.relatedId("type").eq(type
.getId()));
435 result
= query
.getResultList();
438 defaultBeanInitializer
.initializeAll(result
, propertyPaths
);
445 public <T
extends DefinedTermBase
> long countGeneralizationOf(T kindOf
) {
446 AuditEvent auditEvent
= getAuditEventFromContext();
447 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
448 Query query
= getSession().createQuery("select count(term) from DefinedTermBase term where term.kindOf = :kindOf");
449 query
.setParameter("kindOf", kindOf
);
450 return (Long
)query
.uniqueResult();
452 AuditQuery query
= makeAuditQuery(DefinedTermBase
.class,auditEvent
);
453 query
.add(AuditEntity
.relatedId("kindOf").eq(kindOf
.getId()));
454 query
.addProjection(AuditEntity
.id().count());
455 return (Long
)query
.getSingleResult();
460 public <T
extends DefinedTermBase
> long countIncludes(Collection
<T
> partOf
) {
461 if (partOf
== null || partOf
.isEmpty()){
464 AuditEvent auditEvent
= getAuditEventFromContext();
465 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
466 Query query
= getSession().createQuery("select count(term) from DefinedTermBase term where term.partOf in (:partOf)");
467 query
.setParameterList("partOf", partOf
);
468 return (Long
)query
.uniqueResult();
472 AuditQuery query
= makeAuditQuery(DefinedTermBase
.class, auditEvent
);
473 query
.add(AuditEntity
.relatedId("partOf").eq(t
.getId()));
474 query
.addProjection(AuditEntity
.id().count());
475 count
+= (Long
)query
.getSingleResult();
482 public <T
extends DefinedTermBase
> List
<T
> getGeneralizationOf(T kindOf
, Integer pageSize
, Integer pageNumber
) {
483 AuditEvent auditEvent
= getAuditEventFromContext();
484 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
485 Query query
= getSession().createQuery("select term from DefinedTermBase term where term.kindOf = :kindOf");
486 query
.setParameter("kindOf", kindOf
);
488 addPageSizeAndNumber(query
, pageSize
, pageNumber
);
490 @SuppressWarnings("unchecked")
491 List
<T
> result
= query
.list();
494 AuditQuery query
= makeAuditQuery(DefinedTermBase
.class, auditEvent
);
495 query
.add(AuditEntity
.relatedId("kindOf").eq(kindOf
.getId()));
497 addPageSizeAndNumber(query
, pageSize
, pageNumber
);
499 @SuppressWarnings("unchecked")
500 List
<T
> result
= query
.getResultList();
506 public <T
extends DefinedTermBase
> List
<T
> getIncludes(Collection
<T
> partOf
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
507 if (partOf
== null || partOf
.isEmpty()){
508 return new ArrayList
<>();
510 AuditEvent auditEvent
= getAuditEventFromContext();
511 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
512 Query query
= getSession().createQuery("select term from DefinedTermBase term where term.partOf in (:partOf)");
513 query
.setParameterList("partOf", partOf
);
515 addPageSizeAndNumber(query
, pageSize
, pageNumber
);
517 @SuppressWarnings("unchecked")
518 List
<T
> results
= query
.list();
519 defaultBeanInitializer
.initializeAll(results
, propertyPaths
);
522 List
<T
> result
= new ArrayList
<T
>();
524 AuditQuery query
= makeAuditQuery(DefinedTermBase
.class, auditEvent
);
525 query
.add(AuditEntity
.relatedId("partOf").eq(t
.getId()));
526 addPageSizeAndNumber(query
, pageSize
, pageNumber
);
528 result
.addAll(query
.getResultList());
530 defaultBeanInitializer
.initializeAll(result
, propertyPaths
);
536 public <T
extends DefinedTermBase
> long countPartOf(Set
<T
> definedTerms
) {
537 checkNotInPriorView("DefinedTermDaoImpl.countPartOf(Set<T> definedTerms)");
538 Query query
= getSession().createQuery("select count(distinct definedTerm) from DefinedTermBase definedTerm join definedTerm.includes included where included in (:definedTerms)");
539 query
.setParameterList("definedTerms", definedTerms
);
540 return (Long
)query
.uniqueResult();
544 public <T
extends DefinedTermBase
> List
<T
> getPartOf(Set
<T
> definedTerms
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
545 checkNotInPriorView("DefinedTermDaoImpl.getPartOf(Set<T> definedTerms, Integer pageSize, Integer pageNumber)");
546 Query query
= getSession().createQuery("select distinct definedTerm from DefinedTermBase definedTerm join definedTerm.includes included where included in (:definedTerms)");
547 query
.setParameterList("definedTerms", definedTerms
);
549 addPageSizeAndNumber(query
, pageSize
, pageNumber
);
551 @SuppressWarnings("unchecked")
552 List
<T
> r
= query
.list();
554 * For some weird reason, hibernate returns proxies (extending the superclass), not the actual class on this,
555 * despite querying the damn database and returning the discriminator along with the rest of the object properties!
557 * Probably a bug in hibernate, but we'll manually deproxy for now since the objects are initialized anyway, the
558 * performance implications are small (we're swapping one array of references for another, not hitting the db or
561 List
<T
> results
= new ArrayList
<>();
562 if(!definedTerms
.isEmpty()) {
564 T deproxied
= CdmBase
.deproxy(t
);
565 results
.add(deproxied
);
567 defaultBeanInitializer
.initializeAll(results
, propertyPaths
);
573 public DefinedTermBase
findByUri(URI uri
) {
574 AuditEvent auditEvent
= getAuditEventFromContext();
575 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
576 Query query
= getSession().createQuery("select term from DefinedTermBase term where term.uri = :uri");
577 query
.setParameter("uri", uri
);
578 return (DefinedTermBase
<?
>)query
.uniqueResult();
580 AuditQuery query
= makeAuditQuery(DefinedTermBase
.class, auditEvent
);
581 query
.add(AuditEntity
.property("uri").eq(uri
));
582 return (DefinedTermBase
<?
>)query
.getSingleResult();
587 public <T
extends DefinedTermBase
> List
<T
> listByTermType(TermType termType
, Integer limit
, Integer start
,
588 List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
589 Query query
= getSession().createQuery("SELECT term FROM DefinedTermBase term WHERE term.termType = :termType");
590 query
.setParameter("termType", termType
);
592 @SuppressWarnings("unchecked")
593 List
<T
> result
= query
.list();
595 defaultBeanInitializer
.initializeAll(result
, propertyPaths
);
600 public <TERM
extends DefinedTermBase
> List
<TERM
> listByTermClass(Class
<TERM
> clazz
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
601 // checkNotInPriorView("DefinedTermDaoImpl.listByTermClass(Class<TERM> clazz, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths)");
603 Query query
= getSession().createQuery("FROM " + clazz
.getSimpleName());
605 @SuppressWarnings("unchecked")
606 List
<TERM
> result
= query
.list();
608 defaultBeanInitializer
.initializeAll(result
, propertyPaths
);
614 public <S
extends DefinedTermBase
> List
<S
> list(Class
<S
> type
, Integer limit
, Integer start
,
615 List
<OrderHint
> orderHints
, List
<String
> propertyPath
) {
617 return deduplicateResult(super.list(type
, limit
, start
, orderHints
, propertyPath
));
621 * Workaround for http://dev.e-taxonomy.eu/trac/ticket/5871 and #5945
622 * Terms with multiple representations return identical duplicates
623 * due to eager representation loading. We expect these duplicates to appear
624 * in line wo we only compare one term with its predecessor. If it already
625 * exists we remove it from the result.
629 private <S
extends DefinedTermBase
<?
>> List
<S
> deduplicateResult(List
<S
> orginals
) {
630 List
<S
> result
= new ArrayList
<>();
631 Iterator
<S
> it
= orginals
.iterator();
633 while (it
.hasNext()){
636 if (!result
.contains(a
)){
647 public List
<NamedArea
> listNamedArea(List
<TermVocabulary
> vocs
, Integer pageNumber
, Integer limit
, String pattern
, MatchMode matchmode
){
648 Session session
= getSession();
649 // Query query = null;
650 // if (pattern != null){
651 // if (vocs != null && !vocs.isEmpty()){
652 // query = session.createQuery("from NamedArea where titleCache like :pattern and vocabulary in :vocs ORDER BY titleCache");
653 // query.setParameterList("vocs", vocs);
655 // query = session.createQuery("from NamedArea where titleCache like :pattern ORDER BY titleCache");
657 // pattern = pattern.replace("*", "%");
658 // pattern = pattern.replace("?", "_");
659 // pattern = pattern + "%";
660 // query.setParameter("pattern", pattern);
663 // query = session.createQuery("FROM NamedArea WHERE vocabulary IN :vocs ORDER BY titleCache");
664 // query.setParameterList("vocs", vocs);
666 // if (limit != null && limit > 0){
667 // query.setMaxResults(limit);
670 Criteria crit
= getSession().createCriteria(type
, "namedArea");
671 if (!StringUtils
.isBlank(pattern
)){
672 if (matchmode
== MatchMode
.EXACT
) {
673 crit
.add(Restrictions
.eq("titleCache", matchmode
.queryStringFrom(pattern
)));
675 // crit.add(Restrictions.ilike("titleCache", matchmode.queryStringFrom(queryString)));
676 crit
.add(Restrictions
.like("titleCache", matchmode
.queryStringFrom(pattern
)));
679 if (limit
!= null && limit
>= 0) {
680 crit
.setMaxResults(limit
);
683 if (vocs
!= null &&!vocs
.isEmpty()){
684 crit
.createAlias("namedArea.vocabulary", "voc");
685 Disjunction or
= Restrictions
.disjunction();
686 for (TermVocabulary voc
: vocs
){
687 Criterion criterion
= Restrictions
.eq("voc.id", voc
.getId());
693 crit
.addOrder(Order
.asc("titleCache"));
697 int firstItem
= (pageNumber
- 1) * limit
;
699 crit
.setFirstResult(0);
700 @SuppressWarnings("unchecked")
701 List
<NamedArea
> results
= crit
.list();
707 public List
<NamedArea
> listNamedAreaByAbbrev(List
<TermVocabulary
> vocs
, Integer pageNumber
, Integer limit
, String pattern
, MatchMode matchmode
, NamedAreaSearchField abbrevType
){
708 Session session
= getSession();
710 Criteria crit
= getSession().createCriteria(type
, "namedArea");
711 if (!StringUtils
.isBlank(pattern
)){
712 if (matchmode
== MatchMode
.EXACT
) {
713 crit
.add(Restrictions
.eq(abbrevType
.getKey(), matchmode
.queryStringFrom(pattern
)));
715 // crit.add(Restrictions.ilike("titleCache", matchmode.queryStringFrom(queryString)));
716 crit
.add(Restrictions
.like(abbrevType
.getKey(), matchmode
.queryStringFrom(pattern
)));
719 if (limit
!= null && limit
>= 0) {
720 crit
.setMaxResults(limit
);
723 if (vocs
!= null &&!vocs
.isEmpty()){
724 crit
.createAlias("namedArea.vocabulary", "voc");
725 Disjunction or
= Restrictions
.disjunction();
726 for (TermVocabulary voc
: vocs
){
727 Criterion criterion
= Restrictions
.eq("voc.id", voc
.getId());
733 crit
.addOrder(Order
.asc(abbrevType
.getKey()));
737 int firstItem
= (pageNumber
- 1) * limit
;
739 crit
.setFirstResult(0);
740 @SuppressWarnings("unchecked")
741 List
<NamedArea
> results
= crit
.list();
746 public long count(List
<TermVocabulary
> vocs
, String pattern
){
747 Session session
= getSession();
749 if (pattern
!= null){
750 if (vocs
!= null && !vocs
.isEmpty()){
751 query
= session
.createQuery("SELECT COUNT(*) FROM NamedArea WHERE titleCache LIKE :pattern AND vocabulary IN :vocs");
752 query
.setParameterList("vocs", vocs
);
754 query
= session
.createQuery("SELECT COUNT(*) FROM NamedArea WHERE titleCache LIKE :pattern ");
756 pattern
= pattern
.replace("*", "%");
757 pattern
= pattern
.replace("?", "_");
758 pattern
= pattern
+ "%";
759 query
.setParameter("pattern", pattern
);
762 query
= session
.createQuery("SELECT COUNT(*) FROM NamedArea WHERE vocabulary IN :vocs");
763 query
.setParameterList("vocs", vocs
);
767 @SuppressWarnings("unchecked")
768 Long result
= (Long
) query
.uniqueResult();
773 public Collection
<TermDto
> getIncludesAsDto(
774 TermDto parentTerm
) {
775 String queryString
= TermDto
.getTermDtoSelect()
776 + "where a.partOf.uuid = :parentUuid";
777 Query query
= getSession().createQuery(queryString
);
778 query
.setParameter("parentUuid", parentTerm
.getUuid());
780 @SuppressWarnings("unchecked")
781 List
<Object
[]> result
= query
.list();
783 List
<TermDto
> list
= TermDto
.termDtoListFrom(result
);
788 public Collection
<TermDto
> getKindOfsAsDto(
789 TermDto parentTerm
) {
790 String queryString
= TermDto
.getTermDtoSelect()
791 + "where a.kindOf.uuid = :parentUuid";
792 Query query
= getSession().createQuery(queryString
);
793 query
.setParameter("parentUuid", parentTerm
.getUuid());
795 @SuppressWarnings("unchecked")
796 List
<Object
[]> result
= query
.list();
798 List
<TermDto
> list
= TermDto
.termDtoListFrom(result
);
804 public Collection
<TermDto
> findByTitleAsDto(String title
, TermType termType
) {
805 String queryString
= TermDto
.getTermDtoSelect()
806 + " where a.titleCache like :title "
807 + (termType
!=null?
" and a.termType = :termType ":"");
808 Query query
= getSession().createQuery(queryString
);
809 query
.setParameter("title", "%"+title
+"%");
811 query
.setParameter("termType", termType
);
814 @SuppressWarnings("unchecked")
815 List
<Object
[]> result
= query
.list();
817 List
<TermDto
> list
= TermDto
.termDtoListFrom(result
);
822 public Collection
<TermDto
> findByTypeAsDto(TermType termType
) {
823 if (termType
== null){
826 String queryString
= TermDto
.getTermDtoSelect()
827 + " where a.termType = :termType ";
828 Query query
= getSession().createQuery(queryString
);
831 query
.setParameter("termType", termType
);
834 @SuppressWarnings("unchecked")
835 List
<Object
[]> result
= query
.list();
837 List
<TermDto
> list
= TermDto
.termDtoListFrom(result
);
842 public Collection
<TermDto
> findByUriAsDto(URI uri
, String termLabel
, TermType termType
) {
843 String queryString
= TermDto
.getTermDtoSelect()
844 + " where a.uri like :uri "
845 + (termType
!=null?
" and a.termType = :termType ":"")
846 + (termLabel
!=null?
" and a.titleCache = :termLabel ":"")
848 Query query
= getSession().createQuery(queryString
);
849 query
.setParameter("uri", uri
.toString());
851 query
.setParameter("termLabel", "%"+termLabel
+"%");
854 query
.setParameter("termType", termType
);
857 @SuppressWarnings("unchecked")
858 List
<Object
[]> result
= query
.list();
860 List
<TermDto
> list
= TermDto
.termDtoListFrom(result
);
868 public List
<NamedArea
> listNamedArea(List
<TermVocabulary
> vocs
, Integer limit
, String pattern
) {
870 return listNamedArea(vocs
, 0, limit
, pattern
, MatchMode
.BEGINNING
);
875 public List
<NamedArea
> listNamedAreaByAbbrev(List
<TermVocabulary
> vocs
, Integer limit
, String pattern
, NamedAreaSearchField type
) {
877 return listNamedAreaByAbbrev(vocs
, 0, limit
, pattern
, MatchMode
.BEGINNING
, type
);
882 public List
<TermDto
> getSupportedStatesForFeature(UUID featureUuid
){
883 List
<TermDto
> list
= new ArrayList
<>();
884 String supportedCategoriesQueryString
= "SELECT cat.uuid "
885 + "from DefinedTermBase t "
886 + "join t.supportedCategoricalEnumerations as cat "
887 + "where t.uuid = :featureUuid";
888 Query supportedCategoriesQuery
= getSession().createQuery(supportedCategoriesQueryString
);
889 supportedCategoriesQuery
.setParameter("featureUuid", featureUuid
);
890 List
<UUID
> supportedCategories
= supportedCategoriesQuery
.list();
891 if(supportedCategories
.isEmpty()){
895 String queryString
= TermDto
.getTermDtoSelect()
896 + "where v.uuid in (:supportedCategories) "
897 + "order by a.titleCache";
898 Query query
= getSession().createQuery(queryString
);
899 query
.setParameterList("supportedCategories", supportedCategories
);
901 @SuppressWarnings("unchecked")
902 List
<Object
[]> result
= query
.list();
904 list
= TermDto
.termDtoListFrom(result
);
909 public Collection
<TermDto
> findByUUIDsAsDto(List
<UUID
> uuidList
) {
910 List
<TermDto
> list
= new ArrayList
<>();
911 if (uuidList
== null || uuidList
.isEmpty()){
915 String queryString
= TermDto
.getTermDtoSelect()
916 + "where a.uuid in :uuidList "
917 + "order by a.titleCache";
918 Query query
= getSession().createQuery(queryString
);
919 query
.setParameterList("uuidList", uuidList
);
921 @SuppressWarnings("unchecked")
922 List
<Object
[]> result
= query
.list();
924 list
= TermDto
.termDtoListFrom(result
);