Merge branch 'release/5.45.0'
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / hibernate / search / DescriptionBaseClassBridge.java
1 /**
2 * Copyright (C) 2011 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.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;
17
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;
23
24 /**
25 * This class bridge is needed to overcome limitations in hibernate search with polymorphism on associations. See:
26 * <ol>
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>
31 *</ol>
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>
35 *
36 * @author Andreas Kohlbecker
37 * @since Dec 19, 2011
38 */
39 public class DescriptionBaseClassBridge extends AbstractClassBridge {
40
41 @Override
42 public void set(String name, Object entity, Document document, LuceneOptions luceneOptions) {
43
44 if (entity instanceof TaxonDescription) {
45 handleTaxonDescription(name, (TaxonDescription)entity, document);
46 }
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);
51 }
52 }
53 }
54
55 private void handleTaxonDescription(String name, TaxonDescription entity, Document document) {
56 Taxon taxon = entity.getTaxon();
57 if (taxon != null) {
58
59 idFieldBridge.set(name + "taxon.id", taxon.getId(), document, idFieldOptions);
60
61 Field titleCachefield = new TextField(name + "taxon.titleCache", taxon.getTitleCache(), Store.YES);
62 document.add(titleCachefield);
63
64 // this should not be necessary since the IdentifiableEntity.titleCache already has the according annotation
65 /*
66 Field titleCacheSortfield = new SortedDocValuesField(
67 name + "taxon.titleCache__sort",
68 new BytesRef(taxon.getTitleCache())
69 );
70 LuceneDocumentUtility.setOrReplaceDocValueField(titleCacheSortfield, document);
71 */
72
73 Field uuidfield = new StringField(name + "taxon.uuid", taxon.getUuid().toString(), Store.YES);
74 document.add(uuidfield);
75
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);
80 }
81 if(node.treeIndex() != null){
82 Field treeIndexField = new StringField("inDescription.taxon.taxonNodes.treeIndex",
83 node.treeIndex(),
84 Store.YES
85 );
86 document.add(treeIndexField);
87 }
88 }
89 }
90 }
91 }