Merge branch 'release/4.0.0'
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / hibernate / search / DescriptionBaseClassBridge.java
1 // $Id$
2 /**
3 * Copyright (C) 2011 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.hibernate.search;
11
12 import org.apache.lucene.document.Document;
13 import org.apache.lucene.document.Field;
14 import org.apache.lucene.document.Field.Store;
15 import org.apache.lucene.document.StringField;
16 import org.apache.lucene.document.TextField;
17 import org.hibernate.search.bridge.LuceneOptions;
18
19 import eu.etaxonomy.cdm.model.description.TaxonDescription;
20 import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
21 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
22 import eu.etaxonomy.cdm.model.taxon.Taxon;
23 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
24
25 /**
26 * This class bridge is needed to overcome limitations in hibernate search with polymorphism on associations. See:
27 * <ol>
28 * <li>"Support runtime polymorphism on associations (instead of defining the indexed properties based on the returned type"
29 * (https://hibernate.onjira.com/browse/HSEARCH-438)</li>
30 * <li>https://forum.hibernate.org/search.php?keywords=indexembedded+subclass&terms=all
31 * &author=&sc=1&sf=all&sk=t&sd=d&sr=posts&st=0&ch=300&t=0&submit=Search</li>
32 *</ol>
33 * DEVELOPER NOTE: the problem is in {@link org.hibernate.search.engine.DocumentBuilderContainedEntity#initializeClass()} which
34 * is not taking subclasses into account, so the <code>taxon</code> field defined in {@link TaxonDescription} is not
35 * registered in the <code>propertiesMetdata</code>
36 *
37 * @author Andreas Kohlbecker
38 * @date Dec 19, 2011
39 *
40 */
41 public class DescriptionBaseClassBridge extends AbstractClassBridge {
42
43
44 /*
45 * (non-Javadoc)
46 *
47 * @see org.hibernate.search.bridge.FieldBridge#set(java.lang.String,
48 * java.lang.Object, org.apache.lucene.document.Document,
49 * org.hibernate.search.bridge.LuceneOptions)
50 */
51 @Override
52 public void set(String name, Object entity, Document document, LuceneOptions luceneOptions) {
53
54 if (entity instanceof TaxonDescription) {
55
56 Taxon taxon = ((TaxonDescription)entity).getTaxon();
57
58 if (taxon != null) {
59
60 idFieldBridge.set(name + "taxon.id", taxon.getId(), document, idFieldOptions);
61
62 Field titleCachefield = new TextField(name + "taxon.titleCache", taxon.getTitleCache(), Store.YES);
63 document.add(titleCachefield);
64
65 // this should not be necessary since the IdentifiableEntity.titleCache already has the according annotation
66 /*
67 Field titleCacheSortfield = new SortedDocValuesField(
68 name + "taxon.titleCache__sort",
69 new BytesRef(taxon.getTitleCache())
70 );
71 LuceneDocumentUtility.setOrReplaceDocValueField(titleCacheSortfield, document);
72 */
73
74 Field uuidfield = new StringField(name + "taxon.uuid", taxon.getUuid().toString(), Store.YES);
75 document.add(uuidfield);
76
77 for(TaxonNode node : taxon.getTaxonNodes()){
78 if(node.getClassification() != null){
79 idFieldBridge.set(name + "taxon.taxonNodes.classification.id", node.getClassification().getId(), document, idFieldOptions);
80 }
81 }
82 }
83
84 }
85 if (entity instanceof TaxonNameDescription) {
86 TaxonNameBase taxonName = ((TaxonNameDescription) entity).getTaxonName();
87 if (taxonName != null) {
88 idFieldBridge.set(name + "taxonName.id", taxonName.getId(), document, idFieldOptions);
89 }
90 }
91 }
92
93
94 }