Updated version in pom / project files to taxeditor version : 5.22.0 and cdmlib versi...
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / description / SourceComparator.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.ui.section.description;
10
11 import java.util.Comparator;
12
13 import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
14 import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
15 import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
16 import eu.etaxonomy.cdm.model.reference.Reference;
17
18 /**
19 * @author pplitzner
20 * @date Apr 13, 2016
21 */
22 public class SourceComparator <T extends OriginalSourceBase> implements Comparator<T> {
23
24 @Override
25 public int compare(T o1, T o2) {
26
27 //same and null compare, to be on the save side
28 if (o1 == o2){
29 return 0;
30 }else if (o1 == null){
31 return -1;
32 }else if (o2 == null){
33 return 1;
34 }
35
36 boolean isDescriptionElementSource1 = false;
37 boolean isDescriptionElementSource2 = false;
38 if (o1.isInstanceOf(DescriptionElementSource.class)){
39 isDescriptionElementSource1 = true;
40 }
41 if (o2.isInstanceOf(DescriptionElementSource.class)){
42 isDescriptionElementSource2 = true;
43 }
44 if (isDescriptionElementSource1 != isDescriptionElementSource2){
45 if (isDescriptionElementSource1){
46 return -1;
47 }else{
48 return 1;
49 }
50 }
51
52 // the newly created should always be on top
53 if (!o1.isPersited() && o2.isPersited()) {
54 return -1;
55 } else if(o1.isPersited() && !o2.isPersited()){
56 return 1;
57 }
58
59 // sort by type (Primary taxonomic > Primary Media > others
60 // alphabetically by reference title cache)
61 OriginalSourceType type1 = o1.getType();
62 OriginalSourceType type2 = o2.getType();
63 if (type1 == null){
64 if (type2!=null){
65 return -1;
66 }
67 } else if (type2 == null){
68 return 1;
69 } else if(type1.equals(type2)){
70 //continue with citation compare
71 } else if (type1.equals(OriginalSourceType.PrimaryTaxonomicSource)){
72 return 1;
73 } else if (type2.equals(OriginalSourceType.PrimaryTaxonomicSource)){
74 return -1;
75 } else if (type1.equals(OriginalSourceType.PrimaryMediaSource)){
76 return 1;
77 } else if (type2.equals(OriginalSourceType.PrimaryMediaSource)){
78 return -1;
79 }
80
81 int result;
82
83 //sort by citation title cache if types are equal
84 Reference citation1 = o1.getCitation();
85 Reference citation2 = o2.getCitation();
86 if(citation1!=null && citation2!=null){
87 result = citation1.getTitleCache().compareTo(citation2.getTitleCache());
88 if (result != 0){
89 return result;
90 }
91 }
92
93 //sort by created
94 if(o2.getCreated()!=null && o1.getCreated()!=null){
95 result = o1.getCreated().compareTo(o2.getCreated());
96 if (result != 0){
97 return result;
98 }
99 }else if (o1.getCreated() == null){
100 if (o2.getCreated() != null){
101 return -1;
102 }
103 }else if (o2.getCreated() == null){
104 return 1;
105 }
106
107 //default fallback
108 return o1.getUuid().compareTo(o2.getUuid());
109 }
110 }