1
|
/**
|
2
|
* Copyright (C) 2012 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.hibernate.search;
|
10
|
|
11
|
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
|
12
|
import org.apache.lucene.document.Document;
|
13
|
import org.apache.lucene.document.Field;
|
14
|
import org.apache.lucene.document.SortedDocValuesField;
|
15
|
import org.apache.lucene.util.BytesRef;
|
16
|
import org.hibernate.search.bridge.LuceneOptions;
|
17
|
|
18
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
19
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
20
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
21
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
22
|
|
23
|
/**
|
24
|
* The <code>GroupByTaxonClassBridge</code> adds the field
|
25
|
* <code>groupby_taxon.id</code> to the lucene document which can be used to
|
26
|
* group search results based on the taxon which is associated with the indexed
|
27
|
* cdm entity. So any cdm class which is involved in querying for taxa must
|
28
|
* use this class bridge, e.g.:
|
29
|
*
|
30
|
<pre>
|
31
|
@ClassBridge(impl=GroupByTaxonClassBridge.class))
|
32
|
</pre>
|
33
|
* or
|
34
|
*
|
35
|
* <pre>
|
36
|
@ClassBridges({
|
37
|
@ClassBridge(impl=GroupByTaxonClassBridge.class),
|
38
|
@ClassBridge(impl=DescriptionBaseClassBridge.class),
|
39
|
})
|
40
|
}
|
41
|
* </pre>
|
42
|
*
|
43
|
* @author a.kohlbecker
|
44
|
* @since Oct 4, 2012
|
45
|
*/
|
46
|
public class GroupByTaxonClassBridge extends AbstractClassBridge{
|
47
|
|
48
|
public static final String GROUPBY_TAXON_FIELD = "groupby_taxon.id__sort";
|
49
|
|
50
|
public static final Logger logger = LogManager.getLogger(GroupByTaxonClassBridge.class);
|
51
|
|
52
|
public GroupByTaxonClassBridge() {
|
53
|
super();
|
54
|
}
|
55
|
|
56
|
protected Taxon getAssociatedTaxon(Object entity) {
|
57
|
|
58
|
if (entity instanceof DescriptionBase<?>) {
|
59
|
if (entity instanceof TaxonDescription) {
|
60
|
return ((TaxonDescription) entity).getTaxon();
|
61
|
}
|
62
|
return null;
|
63
|
}
|
64
|
if (entity instanceof TaxonBase){
|
65
|
if (entity instanceof Taxon) {
|
66
|
return (Taxon)entity;
|
67
|
}
|
68
|
return null;
|
69
|
}
|
70
|
|
71
|
throw new RuntimeException("CDM class " + entity.getClass() + " not yet supported");
|
72
|
}
|
73
|
|
74
|
@Override
|
75
|
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
|
76
|
|
77
|
Taxon taxon = getAssociatedTaxon(value);
|
78
|
if(taxon != null){
|
79
|
Field field = new SortedDocValuesField(GROUPBY_TAXON_FIELD, new BytesRef(String.valueOf(taxon.getId())));
|
80
|
LuceneDocumentUtility.setOrReplaceDocValueField(field, document);
|
81
|
}
|
82
|
}
|
83
|
|
84
|
}
|