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