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.editor.view.checklist.e4;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.HashMap;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
import java.util.Set;
|
17
|
|
18
|
import javax.inject.Inject;
|
19
|
|
20
|
import org.eclipse.e4.core.services.events.IEventBroker;
|
21
|
import org.eclipse.nebula.widgets.nattable.data.IColumnPropertyAccessor;
|
22
|
|
23
|
import eu.etaxonomy.cdm.api.service.dto.TaxonDistributionDTO;
|
24
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
25
|
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
|
26
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
27
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
28
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
29
|
import eu.etaxonomy.taxeditor.event.EventUtility;
|
30
|
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
|
31
|
|
32
|
|
33
|
/**
|
34
|
* @author k.luther
|
35
|
* @since 28.11.2018
|
36
|
*
|
37
|
*/
|
38
|
public class DistributionColumnAccessor implements IColumnPropertyAccessor<TaxonDistributionDTO> {
|
39
|
private DistributionEditor editor;
|
40
|
public static final String DEFAULT_ENTRY = "";
|
41
|
@Inject
|
42
|
private IEventBroker eventBroker;
|
43
|
|
44
|
public DistributionColumnAccessor(DistributionEditor editor) {
|
45
|
this.editor = editor;
|
46
|
}
|
47
|
|
48
|
|
49
|
/**
|
50
|
* {@inheritDoc}
|
51
|
*/
|
52
|
@Override
|
53
|
public Object getDataValue(TaxonDistributionDTO rowObject, int columnIndex) {
|
54
|
switch (columnIndex) {
|
55
|
case 0:
|
56
|
return rowObject.getNameCache();
|
57
|
case 1:
|
58
|
if (editor.isShowRank()){
|
59
|
return rowObject.getRankString();
|
60
|
}else{
|
61
|
return rowObject.getConcatenatedSynonyms();
|
62
|
}
|
63
|
case 2:
|
64
|
if (editor.isShowRank()){
|
65
|
return rowObject.getConcatenatedSynonyms();
|
66
|
}else{
|
67
|
break;
|
68
|
}
|
69
|
default:
|
70
|
break;
|
71
|
}
|
72
|
NamedArea area = editor.getAreaToColumnIndexMap().get(columnIndex);
|
73
|
|
74
|
//TODO: do not get the distribution objcts, but the label of the status.
|
75
|
Map<NamedArea, Set<DescriptionElementBase>> distributionMap = editor.taxonDistributionMap.get(rowObject.getTaxonUuid());
|
76
|
if (distributionMap != null){
|
77
|
Set<DescriptionElementBase> distributionsForArea = editor.taxonDistributionMap.get(rowObject.getTaxonUuid()).get(area);
|
78
|
if (distributionsForArea == null){
|
79
|
return null;
|
80
|
}
|
81
|
if (distributionsForArea.size() == 1){
|
82
|
return distributionsForArea.iterator().next();
|
83
|
}
|
84
|
if (distributionsForArea.size() > 1){
|
85
|
List<String> labels = new ArrayList();
|
86
|
distributionsForArea.forEach(desc -> labels.add(((Distribution)desc).getStatus().getLabel()));
|
87
|
return distributionsForArea;
|
88
|
}
|
89
|
}
|
90
|
return null;
|
91
|
|
92
|
}
|
93
|
|
94
|
|
95
|
/**
|
96
|
* {@inheritDoc}
|
97
|
*/
|
98
|
@Override
|
99
|
public int getColumnCount() {
|
100
|
return editor.getPropertyToLabelMap().size();
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* {@inheritDoc}
|
105
|
*/
|
106
|
@Override
|
107
|
public String getColumnProperty(int columnIndex) {
|
108
|
return editor.getPropertyToLabelMap().get(columnIndex);
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* {@inheritDoc}
|
113
|
*/
|
114
|
@Override
|
115
|
public int getColumnIndex(String propertyName){
|
116
|
return editor.getPropertyToLabelMap().indexOf(propertyName);
|
117
|
}
|
118
|
|
119
|
|
120
|
/**
|
121
|
* {@inheritDoc}
|
122
|
*/
|
123
|
@Override
|
124
|
public void setDataValue(TaxonDistributionDTO taxonWrapper, int columnIndex, Object newValue) {
|
125
|
if (newValue instanceof StatusHelper){
|
126
|
NamedArea area =editor.getAreaToColumnIndexMap().get(columnIndex);
|
127
|
Map<NamedArea, Set<DescriptionElementBase>> distributionMap = editor.taxonDistributionMap.get(taxonWrapper.getTaxonUuid());
|
128
|
if (distributionMap == null){
|
129
|
distributionMap = new HashMap();
|
130
|
editor.taxonDistributionMap.put(taxonWrapper.getTaxonUuid(),distributionMap);
|
131
|
}
|
132
|
Distribution dist = null;
|
133
|
Set<DescriptionElementBase> distributions = distributionMap.get(area);
|
134
|
if (distributions != null && !distributions.isEmpty()){
|
135
|
DescriptionElementBase desc = distributions.iterator().next();
|
136
|
if (desc instanceof Distribution){
|
137
|
if (((StatusHelper)newValue).term.getId() == 0){
|
138
|
desc.getInDescription().removeElement(desc);
|
139
|
distributions.remove(desc);
|
140
|
}else {
|
141
|
((Distribution)desc).setStatus(((StatusHelper)newValue).term);
|
142
|
}
|
143
|
}
|
144
|
}else{
|
145
|
if (((StatusHelper)newValue).term.getId() == 0){
|
146
|
return;
|
147
|
}
|
148
|
if (distributions == null){
|
149
|
distributions = new HashSet();
|
150
|
distributionMap.put(area, distributions);
|
151
|
}
|
152
|
dist = Distribution.NewInstance(area, ((StatusHelper)newValue).term);
|
153
|
Set<TaxonDescription> descs = taxonWrapper.getDescriptionsWrapper().getDescriptions();
|
154
|
TaxonDescription desc;
|
155
|
if (descs.size() >= 1){
|
156
|
desc = descs.iterator().next();
|
157
|
}else {
|
158
|
desc = TaxonDescription.NewInstance();
|
159
|
taxonWrapper.getDescriptionsWrapper().getDescriptions().add(desc);
|
160
|
}
|
161
|
if (editor.getDefaultSource() != null){
|
162
|
dist.addSource(DescriptionElementSource.NewPrimarySourceInstance(editor.getDefaultSource(), null));
|
163
|
}
|
164
|
desc.addElement(dist);
|
165
|
if(desc.isPersited()){
|
166
|
editor.part.getCdmEntitySession().load(desc, true);
|
167
|
}
|
168
|
distributions.add(dist);
|
169
|
|
170
|
|
171
|
}
|
172
|
|
173
|
editor.descriptionsToSave.add(taxonWrapper);
|
174
|
}
|
175
|
EventUtility.postEvent(WorkbenchEventConstants.REFRESH_DETAILS, true);
|
176
|
}
|
177
|
|
178
|
}
|