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.
9 package eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.reference
;
11 import java
.util
.ArrayList
;
12 import java
.util
.HashSet
;
13 import java
.util
.List
;
15 import java
.util
.UUID
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.hibernate
.Criteria
;
19 import org
.hibernate
.Hibernate
;
20 import org
.hibernate
.Query
;
21 import org
.hibernate
.Session
;
22 import org
.hibernate
.criterion
.Restrictions
;
23 import org
.hibernate
.search
.FullTextSession
;
24 import org
.hibernate
.search
.Search
;
25 import org
.springframework
.beans
.factory
.annotation
.Qualifier
;
26 import org
.springframework
.stereotype
.Repository
;
28 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
29 import eu
.etaxonomy
.cdm
.model
.reference
.IArticle
;
30 import eu
.etaxonomy
.cdm
.model
.reference
.IBookSection
;
31 import eu
.etaxonomy
.cdm
.model
.reference
.IInProceedings
;
32 import eu
.etaxonomy
.cdm
.model
.reference
.IPrintedUnitBase
;
33 import eu
.etaxonomy
.cdm
.model
.reference
.IReport
;
34 import eu
.etaxonomy
.cdm
.model
.reference
.IThesis
;
35 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
36 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
37 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceType
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
39 import eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.common
.IdentifiableDaoBase
;
40 import eu
.etaxonomy
.cdm
.persistence
.dao
.reference
.IReferenceDao
;
41 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
42 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
43 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
44 import eu
.etaxonomy
.cdm
.strategy
.cache
.reference
.DefaultReferenceCacheStrategy
;
51 @Qualifier("referenceDaoHibernateImpl")
52 public class ReferenceDaoHibernateImpl
extends IdentifiableDaoBase
<Reference
> implements IReferenceDao
{
53 private static final Logger logger
= Logger
.getLogger(ReferenceDaoHibernateImpl
.class);
55 public ReferenceDaoHibernateImpl() {
56 super(Reference
.class);
60 public void rebuildIndex() {
61 FullTextSession fullTextSession
= Search
.getFullTextSession(getSession());
63 for(Reference reference
: list(null,null)) { // re-index all agents
64 Hibernate
.initialize(reference
.getAuthorship());
66 if(reference
.getType().equals(ReferenceType
.Article
)) {
67 Hibernate
.initialize(((IArticle
)reference
).getInJournal());
68 } else if(reference
.getType().equals(ReferenceType
.BookSection
)) {
69 Hibernate
.initialize(((IBookSection
)reference
).getInBook());
70 } else if(reference
.getType().equals(ReferenceType
.InProceedings
)) {
71 Hibernate
.initialize(((IInProceedings
)reference
).getInProceedings());
72 }else if(reference
.getType().equals(ReferenceType
.Thesis
)) {
73 Hibernate
.initialize(((IThesis
)reference
).getSchool());
74 } else if(reference
.getType().equals(ReferenceType
.Report
)) {
75 Hibernate
.initialize(((IReport
)reference
).getInstitution());
76 } else if(reference
.getType().isPrintedUnit()) {
77 Hibernate
.initialize(((IPrintedUnitBase
)reference
).getInSeries());
79 fullTextSession
.index(reference
);
81 fullTextSession
.flushToIndexes();
85 public List
<UuidAndTitleCache
<Reference
>> getUuidAndTitle(){
86 List
<UuidAndTitleCache
<Reference
>> list
= new ArrayList
<UuidAndTitleCache
<Reference
>>();
87 Session session
= getSession();
89 Query query
= session
.createQuery("select uuid, id, titleCache from " + type
.getSimpleName());
91 @SuppressWarnings("unchecked")
92 List
<Object
[]> result
= query
.list();
94 for(Object
[] object
: result
){
95 list
.add(new UuidAndTitleCache
<Reference
>(type
, (UUID
) object
[0], (Integer
)object
[1], (String
) object
[2]));
102 public List
<UuidAndTitleCache
<Reference
>> getUuidAndTitle(Set
<UUID
> uuids
){
103 List
<UuidAndTitleCache
<Reference
>> list
= new ArrayList
<UuidAndTitleCache
<Reference
>>();
105 Criteria criteria
= null;
107 criteria
= getSession().createCriteria(Reference
.class);
108 criteria
.add(Restrictions
.in("uuid", uuids
) );
110 @SuppressWarnings("unchecked")
111 List
<Reference
> result
= criteria
.list();
113 for(Reference object
: result
){
114 list
.add(new UuidAndTitleCache
<Reference
>(type
, object
.getUuid(), object
.getId(), object
.getTitleCache()));
121 public List
<UuidAndTitleCache
<Reference
>> getUuidAndTitleCache(Integer limit
, String pattern
, ReferenceType refType
) {
122 List
<UuidAndTitleCache
<Reference
>> list
= new ArrayList
<>();
123 Session session
= getSession();
125 String queryString
= "SELECT " +"r.uuid, r.id, r.titleCache, ab.titleCache "
126 + " FROM " + type
.getSimpleName() + " AS r LEFT OUTER JOIN r.authorship AS ab ";
128 if (refType
!= null || pattern
!= null){
129 queryString
+= " WHERE (1=1) ";
130 if (refType
!= null ){
131 queryString
+= " AND (r.type = :type OR r.type = :genericType) " ;
133 if (pattern
!= null){
134 queryString
+= " AND (r.titleCache LIKE :pattern) ";
139 //if (pattern != null){
140 query
= session
.createQuery(queryString
);
142 // query = session.createQuery("SELECT " +"r.uuid, r.id, r.titleCache, ab.titleCache FROM " + type.getSimpleName() + " AS r LEFT OUTER JOIN r.authorship AS ab ");//"select uuid, titleCache from " + type.getSimpleName());
146 query
.setMaxResults(limit
);
148 if (pattern
!= null){
149 pattern
= pattern
.replace("*", "%");
150 pattern
= pattern
.replace("?", "_");
151 pattern
= pattern
+ "%";
152 query
.setParameter("pattern", pattern
);
154 if (refType
!= null){
155 query
.setParameter("type", refType
);
156 query
.setParameter("genericType", ReferenceType
.Generic
);
158 @SuppressWarnings("unchecked")
159 List
<Object
[]> result
= query
.list();
161 for(Object
[] object
: result
){
162 String referenceTitle
= (String
) object
[2];
164 if(referenceTitle
!= null){
165 String teamTitle
= (String
) object
[3];
166 referenceTitle
= DefaultReferenceCacheStrategy
.putAuthorToEndOfString(referenceTitle
, teamTitle
);
168 list
.add(new UuidAndTitleCache
<Reference
>(Reference
.class, (UUID
) object
[0],(Integer
)object
[1], referenceTitle
));
170 logger
.warn("Title cache of reference is null. This should not happen. Please fix data. UUID: " + object
[0]);
178 public List
<Object
[]> findByIdentifierAbbrev(String identifier
, DefinedTermBase identifierType
,
179 MatchMode matchmode
,Integer limit
){
180 checkNotInPriorView("IdentifiableDaoBase.findByIdentifier(T clazz, String identifier, DefinedTerm identifierType, MatchMode matchmode, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)");
183 String queryString
= "SELECT ids.type, ids.identifier, %s FROM %s as c " +
184 " INNER JOIN c.identifiers as ids " +
186 queryString
= String
.format(queryString
, "c.uuid, c.titleCache, c.abbrevTitleCache" , "Reference");
188 //Matchmode and identifier
189 if (identifier
!= null){
190 if (matchmode
== null || matchmode
== MatchMode
.EXACT
){
191 queryString
+= " AND ids.identifier = '" + identifier
+ "'";
193 queryString
+= " AND ids.identifier LIKE '" + matchmode
.queryStringFrom(identifier
) + "'";
196 if (identifierType
!= null){
197 queryString
+= " AND ids.type = :type";
200 queryString
+=" ORDER BY ids.type.uuid, ids.identifier, c.uuid ";
202 Query query
= getSession().createQuery(queryString
);
205 if (identifierType
!= null){
206 query
.setEntity("type", identifierType
);
211 List
<Object
[]> results
= query
.list();
218 public List
<Reference
> getAllReferencesForPublishing(){
219 @SuppressWarnings("unchecked")
220 List
<Reference
> references
= getSession().createQuery("SELECT r FROM Reference r "+
222 "(SELECT m.markedObj.id FROM Marker m WHERE "+
223 "m.markerType.id = "+
224 "(SELECT dtb.id FROM DefinedTermBase dtb, Representation r WHERE r MEMBER OF dtb.representations AND r.text='publish'))").list();
229 public List
<Reference
> getAllNotNomenclaturalReferencesForPublishing(){
231 @SuppressWarnings("unchecked")
232 List
<Reference
> references
= getSession().createQuery("select t.nomenclaturalReference from TaxonName t").list();
233 String queryString
= "from Reference b where b not in (:referenceList) and b in (:publish)" ;
234 Query referenceQuery
= getSession().createQuery(queryString
).setParameterList("referenceList", references
);
235 referenceQuery
.setParameterList("publish", getAllReferencesForPublishing());
236 @SuppressWarnings("unchecked")
237 List
<Reference
> resultRefernces
=referenceQuery
.list();
239 return resultRefernces
;
242 // the result list held doubles therefore I put a "distinct" in the query string
244 public List
<Reference
> getAllNomenclaturalReferences() {
245 @SuppressWarnings("unchecked")
246 List
<Reference
> references
= getSession().createQuery(
247 "SELECT DISTINCT t.nomenclaturalReference FROM TaxonName t").list();
253 public List
<Reference
> getSubordinateReferences(Reference reference
) {
255 List
<Reference
> references
= new ArrayList();
256 List
<Reference
> subordinateReferences
= new ArrayList
<Reference
>();
258 Query query
= getSession().createQuery("select r from Reference r where r.inReference = (:reference)");
259 query
.setParameter("reference", reference
);
261 @SuppressWarnings("unchecked")
262 List
<Reference
> list
= query
.list();
263 references
.addAll(list
);
264 for(Reference ref
: references
){
265 subordinateReferences
.addAll(getSubordinateReferences(ref
));
267 references
.addAll(subordinateReferences
);
272 public List
<TaxonBase
> listCoveredTaxa(Reference reference
, boolean includeSubordinateReferences
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
275 * <li>taxon.name.nomenclaturalreference</li>
276 * <li>taxon.descriptions.descriptionElement.sources.citation</li>
277 * <li>taxon.descriptions.descriptionSources</li>
278 * <li>taxon.name.descriptions.descriptionElement.sources</li>
279 * <li>taxon.name.descriptions.descriptionSources</li>
282 //TODO implement search in nameDescriptions
283 Set
<Reference
> referenceSet
= new HashSet
<Reference
>();
284 referenceSet
.add(reference
);
285 if(includeSubordinateReferences
){
286 referenceSet
.addAll(getSubordinateReferences(reference
));
290 StringBuilder taxonDescriptionSql
= new StringBuilder();
291 taxonDescriptionSql
.append(
292 "select distinct t from Taxon t " +
294 "left join t.descriptions td " +
295 "left join td.descriptionSources td_s " +
296 "left join td.descriptionElements td_e " +
297 "left join td_e.sources td_e_s " +
298 // TaxonNameDescription
299 "left join t.name n " +
300 "left join n.descriptions nd " +
301 "left join nd.descriptionSources nd_s " +
302 "left join nd.descriptionElements nd_e " +
303 "left join nd_e.sources nd_e_s " +
305 "where td_e_s.citation in (:referenceBase_1) " +
306 "or td_s in (:referenceBase_2) " +
307 "or nd_e_s.citation in (:referenceBase_3) " +
308 "or nd_s in (:referenceBase_4) or " +
309 "n.nomenclaturalReference in (:referenceBase_5) or " +
310 "t.sec in (:referenceBase_6)"
313 if (orderHints
!= null && orderHints
.size() > 0){
314 taxonDescriptionSql
.append(" order by ");
316 for (OrderHint hint
: orderHints
) {
318 taxonDescriptionSql
.append(", ");
320 taxonDescriptionSql
.append("t.").append(hint
.toHql());
328 Query query
= getSession().createQuery(taxonDescriptionSql
.toString());
329 query
.setParameterList("referenceBase_1", referenceSet
);
330 query
.setParameterList("referenceBase_2", referenceSet
);
331 query
.setParameterList("referenceBase_3", referenceSet
);
332 query
.setParameterList("referenceBase_4", referenceSet
);
333 query
.setParameterList("referenceBase_5", referenceSet
);
334 query
.setParameterList("referenceBase_6", referenceSet
);
336 @SuppressWarnings("unchecked")
337 List
<TaxonBase
> taxonBaseList
= query
.list();
339 defaultBeanInitializer
.initializeAll(taxonBaseList
, propertyPaths
);
341 return taxonBaseList
;
345 * @see eu.etaxonomy.cdm.persistence.dao.reference.IReferenceDao#getUuidAndAbbrevTitleCache(java.lang.Integer, java.lang.String)
348 public List
<UuidAndTitleCache
<Reference
>> getUuidAndAbbrevTitleCache(Integer limit
, String pattern
, ReferenceType refType
) {
349 Session session
= getSession();
350 Reference ref
= ReferenceFactory
.newArticle();
353 if (pattern
!= null){
354 if (pattern
.startsWith("*")){
355 query
= session
.createQuery("select uuid, id, abbrevTitleCache, titleCache from " + type
.getSimpleName() +" where abbrevTitleCache like :pattern OR titleCache like :pattern ");
357 query
= session
.createQuery("select uuid, id, abbrevTitleCache, titleCache from " + type
.getSimpleName() +" where abbrevTitleCache like :pattern ");
359 pattern
= pattern
+ "%";
360 pattern
= pattern
.replace("*", "%");
361 pattern
= pattern
.replace("?", "_");
362 query
.setParameter("pattern", pattern
);
364 query
= session
.createQuery("select uuid, id, abbrevTitleCache, titleCache from " + type
.getSimpleName() );
367 query
.setMaxResults(limit
);
370 return getUuidAndAbbrevTitleCache(query
);
375 * @see eu.etaxonomy.cdm.persistence.dao.reference.IReferenceDao#getUuidAndAbbrevTitleCache(java.lang.Integer, java.lang.String)
378 public List
<UuidAndTitleCache
<Reference
>> getUuidAndAbbrevTitleCacheForAuthor(Integer limit
, String pattern
, ReferenceType refType
) {
379 Session session
= getSession();
380 Reference ref
= ReferenceFactory
.newArticle();
383 if (pattern
!= null){
384 query
= session
.createQuery("SELECT uuid, id, abbrevTitleCache, titleCache from " + type
.getSimpleName()
385 +" as r where r.authorship.nomenclaturalTitle like :pattern ");
387 query
.setParameter("pattern", pattern
);
389 query
= session
.createQuery("select uuid, id, abbrevTitleCache, titleCache from " + type
.getSimpleName() );
392 query
.setMaxResults(limit
);
394 pattern
= pattern
.replace("*", "%");
395 pattern
= pattern
.replace("?", "_");
396 query
.setParameter("pattern", pattern
);
397 return getUuidAndAbbrevTitleCache(query
);