1
|
package eu.etaxonomy.taxeditor.ui.section.description;
|
2
|
|
3
|
|
4
|
import java.util.Comparator;
|
5
|
|
6
|
import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
|
7
|
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
|
8
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
9
|
|
10
|
/**
|
11
|
* @author pplitzner
|
12
|
* @date Apr 13, 2016
|
13
|
*
|
14
|
*/
|
15
|
public class SourceComparator implements Comparator<OriginalSourceBase> {
|
16
|
@Override
|
17
|
public int compare(OriginalSourceBase o1, OriginalSourceBase o2) {
|
18
|
int id1 = o1.getId();
|
19
|
int id2 = o2.getId();
|
20
|
OriginalSourceType type1 = o1.getType();
|
21
|
OriginalSourceType type2 = o2.getType();
|
22
|
Reference citation1 = o1.getCitation();
|
23
|
Reference citation2 = o2.getCitation();
|
24
|
|
25
|
// the newly created should always be on top
|
26
|
if (id1 == 0 && id2!=0) {
|
27
|
return -1;
|
28
|
}
|
29
|
else if (id2==0) {
|
30
|
return 1;
|
31
|
}
|
32
|
|
33
|
// sort by type (Primary taxonomic > Primary Media > others
|
34
|
// alphabetically by reference title cache)
|
35
|
if (type1 != null && type1.equals(OriginalSourceType.PrimaryTaxonomicSource)
|
36
|
&& (type2 == null || !type2.equals(OriginalSourceType.PrimaryTaxonomicSource))) {
|
37
|
return -1;
|
38
|
}
|
39
|
if ((type1 == null || !type1.equals(OriginalSourceType.PrimaryTaxonomicSource))
|
40
|
&& type2 != null && type2.equals(OriginalSourceType.PrimaryTaxonomicSource)) {
|
41
|
return 1;
|
42
|
}
|
43
|
|
44
|
if ((type1 == null || type1.equals(OriginalSourceType.PrimaryMediaSource))
|
45
|
&& (type2==null || !type2.equals(OriginalSourceType.PrimaryMediaSource))) {
|
46
|
return -1;
|
47
|
}
|
48
|
if (type2 != null && type2.equals(OriginalSourceType.PrimaryMediaSource)
|
49
|
&& type1!=null && !type1.equals(OriginalSourceType.PrimaryMediaSource)) {
|
50
|
return 1;
|
51
|
}
|
52
|
|
53
|
//sort by citation title cache
|
54
|
if(citation1!=null && citation2!=null){
|
55
|
return citation1.getTitleCache().compareTo(citation2.getTitleCache());
|
56
|
}
|
57
|
|
58
|
if(o2.getCreated()!=null && o1.getCreated()!=null){
|
59
|
return o2.getCreated().compareTo(o1.getCreated());
|
60
|
}
|
61
|
|
62
|
//default fallback
|
63
|
return o1.getId() - o2.getId();
|
64
|
|
65
|
}
|
66
|
}
|