1
|
/**
|
2
|
* Copyright (C) 2007 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 java.util.Set;
|
12
|
|
13
|
import org.apache.lucene.document.Document;
|
14
|
import org.apache.lucene.document.Field;
|
15
|
import org.apache.lucene.document.Field.Index;
|
16
|
import org.hibernate.search.bridge.FieldBridge;
|
17
|
import org.hibernate.search.bridge.LuceneOptions;
|
18
|
|
19
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
20
|
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
|
21
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
22
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
23
|
|
24
|
/**
|
25
|
* Lucene index class bridge which sets the uuids of the accepted taxon for the
|
26
|
* TaxonBase object into the index.
|
27
|
* <p>
|
28
|
* Adds multivalued id fields with the uuid and id of the accepted taxa of the
|
29
|
* current {@link TaxonBase} entity. Id fields should not be analyzed, therefore
|
30
|
* this FieldBridge ignores the settings {@link org.hibernate.annotations.Index}
|
31
|
* annotation and always sets this to <code>NOT_ANALYZED</code>.
|
32
|
*
|
33
|
*
|
34
|
* @author c.mathew
|
35
|
* @author a.kohlbecker
|
36
|
* @version 1.0
|
37
|
* @created 26 Jul 2013
|
38
|
*/
|
39
|
public class AcceptedTaxonBridge implements FieldBridge { // TODO inherit from AbstractClassBridge since this base class provides presets for id fields?
|
40
|
|
41
|
public final static String ACCEPTED_TAXON_UUID_LIST_SEP = ",";
|
42
|
private final static String DOC_KEY_UUID_SUFFIX = ".uuids"; // TODO do not use the plural for the field name
|
43
|
private static final String DOC_KEY_ID_SUFFIX = ".id";
|
44
|
|
45
|
@Override
|
46
|
public void set(String name, Object value, Document document,
|
47
|
LuceneOptions luceneOptions) {
|
48
|
String accTaxonUuids = "";
|
49
|
|
50
|
// in the case of taxon this is just the uuid
|
51
|
if(value instanceof Taxon) {
|
52
|
accTaxonUuids = ((Taxon)value).getUuid().toString();
|
53
|
Field canonicalNameIdField = new Field(name + DOC_KEY_ID_SUFFIX,
|
54
|
Integer.toString(((Taxon)value).getId()),
|
55
|
luceneOptions.getStore(),
|
56
|
Index.NOT_ANALYZED,
|
57
|
Field.TermVector.NO
|
58
|
);
|
59
|
document.add(canonicalNameIdField);
|
60
|
}
|
61
|
// in the case of synonym this is the accepted taxon in the synonym
|
62
|
// relationships
|
63
|
if (value instanceof Synonym) {
|
64
|
StringBuilder sb = new StringBuilder();
|
65
|
Synonym synonym = (Synonym) value;
|
66
|
Set<SynonymRelationship> synRelationships = synonym.getSynonymRelations();
|
67
|
for (SynonymRelationship sr : synRelationships) {
|
68
|
Taxon accTaxon = sr.getAcceptedTaxon();
|
69
|
sb.append(accTaxon.getUuid().toString());
|
70
|
sb.append(ACCEPTED_TAXON_UUID_LIST_SEP);
|
71
|
|
72
|
// adding the accTaxon id as multivalue field:
|
73
|
Field canonicalNameIdField = new Field(name + DOC_KEY_ID_SUFFIX,
|
74
|
Integer.toString(accTaxon.getId()),
|
75
|
luceneOptions.getStore(),
|
76
|
Index.NOT_ANALYZED,
|
77
|
Field.TermVector.NO
|
78
|
);
|
79
|
document.add(canonicalNameIdField);
|
80
|
}
|
81
|
accTaxonUuids = sb.toString();
|
82
|
if(accTaxonUuids.length() > 0) {
|
83
|
accTaxonUuids = accTaxonUuids.substring(0, accTaxonUuids.length()-1);
|
84
|
}
|
85
|
}
|
86
|
|
87
|
// TODO can't we also add the uuid as multivalue field?
|
88
|
|
89
|
// the id field is shorter and should be sufficient
|
90
|
Field canonicalNameUuidField = new Field(name + DOC_KEY_UUID_SUFFIX,
|
91
|
accTaxonUuids,
|
92
|
luceneOptions.getStore(),
|
93
|
Index.NOT_ANALYZED,
|
94
|
Field.TermVector.NO);
|
95
|
//TODO do we really need to set the boost for an id field?
|
96
|
canonicalNameUuidField.setBoost(luceneOptions.getBoost());
|
97
|
document.add(canonicalNameUuidField);
|
98
|
|
99
|
|
100
|
|
101
|
|
102
|
}
|
103
|
}
|