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