Project

General

Profile

Download (3.03 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2018 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix;
11

    
12
import java.util.Comparator;
13
import java.util.HashMap;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.UUID;
17

    
18
import ca.odell.glazedlists.TreeList;
19
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
20
import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
21
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
22
import eu.etaxonomy.cdm.model.name.Rank;
23
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
24
import eu.etaxonomy.taxeditor.store.CdmStore;
25

    
26
/**
27
 * The tree format defines how to build the hierarchy path for the
28
 * taxon column in the character matrix
29
 *
30
 * @author pplitzner
31
 * @date 23.01.2018
32
 *
33
 */
34
 public class DescriptionTreeFormat implements TreeList.Format<Object> {
35

    
36
     private Rank maxRank;
37

    
38
     private Map<UUID, TaxonNodeDto> uuidToTaxonNodeDtoMap = new HashMap<>();
39

    
40
     public DescriptionTreeFormat(DescriptiveDataSet descriptiveDataSet) {
41
         this.maxRank = descriptiveDataSet.getMaxRank();
42
     }
43

    
44
     @Override
45
     public void getPath(List path, Object element) {
46
         if(element instanceof RowWrapperDTO){
47
             //TODO: check for multiple taxon nodes in multiple classifications
48
             TaxonNodeDto taxonNode = ((RowWrapperDTO) element).getTaxonNode();
49
             //use dto from map cache because the elements have to be
50
             //exactly the same for the tree format to work
51
             if(uuidToTaxonNodeDtoMap.get(taxonNode.getUuid())!=null){
52
                 taxonNode = uuidToTaxonNodeDtoMap.get(taxonNode.getUuid());
53
             }
54
             else{
55
                 uuidToTaxonNodeDtoMap.put(taxonNode.getUuid(), taxonNode);
56
             }
57
             addPathRecursive(path, taxonNode);
58
         }
59
         path.add(element);
60
     }
61

    
62
     private void addPathRecursive(List path, TaxonNodeDto node){
63
         if(node!=null){
64
             if(maxRank!=null && node.getRankOrderIndex()==maxRank.getOrderIndex()){
65
                 path.add(node);
66
                 return;
67
             }
68
             TaxonNodeDto parentNodeDto = uuidToTaxonNodeDtoMap.get(node.getParentUUID());
69
             if(parentNodeDto==null){
70
                 parentNodeDto = CdmStore.getService(ITaxonNodeService.class).parentDto(node.getUuid());
71
                 if(parentNodeDto!=null){
72
                     uuidToTaxonNodeDtoMap.put(node.getParentUUID(), parentNodeDto);
73
                 }
74
             }
75
             if(parentNodeDto!=null){
76
                 addPathRecursive(path, parentNodeDto);
77
                 path.add(node);
78
             }
79
         }
80
     }
81

    
82
     @Override
83
     public boolean allowsChildren(Object element) {
84
         return true;
85
     }
86

    
87
     @Override
88
     public Comparator<Object> getComparator(int depth) {
89
         return new MatrixRowComparator();
90
     }
91

    
92
 }
(16-16/23)