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
.Hibernate
;
19 import org
.hibernate
.Query
;
20 import org
.hibernate
.Session
;
21 import org
.hibernate
.search
.FullTextSession
;
22 import org
.hibernate
.search
.Search
;
23 import org
.springframework
.beans
.factory
.annotation
.Qualifier
;
24 import org
.springframework
.stereotype
.Repository
;
26 import eu
.etaxonomy
.cdm
.model
.reference
.IArticle
;
27 import eu
.etaxonomy
.cdm
.model
.reference
.IBookSection
;
28 import eu
.etaxonomy
.cdm
.model
.reference
.IInProceedings
;
29 import eu
.etaxonomy
.cdm
.model
.reference
.IPrintedUnitBase
;
30 import eu
.etaxonomy
.cdm
.model
.reference
.IReport
;
31 import eu
.etaxonomy
.cdm
.model
.reference
.IThesis
;
32 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
33 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceType
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
35 import eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.common
.IdentifiableDaoBase
;
36 import eu
.etaxonomy
.cdm
.persistence
.dao
.reference
.IReferenceDao
;
37 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
38 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
39 import eu
.etaxonomy
.cdm
.strategy
.cache
.reference
.ReferenceDefaultCacheStrategy
;
46 @Qualifier("referenceDaoHibernateImpl")
47 public class ReferenceDaoHibernateImpl
extends IdentifiableDaoBase
<Reference
> implements IReferenceDao
{
48 private static final Logger logger
= Logger
.getLogger(ReferenceDaoHibernateImpl
.class);
50 public ReferenceDaoHibernateImpl() {
51 super(Reference
.class);
55 public void rebuildIndex() {
56 FullTextSession fullTextSession
= Search
.getFullTextSession(getSession());
58 for(Reference
<?
> reference
: list(null,null)) { // re-index all agents
59 Hibernate
.initialize(reference
.getAuthorship());
61 if(reference
.getType().equals(ReferenceType
.Article
)) {
62 Hibernate
.initialize(((IArticle
)reference
).getInJournal());
63 } else if(reference
.getType().equals(ReferenceType
.BookSection
)) {
64 Hibernate
.initialize(((IBookSection
)reference
).getInBook());
65 } else if(reference
.getType().equals(ReferenceType
.InProceedings
)) {
66 Hibernate
.initialize(((IInProceedings
)reference
).getInProceedings());
67 }else if(reference
.getType().equals(ReferenceType
.Thesis
)) {
68 Hibernate
.initialize(((IThesis
)reference
).getSchool());
69 } else if(reference
.getType().equals(ReferenceType
.Report
)) {
70 Hibernate
.initialize(((IReport
)reference
).getInstitution());
71 } else if(reference
.getType().isPrintedUnit()) {
72 Hibernate
.initialize(((IPrintedUnitBase
)reference
).getInSeries());
74 fullTextSession
.index(reference
);
76 fullTextSession
.flushToIndexes();
80 public List
<UuidAndTitleCache
<Reference
>> getUuidAndTitle(){
81 List
<UuidAndTitleCache
<Reference
>> list
= new ArrayList
<UuidAndTitleCache
<Reference
>>();
82 Session session
= getSession();
84 Query query
= session
.createQuery("select uuid, id, titleCache from " + type
.getSimpleName());
86 @SuppressWarnings("unchecked")
87 List
<Object
[]> result
= query
.list();
89 for(Object
[] object
: result
){
90 list
.add(new UuidAndTitleCache
<Reference
>(type
, (UUID
) object
[0], (Integer
)object
[1], (String
) object
[2]));
97 public List
<UuidAndTitleCache
<Reference
>> getUuidAndTitleCache() {
98 List
<UuidAndTitleCache
<Reference
>> list
= new ArrayList
<UuidAndTitleCache
<Reference
>>();
99 Session session
= getSession();
101 Query query
= session
.createQuery("SELECT " +
102 "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());
104 @SuppressWarnings("unchecked")
105 List
<Object
[]> result
= query
.list();
107 for(Object
[] object
: result
){
108 String referenceTitle
= (String
) object
[2];
110 if(referenceTitle
!= null){
111 String teamTitle
= (String
) object
[3];
112 referenceTitle
= ReferenceDefaultCacheStrategy
.putAuthorToEndOfString(referenceTitle
, teamTitle
);
114 list
.add(new UuidAndTitleCache
<Reference
>(Reference
.class, (UUID
) object
[0],(Integer
)object
[1], referenceTitle
));
116 logger
.error("title cache of reference is null. UUID: " + object
[0]);
124 public List
<Reference
> getAllReferencesForPublishing(){
125 @SuppressWarnings("unchecked")
126 List
<Reference
> references
= getSession().createQuery("Select r from Reference r "+
128 "(Select m.markedObj.id from Marker m where "+
129 "m.markerType.id = "+
130 "(Select dtb.id from DefinedTermBase dtb, Representation r where r member of dtb.representations and r.text='publish'))").list();
135 public List
<Reference
> getAllNotNomenclaturalReferencesForPublishing(){
137 @SuppressWarnings("unchecked")
138 List
<Reference
> references
= getSession().createQuery("select t.nomenclaturalReference from TaxonNameBase t").list();
139 String queryString
= "from Reference b where b not in (:referenceList) and b in (:publish)" ;
140 Query referenceQuery
= getSession().createQuery(queryString
).setParameterList("referenceList", references
);
141 referenceQuery
.setParameterList("publish", getAllReferencesForPublishing());
142 @SuppressWarnings("unchecked")
143 List
<Reference
> resultRefernces
=referenceQuery
.list();
145 return resultRefernces
;
148 // the result list held doubles therefore i put a "distinct" in the query string
150 public List
<Reference
> getAllNomenclaturalReferences() {
151 List
<Reference
> references
= getSession().createQuery(
152 "select distinct t.nomenclaturalReference from TaxonNameBase t").list();
158 public List
<Reference
> getSubordinateReferences(Reference reference
) {
160 List
<Reference
> references
= new ArrayList();
161 List
<Reference
> subordinateReferences
= new ArrayList
<Reference
>();
163 Query query
= getSession().createQuery("select r from Reference r where r.inReference = (:reference)");
164 query
.setParameter("reference", reference
);
166 @SuppressWarnings("unchecked")
167 List
<Reference
<?
>> list
= query
.list();
168 references
.addAll(list
);
169 for(Reference
<?
> ref
: references
){
170 subordinateReferences
.addAll(getSubordinateReferences(ref
));
172 references
.addAll(subordinateReferences
);
177 public List
<TaxonBase
> listCoveredTaxa(Reference reference
, boolean includeSubordinateReferences
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
180 * <li>taxon.name.nomenclaturalreference</li>
181 * <li>taxon.descriptions.descriptionElement.sources.citation</li>
182 * <li>taxon.descriptions.descriptionSources</li>
183 * <li>taxon.name.descriptions.descriptionElement.sources</li>
184 * <li>taxon.name.descriptions.descriptionSources</li>
187 //TODO implement search in nameDescriptions
188 Set
<Reference
> referenceSet
= new HashSet
<Reference
>();
189 referenceSet
.add(reference
);
190 if(includeSubordinateReferences
){
191 referenceSet
.addAll(getSubordinateReferences(reference
));
195 StringBuilder taxonDescriptionSql
= new StringBuilder();
196 taxonDescriptionSql
.append(
197 "select distinct t from Taxon t " +
199 "left join t.descriptions td " +
200 "left join td.descriptionSources td_s " +
201 "left join td.descriptionElements td_e " +
202 "left join td_e.sources td_e_s " +
203 // TaxonNameDescription
204 "left join t.name n " +
205 "left join n.descriptions nd " +
206 "left join nd.descriptionSources nd_s " +
207 "left join nd.descriptionElements nd_e " +
208 "left join nd_e.sources nd_e_s " +
210 "where td_e_s.citation in (:referenceBase_1) " +
211 "or td_s in (:referenceBase_2) " +
212 "or nd_e_s.citation in (:referenceBase_3) " +
213 "or nd_s in (:referenceBase_4) or " +
214 "n.nomenclaturalReference in (:referenceBase_5) or " +
215 "t.sec in (:referenceBase_6)"
218 if (orderHints
!= null && orderHints
.size() > 0){
219 taxonDescriptionSql
.append(" order by ");
221 for (OrderHint hint
: orderHints
) {
223 taxonDescriptionSql
.append(", ");
225 taxonDescriptionSql
.append("t.").append(hint
.toHql());
233 Query query
= getSession().createQuery(taxonDescriptionSql
.toString());
234 query
.setParameterList("referenceBase_1", referenceSet
);
235 query
.setParameterList("referenceBase_2", referenceSet
);
236 query
.setParameterList("referenceBase_3", referenceSet
);
237 query
.setParameterList("referenceBase_4", referenceSet
);
238 query
.setParameterList("referenceBase_5", referenceSet
);
239 query
.setParameterList("referenceBase_6", referenceSet
);
241 @SuppressWarnings("unchecked")
242 List
<TaxonBase
> taxonBaseList
= query
.list();
244 defaultBeanInitializer
.initializeAll(taxonBaseList
, propertyPaths
);
246 return taxonBaseList
;