Project

General

Profile

Download (9.23 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.description;
10

    
11
import java.util.ArrayList;
12
import java.util.List;
13

    
14
import org.apache.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;
20
import org.hibernate.search.FullTextSession;
21
import org.hibernate.search.Search;
22
import org.hibernate.search.SearchFactory;
23
import org.springframework.stereotype.Repository;
24

    
25
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
26
import eu.etaxonomy.cdm.model.description.TextData;
27
import eu.etaxonomy.cdm.model.media.Media;
28
import eu.etaxonomy.cdm.model.view.AuditEvent;
29
import eu.etaxonomy.cdm.persistence.dao.QueryParseException;
30
import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionElementDao;
31
import eu.etaxonomy.cdm.persistence.dao.hibernate.common.AnnotatableDaoImpl;
32
import eu.etaxonomy.cdm.persistence.query.OrderHint;
33

    
34
@Repository
35
public class DescriptionElementDaoImpl extends AnnotatableDaoImpl<DescriptionElementBase> implements IDescriptionElementDao {
36
    @SuppressWarnings("unused")
37
    private static final Logger logger = Logger.getLogger(DescriptionElementDaoImpl.class);
38

    
39
    private final String defaultField = "multilanguageText.text";
40

    
41
    private final Class<? extends DescriptionElementBase> indexedClasses[];
42

    
43
    public DescriptionElementDaoImpl() {
44
        super(DescriptionElementBase.class);
45
        indexedClasses = new Class[1];
46
        indexedClasses[0] = TextData.class;
47
    }
48

    
49
    @Override
50
    public long countMedia(DescriptionElementBase descriptionElement) {
51
        AuditEvent auditEvent = getAuditEventFromContext();
52
        if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
53
            Query query = getSession().createQuery("select count(media) from DescriptionElementBase descriptionElement join descriptionElement.media media where descriptionElement = :descriptionElement");
54
            query.setParameter("descriptionElement", descriptionElement);
55

    
56
            return ((Long)query.uniqueResult());
57
        } else {
58
            // Horribly inefficient, I know, but hard to do at the moment with envers.
59
            // FIXME Improve this (by improving envers)
60
            List<String> propertyPaths = new ArrayList<>();
61
            propertyPaths.add("media");
62
            DescriptionElementBase d = super.load(descriptionElement.getUuid(), propertyPaths);
63
            return d.getMedia().size();
64
        }
65
    }
66

    
67
    @Override
68
    public long count(Class<? extends DescriptionElementBase> clazz, String queryString) {
69
        checkNotInPriorView("DescriptionElementDaoImpl.countTextData(String queryString)");
70
        QueryParser queryParser = new QueryParser(defaultField, new StandardAnalyzer());
71

    
72
        try {
73
            org.apache.lucene.search.Query query = queryParser.parse(queryString);
74

    
75
            FullTextSession fullTextSession = Search.getFullTextSession(getSession());
76
            org.hibernate.search.FullTextQuery fullTextQuery = null;
77

    
78
            if(clazz == null) {
79
                fullTextQuery = fullTextSession.createFullTextQuery(query, type);
80
            } else {
81
                fullTextQuery = fullTextSession.createFullTextQuery(query, clazz);
82
            }
83
            return fullTextQuery.getResultSize();
84
        } catch (ParseException e) {
85
            throw new QueryParseException(e, queryString);
86
        }
87
    }
88

    
89
    @Override
90
    public List<Media> getMedia(DescriptionElementBase descriptionElement, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
91
        AuditEvent auditEvent = getAuditEventFromContext();
92
        if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
93
            Query query = getSession().createQuery("select media from DescriptionElementBase descriptionElement join descriptionElement.media media where descriptionElement = :descriptionElement order by index(media)");
94
            query.setParameter("descriptionElement", descriptionElement);
95

    
96
            addPageSizeAndNumber(query, pageSize, pageNumber);
97

    
98
            @SuppressWarnings("unchecked")
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
            org.hibernate.search.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
            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(type); // 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
}
(2-2/7)