Project

General

Profile

Download (8.98 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
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.
8
*/
9
package eu.etaxonomy.cdm.persistence.dao.hibernate.reference;
10

    
11
import java.util.ArrayList;
12
import java.util.HashSet;
13
import java.util.List;
14
import java.util.Set;
15
import java.util.UUID;
16

    
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;
25

    
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;
40

    
41
/**
42
 * @author a.mueller
43
 *
44
 */
45
@Repository
46
@Qualifier("referenceDaoHibernateImpl")
47
public class ReferenceDaoHibernateImpl extends IdentifiableDaoBase<Reference> implements IReferenceDao {
48
	private static final Logger logger = Logger.getLogger(ReferenceDaoHibernateImpl.class);
49

    
50
	public ReferenceDaoHibernateImpl() {
51
		super(Reference.class);
52
	}
53

    
54
	@Override
55
	public void rebuildIndex() {
56
		FullTextSession fullTextSession = Search.getFullTextSession(getSession());
57

    
58
		for(Reference<?> reference : list(null,null)) { // re-index all agents
59
			Hibernate.initialize(reference.getAuthorship());
60

    
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());
73
			}
74
			fullTextSession.index(reference);
75
		}
76
		fullTextSession.flushToIndexes();
77
	}
78

    
79
	@Override
80
    public List<UuidAndTitleCache<Reference>> getUuidAndTitle(){
81
		List<UuidAndTitleCache<Reference>> list = new ArrayList<UuidAndTitleCache<Reference>>();
82
		Session session = getSession();
83

    
84
		Query query = session.createQuery("select uuid, id, titleCache from " + type.getSimpleName());
85

    
86
		@SuppressWarnings("unchecked")
87
        List<Object[]> result = query.list();
88

    
89
		for(Object[] object : result){
90
			list.add(new UuidAndTitleCache<Reference>(type, (UUID) object[0], (Integer)object[1], (String) object[2]));
91
		}
92

    
93
		return list;
94
	}
95

    
96
	@Override
97
	public List<UuidAndTitleCache<Reference>> getUuidAndTitleCache() {
98
		List<UuidAndTitleCache<Reference>> list = new ArrayList<UuidAndTitleCache<Reference>>();
99
		Session session = getSession();
100

    
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());
103

    
104
		@SuppressWarnings("unchecked")
105
        List<Object[]> result = query.list();
106

    
107
		for(Object[] object : result){
108
			String referenceTitle = (String) object[2];
109

    
110
			if(referenceTitle != null){
111
				String teamTitle = (String) object[3];
112
				referenceTitle = ReferenceDefaultCacheStrategy.putAuthorToEndOfString(referenceTitle, teamTitle);
113

    
114
				list.add(new UuidAndTitleCache<Reference>(Reference.class, (UUID) object[0],(Integer)object[1], referenceTitle));
115
			}else{
116
				logger.error("title cache of reference is null. UUID: " + object[0]);
117
			}
118
		}
119

    
120
		return list;
121
	}
122

    
123
	@Override
124
    public List<Reference> getAllReferencesForPublishing(){
125
		@SuppressWarnings("unchecked")
126
        List<Reference> references = getSession().createQuery("SELECT r FROM Reference r "+
127
				"WHERE r.id IN "+
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();
131
		return references;
132
	}
133

    
134
	@Override
135
    public List<Reference> getAllNotNomenclaturalReferencesForPublishing(){
136

    
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();
144

    
145
		return resultRefernces;
146
	}
147

    
148
	// the result list held doubles therefore i put a "distinct" in the query string
149
	@Override
150
    public List<Reference> getAllNomenclaturalReferences() {
151
		List<Reference> references = getSession().createQuery(
152
				"select distinct t.nomenclaturalReference from TaxonNameBase t").list();
153
		return references;
154
	}
155

    
156

    
157
    @Override
158
	public List<Reference> getSubordinateReferences(Reference reference) {
159

    
160
		List<Reference> references = new ArrayList();
161
		List<Reference> subordinateReferences = new ArrayList<Reference>();
162

    
163
		Query query = getSession().createQuery("select r from Reference r where r.inReference = (:reference)");
164
		query.setParameter("reference", reference);
165

    
166
		@SuppressWarnings("unchecked")
167
	    List<Reference<?>> list = query.list();
168
	    references.addAll(list);
169
		for(Reference<?> ref : references){
170
			subordinateReferences.addAll(getSubordinateReferences(ref));
171
		}
172
		references.addAll(subordinateReferences);
173
		return references;
174
	}
175

    
176
    @Override
177
	public List<TaxonBase> listCoveredTaxa(Reference reference, boolean includeSubordinateReferences, List<OrderHint> orderHints, List<String> propertyPaths) {
178

    
179
		/*
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>
185
		 */
186

    
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));
192
		}
193

    
194

    
195
		StringBuilder taxonDescriptionSql = new StringBuilder();
196
		taxonDescriptionSql.append(
197
			"select distinct t from Taxon t " +
198
			// TaxonDescription
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 " +
209

    
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)"
216
			);
217

    
218
		if (orderHints != null && orderHints.size() > 0){
219
		    taxonDescriptionSql.append(" order by ");
220
		    int i = 0;
221
		    for (OrderHint hint : orderHints) {
222
		        if(i > 0) {
223
		            taxonDescriptionSql.append(", ");
224
		        }
225
		        taxonDescriptionSql.append("t.").append(hint.toHql());
226
            }
227
		}
228

    
229
		// TODO include:
230
		// name relations
231
		// taxon relations
232

    
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);
240

    
241
		@SuppressWarnings("unchecked")
242
        List<TaxonBase> taxonBaseList = query.list();
243

    
244
		defaultBeanInitializer.initializeAll(taxonBaseList, propertyPaths);
245

    
246
		return taxonBaseList;
247
	}
248
}
    (1-1/1)