Project

General

Profile

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

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

    
19
import eu.etaxonomy.cdm.model.taxon.Taxon;
20
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
21

    
22
/**
23
 * Adds fields for related to and related from taxon relations.
24
 *
25
 *
26
 * @author a.kohlbecker
27
 \* @since Sep 24, 2013
28
 *
29
 */
30
public class TaxonRelationshipClassBridge extends AbstractClassBridge {
31

    
32
    public static final Logger logger = Logger.getLogger(TaxonRelationshipClassBridge.class);
33

    
34
    private static final String FROM = ".from.";
35
    private static final String TO = ".to.";
36

    
37
    /* (non-Javadoc)
38
     * @see eu.etaxonomy.cdm.hibernate.search.AbstractClassBridge#set(java.lang.String, java.lang.Object, org.apache.lucene.document.Document, org.hibernate.search.bridge.LuceneOptions)
39
     */
40
    @Override
41
    public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
42
        if(value instanceof Taxon){
43

    
44
            String fieldName = name;
45
            if(!fieldName.isEmpty()){
46
                fieldName += ".";
47
            }
48

    
49
            Taxon taxon = (Taxon)value;
50

    
51
            String directionName = FROM;
52
            addRelationsFields(fieldName, document, directionName, taxon.getRelationsToThisTaxon());
53

    
54
            directionName = TO;
55
            addRelationsFields(fieldName, document, directionName, taxon.getRelationsFromThisTaxon());
56

    
57
        } else {
58
            logger.error("Unsupported type " + value.getClass());
59
        }
60

    
61
    }
62

    
63
    /**
64
     * @param name
65
     * @param document
66
     * @param directionName
67
     * @param relations
68
     */
69
    private void addRelationsFields(String name, Document document, String directionName,
70
            Set<TaxonRelationship> relations) {
71
        Taxon relTaxon;
72

    
73

    
74
        for(TaxonRelationship rel : relations){
75

    
76
            if(directionName.equals(FROM)){
77
                relTaxon = rel.getFromTaxon();
78
            } else {
79
                relTaxon = rel.getToTaxon();
80
            }
81

    
82
            Field relfield = new StringField(
83
                    name + "relation." + rel.getType().getUuid().toString() + directionName + "id",
84
                    Integer.toString(relTaxon.getId()),
85
                    idFieldOptions.getStore());
86
            relfield.setBoost(idFieldOptions.getBoost());
87
            document.add(relfield);
88
        }
89
    }
90

    
91
}
(17-17/18)