2 * Copyright (C) 2011 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
.hibernate
.search
;
11 import org
.apache
.lucene
.document
.Document
;
12 import org
.apache
.lucene
.document
.Field
;
13 import org
.apache
.lucene
.document
.Field
.Store
;
14 import org
.apache
.lucene
.document
.StringField
;
15 import org
.apache
.lucene
.document
.TextField
;
16 import org
.hibernate
.search
.bridge
.LuceneOptions
;
18 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
19 import eu
.etaxonomy
.cdm
.model
.description
.TaxonNameDescription
;
20 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
21 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
22 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
25 * This class bridge is needed to overcome limitations in hibernate search with polymorphism on associations. See:
27 * <li>"Support runtime polymorphism on associations (instead of defining the indexed properties based on the returned type"
28 * (https://hibernate.onjira.com/browse/HSEARCH-438)</li>
29 * <li>https://forum.hibernate.org/search.php?keywords=indexembedded+subclass&terms=all
30 * &author=&sc=1&sf=all&sk=t&sd=d&sr=posts&st=0&ch=300&t=0&submit=Search</li>
32 * DEVELOPER NOTE: the problem is in {@link org.hibernate.search.engine.DocumentBuilderContainedEntity#initializeClass()} which
33 * is not taking subclasses into account, so the <code>taxon</code> field defined in {@link TaxonDescription} is not
34 * registered in the <code>propertiesMetdata</code>
36 * @author Andreas Kohlbecker
39 public class DescriptionBaseClassBridge
extends AbstractClassBridge
{
42 public void set(String name
, Object entity
, Document document
, LuceneOptions luceneOptions
) {
44 if (entity
instanceof TaxonDescription
) {
45 handleTaxonDescription(name
, (TaxonDescription
)entity
, document
);
47 if (entity
instanceof TaxonNameDescription
) {
48 TaxonName taxonName
= ((TaxonNameDescription
) entity
).getTaxonName();
49 if (taxonName
!= null) {
50 idFieldBridge
.set(name
+ "taxonName.id", taxonName
.getId(), document
, idFieldOptions
);
55 private void handleTaxonDescription(String name
, TaxonDescription entity
, Document document
) {
56 Taxon taxon
= entity
.getTaxon();
59 idFieldBridge
.set(name
+ "taxon.id", taxon
.getId(), document
, idFieldOptions
);
61 Field titleCachefield
= new TextField(name
+ "taxon.titleCache", taxon
.getTitleCache(), Store
.YES
);
62 document
.add(titleCachefield
);
64 // this should not be necessary since the IdentifiableEntity.titleCache already has the according annotation
66 Field titleCacheSortfield = new SortedDocValuesField(
67 name + "taxon.titleCache__sort",
68 new BytesRef(taxon.getTitleCache())
70 LuceneDocumentUtility.setOrReplaceDocValueField(titleCacheSortfield, document);
73 Field uuidfield
= new StringField(name
+ "taxon.uuid", taxon
.getUuid().toString(), Store
.YES
);
74 document
.add(uuidfield
);
76 for(TaxonNode node
: taxon
.getTaxonNodes()){
77 if(node
.getClassification() != null){
78 idFieldBridge
.set(name
+ "taxon.taxonNodes.classification.id",
79 node
.getClassification().getId(), document
, idFieldOptions
);
81 if(node
.treeIndex() != null){
82 Field treeIndexField
= new StringField("inDescription.taxon.taxonNodes.treeIndex",
86 document
.add(treeIndexField
);