Project

General

Profile

Download (10.9 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 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

    
10
package eu.etaxonomy.taxeditor.editor.view.checklist;
11

    
12
import java.util.ArrayList;
13
import java.util.Arrays;
14
import java.util.HashMap;
15
import java.util.HashSet;
16
import java.util.List;
17
import java.util.Set;
18
import java.util.SortedSet;
19
import java.util.TreeSet;
20
import java.util.UUID;
21

    
22
import org.apache.commons.lang.StringUtils;
23
import org.eclipse.jface.viewers.ITableLabelProvider;
24
import org.eclipse.jface.viewers.LabelProvider;
25
import org.eclipse.swt.graphics.Image;
26

    
27
import eu.etaxonomy.cdm.api.service.IDescriptionService;
28
import eu.etaxonomy.cdm.api.service.IVocabularyService;
29
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
30
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
31
import eu.etaxonomy.cdm.model.common.Language;
32
import eu.etaxonomy.cdm.model.common.OrderedTermBase;
33
import eu.etaxonomy.cdm.model.common.OrderedTermComparator;
34
import eu.etaxonomy.cdm.model.common.Representation;
35
import eu.etaxonomy.cdm.model.common.TermIdInVocabularyComparator;
36
import eu.etaxonomy.cdm.model.common.TermLanguageComparator;
37
import eu.etaxonomy.cdm.model.common.TermVocabulary;
38
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
39
import eu.etaxonomy.cdm.model.description.Distribution;
40
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
41
import eu.etaxonomy.cdm.model.description.TaxonDescription;
42
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
43
import eu.etaxonomy.cdm.model.name.TaxonName;
44
import eu.etaxonomy.cdm.model.taxon.Taxon;
45
import eu.etaxonomy.taxeditor.editor.view.checklist.e4.ChecklistEditorE4;
46
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
47
import eu.etaxonomy.taxeditor.preference.TermDisplayEnum;
48
import eu.etaxonomy.taxeditor.store.CdmStore;
49

    
50
/**
51
 *
52
 * @author a.oppermann
53
 * @created 30.04.2014
54
 */
55
public class ChecklistLabelProvider extends LabelProvider implements ITableLabelProvider {
56

    
57
    public static final String DEFAULT_ENTRY = ""; //$NON-NLS-1$
58
    private IDescriptionService descriptionService;
59
    private SortedSet<DefinedTermBase> namedAreas;
60
    private ChecklistEditorE4 editor= null;
61

    
62
    /**
63
     *
64
     */
65
    public ChecklistLabelProvider() {
66
        namedAreas = loadNamedAreas();
67
    }
68

    
69
    public ChecklistLabelProvider(ChecklistEditorE4 editor) {
70
        namedAreas = loadNamedAreas();
71
        this.editor = editor;
72
    }
73

    
74
    /*
75
     * (non-Javadoc)
76
     *
77
     * @see
78
     * org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang
79
     * .Object, int)
80
     */
81
    /** {@inheritDoc} */
82
    @Override
83
    public Image getColumnImage(Object element, int columnIndex) {
84
        // TODO
85
        return null;
86
    }
87

    
88
    /** {@inheritDoc} */
89
	@Override
90
    public String getColumnText(Object element, int columnIndex) {
91
       // descriptionService = CdmStore.getService(IDescriptionService.class);
92
        Taxon taxon = (Taxon) element;
93

    
94
        Set<TaxonDescription> listTaxonDescriptions = taxon.getDescriptions();
95
        TaxonName nonViralName = HibernateProxyHelper.deproxy(taxon.getName());
96

    
97
        switch (columnIndex) {
98
        case 0:
99
            String taxonName = null;
100
            taxonName = (nonViralName != null) ? nonViralName.getNameCache() : null;
101
            return (taxonName != null) ? taxonName : DEFAULT_ENTRY;
102

    
103
        case 1:
104
            if (PreferencesUtil.isShowRankInChecklistEditor()){
105
                String rank = null;
106

    
107
                if (taxon.getName().getRank() != null) {
108
                    rank = taxon.getName().getRank().toString();
109
                }
110
                return (rank != null) ? rank : DEFAULT_ENTRY;
111
            }
112
        }
113

    
114
        if(columnIndex >1 || (!PreferencesUtil.isShowRankInChecklistEditor() && columnIndex > 0)){
115

    
116
            for (TaxonDescription td : listTaxonDescriptions) {
117
                for (DescriptionElementBase deb : td.getElements()) {
118
                    if (deb instanceof Distribution) {
119
                        Distribution distribution = HibernateProxyHelper.deproxy(deb, Distribution.class);
120
                        if (distribution.getArea() != null){
121
                            if ( editor.getAreaPosition().get(distribution.getArea().getUuid()) != null && columnIndex == editor.getAreaPosition().get(distribution.getArea().getUuid())){
122
                                if (distribution.getStatus() == null){
123
                                    return DEFAULT_ENTRY;
124
                                }
125
                                PresenceAbsenceTerm term = distribution.getStatus();
126
                                Representation rep = distribution.getStatus().getPreferredRepresentation(CdmStore.getDefaultLanguage());
127
                                String label = rep.getLabel();
128

    
129
                                if (label == null){
130
                                    label = distribution.getStatus().getTitleCache();
131
                                }
132
                                if (PreferencesUtil.displayStatusInChecklistEditor() != null){
133
                                    if(PreferencesUtil.displayStatusInChecklistEditor().equals(TermDisplayEnum.IdInVocabulary.getKey())){
134
                                        return (StringUtils.isNotBlank(distribution.getStatus().getIdInVocabulary()))?distribution.getStatus().getIdInVocabulary():label;
135
                                    }else if(PreferencesUtil.displayStatusInChecklistEditor().equals(TermDisplayEnum.Symbol1.getKey())){
136
                                        System.err.println(columnIndex + " " +label);
137
                                        return (StringUtils.isNotBlank(distribution.getStatus().getSymbol() ))?distribution.getStatus().getSymbol():label;
138
                                    }else if(PreferencesUtil.displayStatusInChecklistEditor().equals(TermDisplayEnum.Symbol2.getKey())){
139
                                        return (StringUtils.isNotBlank(distribution.getStatus().getSymbol2() ))?distribution.getStatus().getSymbol2():label;
140
                                    }else{
141
                                        return (label != null)?label:DEFAULT_ENTRY;
142
                                    }
143

    
144
                                }else{
145
                                    return (label != null)?label:DEFAULT_ENTRY;
146
                                }
147
                            }
148
                        }
149
                    }
150
                }
151
            }
152
        }
153
        return DEFAULT_ENTRY;
154

    
155
    }
156

    
157
    private static final List<String> DESC_INIT_STRATEGY = Arrays.asList(new String[] { "descriptions", //$NON-NLS-1$
158
            "descriptions.*", "description.state" }); //$NON-NLS-1$ //$NON-NLS-2$
159

    
160

    
161
    private SortedSet<DefinedTermBase> loadNamedAreas() {
162
        //IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
163

    
164
        String valuesAreas = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaTerms.getKey());
165
        String values = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey());
166
        if (values != null && values != "") { //$NON-NLS-1$
167
            String[] split = values.split(";"); //$NON-NLS-1$
168
            List<String> listValue = Arrays.asList(split);
169
            List<DefinedTermBase> termlist = new ArrayList<DefinedTermBase>();
170
            Set<UUID> uuidList = new HashSet<UUID>();
171
            UUID uuid;
172
            for(String s : listValue){
173
                uuid = UUID.fromString(s);
174
                uuidList.add(uuid);
175

    
176
            }
177
            IVocabularyService service =  CdmStore.getService(IVocabularyService.class);
178
            List<TermVocabulary> vocs = service.find(uuidList);
179
            split = valuesAreas.split(";");
180
            listValue = Arrays.asList(split);
181
            for (TermVocabulary voc: vocs){
182
                termlist.addAll(service.getTerms(voc, null, null, null, null).getRecords());
183
            }
184
            List<DefinedTermBase> filteredList = new ArrayList();
185
            for (DefinedTermBase area: termlist){
186
                if (listValue.contains(area.getUuid().toString())) {
187
                    filteredList.add(area);
188
                }
189

    
190
            }
191

    
192
            if (PreferencesUtil.isSortNamedAreaByOrderInVocabulary()){
193
                return getTermsOrderedByVocabularyOrder(filteredList);
194
            } else if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
195
                return getTermsOrderedByIdInVocabulary(filteredList);
196
            }else{
197
                return getTermsOrderedByLabels(filteredList, CdmStore.getDefaultLanguage());
198
            }
199
        }
200
        return null;
201
    }
202

    
203
    public SortedSet<DefinedTermBase> getTermsOrderedByLabels(List<DefinedTermBase> listTerm,Language language){
204
        TermLanguageComparator<?> comp = new TermLanguageComparator<>();
205
        comp.setCompareLanguage(language);
206
        SortedSet<DefinedTermBase> result = new TreeSet(comp);
207
        if(listTerm != null){
208
            result.addAll(listTerm);
209
        }
210
        return result;
211
    }
212

    
213
    public SortedSet<DefinedTermBase> getTermsOrderedByVocabularyOrder(List<DefinedTermBase> listAreas){
214
        HashMap<TermVocabulary<DefinedTermBase>, List<DefinedTermBase>> vocs = new HashMap<>();
215
        OrderedTermComparator<?> comp = new OrderedTermComparator<>();
216
        boolean allOrderedTerms = true;
217
        List<TermVocabulary> alreadyOrderIndexNull = new ArrayList<>();
218
        for (DefinedTermBase term: listAreas){
219
            if (!(term instanceof OrderedTermBase)){
220
                allOrderedTerms = false;
221
                break;
222
            }else if (((OrderedTermBase)term).getOrderIndex() == 0){
223
                if(alreadyOrderIndexNull.contains(term.getVocabulary())) {
224
                    allOrderedTerms = false;
225
                    break;
226
                }else{
227
                    alreadyOrderIndexNull.add(term.getVocabulary());
228
                }
229

    
230

    
231
            }
232
        }
233
        if (allOrderedTerms){
234
            SortedSet<DefinedTermBase> result = new TreeSet(comp.reversed());
235
            result.addAll(listAreas);
236
            return result;
237
        }else{
238
            return getTermsOrderedByLabels(listAreas, PreferencesUtil.getGlobalLanguage());
239
        }
240

    
241

    
242
    }
243

    
244
    /**
245
     * @return the namedAreas
246
     */
247
    public SortedSet<DefinedTermBase> getNamedAreas(boolean load) {
248
        if (load){
249
            namedAreas = loadNamedAreas();
250
        }
251
        return namedAreas;
252
    }
253

    
254
    /**
255
     * @param namedAreas
256
     * @param defaultLanguage
257
     * @return
258
     */
259
    public SortedSet<DefinedTermBase> getTermsOrderedByIdInVocabulary(List<DefinedTermBase> namedAreas) {
260
        TermIdInVocabularyComparator<?> comp = new TermIdInVocabularyComparator<>();
261

    
262
        SortedSet<DefinedTermBase> result = new TreeSet(comp);
263
        if(namedAreas != null){
264
            result.addAll(namedAreas);
265
        }
266
        return result;
267
    }
268
}
(4-4/4)