Project

General

Profile

Download (3.58 KB) Statistics
| Branch: | Tag: | Revision:
1 8287f797 Cherian Mathew
/**
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 e6df36cc Andreas Kohlbecker
import org.apache.lucene.document.StringField;
14 8287f797 Cherian Mathew
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 41bb4c3b Andreas Kohlbecker
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
20 8287f797 Cherian Mathew
21
/**
22 41bb4c3b Andreas Kohlbecker
 * Lucene index class bridge which sets the uuids of the accepted taxon for the
23 8287f797 Cherian Mathew
 * TaxonBase object into the index.
24 41bb4c3b Andreas Kohlbecker
 * <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 8287f797 Cherian Mathew
 *
31
 * @author c.mathew
32 41bb4c3b Andreas Kohlbecker
 * @author a.kohlbecker
33 8287f797 Cherian Mathew
 * @version 1.0
34
 * @created 26 Jul 2013
35
 */
36 e18a1639 Andreas Kohlbecker
public class AcceptedTaxonBridge implements FieldBridge { // TODO inherit from AbstractClassBridge since this base class provides presets for id fields?
37 8287f797 Cherian Mathew
38 41bb4c3b Andreas Kohlbecker
    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 cd26eb20 Andreas Kohlbecker
            Field canonicalNameIdField = new StringField(name + DOC_KEY_ID_SUFFIX,
51
                    Integer.toString(((Taxon)value).getId()),
52 e6df36cc Andreas Kohlbecker
                    luceneOptions.getStore()
53 41bb4c3b Andreas Kohlbecker
                    );
54
            document.add(canonicalNameIdField);
55
        }
56
        // in the case of synonym this is the accepted taxon in the synonym
57
        // relationships
58 8287f797 Cherian Mathew
        if (value instanceof Synonym) {
59 41bb4c3b Andreas Kohlbecker
            StringBuilder sb = new StringBuilder();
60 8287f797 Cherian Mathew
            Synonym synonym = (Synonym) value;
61 f476ff86 Andreas Müller
            Taxon accTaxon = synonym.getAcceptedTaxon();
62
            if (accTaxon != null){
63 41bb4c3b Andreas Kohlbecker
                sb.append(accTaxon.getUuid().toString());
64
                sb.append(ACCEPTED_TAXON_UUID_LIST_SEP);
65
                // adding the accTaxon id as multivalue field:
66 00bea758 Andreas Kohlbecker
                Field canonicalNameIdField = new StringField(name + DOC_KEY_ID_SUFFIX,
67
                        Integer.toString(accTaxon.getId()),
68 e6df36cc Andreas Kohlbecker
                        luceneOptions.getStore()
69 41bb4c3b Andreas Kohlbecker
                        );
70
                document.add(canonicalNameIdField);
71 8287f797 Cherian Mathew
            }
72 f476ff86 Andreas Müller
73 8287f797 Cherian Mathew
            accTaxonUuids = sb.toString();
74
            if(accTaxonUuids.length() > 0) {
75 41bb4c3b Andreas Kohlbecker
                accTaxonUuids = accTaxonUuids.substring(0, accTaxonUuids.length()-1);
76 8287f797 Cherian Mathew
            }
77
        }
78
79 41bb4c3b Andreas Kohlbecker
        // TODO can't we also add the uuid as multivalue field?
80
81
        // the id field is shorter and should be sufficient
82 e6df36cc Andreas Kohlbecker
        Field canonicalNameUuidField = new StringField(name + DOC_KEY_UUID_SUFFIX,
83 41bb4c3b Andreas Kohlbecker
                accTaxonUuids,
84 e6df36cc Andreas Kohlbecker
                luceneOptions.getStore()
85
                );
86 41bb4c3b Andreas Kohlbecker
        //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 8287f797 Cherian Mathew
}