ref #6241 change @date to @since (fix)
[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 */
40 public class DescriptionBaseClassBridge extends AbstractClassBridge {
41
42
43 /*
44 * (non-Javadoc)
45 *
46 * @see org.hibernate.search.bridge.FieldBridge#set(java.lang.String,
47 * java.lang.Object, org.apache.lucene.document.Document,
48 * org.hibernate.search.bridge.LuceneOptions)
49 */
50 @Override
51 public void set(String name, Object entity, Document document, LuceneOptions luceneOptions) {
52
53 if (entity instanceof TaxonDescription) {
54
55 Taxon taxon = ((TaxonDescription)entity).getTaxon();
56
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", node.getClassification().getId(), document, idFieldOptions);
79 }
80 }
81 }
82
83 }
84 if (entity instanceof TaxonNameDescription) {
85 TaxonName taxonName = ((TaxonNameDescription) entity).getTaxonName();
86 if (taxonName != null) {
87 idFieldBridge.set(name + "taxonName.id", taxonName.getId(), document, idFieldOptions);
88 }
89 }
90 }
91
92
93 }