1
|
package eu.etaxonomy.cdm.vaadin.presenter.dbstatus;
|
2
|
|
3
|
import java.sql.SQLException;
|
4
|
import java.util.ArrayList;
|
5
|
import java.util.Arrays;
|
6
|
import java.util.Collections;
|
7
|
import java.util.Comparator;
|
8
|
import java.util.HashMap;
|
9
|
import java.util.HashSet;
|
10
|
import java.util.List;
|
11
|
import java.util.Set;
|
12
|
import java.util.UUID;
|
13
|
|
14
|
import com.vaadin.server.VaadinSession;
|
15
|
import com.vaadin.ui.Notification;
|
16
|
|
17
|
import eu.etaxonomy.cdm.api.service.IClassificationService;
|
18
|
import eu.etaxonomy.cdm.api.service.IDescriptionService;
|
19
|
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
|
20
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
21
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
22
|
import eu.etaxonomy.cdm.api.service.IVocabularyService;
|
23
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
24
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
25
|
import eu.etaxonomy.cdm.model.common.Language;
|
26
|
import eu.etaxonomy.cdm.model.common.Representation;
|
27
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
28
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
29
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
30
|
import eu.etaxonomy.cdm.model.description.Feature;
|
31
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
32
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
33
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
34
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
35
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
36
|
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
|
37
|
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
|
38
|
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
|
39
|
import eu.etaxonomy.cdm.vaadin.util.DistributionEditorUtil;
|
40
|
import eu.etaxonomy.cdm.vaadin.view.dbstatus.DistributionTableView;
|
41
|
|
42
|
|
43
|
public class DistributionTablePresenter {
|
44
|
|
45
|
private final IClassificationService classificationService;
|
46
|
private final IVocabularyService vocabularyService;
|
47
|
private final IDescriptionService descriptionService;
|
48
|
private final ITaxonNodeService taxonNodeService;
|
49
|
private final ITermService termService;
|
50
|
private final DistributionTableView view;
|
51
|
private final ITaxonService taxonService;
|
52
|
|
53
|
public DistributionTablePresenter(DistributionTableView dtv){
|
54
|
this.view = dtv;
|
55
|
view.addListener(this);
|
56
|
taxonService = CdmSpringContextHelper.getTaxonService();
|
57
|
classificationService = CdmSpringContextHelper.getClassificationService();
|
58
|
taxonNodeService = CdmSpringContextHelper.getTaxonNodeService();
|
59
|
vocabularyService = CdmSpringContextHelper.getVocabularyService();
|
60
|
descriptionService = CdmSpringContextHelper.getDescriptionService();
|
61
|
termService = CdmSpringContextHelper.getTermService();
|
62
|
}
|
63
|
|
64
|
public int updateDistributionField(String distributionAreaString, Object comboValue, Taxon taxon) {
|
65
|
Set<DefinedTermBase> chosenTerms = getChosenTerms();
|
66
|
NamedArea namedArea = null;
|
67
|
for(DefinedTermBase term:chosenTerms){
|
68
|
Representation representation = term.getRepresentation(Language.DEFAULT());
|
69
|
if(representation!=null){
|
70
|
if(DistributionEditorUtil.isAbbreviatedLabels()){
|
71
|
String label = representation.getLabel();
|
72
|
String abbreviatedLabel = representation.getAbbreviatedLabel();
|
73
|
if(abbreviatedLabel!=null && abbreviatedLabel.equalsIgnoreCase(distributionAreaString)){
|
74
|
namedArea = (NamedArea) term;
|
75
|
break;
|
76
|
}
|
77
|
else if(label!=null && label.equalsIgnoreCase(distributionAreaString)){
|
78
|
namedArea = (NamedArea) term;
|
79
|
break;
|
80
|
}
|
81
|
}
|
82
|
}
|
83
|
if(term.getTitleCache().equalsIgnoreCase(distributionAreaString)){
|
84
|
namedArea = (NamedArea) term;
|
85
|
break;
|
86
|
}
|
87
|
}
|
88
|
if(namedArea==null){
|
89
|
Notification.show("Error during update of distribution term!");
|
90
|
return -1;
|
91
|
}
|
92
|
List<Distribution> distributions = getDistributions(taxon);
|
93
|
Distribution distribution = null;
|
94
|
for(Distribution dist : distributions){
|
95
|
if(dist.getArea()!=null && dist.getArea().equals(namedArea)){
|
96
|
distribution = dist;
|
97
|
break;
|
98
|
}
|
99
|
}
|
100
|
if(distribution==null){
|
101
|
//create new distribution
|
102
|
distribution = Distribution.NewInstance(namedArea, (PresenceAbsenceTerm) comboValue);
|
103
|
Set<TaxonDescription> descriptions = taxon.getDescriptions();
|
104
|
if (descriptions != null && !descriptions.isEmpty()) {
|
105
|
for (TaxonDescription desc : descriptions) {
|
106
|
// add to first taxon description
|
107
|
desc.addElement(distribution);
|
108
|
getTaxonService().saveOrUpdate(taxon);
|
109
|
return 0;
|
110
|
}
|
111
|
} else {// there are no TaxonDescription yet.
|
112
|
TaxonDescription taxonDescription = TaxonDescription.NewInstance(taxon);
|
113
|
taxonDescription.addElement(distribution);
|
114
|
taxon.addDescription(taxonDescription);
|
115
|
getTaxonService().saveOrUpdate(taxon);
|
116
|
return 0;
|
117
|
}
|
118
|
}
|
119
|
else if(comboValue == null){//delete descriptionElementBase
|
120
|
distribution.getInDescription().removeElement(distribution);
|
121
|
getTaxonService().saveOrUpdate(taxon);
|
122
|
return 1;
|
123
|
}
|
124
|
else{
|
125
|
distribution.setStatus((PresenceAbsenceTerm)comboValue);
|
126
|
getTaxonService().saveOrUpdate(taxon);
|
127
|
return 0;
|
128
|
}
|
129
|
return -1;
|
130
|
}
|
131
|
|
132
|
public Set<DefinedTermBase> getChosenTerms() {
|
133
|
VaadinSession session = VaadinSession.getCurrent();
|
134
|
UUID termUUID = (UUID) session.getAttribute(DistributionEditorUtil.SATTR_SELECTED_VOCABULARY_UUID);
|
135
|
TermVocabulary<DefinedTermBase> term = vocabularyService.load(termUUID);
|
136
|
term = CdmBase.deproxy(term, TermVocabulary.class);
|
137
|
return term.getTerms();
|
138
|
}
|
139
|
|
140
|
public List<String> getAbbreviatedTermList() {
|
141
|
Set<NamedArea> terms = getTermSet();
|
142
|
List<String> list = new ArrayList<String>();
|
143
|
for(DefinedTermBase dtb: terms){
|
144
|
for(Representation r : dtb.getRepresentations()){
|
145
|
list.add(r.getAbbreviatedLabel());
|
146
|
}
|
147
|
}
|
148
|
return list;
|
149
|
}
|
150
|
|
151
|
public Set<NamedArea> getNamedAreas(){
|
152
|
Set<NamedArea> namedAreas = (Set<NamedArea>) VaadinSession.getCurrent().getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREAS);
|
153
|
if(namedAreas!=null && namedAreas.isEmpty()){
|
154
|
return getTermSet();
|
155
|
}
|
156
|
return namedAreas;
|
157
|
}
|
158
|
|
159
|
public List<String> getNamedAreasLabels(){
|
160
|
Set<NamedArea> selectedAreas = getNamedAreas();
|
161
|
List<String> namedAreaTitles = new ArrayList<>();
|
162
|
for (NamedArea namedArea : selectedAreas) {
|
163
|
String title = null;
|
164
|
Representation representation = namedArea.getRepresentation(Language.DEFAULT());
|
165
|
if(representation!=null){
|
166
|
if(DistributionEditorUtil.isAbbreviatedLabels()){
|
167
|
title = representation.getAbbreviatedLabel();
|
168
|
}
|
169
|
else{
|
170
|
title = representation.getLabel();
|
171
|
}
|
172
|
}
|
173
|
if(title==null){
|
174
|
title = namedArea.getTitleCache();
|
175
|
}
|
176
|
namedAreaTitles.add(title);
|
177
|
}
|
178
|
return namedAreaTitles;
|
179
|
}
|
180
|
|
181
|
private Set<NamedArea> getTermSet(){
|
182
|
VaadinSession session = VaadinSession.getCurrent();
|
183
|
UUID termUUID = (UUID) session.getAttribute(DistributionEditorUtil.SATTR_SELECTED_VOCABULARY_UUID);
|
184
|
TermVocabulary<NamedArea> vocabulary = vocabularyService.load(termUUID);
|
185
|
vocabulary = CdmBase.deproxy(vocabulary, TermVocabulary.class);
|
186
|
return vocabulary.getTermsOrderedByLabels(Language.DEFAULT());
|
187
|
}
|
188
|
|
189
|
public HashMap<DescriptionElementBase, Distribution> getDistribution(DefinedTermBase dt, Taxon taxon) {
|
190
|
Set<Feature> setFeature = new HashSet<Feature>(Arrays.asList(Feature.DISTRIBUTION()));
|
191
|
List<DescriptionElementBase> listTaxonDescription = descriptionService.listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
|
192
|
HashMap<DescriptionElementBase, Distribution> map = null;
|
193
|
for(DescriptionElementBase deb : listTaxonDescription){
|
194
|
if(deb instanceof Distribution){
|
195
|
Distribution db = (Distribution)deb;
|
196
|
String titleCache = dt.getTitleCache();
|
197
|
if(db.getArea().getTitleCache().equalsIgnoreCase(titleCache)){
|
198
|
map = new HashMap<DescriptionElementBase, Distribution>();
|
199
|
map.put(deb, db);
|
200
|
}
|
201
|
}
|
202
|
}
|
203
|
return map;
|
204
|
}
|
205
|
|
206
|
public List<DescriptionElementBase> listDescriptionElementsForTaxon(Taxon taxon, Set<Feature> setFeature){
|
207
|
List<DescriptionElementBase> listDescriptionElementsForTaxon = descriptionService.listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
|
208
|
sort(listDescriptionElementsForTaxon);
|
209
|
return listDescriptionElementsForTaxon;
|
210
|
}
|
211
|
|
212
|
public List<Distribution> getDistributions(Taxon taxon) {
|
213
|
Set<Feature> setFeature = new HashSet<Feature>(Arrays.asList(Feature.DISTRIBUTION()));
|
214
|
List<Distribution> listTaxonDescription = descriptionService.listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
|
215
|
return listTaxonDescription;
|
216
|
|
217
|
}
|
218
|
|
219
|
public List<TaxonNode> getAllNodes(){
|
220
|
TaxonNode taxonNode = getChosenTaxonNode();
|
221
|
List<TaxonNode> nodes = new ArrayList<TaxonNode>();
|
222
|
if(taxonNode!=null){
|
223
|
if(taxonNode.getTaxon()!=null){
|
224
|
nodes.add(taxonNode);
|
225
|
}
|
226
|
nodes.addAll(taxonNodeService.loadChildNodesOfTaxonNode(taxonNode, null, true, null));
|
227
|
}
|
228
|
return nodes;
|
229
|
}
|
230
|
|
231
|
|
232
|
public TaxonNode getChosenTaxonNode() {
|
233
|
VaadinSession session = VaadinSession.getCurrent();
|
234
|
UUID taxonNodeUUID = (UUID) session.getAttribute(DistributionEditorUtil.SATTR_TAXON_NODE_UUID);
|
235
|
TaxonNode classificationNode = taxonNodeService.load(taxonNodeUUID);
|
236
|
return classificationNode;
|
237
|
}
|
238
|
|
239
|
public int getSizeOfTaxonNode(){
|
240
|
TaxonNode taxonNode = getChosenTaxonNode();
|
241
|
return taxonNodeService.loadChildNodesOfTaxonNode(taxonNode, null, true, null).size();
|
242
|
}
|
243
|
|
244
|
|
245
|
public CdmSQLContainer getSQLContainer() throws SQLException{
|
246
|
List<Integer> nodeIds = new ArrayList<Integer>();
|
247
|
for (TaxonNode taxonNode : getAllNodes()) {
|
248
|
nodeIds.add(taxonNode.getId());
|
249
|
}
|
250
|
Set<NamedArea> namedAreas = getNamedAreas();
|
251
|
if(namedAreas!=null){
|
252
|
return new CdmSQLContainer(CdmQueryFactory.generateTaxonDistributionQuery(nodeIds, namedAreas));
|
253
|
}
|
254
|
return null;
|
255
|
}
|
256
|
|
257
|
protected static final List<String> DESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
|
258
|
"$",
|
259
|
"elements.*",
|
260
|
"elements.sources.citation.authorship.$",
|
261
|
"elements.sources.nameUsedInSource.originalNameString",
|
262
|
"elements.area.level",
|
263
|
"elements.modifyingText",
|
264
|
"elements.states.*",
|
265
|
"elements.media",
|
266
|
"elements.multilanguageText",
|
267
|
"multilanguageText",
|
268
|
"stateData.$",
|
269
|
"annotations",
|
270
|
"markers",
|
271
|
"sources.citation.authorship",
|
272
|
"sources.nameUsedInSource",
|
273
|
"multilanguageText",
|
274
|
"media",
|
275
|
"name.$",
|
276
|
"name.rank.representations",
|
277
|
"name.status.type.representations",
|
278
|
"taxon2.name"
|
279
|
});
|
280
|
|
281
|
public IClassificationService getClassificationService() {
|
282
|
return classificationService;
|
283
|
}
|
284
|
|
285
|
public IVocabularyService getVocabularyService() {
|
286
|
return vocabularyService;
|
287
|
}
|
288
|
|
289
|
public IDescriptionService getDescriptionService() {
|
290
|
return descriptionService;
|
291
|
}
|
292
|
|
293
|
public ITaxonNodeService getTaxonNodeService() {
|
294
|
return taxonNodeService;
|
295
|
}
|
296
|
|
297
|
public ITermService getTermService() {
|
298
|
return termService;
|
299
|
}
|
300
|
public ITaxonService getTaxonService() {
|
301
|
return taxonService;
|
302
|
}
|
303
|
|
304
|
/**Helper Methods*/
|
305
|
|
306
|
private void sort(List<DescriptionElementBase> list){
|
307
|
Collections.sort(list, new Comparator<DescriptionElementBase>() {
|
308
|
|
309
|
@Override
|
310
|
public int compare(DescriptionElementBase o1, DescriptionElementBase o2) {
|
311
|
String feature1 = o1.getFeature().getTitleCache();
|
312
|
String feature2 = o2.getFeature().getTitleCache();
|
313
|
if(feature1 !=null && feature2 !=null){
|
314
|
return feature1.compareTo(feature2);
|
315
|
}else{
|
316
|
return 0;
|
317
|
|
318
|
}
|
319
|
}
|
320
|
});
|
321
|
}
|
322
|
}
|