Revert "ref #7589 Removed unnecessary sorting"
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / descriptiveDataSet / matrix / DescriptionTreeFormat.java
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.model.taxon.TaxonNode;
24 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
25 import eu.etaxonomy.taxeditor.store.CdmStore;
26
27 /**
28 * The tree format defines how to build the hierarchy path for the
29 * taxon column in the character matrix
30 *
31 * @author pplitzner
32 * @date 23.01.2018
33 *
34 */
35 public class DescriptionTreeFormat implements TreeList.Format<Object> {
36
37 private Rank maxRank;
38
39 private Map<UUID, TaxonNodeDto> uuidToTaxonNodeDtoMap = new HashMap<>();
40
41 public DescriptionTreeFormat(DescriptiveDataSet descriptiveDataSet) {
42 this.maxRank = descriptiveDataSet.getMaxRank();
43 }
44
45 @Override
46 public void getPath(List path, Object element) {
47 if(element instanceof RowWrapperDTO){
48 //TODO: check for multiple taxon nodes in multiple classifications
49 TaxonNodeDto taxonNode = ((RowWrapperDTO) element).getTaxonNode();
50 //use dto from map cache because the elements have to be
51 //exactly the same for the tree format to work
52 if(uuidToTaxonNodeDtoMap.get(taxonNode.getUuid())!=null){
53 taxonNode = uuidToTaxonNodeDtoMap.get(taxonNode.getUuid());
54 }
55 else{
56 uuidToTaxonNodeDtoMap.put(taxonNode.getUuid(), taxonNode);
57 }
58 addPathRecursive(path, taxonNode);
59 }
60 path.add(element);
61 }
62
63 private void addPathRecursive(List path, TaxonNodeDto node){
64 if(node!=null){
65 if(maxRank!=null && node.getRankOrderIndex()==maxRank.getOrderIndex()){
66 path.add(node);
67 return;
68 }
69 TaxonNodeDto parentNodeDto = uuidToTaxonNodeDtoMap.get(node.getParentUUID());
70 if(parentNodeDto==null){
71 TaxonNode parent = CdmStore.getService(ITaxonNodeService.class).load(node.getParentUUID());
72 if(parent!=null){
73 parentNodeDto = new TaxonNodeDto(parent);
74 uuidToTaxonNodeDtoMap.put(node.getParentUUID(), parentNodeDto);
75 }
76 }
77 if(parentNodeDto!=null){
78 addPathRecursive(path, parentNodeDto);
79 path.add(node);
80 }
81 }
82 }
83
84 @Override
85 public boolean allowsChildren(Object element) {
86 return true;
87 }
88
89 @Override
90 public Comparator<Object> getComparator(int depth) {
91 return new MatrixRowComparator();
92 }
93
94 }