Project

General

Profile

Download (3.77 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 org.apache.commons.lang3.StringUtils;
12
import org.apache.log4j.Logger;
13
import org.apache.lucene.document.Document;
14
import org.apache.lucene.document.Field;
15
import org.apache.lucene.document.SortedDocValuesField;
16
import org.apache.lucene.util.BytesRef;
17
import org.hibernate.search.bridge.LuceneOptions;
18

    
19
import eu.etaxonomy.cdm.model.common.CdmBase;
20
import eu.etaxonomy.cdm.model.name.INonViralName;
21
import eu.etaxonomy.cdm.model.name.TaxonName;
22
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
23

    
24
/**
25
 * Creates a special sort column to allow nomenclatural ordering of taxa and names.
26
 * This class bridge can handle all {@link TaxonBase} and {@link INonViralName}s
27
 * instances. {@link ViralNames} are not supported!
28
 * <p>
29
 * Ignores the <code>name</code> parameter!
30
 * <p>
31
 * The order follows the hql equivalent:
32
 * <pre>order by
33
 *  t.name.genusOrUninomial,
34
 *  case when t.name.specificEpithet like '\"%\"'
35
 *      then 1
36
 *      else 0
37
 *   end,
38
 *   t.name.specificEpithet,
39
 *   t.name.rank desc,
40
 *   t.name.nameCache";
41
 * <pre>
42
 *
43
 * @author a.kohlbecker
44
 * @since Oct 9, 2013
45
 */
46
public class NomenclaturalSortOrderBrigde extends AbstractClassBridge {
47

    
48
    private static final char PAD_CHAR = '0';
49

    
50
    public static final Logger logger = Logger.getLogger(NomenclaturalSortOrderBrigde.class);
51

    
52
    final static int MAX_FIELD_LENGTH = 50; // used to pab the strings, should be 255 set to 50 for debugging FIXME
53
    public final static String NAME_SORT_FIELD_NAME = "nomenclaturalOrder__sort";
54

    
55
    @Override
56
    public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
57
        INonViralName nvn = null;
58

    
59
        if(value instanceof TaxonBase) {
60
            try {
61
                nvn = CdmBase.deproxy((TaxonBase) value).getName();
62
                if (nvn == null){
63
                	return;
64
                }
65
            } catch (ClassCastException e) {
66
                logger.info(e);
67
                /* IGNORE */
68
            }
69

    
70
        }else if(value instanceof TaxonName){
71
            nvn = (INonViralName)value;
72
        }
73
        if(nvn == null) {
74
            logger.error("Unsupported type: " + value.getClass().getName());
75
            return;
76
        }
77

    
78
        // compile sort field
79
        StringBuilder txt = new StringBuilder();
80

    
81
        if(nvn.isProtectedNameCache()){
82
            txt.append(nvn.getNameCache());
83
        } else {
84
            if(StringUtils.isNotBlank(nvn.getGenusOrUninomial())){
85
                txt.append(StringUtils.rightPad(nvn.getGenusOrUninomial(), MAX_FIELD_LENGTH, PAD_CHAR));
86
            }
87
            if(StringUtils.isNotBlank(nvn.getSpecificEpithet())){
88
                String matchQuotes = "\".*\"";
89
                if(nvn.getSpecificEpithet().matches(matchQuotes)){
90
                    txt.append("1");
91
                } else {
92
                    txt.append("0");
93
                }
94
                txt.append(StringUtils.rightPad(nvn.getSpecificEpithet(), MAX_FIELD_LENGTH, PAD_CHAR));
95
            } else {
96
                txt.append(StringUtils.rightPad("", MAX_FIELD_LENGTH, PAD_CHAR));
97
            }
98
            String rankStr = "99"; // default for no rank
99
            if(nvn.getRank() != null){
100
                rankStr = Integer.toString(nvn.getRank().getOrderIndex());
101
            }
102
            txt.append(StringUtils.rightPad(rankStr, 2, PAD_CHAR));
103
        }
104

    
105
        Field nameSortField = new SortedDocValuesField(NAME_SORT_FIELD_NAME, new BytesRef(txt.toString()));
106
        LuceneDocumentUtility.setOrReplaceDocValueField(nameSortField, document);
107
    }
108

    
109
}
(12-12/20)