Project

General

Profile

Download (3.17 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
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.
8
*/
9
package eu.etaxonomy.taxeditor.editor.definedterm;
10

    
11
import java.util.ArrayList;
12
import java.util.Collection;
13
import java.util.HashMap;
14
import java.util.HashSet;
15
import java.util.Map;
16

    
17
import org.eclipse.jface.viewers.TreeNodeContentProvider;
18

    
19
import eu.etaxonomy.cdm.api.service.IVocabularyService;
20
import eu.etaxonomy.cdm.persistence.dto.TermDto;
21
import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
22
import eu.etaxonomy.taxeditor.store.CdmStore;
23

    
24
/**
25
 *
26
 * @author pplitzner
27
 * @since Oct 29, 2018
28
 *
29
 */
30
public class TermDtoContentProvider extends TreeNodeContentProvider {
31

    
32
    private Map<TermVocabularyDto, Collection<TermDto>> vocabularyToChildTermMap = new HashMap<>();
33

    
34
    @Override
35
	public Object[] getElements(Object inputElement) {
36
		Collection<TermVocabularyDto> inputElements = (Collection<TermVocabularyDto>) inputElement;
37
		return inputElements.toArray();
38
	}
39

    
40
	@Override
41
	public Object[] getChildren(Object parentElement) {
42
	    Collection<TermDto> children = new HashSet<>();
43
	    if(parentElement instanceof TermVocabularyDto){
44
	        children.addAll(getChildTerms((TermVocabularyDto)parentElement));
45
	    } else if(parentElement instanceof TermDto){
46
	            if(((TermDto) parentElement).getIncludes()!=null){
47
	                children.addAll(((TermDto) parentElement).getIncludes());
48
	            }
49
                if(((TermDto) parentElement).getGeneralizationOf()!=null){
50
                    children.addAll(((TermDto) parentElement).getGeneralizationOf());
51
                }
52
	    }
53
	    return children.toArray();
54
	}
55

    
56
	@Override
57
	public Object getParent(Object element) {
58
		if(element instanceof TermDto){
59
		    TermDto termDto = (TermDto) element;
60
		    TermDto partOfDto = termDto.getPartOfDto();
61
		    if(partOfDto!=null){
62
		        return partOfDto;
63
		    }
64
		    TermDto kindOfDto = termDto.getKindOfDto();
65
		    if(kindOfDto!=null){
66
		        return kindOfDto;
67
		    }
68
		    TermVocabularyDto vocabularyDto = termDto.getVocabularyDto();
69
		    if(vocabularyDto!=null){
70
		        return vocabularyDto;
71
		    }
72
		    //parent is the vocabulary
73
		    return new TermVocabularyDto(termDto.getVocabularyUuid(), null, termDto.getTermType());
74
		}
75
		return null;
76

    
77
	}
78

    
79
	@Override
80
	public boolean hasChildren(Object element) {
81
	    if(element instanceof TermVocabularyDto){
82
	        return getChildren(element).length > 0;
83
	    }
84
		if (getChildren(element) != null){
85
			return getChildren(element).length > 0;
86
		}
87
		return false;
88
	}
89

    
90
    public Collection<? extends TermDto> getChildTerms(TermVocabularyDto voc) {
91
        Collection<TermDto> children = vocabularyToChildTermMap.get(voc);
92
        if(children==null){
93
            children = new ArrayList<>(CdmStore.getService(IVocabularyService.class).getCompleteTermHierarchy(voc));
94
            vocabularyToChildTermMap.put(voc, children);
95
        }
96
        return children;
97
    }
98

    
99
    public void removeVocabularyFromCache(TermVocabularyDto voc){
100
        vocabularyToChildTermMap.remove(voc);
101
    }
102

    
103
}
(4-4/6)