Project

General

Profile

Download (3.78 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.lang.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 allows nomenclatorical 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
 */
47
public class NomenclaturalSortOrderBrigde extends AbstractClassBridge {
48

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

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

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

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

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

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

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

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

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

    
110
}
(12-12/18)