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