Further adapt to new jackson and new lucene version #4716
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / search / LuceneIndexToolProviderImpl.java
1 // $Id$
2 /**
3 * Copyright (C) 2013 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.cdm.api.service.search;
11
12 import java.util.HashMap;
13 import java.util.Map;
14
15 import org.apache.lucene.analysis.Analyzer;
16 import org.apache.lucene.index.IndexReader;
17 import org.apache.lucene.queryparser.classic.QueryParser;
18 import org.hibernate.SessionFactory;
19 import org.hibernate.search.Search;
20 import org.hibernate.search.SearchFactory;
21 import org.hibernate.search.indexes.IndexReaderAccessor;
22 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.stereotype.Component;
24
25 import eu.etaxonomy.cdm.model.common.CdmBase;
26 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
27 import eu.etaxonomy.cdm.model.description.TextData;
28 import eu.etaxonomy.cdm.model.name.NonViralName;
29 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
30 import eu.etaxonomy.cdm.model.taxon.Taxon;
31 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
32
33 /**
34 * @author a.kohlbecker
35 * @date Sep 18, 2013
36 *
37 */
38 @Component
39 public class LuceneIndexToolProviderImpl implements ILuceneIndexToolProvider {
40
41 private final static String DEFAULT_QURERY_FIELD_NAME = "titleCache";
42
43 @Autowired
44 private SessionFactory sessionFactory;
45
46 private final Map<Class<? extends CdmBase>, QueryParser> queryParsers = new HashMap<Class<? extends CdmBase>, QueryParser>();
47
48 /**
49 * @param sessionfactory
50 * @return
51 */
52 private SearchFactory getCurrentSearchFactory() {
53 return Search.getFullTextSession(sessionFactory.getCurrentSession()).getSearchFactory();
54 }
55
56
57 /**
58 * TODO the abstract base class DescriptionElementBase can not be used, so
59 * we are using an arbitrary subclass to find the DirectoryProvider, future
60 * versions of hibernate search my allow using abstract base classes see
61 * {@link http://stackoverflow.com/questions/492184/how-do-you-find-all-subclasses-of-a-given-class-in-java}
62 *
63 * @param type must not be null
64 * @return
65 */
66 protected Class<? extends CdmBase> pushAbstractBaseTypeDown(Class<? extends CdmBase> type) {
67 if(type == null) {
68 throw new NullPointerException("parameter type must not be null");
69 }
70 if (type.equals(DescriptionElementBase.class)) {
71 return TextData.class;
72 }
73 if (type.equals(TaxonBase.class)) {
74 return Taxon.class;
75 }
76 if (type.equals(TaxonNameBase.class)) {
77 return NonViralName.class;
78 }
79 return type;
80 }
81
82 /* (non-Javadoc)
83 * @see eu.etaxonomy.cdm.api.service.search.ILuceneIndexToolProvider#getIndexReaderFor(java.lang.Class)
84 */
85 @Override
86 public IndexReader getIndexReaderFor(Class<? extends CdmBase> clazz) {
87 IndexReader reader = getCurrentSearchFactory().getIndexReaderAccessor().open(pushAbstractBaseTypeDown(clazz));
88 return reader;
89 }
90
91 /* (non-Javadoc)
92 * @see eu.etaxonomy.cdm.api.service.search.ILuceneIndexToolProvider#getQueryParserFor(java.lang.Class)
93 */
94 @Override
95 public QueryParser getQueryParserFor(Class<? extends CdmBase> clazz) {
96 if(!queryParsers.containsKey(clazz)){
97 Analyzer analyzer = getAnalyzerFor(clazz);
98 QueryParser parser = new QueryParser(DEFAULT_QURERY_FIELD_NAME, analyzer);
99 queryParsers.put(clazz, parser);
100 }
101 return queryParsers.get(clazz);
102 }
103
104 /**
105 * <b>WARING</b> This method might return an Analyzer
106 * which is not suitable for all fields of the lucene document. This method
107 * internally uses the simplified method from {@link {
108 * @link org.hibernate.search.SearchFactory#getAnalyzer(Class)}
109 *
110 * TODO implement method which allows to retrieve the correct Analyzer
111 * per document field, this method will have another signature.
112 *
113 * @return the Analyzer suitable for the lucene index of the given
114 * <code>clazz</code>
115 */
116 @Override
117 public Analyzer getAnalyzerFor(Class<? extends CdmBase> clazz) {
118 Analyzer analyzer = getCurrentSearchFactory().getAnalyzer(pushAbstractBaseTypeDown(clazz));
119 return analyzer;
120 }
121
122 /* (non-Javadoc)
123 * @see eu.etaxonomy.cdm.api.service.search.ILuceneIndexToolProvider#getQueryFactoryFor(java.lang.Class)
124 */
125 @Override
126 public QueryFactory newQueryFactoryFor(Class<? extends CdmBase> clazz){
127 return new QueryFactory(this, pushAbstractBaseTypeDown(clazz));
128 }
129
130 /* (non-Javadoc)
131 * @see eu.etaxonomy.cdm.api.service.search.ILuceneIndexToolProvider#getIndexReaderAccessor()
132 */
133 @Override
134 public IndexReaderAccessor getIndexReaderAccessor(){
135 return getCurrentSearchFactory().getIndexReaderAccessor();
136 }
137
138 }