2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.persistence
.dto
;
11 import java
.io
.Serializable
;
12 import java
.util
.Comparator
;
13 import java
.util
.UUID
;
15 import eu
.etaxonomy
.cdm
.model
.common
.OrderIndexComparator
;
16 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
17 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
18 import eu
.etaxonomy
.cdm
.strategy
.cache
.TagEnum
;
19 import eu
.etaxonomy
.cdm
.strategy
.cache
.TaggedText
;
26 public class TaxonNodeDtoByRankAndNameComparator
implements Serializable
, Comparator
<TaxonNodeDto
> {
27 private static final long serialVersionUID
= 2596641007876609704L;
30 public int compare(TaxonNodeDto node1
, TaxonNodeDto node2
) {
32 boolean node1Excluded
= node1
.isExcluded();
33 boolean node2Excluded
= node2
.isExcluded();
34 boolean node1Unplaced
= node1
.isUnplaced();
35 boolean node2Unplaced
= node2
.isUnplaced();
38 if (node1
.getUuid().equals(node2
.getUuid())){
41 //They should both be put to the end (first unplaced then excluded)
42 if (node2Excluded
&& !node1Excluded
){
45 if (node2Unplaced
&& !(node1Unplaced
|| node1Excluded
)){
49 if (node1Excluded
&& !node2Excluded
){
52 if (node1Unplaced
&& !(node2Unplaced
|| node2Excluded
)){
56 if (node1Unplaced
&& node2Excluded
){
59 if (node2Unplaced
&& node1Excluded
){
66 Integer rankTax1
= node1
.getRankOrderIndex();
67 Integer rankTax2
= node2
.getRankOrderIndex();
69 //first compare ranks, if ranks are equal (or both null) compare names or taxon title cache if names are null
70 int rankOrder
= OrderIndexComparator
.instance().compare(rankTax1
, rankTax2
);
73 if (node1
.getTaggedTitle() != null && node2
.getTaggedTitle() != null){
74 //same rank, order by name
75 String sortableName1
= "";
76 for (TaggedText tagged
: node1
.getTaggedTitle()){
77 if (tagged
.getType().equals(TagEnum
.name
)){
78 sortableName1
+= " " + tagged
.getText();
82 String sortableName2
= "";
83 for (TaggedText tagged
: node2
.getTaggedTitle()){
84 if (tagged
.getType().equals(TagEnum
.name
)){
85 sortableName2
+= " " + tagged
.getText();
88 int result
= sortableName1
.compareTo(sortableName2
);
90 return node1
.getTaxonUuid().compareTo(node2
.getTaxonUuid());
95 //this is maybe not 100% correct, we need to compare name cases, but it is a very rare case
96 return node1
.getTaxonTitleCache().compareTo(node2
.getTaxonTitleCache());
99 //rankTax2.isHigher(rankTax1)
108 public String
getTaxonTitle(TaxonBase
<?
> taxon
, TaxonNode node
) {
109 return (taxon
== null) ? node
.getUuid().toString(): taxon
.getTitleCache();
117 private UUID
getTaxonUuid(TaxonBase
<?
> taxon
, TaxonNode node
) {
118 return (taxon
== null) ? node
.getUuid(): taxon
.getUuid();