Project

General

Profile

Download (3.91 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2012 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.Map;
12

    
13
import org.apache.lucene.document.Document;
14
import org.apache.lucene.document.Field;
15
import org.apache.lucene.document.StringField;
16
import org.apache.lucene.document.TextField;
17
import org.hibernate.search.bridge.LuceneOptions;
18
import org.hibernate.search.bridge.ParameterizedBridge;
19

    
20
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
21
import eu.etaxonomy.cdm.model.common.Representation;
22

    
23
/**
24
 * @author Andreas Kohlbecker
25
 * @date Jun 4, 2012
26
 *
27
 */
28
public class DefinedTermBaseClassBridge extends AbstractClassBridge implements ParameterizedBridge {
29

    
30
    /**
31
     *
32
     */
33
    private static final String INCLUDE_PARENT_TERMS_KEY = "includeParentTerms";
34
    private boolean includeParentTerms = false;
35

    
36

    
37
    @Override
38
    public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
39

    
40
        if(value == null){
41
            return;
42
        }
43
        NotNullAwareIdBridge idFieldBridge = new NotNullAwareIdBridge();
44

    
45
        DefinedTermBase term = (DefinedTermBase)value;
46

    
47
        idFieldBridge.set(name + "id", term.getId(), document, idFieldOptions);
48

    
49
        Field uuidField = new StringField(name + "uuid",
50
                term.getUuid().toString(),
51
                luceneOptions.getStore());
52
        document.add(uuidField);
53

    
54
        Field langLabelField = new TextField(name + "label",
55
              term.getLabel(),
56
              luceneOptions.getStore());
57
        langLabelField.setBoost(luceneOptions.getBoost());
58
        document.add(langLabelField);
59

    
60
        for(Representation representation : term.getRepresentations()){
61
            addRepresentationField(name, representation, "text", representation.getText(), document, luceneOptions);
62
            addRepresentationField(name, representation, "label", representation.getLabel(), document, luceneOptions);
63
            addRepresentationField(name, representation, "abbreviatedLabel", representation.getAbbreviatedLabel(), document, luceneOptions);
64
        }
65

    
66
        if(includeParentTerms){
67

    
68
            DefinedTermBase parentTerm = term.getPartOf();
69
            while(parentTerm != null){
70
                Field setOfParentsField = new StringField(name + "setOfParents",
71
                        parentTerm.getUuid().toString(),
72
                        luceneOptions.getStore());
73
                document.add(setOfParentsField);
74
                parentTerm = parentTerm.getPartOf();
75
            }
76

    
77
        }
78
    }
79

    
80
    /**
81
     * @param name
82
     * @param representation
83
     * @param text
84
     * @param document
85
     * @param luceneOptions
86
     */
87
    private void addRepresentationField(String name, Representation representation, String representationField, String text, Document document, LuceneOptions luceneOptions) {
88
        if(text == null){
89
            return;
90
        }
91
        Field allField = new TextField(name + "representation." + representationField + ".ALL",
92
                text,
93
                luceneOptions.getStore());
94
        allField.setBoost(luceneOptions.getBoost());
95
        document.add(allField);
96

    
97

    
98
        if (representation.getLanguage() != null){
99
            Field langField = new TextField(name + "representation." + representationField + "."+ representation.getLanguage().getUuid().toString(),
100
                    text,
101
                    luceneOptions.getStore());
102

    
103
            allField.setBoost(luceneOptions.getBoost());
104
            document.add(langField);
105
        }
106
    }
107

    
108
    @Override
109
    public void setParameterValues(Map<String, String> parameters) {
110
        if(parameters.containsKey(INCLUDE_PARENT_TERMS_KEY)){
111
            includeParentTerms = Boolean.parseBoolean(parameters.get(INCLUDE_PARENT_TERMS_KEY));
112
        }
113
    }
114

    
115

    
116
}
(5-5/18)