Project

General

Profile

Download (2.81 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2013 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.Set;
13

    
14
import org.apache.log4j.Logger;
15
import org.apache.lucene.document.Document;
16
import org.apache.lucene.document.Field;
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
 * @date 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 Field(
83
                    name + "relation." + rel.getType().getUuid().toString() + directionName + "id",
84
                    Integer.toString(relTaxon.getId()),
85
                    idFieldOptions.getStore(),
86
                    idFieldOptions.getIndex(),
87
                    idFieldOptions.getTermVector()
88
                    );
89
            relfield.setBoost(idFieldOptions.getBoost());
90
            document.add(relfield);
91
        }
92
    }
93

    
94
}
(16-16/17)