import java.util.Comparator;
-import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
import eu.etaxonomy.cdm.model.description.DescriptionBase;
import eu.etaxonomy.cdm.model.description.DescriptionType;
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDtoByRankAndNameComparator;
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDtoNaturalComparator;
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
-import eu.etaxonomy.taxeditor.store.CdmStore;
/**
* @author pplitzner
public class MatrixRowComparator implements Comparator<Object>{
private Comparator<TaxonNodeDto> comparator;
- private ITaxonNodeService taxonNodeService;
public MatrixRowComparator() {
- taxonNodeService = CdmStore.getService(ITaxonNodeService.class);
if (PreferencesUtil.isNodesSortedNaturally()){
comparator = new TaxonNodeDtoNaturalComparator();
} else if (PreferencesUtil.isNodesSortedByName()){
//both descriptions are of the same type
if(description1 instanceof SpecimenDescription){
//description2 has to also be a SpecimenDescription
- SpecimenDescription specimenDescription1 = (SpecimenDescription)description1;
- SpecimenDescription specimenDescription2 = (SpecimenDescription)description2;
- int id1 = specimenDescription1.getDescribedSpecimenOrObservation().getId();
- int id2 = specimenDescription2.getDescribedSpecimenOrObservation().getId();
- return id1-id2;
+ return compareSpecimenDescriptions((SpecimenDescription)description1, (SpecimenDescription)description2);
}
else if(description1 instanceof TaxonDescription){
//description2 has to also be a TaxonDescription
- TaxonDescription taxonDescription1 = (TaxonDescription)description1;
- TaxonDescription taxonDescription2 = (TaxonDescription)description2;
- boolean isComputed1 = taxonDescription1.getTypes().stream()
- .anyMatch(type -> type.equals(DescriptionType.AGGREGATED));
- boolean isComputed2 = taxonDescription2.getTypes().stream()
- .anyMatch(type -> type.equals(DescriptionType.AGGREGATED));
- if(isComputed1){
- if(!isComputed2){
- return -1;
- }
- }
- if(isComputed2){
- return 1;
- }
- //TODO: implement compare for different description types
+ return compareTaxonDescriptions((TaxonDescription)description1, (TaxonDescription)description2);
}
}
}
return o1.hashCode()-o2.hashCode();
}
+ private int compareTaxonDescriptions(TaxonDescription taxonDescription1, TaxonDescription taxonDescription2) {
+ boolean isComputed1 = taxonDescription1.getTypes().stream()
+ .anyMatch(type -> type.equals(DescriptionType.AGGREGATED_STRUC_DESC));
+ boolean isComputed2 = taxonDescription2.getTypes().stream()
+ .anyMatch(type -> type.equals(DescriptionType.AGGREGATED_STRUC_DESC));
+ if(isComputed1){
+ if(!isComputed2){
+ return -1;
+ }
+ }
+ if(isComputed2){
+ return 1;
+ }
+ // no aggregation description found
+ boolean isLiterature1 = taxonDescription1.getTypes().stream()
+ .anyMatch(type -> type.equals(DescriptionType.SECONDARY_DATA));
+ boolean isLiterature2 = taxonDescription2.getTypes().stream()
+ .anyMatch(type -> type.equals(DescriptionType.SECONDARY_DATA));
+ if(isLiterature1){
+ if(!isLiterature2){
+ return -1;
+ }
+ }
+ if(isLiterature2){
+ return 1;
+ }
+ return taxonDescription1.hashCode()-taxonDescription2.hashCode();
+ }
+
+ private int compareSpecimenDescriptions(SpecimenDescription specimenDescription1,
+ SpecimenDescription specimenDescription2) {
+ int id1 = specimenDescription1.getDescribedSpecimenOrObservation().getId();
+ int id2 = specimenDescription2.getDescribedSpecimenOrObservation().getId();
+ return id1-id2;
+ }
+
}