almost 100% fixing #2950 (Allow grouping of lucene (hibernate search) results) -...
[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.Index;
15 import org.apache.lucene.document.Field.Store;
16 import org.hibernate.search.bridge.FieldBridge;
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 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 Field(name + "taxon.titleCache", taxon.getTitleCache(), Store.YES, Index.ANALYZED,
62 luceneOptions.getTermVector());
63 document.add(titleCachefield);
64
65 Field titleCacheSortfield = new Field(name + "taxon.titleCache__sort", taxon.getTitleCache(), sortFieldOptions.getStore(), sortFieldOptions.getIndex(),
66 luceneOptions.getTermVector());
67 document.add(titleCacheSortfield);
68
69 Field uuidfield = new Field(name + "taxon.uuid", taxon.getUuid().toString(), Store.YES, Index.ANALYZED,
70 luceneOptions.getTermVector());
71 document.add(uuidfield);
72
73 for(TaxonNode node : taxon.getTaxonNodes()){
74 if(node.getClassification() != null){
75 idFieldBridge.set(name + "taxon.taxonNodes.classification.id", node.getClassification().getId(), document, idFieldOptions);
76 }
77 }
78 }
79
80 }
81 if (entity instanceof TaxonNameDescription) {
82 TaxonNameBase taxonName = ((TaxonNameDescription) entity).getTaxonName();
83 if (taxonName != null) {
84 idFieldBridge.set(name + "taxonName.id", taxonName.getId(), document, idFieldOptions);
85 }
86 }
87 }
88
89
90 }