1 |
e8d97a4e
|
Andreas Kohlbecker
|
// $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 org.apache.commons.lang.StringUtils;
|
13 |
|
|
import org.apache.log4j.Logger;
|
14 |
|
|
import org.apache.lucene.document.Document;
|
15 |
|
|
import org.apache.lucene.document.Field;
|
16 |
|
|
import org.hibernate.search.bridge.LuceneOptions;
|
17 |
|
|
|
18 |
|
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
19 |
|
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
20 |
|
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
21 |
|
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
22 |
|
|
|
23 |
|
|
/**
|
24 |
|
|
* Creates a special sort column to allows nomenclatorical ordering of taxa and names.
|
25 |
|
|
* This class bridge can handle all {@link TaxonBase} and {@link NonViralName}s
|
26 |
|
|
* instances. {@link ViralNames} are not supported!
|
27 |
|
|
* <p>
|
28 |
|
|
* Ignores the <code>name</code> parameter!
|
29 |
|
|
* <p>
|
30 |
|
|
* The order follows the hql equivalent:
|
31 |
|
|
* <pre>order by
|
32 |
|
|
* t.name.genusOrUninomial,
|
33 |
|
|
* case when t.name.specificEpithet like '\"%\"'
|
34 |
|
|
* then 1
|
35 |
|
|
* else 0
|
36 |
|
|
* end,
|
37 |
|
|
* t.name.specificEpithet,
|
38 |
|
|
* t.name.rank desc,
|
39 |
|
|
* t.name.nameCache";
|
40 |
|
|
* <pre>
|
41 |
|
|
*
|
42 |
|
|
* @author a.kohlbecker
|
43 |
|
|
* @date Oct 9, 2013
|
44 |
|
|
*
|
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 |
|
|
/* (non-Javadoc)
|
56 |
|
|
* @see eu.etaxonomy.cdm.hibernate.search.AbstractClassBridge#set(java.lang.String, java.lang.Object, org.apache.lucene.document.Document, org.hibernate.search.bridge.LuceneOptions)
|
57 |
|
|
*/
|
58 |
|
|
@Override
|
59 |
|
|
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
|
60 |
9e7a4fe8
|
Andreas Müller
|
NonViralName<?> n = null;
|
61 |
e8d97a4e
|
Andreas Kohlbecker
|
|
62 |
|
|
if(value instanceof TaxonBase) {
|
63 |
|
|
try {
|
64 |
|
|
n = HibernateProxyHelper.deproxy(((TaxonBase) value).getName(), NonViralName.class);
|
65 |
9e7a4fe8
|
Andreas Müller
|
if (n == null){
|
66 |
|
|
return;
|
67 |
|
|
}
|
68 |
e8d97a4e
|
Andreas Kohlbecker
|
} catch (ClassCastException e) {
|
69 |
|
|
logger.info(e);
|
70 |
|
|
/* IGNORE */
|
71 |
|
|
}
|
72 |
|
|
|
73 |
9e7a4fe8
|
Andreas Müller
|
}else if(value instanceof TaxonNameBase){
|
74 |
e8d97a4e
|
Andreas Kohlbecker
|
n = (NonViralName)value;
|
75 |
|
|
}
|
76 |
|
|
if(n == null) {
|
77 |
|
|
logger.error("Unsupported type: " + value.getClass().getName());
|
78 |
|
|
return;
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
// compile sort field
|
82 |
|
|
StringBuilder txt = new StringBuilder();
|
83 |
|
|
|
84 |
|
|
if(n.isProtectedNameCache()){
|
85 |
|
|
txt.append(n.getNameCache());
|
86 |
|
|
} else {
|
87 |
|
|
if(StringUtils.isNotBlank(n.getGenusOrUninomial())){
|
88 |
|
|
txt.append(StringUtils.rightPad(n.getGenusOrUninomial(), MAX_FIELD_LENGTH, PAD_CHAR));
|
89 |
|
|
}
|
90 |
|
|
if(StringUtils.isNotBlank(n.getSpecificEpithet())){
|
91 |
|
|
String matchQuotes = "\".*\"";
|
92 |
|
|
if(n.getSpecificEpithet().matches(matchQuotes)){
|
93 |
|
|
txt.append("1");
|
94 |
|
|
} else {
|
95 |
|
|
txt.append("0");
|
96 |
|
|
}
|
97 |
|
|
txt.append(StringUtils.rightPad(n.getSpecificEpithet(), MAX_FIELD_LENGTH, PAD_CHAR));
|
98 |
|
|
} else {
|
99 |
|
|
txt.append(StringUtils.rightPad("", MAX_FIELD_LENGTH, PAD_CHAR));
|
100 |
|
|
}
|
101 |
|
|
String rankStr = "99"; // default for no rank
|
102 |
|
|
if(n.getRank() != null){
|
103 |
|
|
rankStr = Integer.toString(n.getRank().getOrderIndex());
|
104 |
|
|
}
|
105 |
|
|
txt.append(StringUtils.rightPad(rankStr, 2, PAD_CHAR));
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
Field nameSortField = new Field(NAME_SORT_FIELD_NAME,
|
109 |
|
|
txt.toString(),
|
110 |
|
|
sortFieldOptions.getStore(),
|
111 |
|
|
sortFieldOptions.getIndex(),
|
112 |
|
|
sortFieldOptions.getTermVector()
|
113 |
|
|
);
|
114 |
|
|
document.add(nameSortField);
|
115 |
|
|
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
}
|