Project

General

Profile

Download (3.92 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2012 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.hibernate.search;
11

    
12
import java.util.Map;
13

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

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

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

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

    
37

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

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

    
46
        DefinedTermBase term = (DefinedTermBase)value;
47

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

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

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

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

    
67
        if(includeParentTerms){
68

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

    
78
        }
79
    }
80

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

    
98

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

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

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

    
116

    
117
}
(5-5/18)