Implement generic comparator for OriginalSourceBase #3185
[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 // the newly created should always be on top
26 if (id1 == 0 && id2!=0) {
27 return -1;
28 }
29 if (id1 != 0 && 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 if (type1 != null && type1.equals(OriginalSourceType.PrimaryMediaSource)
44 && type2!=null && !type2.equals(OriginalSourceType.PrimaryTaxonomicSource)) {
45 return -1;
46 }
47 if (type2 != null && type2.equals(OriginalSourceType.PrimaryMediaSource)
48 && type1!=null && !type1.equals(OriginalSourceType.PrimaryTaxonomicSource)) {
49 return 1;
50 }
51
52 //sort by citation title cache
53 if(citation1!=null && citation2!=null){
54 return citation1.getTitleCache().compareTo(citation2.getTitleCache());
55 }
56 return o2.getCreated().compareTo(o1.getCreated());
57 }
58 }