Merge branch 'release/4.6.0'
[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.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 int result = 0;
26
27 // the newly created should always be on top
28 if (id1 == 0){
29 if(id2!=0) {
30 result = -1;
31 }
32 else{
33 result = 0;
34 }
35 } else if(id2==0){
36 result = 1;
37 }
38
39 // sort by type (Primary taxonomic > Primary Media > others
40 // alphabetically by reference title cache)
41 else if (type1 == null){
42 if(type2==null){
43 result = 0;
44 }
45 else{
46 result = 1;
47 }
48 } else if (type2 == null){
49 result = -1;
50 } else if(type1.equals(type2)){
51 result = 0;
52 } else if (type1.equals(OriginalSourceType.PrimaryTaxonomicSource)){
53 result = -1;
54 } else if (type2.equals(OriginalSourceType.PrimaryTaxonomicSource)){
55 result = 1;
56 } else if (type1.equals(OriginalSourceType.PrimaryMediaSource)){
57 result = -1;
58 } else if (type2.equals(OriginalSourceType.PrimaryMediaSource)){
59 result = 1;
60 }
61
62 //sort by citation title cache if types are equal
63 if (result == 0){
64 if(citation1!=null && citation2!=null){
65 result = citation1.getTitleCache().compareTo(citation2.getTitleCache());
66 }
67
68 if(o2.getCreated()!=null && o1.getCreated()!=null){
69 result = o2.getCreated().compareTo(o1.getCreated());
70 }
71 if (result == 0){
72 //default fallback
73 return o1.getId() - o2.getId();
74 }
75 }
76 return result;
77
78
79 }
80 }