2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.description
;
11 import java
.util
.ArrayList
;
12 import java
.util
.List
;
14 import org
.apache
.logging
.log4j
.LogManager
;
15 import org
.apache
.logging
.log4j
.Logger
;
16 import org
.apache
.lucene
.analysis
.standard
.StandardAnalyzer
;
17 import org
.apache
.lucene
.queryparser
.classic
.ParseException
;
18 import org
.apache
.lucene
.queryparser
.classic
.QueryParser
;
19 import org
.hibernate
.Hibernate
;
20 import org
.hibernate
.query
.Query
;
21 import org
.hibernate
.search
.FullTextQuery
;
22 import org
.hibernate
.search
.FullTextSession
;
23 import org
.hibernate
.search
.Search
;
24 import org
.hibernate
.search
.SearchFactory
;
25 import org
.springframework
.stereotype
.Repository
;
27 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
28 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
29 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
30 import eu
.etaxonomy
.cdm
.model
.view
.AuditEvent
;
31 import eu
.etaxonomy
.cdm
.persistence
.dao
.QueryParseException
;
32 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IDescriptionElementDao
;
33 import eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.common
.AnnotatableDaoBaseImpl
;
34 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
37 public class DescriptionElementDaoImpl
38 extends AnnotatableDaoBaseImpl
<DescriptionElementBase
>
39 implements IDescriptionElementDao
{
41 @SuppressWarnings("unused")
42 private static final Logger logger
= LogManager
.getLogger();
44 private final String defaultField
= "multilanguageText.text";
46 private final Class
<?
extends DescriptionElementBase
> indexedClasses
[];
48 public DescriptionElementDaoImpl() {
49 super(DescriptionElementBase
.class);
50 indexedClasses
= new Class
[1];
51 indexedClasses
[0] = TextData
.class;
55 public long countMedia(DescriptionElementBase descriptionElement
) {
56 AuditEvent auditEvent
= getAuditEventFromContext();
57 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
58 Query
<Long
> query
= getSession().createQuery("select count(media) from DescriptionElementBase descriptionElement join descriptionElement.media media where descriptionElement = :descriptionElement", Long
.class);
59 query
.setParameter("descriptionElement", descriptionElement
);
61 return (query
.uniqueResult());
63 // Horribly inefficient, I know, but hard to do at the moment with envers.
64 // FIXME Improve this (by improving envers)
65 List
<String
> propertyPaths
= new ArrayList
<>();
66 propertyPaths
.add("media");
67 DescriptionElementBase d
= super.load(descriptionElement
.getUuid(), propertyPaths
);
68 return d
.getMedia().size();
73 public long count(Class
<?
extends DescriptionElementBase
> clazz
, String queryString
) {
74 checkNotInPriorView("DescriptionElementDaoImpl.countTextData(String queryString)");
75 QueryParser queryParser
= new QueryParser(defaultField
, new StandardAnalyzer());
78 org
.apache
.lucene
.search
.Query query
= queryParser
.parse(queryString
);
80 FullTextSession fullTextSession
= Search
.getFullTextSession(getSession());
81 FullTextQuery fullTextQuery
= null;
84 fullTextQuery
= fullTextSession
.createFullTextQuery(query
, type
);
86 fullTextQuery
= fullTextSession
.createFullTextQuery(query
, clazz
);
88 return fullTextQuery
.getResultSize();
89 } catch (ParseException e
) {
90 throw new QueryParseException(e
, queryString
);
95 public List
<Media
> getMedia(DescriptionElementBase descriptionElement
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
96 AuditEvent auditEvent
= getAuditEventFromContext();
97 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
98 Query
<Media
> query
= getSession().createQuery("select media from DescriptionElementBase descriptionElement join descriptionElement.media media where descriptionElement = :descriptionElement order by index(media)", Media
.class);
99 query
.setParameter("descriptionElement", descriptionElement
);
101 addPageSizeAndNumber(query
, pageSize
, pageNumber
);
103 List
<Media
> results
= query
.list();
104 defaultBeanInitializer
.initializeAll(results
, propertyPaths
);
107 // Horribly inefficient, I know, but hard to do at the moment with envers.
108 // FIXME Improve this (by improving envers)
109 List
<String
> pPaths
= new ArrayList
<>();
110 propertyPaths
.add("media");
111 DescriptionElementBase d
= super.load(descriptionElement
.getUuid(), pPaths
);
112 List
<Media
> results
= new ArrayList
<>();
113 results
.addAll(d
.getMedia());
114 if(pageSize
!= null) {
117 if(pageNumber
!= null) {
118 // if the page is out of scope
119 if(results
.size() < (pageNumber
* pageSize
)) {
120 return new ArrayList
<>();
122 fromIndex
= pageNumber
* pageSize
;
124 toIndex
= results
.size() < (fromIndex
+ pageSize
) ? results
.size() : fromIndex
+ pageSize
;
125 results
= results
.subList(fromIndex
, toIndex
);
127 defaultBeanInitializer
.initializeAll(results
, propertyPaths
);
133 public List
<DescriptionElementBase
> search(Class
<?
extends DescriptionElementBase
> clazz
, String queryString
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
134 // public <S extends DescriptionElementBase> List<S> search(Class<S> clazz, String queryString, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
135 checkNotInPriorView("DescriptionElementDaoImpl.searchTextData(String queryString, Integer pageSize, Integer pageNumber)");
136 QueryParser queryParser
= new QueryParser(defaultField
, new StandardAnalyzer());
139 org
.apache
.lucene
.search
.Query query
= queryParser
.parse(queryString
);
140 FullTextQuery fullTextQuery
= null;
141 FullTextSession fullTextSession
= Search
.getFullTextSession(getSession());
143 fullTextQuery
= fullTextSession
.createFullTextQuery(query
, type
);
145 fullTextQuery
= fullTextSession
.createFullTextQuery(query
, clazz
);
147 addOrder(fullTextQuery
,orderHints
);
149 this.addPageSizeAndNumber(fullTextQuery
, pageSize
, pageNumber
);
151 @SuppressWarnings("unchecked")
152 List
<DescriptionElementBase
> results
= fullTextQuery
.list();
153 defaultBeanInitializer
.initializeAll(results
, propertyPaths
);
156 } catch (ParseException e
) {
157 throw new QueryParseException(e
, queryString
);
162 public void purgeIndex() {
163 FullTextSession fullTextSession
= Search
.getFullTextSession(getSession());
164 for(Class
<?
extends DescriptionElementBase
> clazz
: indexedClasses
) {
165 fullTextSession
.purgeAll(clazz
); // remove all description element base from indexes
167 fullTextSession
.flushToIndexes();
171 public void rebuildIndex() {
172 FullTextSession fullTextSession
= Search
.getFullTextSession(getSession());
174 for(DescriptionElementBase descriptionElementBase
: list(null,null)) { // re-index all descriptionElements
175 Hibernate
.initialize(descriptionElementBase
.getInDescription());
176 Hibernate
.initialize(descriptionElementBase
.getFeature());
177 fullTextSession
.index(descriptionElementBase
);
179 fullTextSession
.flushToIndexes();
183 public void optimizeIndex() {
184 FullTextSession fullTextSession
= Search
.getFullTextSession(getSession());
185 SearchFactory searchFactory
= fullTextSession
.getSearchFactory();
186 for(Class
<?
extends DescriptionElementBase
> clazz
: indexedClasses
) {
187 searchFactory
.optimize(clazz
); // optimize the indices ()
189 fullTextSession
.flushToIndexes();
192 public int count(String queryString
) {
193 checkNotInPriorView("DescriptionElementDaoImpl.count(String queryString)");
194 QueryParser queryParser
= new QueryParser(defaultField
, new StandardAnalyzer());
197 org
.apache
.lucene
.search
.Query query
= queryParser
.parse(queryString
);
199 FullTextSession fullTextSession
= Search
.getFullTextSession(this.getSession());
200 org
.hibernate
.search
.FullTextQuery fullTextQuery
= fullTextSession
.createFullTextQuery(query
, type
);
202 return fullTextQuery
.getResultSize();
204 } catch (ParseException e
) {
205 throw new QueryParseException(e
, queryString
);
210 public String
suggestQuery(String string
) {
211 throw new UnsupportedOperationException("suggest query is not supported yet");