Project

General

Profile

Revision aa478e85

IDaa478e8528bf68e9a280f64fc6af8e2f42d3afb4
Parent a9e92136
Child 889c7e91

Added by Patrick Plitzner almost 3 years ago

ref #6045 Fix NPE for TaxonNaturalComparator
- fix getAncestors() method of taxon node
- added JUnit test

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/taxon/TaxonNaturalComparator.java
24 24

  
25 25
	@Override
26 26
	public int compare(TaxonNode node1, TaxonNode node2) {
27
//		TaxonNode node1 = null;
28
//		TaxonNode node2 = null;
29
//		if (taxon1.getTaxonNodes().size()>0 && taxon2.getTaxonNodes().size()>0){
30
//			for (TaxonNode node: taxon1.getTaxonNodes()){
31
//				if (node.getClassification().equals(classification)){
32
//					node1= node;
33
//					break;
34
//				}
35
//			}
36
//			for (TaxonNode node: taxon2.getTaxonNodes()){
37
//				if (node.getClassification().equals(classification)){
38
//					node2= node;
39
//					break;
40
//				}
41
//			}
42
//		}else{
43
//			return 0;
44
//		}
45 27
		if (node1.isAncestor(node2)) {
46 28
            return 1;
47 29
        }
......
75 57
					TaxonNode lastEqualTreeIndexAncestorNode2 = null;
76 58
					while (ancestorsNode1.hasNext()){
77 59
						TaxonNode next1 = ancestorsNode1.next();
78
						if (next1.treeIndex().equals(lastEqualAncestorTreeIndex)){
60
						String[] split = next1.treeIndex().split("#");
61
						if (split[split.length-1].equals(lastEqualAncestorTreeIndex)){
79 62
							lastEqualTreeIndexAncestorNode1 = next1;
80 63
						}
81 64
					}
82 65
					while (ancestorsNode2.hasNext()){
83 66
						TaxonNode next2 = ancestorsNode2.next();
84
						if (next2.treeIndex().equals(lastEqualAncestorTreeIndex)){
67
						String[] split = next2.treeIndex().split("#");
68
						if (split[split.length-1].equals(lastEqualAncestorTreeIndex)){
85 69
							lastEqualTreeIndexAncestorNode2 = next2;
86 70
						}
87 71
					}
88 72
					return lastEqualTreeIndexAncestorNode1.getSortIndex().compareTo(lastEqualTreeIndexAncestorNode2.getSortIndex());
89

  
90 73
				}
91 74
			}
92 75
			lastEqualAncestorTreeIndex = splitNode1[i];
93 76
		}
94

  
95 77
		return 0;
96 78
	}
97

  
98 79
}
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/taxon/TaxonNode.java
717 717
    }
718 718

  
719 719
    /**
720
     * Returns a
720
     * Returns all ancestor nodes of this node
721 721
     *
722
     * @return
722
     * @return a set of all parent nodes
723 723
     */
724 724
    protected Set<TaxonNode> getAncestors(){
725 725
        Set<TaxonNode> nodeSet = new HashSet<TaxonNode>();
726
       // nodeSet.add(this);
727 726
        if(this.getParent() != null){
728 727
        	TaxonNode parent =  CdmBase.deproxy(this.getParent(), TaxonNode.class);
728
        	nodeSet.add(parent);
729 729
            nodeSet.addAll(parent.getAncestors());
730 730
        }
731 731
        return nodeSet;
732 732
    }
733
    
734
    /**
735
     * Retrieves the first ancestor of the given rank
736
     * @param rank the rank the ancestor should have
737
     * @return the first found instance of a parent taxon node with the given rank
738
     */
733 739
    public TaxonNode getAncestorOfRank(Rank rank){
734
        Set<TaxonNode> nodeSet = new HashSet<TaxonNode>();
735
       // nodeSet.add(this);
736 740
        TaxonBase taxon = HibernateProxyHelper.deproxy(this.getTaxon(), Taxon.class);
737 741
        if (taxon == null){
738 742
            return null;
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/TaxonNodeServiceImplTest.java
18 18

  
19 19
import java.io.FileNotFoundException;
20 20
import java.util.ArrayList;
21
import java.util.Collections;
21 22
import java.util.Iterator;
22 23
import java.util.List;
23 24
import java.util.Set;
......
42 43
import eu.etaxonomy.cdm.model.taxon.Synonym;
43 44
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
44 45
import eu.etaxonomy.cdm.model.taxon.Taxon;
46
import eu.etaxonomy.cdm.model.taxon.TaxonNaturalComparator;
45 47
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
46 48
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
47 49
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
......
529 531
	    System.out.println(nodes.size());
530 532

  
531 533
	}
534
	
535
    @Test
536
    public void testCompareNaturalOrder() {
537
    	/*
538
    	 * Classification
539
    	 *  * Abies
540
    	 *  `- Abies alba
541
    	 *  * Pinus
542
    	 *  `- Pinus pampa
543
    	 */
544
    	Classification classification = Classification.NewInstance("Classification");
545
    	BotanicalName abiesName = BotanicalName.NewInstance(Rank.GENUS());
546
    	abiesName.setGenusOrUninomial("Abies");
547
    	Taxon abies = Taxon.NewInstance(abiesName, null);
548
    	BotanicalName abiesAlbaName = BotanicalName.NewInstance(Rank.SPECIES());
549
    	abiesAlbaName.setGenusOrUninomial("Abies");
550
    	abiesAlbaName.setSpecificEpithet("alba");
551
    	Taxon abiesAlba = Taxon.NewInstance(abiesAlbaName, null);
552
    	BotanicalName pinusName = BotanicalName.NewInstance(Rank.GENUS());
553
    	pinusName.setGenusOrUninomial("Pinus");
554
    	Taxon pinus = Taxon.NewInstance(pinusName, null);
555
    	BotanicalName pinusPampaName = BotanicalName.NewInstance(Rank.SPECIES());
556
    	pinusPampaName.setGenusOrUninomial("Pinus");
557
    	pinusPampaName.setSpecificEpithet("pampa");
558
    	Taxon pinusPampa = Taxon.NewInstance(pinusPampaName, null);
559
    	
560
    	classification.addChildTaxon(abies, null, null);
561
    	classification.addParentChild(abies, abiesAlba, null, null);
562
    	classification.addChildTaxon(pinus, null, null);
563
    	classification.addParentChild(pinus, pinusPampa, null, null);
564
    	classificationService.save(classification);
565
    	
566
    	TaxonNaturalComparator comparator = new TaxonNaturalComparator();
567
    	List<TaxonNode> allNodes = new ArrayList<>(classification.getAllNodes());
568
    	Collections.sort(allNodes, comparator);
569
    	//TODO implement assertion for correct order
570
    }
532 571

  
533 572
    /* (non-Javadoc)
534 573
     * @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)