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