Project

General

Profile

Download (5.59 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 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.preference.wizard;
10

    
11

    
12
import java.util.ArrayList;
13
import java.util.Arrays;
14
import java.util.List;
15
import java.util.UUID;
16

    
17
import org.apache.commons.lang.StringUtils;
18
import org.eclipse.jface.viewers.CheckboxTreeViewer;
19
import org.eclipse.jface.wizard.WizardPage;
20
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.layout.GridData;
22
import org.eclipse.swt.layout.GridLayout;
23
import org.eclipse.swt.widgets.Composite;
24

    
25
import eu.etaxonomy.cdm.api.service.ITermService;
26
import eu.etaxonomy.cdm.api.service.IVocabularyService;
27
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
28
import eu.etaxonomy.cdm.model.term.TermType;
29
import eu.etaxonomy.cdm.model.term.TermVocabulary;
30
import eu.etaxonomy.cdm.persistence.dto.AbstractTermDto;
31
import eu.etaxonomy.cdm.persistence.dto.TermDto;
32
import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
33
import eu.etaxonomy.taxeditor.editor.definedterm.DefinedTermSorter;
34
import eu.etaxonomy.taxeditor.editor.definedterm.TermDtoContentProvider;
35
import eu.etaxonomy.taxeditor.editor.definedterm.TermDtoLabelProvider;
36
import eu.etaxonomy.taxeditor.store.CdmStore;
37

    
38
/**
39
 * @author k.luther
40
 * @since 04.06.2018
41
 *
42
 */
43
public abstract class AbstractTermSelectionWizardPage extends WizardPage {
44

    
45
    protected CheckBoxTreeComposite treeComposite;
46
    private List<TermVocabularyDto> vocabularies = new ArrayList<>();
47
    boolean localPref;
48

    
49
    protected TermType type;
50

    
51
    protected AbstractTermSelectionWizardPage(String pageName, TermType type) {
52
        super(pageName);
53
        // TODO check if configuration exists
54
       // CdmStore.getCurrentSessionManager().bindNullSession();
55
        this.type = type;
56

    
57
    }
58

    
59
    @Override
60
    public void createControl(Composite parent){
61
        parent.setLayout(new GridLayout());
62
        treeComposite = new CheckBoxTreeComposite(parent, new TermDtoContentProvider(), new TermDtoLabelProvider(), SWT.NONE);
63
        treeComposite.getViewer().setComparator(new DefinedTermSorter());
64
        rememberCheckedValues(getCheckedValuesFromPreferences());
65
        treeComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
66
        setControl(treeComposite);
67
    }
68

    
69
    protected abstract String getCheckedValuesFromPreferences();
70

    
71
    protected CheckboxTreeViewer getViewer(){
72
        return treeComposite.getViewer();
73
    }
74

    
75
    public List<TermVocabularyDto> getVocabularies() {
76
        return vocabularies;
77
    }
78

    
79
    public void addVocabularies(TermVocabularyDto vocabulary) {
80
        this.vocabularies.add(vocabulary);
81
    }
82

    
83
    protected void setVocabularies(List<TermVocabularyDto> vocs) {
84
        vocabularies = vocs;
85
    }
86

    
87
    protected void rememberCheckedValues(String checkedValues) {
88
        initialiseVocabularies();
89

    
90
        treeComposite.getViewer().setInput(getVocabularies());
91

    
92
        if (checkedValues != null && checkedValues != "") { //$NON-NLS-1$
93
            String[] listChecked = checkedValues.split(";"); //$NON-NLS-1$
94
            String[] listCheckedComma = checkedValues.split(","); //$NON-NLS-1$
95
            List<String> checked = new ArrayList<>();
96
            if (listChecked != null ){
97
                checked = Arrays.asList(listChecked);
98
            }
99
            if (listCheckedComma != null && checkedValues.contains(",")){ //$NON-NLS-1$
100
                checked = Arrays.asList(listCheckedComma);
101
            }
102
            List<AbstractTermDto> termsFromStringValues = getTermsFromStringValues(checked);
103
            treeComposite.setCheckedElements(termsFromStringValues.toArray());
104

    
105
        }else{
106
            List<AbstractTermDto> termsFromStringValues = getTermsFromStringValues(new ArrayList<String>());
107
            treeComposite.setCheckedElements(termsFromStringValues.toArray());
108
        }
109
    }
110

    
111
    protected List<AbstractTermDto> getTermsFromStringValues(List<String> listValue) {
112
        List<AbstractTermDto> termlist = new ArrayList<>();
113
        ITermService termService = CdmStore.getService(ITermService.class);
114
        for (String s : listValue) {
115
            if (!StringUtils.isBlank(s)){
116
                UUID uuid = UUID.fromString(s);
117

    
118
                DefinedTermBase definedTermBase = termService.load(uuid);
119
                if(definedTermBase != null){
120
                    termlist.add(TermDto.fromTerm(definedTermBase, true));
121
                }else{
122
                    IVocabularyService vocabularyService = CdmStore.getService(IVocabularyService.class);
123
                    TermVocabulary termVocabulary = vocabularyService.load(uuid);
124
                    if (termVocabulary != null){
125
                        termlist.add(new TermVocabularyDto(uuid, termVocabulary.getRepresentations(), termVocabulary.getTermType()));
126
                    }
127
                }
128
            }
129
        }
130
        if (listValue.isEmpty()){
131
            List<DefinedTermBase> terms = CdmStore.getTermManager().getAllTerms(type, null);
132
            for (DefinedTermBase term: terms){
133
                termlist.add(TermDto.fromTerm(term, true));
134
            }
135
        }
136

    
137
        return termlist;
138
    }
139

    
140
    protected void initialiseVocabularies() {
141
        if (getVocabularies() != null) {
142
            getVocabularies().clear();
143
        }
144
        List<TermVocabularyDto> vocs = new ArrayList<>();
145
        vocs = CdmStore.getService(IVocabularyService.class).findVocabularyDtoByTermType(type);
146

    
147
        setVocabularies(vocs);
148
    }
149

    
150
    protected abstract List<TermVocabularyDto> getVocabulariesFromPreference();
151

    
152

    
153
}
(2-2/16)