1
|
package eu.etaxonomy.cdm.persistence.dao.hibernate.description;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.List;
|
5
|
|
6
|
import org.apache.lucene.analysis.SimpleAnalyzer;
|
7
|
import org.apache.lucene.queryParser.ParseException;
|
8
|
import org.apache.lucene.queryParser.QueryParser;
|
9
|
import org.apache.lucene.search.Sort;
|
10
|
import org.apache.lucene.search.SortField;
|
11
|
import org.hibernate.Hibernate;
|
12
|
import org.hibernate.Query;
|
13
|
import org.hibernate.envers.query.AuditQuery;
|
14
|
import org.hibernate.search.FullTextSession;
|
15
|
import org.hibernate.search.Search;
|
16
|
import org.hibernate.search.SearchFactory;
|
17
|
import org.springframework.stereotype.Repository;
|
18
|
|
19
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
20
|
import eu.etaxonomy.cdm.model.description.TextData;
|
21
|
import eu.etaxonomy.cdm.model.media.Media;
|
22
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
23
|
import eu.etaxonomy.cdm.model.view.AuditEvent;
|
24
|
import eu.etaxonomy.cdm.persistence.dao.QueryParseException;
|
25
|
import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionElementDao;
|
26
|
import eu.etaxonomy.cdm.persistence.dao.hibernate.common.AnnotatableDaoImpl;
|
27
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
28
|
|
29
|
@Repository
|
30
|
public class DescriptionElementDaoImpl extends AnnotatableDaoImpl<DescriptionElementBase> implements IDescriptionElementDao {
|
31
|
|
32
|
private String defaultField = "multilanguageText.text";
|
33
|
|
34
|
private Class<? extends DescriptionElementBase> indexedClasses[];
|
35
|
|
36
|
public DescriptionElementDaoImpl() {
|
37
|
super(DescriptionElementBase.class);
|
38
|
indexedClasses = new Class[1];
|
39
|
indexedClasses[0] = TextData.class;
|
40
|
}
|
41
|
|
42
|
public int countMedia(DescriptionElementBase descriptionElement) {
|
43
|
AuditEvent auditEvent = getAuditEventFromContext();
|
44
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
45
|
Query query = getSession().createQuery("select count(media) from DescriptionElementBase descriptionElement join descriptionElement.media media where descriptionElement = :descriptionElement");
|
46
|
query.setParameter("descriptionElement", descriptionElement);
|
47
|
|
48
|
return ((Long)query.uniqueResult()).intValue();
|
49
|
} else {
|
50
|
// Horribly inefficient, I know, but hard to do at the moment with envers.
|
51
|
// FIXME Improve this (by improving envers)
|
52
|
List<String> propertyPaths = new ArrayList<String>();
|
53
|
propertyPaths.add("media");
|
54
|
DescriptionElementBase d = super.load(descriptionElement.getUuid(), propertyPaths);
|
55
|
return d.getMedia().size();
|
56
|
}
|
57
|
}
|
58
|
|
59
|
public int count(Class<? extends DescriptionElementBase> clazz, String queryString) {
|
60
|
checkNotInPriorView("DescriptionElementDaoImpl.countTextData(String queryString)");
|
61
|
QueryParser queryParser = new QueryParser(defaultField, new SimpleAnalyzer());
|
62
|
|
63
|
try {
|
64
|
org.apache.lucene.search.Query query = queryParser.parse(queryString);
|
65
|
|
66
|
FullTextSession fullTextSession = Search.getFullTextSession(getSession());
|
67
|
org.hibernate.search.FullTextQuery fullTextQuery = null;
|
68
|
|
69
|
if(clazz == null) {
|
70
|
fullTextQuery = fullTextSession.createFullTextQuery(query, type);
|
71
|
} else {
|
72
|
fullTextQuery = fullTextSession.createFullTextQuery(query, clazz);
|
73
|
}
|
74
|
return fullTextQuery.getResultSize();
|
75
|
} catch (ParseException e) {
|
76
|
throw new QueryParseException(e, queryString);
|
77
|
}
|
78
|
}
|
79
|
|
80
|
public List<Media> getMedia(DescriptionElementBase descriptionElement, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
81
|
AuditEvent auditEvent = getAuditEventFromContext();
|
82
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
83
|
Query query = getSession().createQuery("select media from DescriptionElementBase descriptionElement join descriptionElement.media media where descriptionElement = :descriptionElement order by index(media)");
|
84
|
query.setParameter("descriptionElement", descriptionElement);
|
85
|
|
86
|
if(pageSize != null) {
|
87
|
query.setMaxResults(pageSize);
|
88
|
if(pageNumber != null) {
|
89
|
query.setFirstResult(pageNumber * pageSize);
|
90
|
} else {
|
91
|
query.setFirstResult(0);
|
92
|
}
|
93
|
}
|
94
|
|
95
|
List<Media> results = (List<Media>)query.list();
|
96
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
97
|
return results;
|
98
|
} else {
|
99
|
// Horribly inefficient, I know, but hard to do at the moment with envers.
|
100
|
// FIXME Improve this (by improving envers)
|
101
|
List<String> pPaths = new ArrayList<String>();
|
102
|
propertyPaths.add("media");
|
103
|
DescriptionElementBase d = super.load(descriptionElement.getUuid(), pPaths);
|
104
|
List<Media> results = new ArrayList<Media>();
|
105
|
results.addAll(d.getMedia());
|
106
|
if(pageSize != null) {
|
107
|
int fromIndex = 0;
|
108
|
int toIndex = 0;
|
109
|
if(pageNumber != null) {
|
110
|
// if the page is out of scope
|
111
|
if(results.size() < (pageNumber * pageSize)) {
|
112
|
return new ArrayList<Media>();
|
113
|
}
|
114
|
fromIndex = pageNumber * pageSize;
|
115
|
}
|
116
|
toIndex = results.size() < (fromIndex + pageSize) ? results.size() : fromIndex + pageSize;
|
117
|
results = results.subList(fromIndex, toIndex);
|
118
|
}
|
119
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
120
|
return results;
|
121
|
}
|
122
|
}
|
123
|
|
124
|
public List<DescriptionElementBase> search(Class<? extends DescriptionElementBase> clazz, String queryString, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
125
|
checkNotInPriorView("DescriptionElementDaoImpl.searchTextData(String queryString, Integer pageSize, Integer pageNumber)");
|
126
|
QueryParser queryParser = new QueryParser(defaultField, new SimpleAnalyzer());
|
127
|
|
128
|
try {
|
129
|
org.apache.lucene.search.Query query = queryParser.parse(queryString);
|
130
|
org.hibernate.search.FullTextQuery fullTextQuery = null;
|
131
|
FullTextSession fullTextSession = Search.getFullTextSession(getSession());
|
132
|
if(clazz == null) {
|
133
|
fullTextQuery = fullTextSession.createFullTextQuery(query, type);
|
134
|
} else {
|
135
|
fullTextQuery = fullTextSession.createFullTextQuery(query, clazz);
|
136
|
}
|
137
|
addOrder(fullTextQuery,orderHints);
|
138
|
|
139
|
if(pageSize != null) {
|
140
|
fullTextQuery.setMaxResults(pageSize);
|
141
|
if(pageNumber != null) {
|
142
|
fullTextQuery.setFirstResult(pageNumber * pageSize);
|
143
|
} else {
|
144
|
fullTextQuery.setFirstResult(0);
|
145
|
}
|
146
|
}
|
147
|
|
148
|
List<DescriptionElementBase> results = fullTextQuery.list();
|
149
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
150
|
return results;
|
151
|
|
152
|
} catch (ParseException e) {
|
153
|
throw new QueryParseException(e, queryString);
|
154
|
}
|
155
|
}
|
156
|
|
157
|
public void purgeIndex() {
|
158
|
FullTextSession fullTextSession = Search.getFullTextSession(getSession());
|
159
|
for(Class clazz : indexedClasses) {
|
160
|
fullTextSession.purgeAll(type); // remove all description element base from indexes
|
161
|
}
|
162
|
fullTextSession.flushToIndexes();
|
163
|
}
|
164
|
|
165
|
public void rebuildIndex() {
|
166
|
FullTextSession fullTextSession = Search.getFullTextSession(getSession());
|
167
|
|
168
|
for(DescriptionElementBase descriptionElementBase : list(null,null)) { // re-index all descriptionElements
|
169
|
Hibernate.initialize(descriptionElementBase.getInDescription());
|
170
|
Hibernate.initialize(descriptionElementBase.getFeature());
|
171
|
fullTextSession.index(descriptionElementBase);
|
172
|
}
|
173
|
fullTextSession.flushToIndexes();
|
174
|
}
|
175
|
|
176
|
public void optimizeIndex() {
|
177
|
FullTextSession fullTextSession = Search.getFullTextSession(getSession());
|
178
|
SearchFactory searchFactory = fullTextSession.getSearchFactory();
|
179
|
for(Class clazz : indexedClasses) {
|
180
|
searchFactory.optimize(clazz); // optimize the indices ()
|
181
|
}
|
182
|
fullTextSession.flushToIndexes();
|
183
|
}
|
184
|
|
185
|
public int count(String queryString) {
|
186
|
checkNotInPriorView("DescriptionElementDaoImpl.count(String queryString)");
|
187
|
QueryParser queryParser = new QueryParser(defaultField, new SimpleAnalyzer());
|
188
|
|
189
|
try {
|
190
|
org.apache.lucene.search.Query query = queryParser.parse(queryString);
|
191
|
|
192
|
FullTextSession fullTextSession = Search.getFullTextSession(this.getSession());
|
193
|
org.hibernate.search.FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(query, type);
|
194
|
|
195
|
return fullTextQuery.getResultSize();
|
196
|
|
197
|
} catch (ParseException e) {
|
198
|
throw new QueryParseException(e, queryString);
|
199
|
}
|
200
|
}
|
201
|
|
202
|
public String suggestQuery(String string) {
|
203
|
throw new UnsupportedOperationException("suggest query is not supported yet");
|
204
|
}
|
205
|
|
206
|
}
|