Project

General

Profile

Download (3.58 KB) Statistics
| Branch: | Tag: | Revision:
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 org.apache.lucene.document.Document;
12
import org.apache.lucene.document.Field;
13
import org.apache.lucene.document.StringField;
14
import org.hibernate.search.bridge.FieldBridge;
15
import org.hibernate.search.bridge.LuceneOptions;
16

    
17
import eu.etaxonomy.cdm.model.taxon.Synonym;
18
import eu.etaxonomy.cdm.model.taxon.Taxon;
19
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
20

    
21
/**
22
 * Lucene index class bridge which sets the uuids of the accepted taxon for the
23
 * TaxonBase object into the index.
24
 * <p>
25
 * Adds multivalued id fields with the uuid and id of the accepted taxa of the
26
 * current {@link TaxonBase} entity. Id fields should not be analyzed, therefore
27
 * this FieldBridge ignores the settings {@link org.hibernate.annotations.Index}
28
 * annotation and always sets this to <code>NOT_ANALYZED</code>.
29
 *
30
 *
31
 * @author c.mathew
32
 * @author a.kohlbecker
33
 * @version 1.0
34
 * @since 26 Jul 2013
35
 */
36
public class AcceptedTaxonBridge implements FieldBridge { // TODO inherit from AbstractClassBridge since this base class provides presets for id fields?
37

    
38
    public final static String ACCEPTED_TAXON_UUID_LIST_SEP = ",";
39
    private final static String DOC_KEY_UUID_SUFFIX = ".uuids"; // TODO do not use the plural for the field name
40
    private static final String DOC_KEY_ID_SUFFIX = ".id";
41

    
42
    @Override
43
    public void set(String name, Object value, Document document,
44
            LuceneOptions luceneOptions) {
45
        String accTaxonUuids = "";
46

    
47
        // in the case of taxon this is just the uuid
48
        if(value instanceof Taxon) {
49
            accTaxonUuids = ((Taxon)value).getUuid().toString();
50
            Field canonicalNameIdField = new StringField(name + DOC_KEY_ID_SUFFIX,
51
                    Integer.toString(((Taxon)value).getId()),
52
                    luceneOptions.getStore()
53
                    );
54
            document.add(canonicalNameIdField);
55
        }
56
        // in the case of synonym this is the accepted taxon in the synonym
57
        // relationships
58
        if (value instanceof Synonym) {
59
            StringBuilder sb = new StringBuilder();
60
            Synonym synonym = (Synonym) value;
61
            Taxon accTaxon = synonym.getAcceptedTaxon();
62
            if (accTaxon != null){
63
                sb.append(accTaxon.getUuid().toString());
64
                sb.append(ACCEPTED_TAXON_UUID_LIST_SEP);
65
                // adding the accTaxon id as multivalue field:
66
                Field canonicalNameIdField = new StringField(name + DOC_KEY_ID_SUFFIX,
67
                        Integer.toString(accTaxon.getId()),
68
                        luceneOptions.getStore()
69
                        );
70
                document.add(canonicalNameIdField);
71
            }
72

    
73
            accTaxonUuids = sb.toString();
74
            if(accTaxonUuids.length() > 0) {
75
                accTaxonUuids = accTaxonUuids.substring(0, accTaxonUuids.length()-1);
76
            }
77
        }
78

    
79
        // TODO can't we also add the uuid as multivalue field?
80

    
81
        // the id field is shorter and should be sufficient
82
        Field canonicalNameUuidField = new StringField(name + DOC_KEY_UUID_SUFFIX,
83
                accTaxonUuids,
84
                luceneOptions.getStore()
85
                );
86
        //TODO  do we really need to set the boost for an id field?
87
        canonicalNameUuidField.setBoost(luceneOptions.getBoost());
88
        document.add(canonicalNameUuidField);
89

    
90
    }
91
}
(2-2/18)