Project

General

Profile

Download (4.69 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
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
21

    
22
/**
23
 * Lucene index class bridge which sets the uuids of the accepted taxon for the
24
 * TaxonBase object into the index.
25
 * <p>
26
 * Adds id fields with the uuid and id of the accepted taxon of the
27
 * current {@link TaxonBase} entity. Id fields should not be analyzed, therefore
28
 * this FieldBridge ignores the settings {@link org.hibernate.annotations.Index}
29
 * annotation and always sets this to <code>NOT_ANALYZED</code>.
30
 *
31
 *
32
 * @author c.mathew
33
 * @author a.kohlbecker
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 DOC_KEY_UUID_SUFFIX = ".uuid";
39
    public static final String DOC_KEY_ID_SUFFIX = ".id";
40
    public final static String DOC_KEY_PUBLISH_SUFFIX = ".publish";
41
    public final static String DOC_KEY_TREEINDEX = "taxonNodes.treeIndex";
42
    public final static String DOC_KEY_CLASSIFICATION_ID = "taxonNodes.classification.id";
43
    public final static String ACC_TAXON = "accTaxon"; //there are probably still some places not using this constant, but for renaming in future we should try to use it everywhere
44

    
45
    @Override
46
    public void set(String name, Object value, Document document,
47
            LuceneOptions luceneOptions) {
48
        String accTaxonUuid = "";
49

    
50
        boolean isSynonym = false;
51
        Taxon accTaxon;
52
        if(value instanceof Taxon){
53
            accTaxon = (Taxon)value;
54
        }else if (value instanceof Synonym){
55
            accTaxon = ((Synonym)value).getAcceptedTaxon();
56
            isSynonym = true;
57
        }else{
58
            throw new RuntimeException("Unhandled taxon base class: " + value.getClass().getSimpleName());
59
        }
60

    
61
        // in the case of taxon this is just the uuid
62
        if(accTaxon != null) {
63
            //id
64
            Field canonicalNameIdField = new StringField(name + DOC_KEY_ID_SUFFIX,
65
                    Integer.toString(accTaxon.getId()),
66
                    luceneOptions.getStore()
67
                    );
68
            document.add(canonicalNameIdField);
69
            //uuid
70
            accTaxonUuid = accTaxon.getUuid().toString();
71
            Field canonicalNameUuidField = new StringField(name + DOC_KEY_UUID_SUFFIX,
72
                    accTaxonUuid,
73
                    luceneOptions.getStore()
74
                    );
75
            //TODO  do we really need to set the boost for an id field?
76
            canonicalNameUuidField.setBoost(luceneOptions.getBoost());
77
            document.add(canonicalNameUuidField);
78

    
79
            //publish
80
            Field accPublishField = new StringField(name + DOC_KEY_PUBLISH_SUFFIX,
81
                    Boolean.toString(accTaxon.isPublish()),
82
                    luceneOptions.getStore()
83
                    );
84
            document.add(accPublishField);
85

    
86
            //treeIndex + Classification
87
            if (isSynonym && ACC_TAXON.equals(name)){
88
                for (TaxonNode node : accTaxon.getTaxonNodes()){
89
                    //treeIndex
90
                    Field treeIndexField;
91
                    if (node.treeIndex()!= null){  //TODO find out why this happens in TaxonServiceSearchTest.testFindByDescriptionElementFullText_modify_Classification
92
                        treeIndexField = new StringField(DOC_KEY_TREEINDEX,
93
                                node.treeIndex(),
94
                                luceneOptions.getStore()
95
                                );
96
                        document.add(treeIndexField);
97
                    }
98

    
99
                    //classification
100
                    if (node.getClassification() != null){  //should never be null, but who knows
101
                        Field classificationIdField = new StringField(DOC_KEY_CLASSIFICATION_ID,
102
                                Integer.toString(node.getClassification().getId()),
103
                                luceneOptions.getStore()
104
                                );
105
                        document.add(classificationIdField);
106
                    }
107
                }
108
            }
109
        }
110
    }
111
}
(2-2/20)