Project

General

Profile

Download (6.33 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2014 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.HashMap;
14
import java.util.HashSet;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.Set;
18
import java.util.UUID;
19

    
20
import org.apache.commons.lang.StringUtils;
21
import org.eclipse.swt.widgets.Composite;
22

    
23
import eu.etaxonomy.cdm.api.service.ITermService;
24
import eu.etaxonomy.cdm.api.service.IVocabularyService;
25
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
26
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
27
import eu.etaxonomy.cdm.model.term.TermType;
28
import eu.etaxonomy.cdm.model.term.TermVocabulary;
29
import eu.etaxonomy.cdm.persistence.dto.TermDto;
30
import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
31
import eu.etaxonomy.taxeditor.l10n.Messages;
32
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
33
import eu.etaxonomy.taxeditor.store.CdmStore;
34

    
35
/**
36
 * @author a.oppermann
37
 * @date 21.07.2014
38
 *
39
 */
40
public class AvailableDistributionPage extends AbstractTermSelectionWizardPage<TermDto> {
41

    
42
    public AvailableDistributionPage(String pageName) {
43
        super(pageName, TermType.NamedArea);
44
        this.localPref = true;
45

    
46

    
47
    }
48

    
49
    @Override
50
    public void createControl(Composite parent) {
51
        setTitle(Messages.AvailableDistributionPage_PAGE_TITLE);
52
        setDescription(Messages.AvailableDistributionPage_PAGE_DESCRIPTION);
53

    
54
        super.createControl(parent);
55
    }
56

    
57
    @Override
58
    protected String getCheckedValuesFromPreferences() {
59
        String checkedValues = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaTerms.getKey(), true);
60

    
61
        return checkedValues;
62
    }
63

    
64
    @Override
65
    protected void rememberCheckedValues(String checkedValues) {
66
        initialiseVocabularies();
67

    
68
        treeComposite.getViewer().setInput(getVocabularies());
69

    
70
        if (checkedValues != null && checkedValues != "") { //$NON-NLS-1$
71
            String[] listChecked = checkedValues.split(";"); //$NON-NLS-1$
72
            String[] listCheckedComma = checkedValues.split(","); //$NON-NLS-1$
73
            List<String> checked = new ArrayList<>();
74
            if (listChecked != null ){
75
                checked = Arrays.asList(listChecked);
76
            }
77
            if (listCheckedComma != null && checkedValues.contains(",")){ //$NON-NLS-1$
78
                checked = Arrays.asList(listCheckedComma);
79
            }
80
            List<TermDto> termsFromStringValues = getTermsFromStringValues(checked);
81
            treeComposite.setCheckedElements(termsFromStringValues.toArray());
82
        }
83

    
84
    }
85

    
86
    @Override
87
    public void dispose() {
88
       // CdmStore.getCurrentSessionManager().dispose(this);
89
        super.dispose();
90
    }
91

    
92
//    @Override
93
//    public ICdmEntitySession getCdmEntitySession() {
94
//        return CdmStore.getCurrentSessionManager().getNullSession();
95
//    }
96
//
97
//    @Override
98
//    public <T extends CdmBase> Collection<T> getRootEntities() {
99
//        return null;
100
//    }
101
//
102
//    @Override
103
    public Map<Object, List<String>> getPropertyPathsMap() {
104
        Map<Object, List<String>> propertyPathsMap = new HashMap<Object, List<String>>();
105
        List<String> termsPropertyPaths = Arrays.asList(new String[] {
106
                "includes" //$NON-NLS-1$
107
        });
108
        propertyPathsMap.put("includes", termsPropertyPaths); //$NON-NLS-1$
109
        propertyPathsMap.put("terms", termsPropertyPaths); //$NON-NLS-1$
110
        return propertyPathsMap;
111
    }
112

    
113
    @Override
114
    protected  List<TermVocabularyDto> getVocabulariesFromPreference(){
115
        List<TermVocabularyDto> vocs = new ArrayList<>();
116
        String test = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey(), true);
117
        if (PreferencesUtil.getPreferenceFromDB(PreferencePredicate.AvailableDistributionAreaVocabularies) == null && PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey()) == null){
118
            vocs = CdmStore.getService(IVocabularyService.class).findVocabularyDtoByTermType(type);
119
        }else{
120
            String vocString = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey());
121
            if (vocString.equals("")){
122
                vocs = CdmStore.getService(IVocabularyService.class).findVocabularyDtoByTermType(type);
123
                return vocs;
124
            }
125
            String[] arrayVocs = vocString.split(";"); //$NON-NLS-1$
126

    
127
            Set<UUID> uuidVocs = new HashSet<>();
128
            for (String voc: arrayVocs){
129
                if (!StringUtils.isBlank(voc)){
130
                    uuidVocs.add(UUID.fromString(voc));
131
                }
132
            }
133
            List<TermVocabulary> tempVocs = CdmStore.getService(IVocabularyService.class).find(uuidVocs);
134
            for (TermVocabulary voc: tempVocs){
135
                vocs.add(new TermVocabularyDto(voc.getUuid(), voc.getRepresentations(), voc.getTermType()));
136
            }
137

    
138
        }
139
        return vocs;
140
    }
141
    @Override
142
    protected void initialiseVocabularies() {
143
        if (getVocabularies() != null) {
144
            getVocabularies().clear();
145
        }
146
        List<TermVocabularyDto> vocs = new ArrayList<>();
147
        vocs = getVocabulariesFromPreference();
148

    
149
        setVocabularies(vocs);
150
    }
151

    
152
    @Override
153
    protected List<TermDto> getTermsFromStringValues(List<String> listValue) {
154

    
155
            List<TermDto> termlist = new ArrayList<>();
156
            ITermService termService = CdmStore.getService(ITermService.class);
157
            List<UUID> uuidList = new ArrayList();
158
            for (String s : listValue) {
159
                if (!StringUtils.isBlank(s)){
160
                    UUID uuid = UUID.fromString(s);
161
                    uuidList.add(uuid);
162
                }
163
            }
164
            termlist = new ArrayList(termService.findByUUIDsAsDto(uuidList));
165

    
166

    
167
            if (listValue.isEmpty()){
168
                List<DefinedTermBase> terms = CdmStore.getTermManager().getAllTerms(type, null);
169
                for (DefinedTermBase term: terms){
170
                    termlist.add(TermDto.fromTerm(term, true));
171
                }
172
            }
173

    
174
            return termlist;
175

    
176

    
177
    }
178

    
179
}
(5-5/18)