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