Merge branch 'hotfix/5.18.2'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / description / SourceComparator.java
1 package eu.etaxonomy.taxeditor.ui.section.description;
2
3
4 import java.util.Comparator;
5
6 import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
7 import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
8 import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
9 import eu.etaxonomy.cdm.model.reference.Reference;
10
11 /**
12 * @author pplitzner
13 * @date Apr 13, 2016
14 *
15 */
16 public class SourceComparator <T extends OriginalSourceBase> implements Comparator<T> {
17 @Override
18 public int compare(T o1, T o2) {
19 int id1 = o1.getId();
20 int id2 = o2.getId();
21 boolean isDescriptionElementSource1 = false;
22 boolean isDescriptionElementSource2 = false;
23 if (o1 instanceof DescriptionElementSource){
24 isDescriptionElementSource1 = true;
25 }
26 if (o2 instanceof DescriptionElementSource){
27 isDescriptionElementSource2 = true;
28 }
29 int result = 0;
30 if (isDescriptionElementSource1 != isDescriptionElementSource2){
31 if (isDescriptionElementSource1){
32 return -1;
33 }else{
34 return 1;
35 }
36 }
37 OriginalSourceType type1 = o1.getType();
38 OriginalSourceType type2 = o2.getType();
39 Reference citation1 = o1.getCitation();
40 Reference citation2 = o2.getCitation();
41
42
43
44 // the newly created should always be on top
45 if (id1 == 0){
46 if(id2!=0) {
47 return -1;
48 }
49 else{
50 result = 0;
51 }
52 } else if(id2==0){
53 return 1;
54 }
55
56 // sort by type (Primary taxonomic > Primary Media > others
57 // alphabetically by reference title cache)
58 else if (type1 == null){
59 if(type2==null){
60 result = 0;
61 }
62 else{
63 result = -1;
64 }
65 } else if (type2 == null){
66 result = 1;
67 } else if(type1.equals(type2)){
68 result = 0;
69 } else if (type1.equals(OriginalSourceType.PrimaryTaxonomicSource)){
70 result = 1;
71 } else if (type2.equals(OriginalSourceType.PrimaryTaxonomicSource)){
72 result = -1;
73 } else if (type1.equals(OriginalSourceType.PrimaryMediaSource)){
74 result = 1;
75 } else if (type2.equals(OriginalSourceType.PrimaryMediaSource)){
76 result = -1;
77 }
78
79 //sort by citation title cache if types are equal
80 if (result == 0){
81 if(citation1!=null && citation2!=null){
82 result = citation1.getTitleCache().compareTo(citation2.getTitleCache());
83 }
84
85 if(o2.getCreated()!=null && o1.getCreated()!=null){
86 result = o1.getCreated().compareTo(o2.getCreated());
87 }else if (o1.getCreated() == null ){
88 if (o2.getCreated() == null){
89 result = 0;
90 }else{
91 return -1;
92 }
93 }else if (o2.getCreated() == null){
94 return 1;
95 }
96 if (result == 0){
97 //default fallback
98 return o1.getUuid().compareTo(o2.getUuid());
99 }
100 }
101 return result;
102
103
104 }
105 }