Project

General

Profile

Download (5.03 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
import java.util.ArrayList;
12
import java.util.Arrays;
13
import java.util.List;
14
import java.util.UUID;
15

    
16
import org.apache.commons.lang.StringUtils;
17
import org.eclipse.jface.viewers.ColumnViewer;
18
import org.eclipse.jface.wizard.WizardPage;
19
import org.eclipse.swt.widgets.Composite;
20

    
21
import eu.etaxonomy.cdm.api.service.ITermService;
22
import eu.etaxonomy.cdm.api.service.IVocabularyService;
23
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
24
import eu.etaxonomy.cdm.model.common.TermBase;
25
import eu.etaxonomy.cdm.model.common.TermType;
26
import eu.etaxonomy.cdm.model.common.TermVocabulary;
27
import eu.etaxonomy.taxeditor.store.CdmStore;
28

    
29
/**
30
 * @author k.luther
31
 * @since 04.06.2018
32
 *
33
 */
34
public abstract class AbstractTermSelectionWizardPage extends WizardPage {
35

    
36
    private ColumnViewer viewer;
37
    private List<TermVocabulary<DefinedTermBase>> vocabularies = new ArrayList<>();
38
    boolean localPref;
39
    List<TermBase> listCheckedTerms = new ArrayList<>();
40
    List<TermBase> listGrayedTerms = new ArrayList<>();
41

    
42
    TermType type;
43

    
44
    /**
45
     * @param pageName
46
     */
47
    protected AbstractTermSelectionWizardPage(String pageName, TermType type) {
48
        super(pageName);
49
        // TODO check if configuration exists
50
        CdmStore.getCurrentSessionManager().bindNullSession();
51
        this.type = type;
52
    }
53

    
54
    /**
55
     * {@inheritDoc}
56
     */
57
    @Override
58
    public abstract void createControl(Composite parent) ;
59

    
60
    public ColumnViewer getViewer() {
61
        return viewer;
62
    }
63

    
64
    public void setViewer(ColumnViewer viewer) {
65
        this.viewer = viewer;
66
    }
67

    
68
    public List<TermVocabulary<DefinedTermBase>> getVocabularies() {
69
        return vocabularies;
70
    }
71

    
72
    public void addVocabularies(TermVocabulary<DefinedTermBase> vocabulary) {
73
        this.vocabularies.add(vocabulary);
74
    }
75

    
76

    
77
    /**
78
     * @param vocs
79
     */
80
    protected void setVocabularies(List<TermVocabulary<DefinedTermBase>> vocs) {
81
        for (TermVocabulary voc:vocs){
82
            vocabularies.add(voc);
83
        }
84
    }
85

    
86
    public List<TermBase> getListCheckedTerms() {
87
        return listCheckedTerms;
88
    }
89

    
90
    public List<TermBase> getListGrayedTerms() {
91
        return listGrayedTerms;
92
    }
93

    
94
    protected void rememberCheckedValues(String checkedValues, String grayedValues) {
95
        initialiseVocabularies();
96
        getViewer().setInput(getVocabularies());
97

    
98
        if (grayedValues != null && grayedValues != "") {
99
            String[] arrayGrayed = grayedValues.split(";");
100
            List<String> listGrayed = Arrays.asList(arrayGrayed);
101
            if (listGrayedTerms == null){
102
                listGrayedTerms = new ArrayList();
103
            }
104
            getTermsFromStringValues(listGrayed, this.listGrayedTerms);
105

    
106
        }
107
        if (checkedValues != null && checkedValues != "") {
108
            String[] listChecked = checkedValues.split(";");
109
            String[] listCheckedComma = checkedValues.split(",");
110
            List<String> checked = new ArrayList<>();
111
            if (listChecked != null ){
112
                checked = Arrays.asList(listChecked);
113
            }
114
            if (listCheckedComma != null && checkedValues.contains(",")){
115
                checked = Arrays.asList(listCheckedComma);
116
            }
117
            if (listCheckedTerms == null){
118
                listCheckedTerms = new ArrayList();
119
            }
120
            getTermsFromStringValues(checked, listCheckedTerms);
121

    
122
        }
123
    }
124

    
125
    /**
126
     * @param split
127
     * @param termlist
128
     */
129
    private void getTermsFromStringValues(List<String> listValue, List<TermBase> termlist) {
130

    
131
        for (String s : listValue) {
132
            if (!StringUtils.isBlank(s)){
133
                UUID uuid = UUID.fromString(s);
134
                ITermService termService = CdmStore.getService(ITermService.class);
135
                DefinedTermBase definedTermBase = termService.load(uuid);
136
                if(definedTermBase != null){
137
                    termlist.add(definedTermBase);
138
                }else{
139
                    IVocabularyService vocabularyService = CdmStore.getService(IVocabularyService.class);
140
                    TermVocabulary termVocabulary = vocabularyService.load(uuid);
141
                    termlist.add(termVocabulary);
142
                }
143
            }
144
        }
145
    }
146

    
147

    
148
    protected void initialiseVocabularies() {
149
        if (getVocabularies() != null) {
150
            getVocabularies().clear();
151
        }
152
        List<TermVocabulary<DefinedTermBase>> vocs = new ArrayList<>();
153
        if (localPref){
154
            vocs = getVocabulariesFromPreference();
155

    
156
        }else{
157
            vocs = CdmStore.getService(IVocabularyService.class).findByTermType(
158
                    type, null);
159
        }
160
        setVocabularies(vocs);
161
    }
162

    
163
    protected abstract List<TermVocabulary<DefinedTermBase>> getVocabulariesFromPreference();
164

    
165

    
166
}
(1-1/13)