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.util.CdmQueryFactory;
|
50
|
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
|
51
|
import eu.etaxonomy.cdm.vaadin.util.DistributionEditorUtil;
|
52
|
import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
|
53
|
|
54
|
/**
|
55
|
* @author freimeier
|
56
|
* @since 18.10.2017
|
57
|
*
|
58
|
*/
|
59
|
@SpringComponent
|
60
|
@ViewScope
|
61
|
public class DistributionTablePresenter extends AbstractPresenter<IDistributionTableView> {
|
62
|
|
63
|
private static final long serialVersionUID = 3313043335587777217L;
|
64
|
|
65
|
@Autowired
|
66
|
private CdmUserHelper userHelper;
|
67
|
|
68
|
@Autowired
|
69
|
@Qualifier("cdmRepository")
|
70
|
private CdmRepository repo;
|
71
|
|
72
|
public int updateDistributionField(String distributionAreaString, Object comboValue, Taxon taxon) {
|
73
|
TransactionStatus tx = repo.startTransaction();
|
74
|
taxon = (Taxon)repo.getTaxonService().find(taxon.getUuid());
|
75
|
Set<DefinedTermBase> chosenTerms = getChosenTerms();
|
76
|
NamedArea namedArea = null;
|
77
|
for(DefinedTermBase term:chosenTerms){
|
78
|
Representation representation = term.getRepresentation(Language.DEFAULT());
|
79
|
if(representation!=null){
|
80
|
if(DistributionEditorUtil.isAbbreviatedLabels()){
|
81
|
String label = representation.getLabel();
|
82
|
String abbreviatedLabel = representation.getAbbreviatedLabel();
|
83
|
if(abbreviatedLabel!=null && abbreviatedLabel.equalsIgnoreCase(distributionAreaString)){
|
84
|
namedArea = (NamedArea) term;
|
85
|
break;
|
86
|
}
|
87
|
else if(label!=null && label.equalsIgnoreCase(distributionAreaString)){
|
88
|
namedArea = (NamedArea) term;
|
89
|
break;
|
90
|
}
|
91
|
}
|
92
|
}
|
93
|
if(term.getTitleCache().equalsIgnoreCase(distributionAreaString)){
|
94
|
namedArea = (NamedArea) term;
|
95
|
break;
|
96
|
}
|
97
|
}
|
98
|
if(namedArea==null){
|
99
|
Notification.show("Error during update of distribution term!");
|
100
|
repo.commitTransaction(tx);
|
101
|
return -1;
|
102
|
}
|
103
|
List<Distribution> distributions = getDistributions(taxon);
|
104
|
Distribution distribution = null;
|
105
|
for(Distribution dist : distributions){
|
106
|
if(dist.getArea()!=null && dist.getArea().equals(namedArea)){
|
107
|
distribution = dist;
|
108
|
break;
|
109
|
}
|
110
|
}
|
111
|
if(distribution==null){
|
112
|
//create new distribution
|
113
|
distribution = Distribution.NewInstance(namedArea, (PresenceAbsenceTerm) comboValue);
|
114
|
Set<TaxonDescription> descriptions = taxon.getDescriptions();
|
115
|
if (descriptions != null && !descriptions.isEmpty()) {
|
116
|
for (TaxonDescription desc : descriptions) {
|
117
|
// add to first taxon description
|
118
|
desc.addElement(distribution);
|
119
|
repo.commitTransaction(tx);
|
120
|
return 0;
|
121
|
}
|
122
|
} else {// there are no TaxonDescription yet.
|
123
|
TaxonDescription taxonDescription = TaxonDescription.NewInstance(taxon);
|
124
|
taxonDescription.addElement(distribution);
|
125
|
repo.commitTransaction(tx);
|
126
|
return 0;
|
127
|
}
|
128
|
}
|
129
|
else if(comboValue == null){//delete descriptionElementBase
|
130
|
DescriptionBase<?> desc = distribution.getInDescription();
|
131
|
desc.removeElement(distribution);
|
132
|
repo.commitTransaction(tx);
|
133
|
return 1;
|
134
|
}
|
135
|
else{//update distribution
|
136
|
distribution.setStatus((PresenceAbsenceTerm)comboValue);
|
137
|
repo.getCommonService().saveOrUpdate(distribution);
|
138
|
repo.commitTransaction(tx);
|
139
|
return 0;
|
140
|
}
|
141
|
repo.commitTransaction(tx);
|
142
|
return -1;
|
143
|
}
|
144
|
|
145
|
public Set<DefinedTermBase> getChosenTerms() {
|
146
|
VaadinSession session = VaadinSession.getCurrent();
|
147
|
UUID vocUUID = (UUID) session.getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREA_VOCABULARY_UUID);
|
148
|
// getConversationHolder().getSession();
|
149
|
TermVocabulary<DefinedTermBase> voc = CdmSpringContextHelper.getVocabularyService().load(vocUUID, Arrays.asList("terms.representations"));
|
150
|
// voc = CdmBase.deproxy(voc);
|
151
|
return voc.getTerms();
|
152
|
}
|
153
|
|
154
|
public List<String> getAbbreviatedTermList() {
|
155
|
Set<NamedArea> terms = getTermSet();
|
156
|
List<String> list = new ArrayList<>();
|
157
|
for(DefinedTermBase<?> dtb: terms){
|
158
|
for(Representation r : dtb.getRepresentations()){
|
159
|
list.add(r.getAbbreviatedLabel());
|
160
|
}
|
161
|
}
|
162
|
return list;
|
163
|
}
|
164
|
|
165
|
public Set<NamedArea> getNamedAreas(){
|
166
|
Set<NamedArea> namedAreas = (Set<NamedArea>) VaadinSession.getCurrent().getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREAS);
|
167
|
if(namedAreas!=null && namedAreas.isEmpty()){
|
168
|
return getTermSet();
|
169
|
}
|
170
|
return namedAreas;
|
171
|
}
|
172
|
|
173
|
private Set<NamedArea> getTermSet(){
|
174
|
VaadinSession session = VaadinSession.getCurrent();
|
175
|
UUID vocUUID = (UUID) session.getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREA_VOCABULARY_UUID);
|
176
|
TermVocabulary<NamedArea> vocabulary = CdmSpringContextHelper.getVocabularyService().load(vocUUID, Arrays.asList("terms.representations"));
|
177
|
vocabulary = CdmBase.deproxy(vocabulary, TermVocabulary.class);
|
178
|
return vocabulary.getTermsOrderedByLabels(Language.DEFAULT());
|
179
|
}
|
180
|
|
181
|
public HashMap<DescriptionElementBase, Distribution> getDistribution(DefinedTermBase dt, Taxon taxon) {
|
182
|
Set<Feature> setFeature = new HashSet<>(Arrays.asList(Feature.DISTRIBUTION()));
|
183
|
List<DescriptionElementBase> listTaxonDescription = CdmSpringContextHelper.getDescriptionService().listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
|
184
|
HashMap<DescriptionElementBase, Distribution> map = null;
|
185
|
for(DescriptionElementBase deb : listTaxonDescription){
|
186
|
if(deb instanceof Distribution){
|
187
|
Distribution db = (Distribution)deb;
|
188
|
String titleCache = dt.getTitleCache();
|
189
|
if(db.getArea().getTitleCache().equalsIgnoreCase(titleCache)){
|
190
|
map = new HashMap<DescriptionElementBase, Distribution>();
|
191
|
map.put(deb, db);
|
192
|
}
|
193
|
}
|
194
|
}
|
195
|
return map;
|
196
|
}
|
197
|
|
198
|
public List<DescriptionElementBase> listDescriptionElementsForTaxon(Taxon taxon, Set<Feature> setFeature){
|
199
|
List<DescriptionElementBase> listDescriptionElementsForTaxon = CdmSpringContextHelper.getDescriptionService().listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
|
200
|
sort(listDescriptionElementsForTaxon);
|
201
|
return listDescriptionElementsForTaxon;
|
202
|
}
|
203
|
|
204
|
public List<Distribution> getDistributions(Taxon taxon) {
|
205
|
Set<Feature> setFeature = new HashSet<>(Arrays.asList(Feature.DISTRIBUTION()));
|
206
|
List<Distribution> listTaxonDescription = CdmSpringContextHelper.getDescriptionService()
|
207
|
.listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
|
208
|
return listTaxonDescription;
|
209
|
|
210
|
}
|
211
|
|
212
|
public List<TaxonNode> getAllNodes(){
|
213
|
List<TaxonNode> allNodes = new ArrayList<>();
|
214
|
|
215
|
List<TaxonNode> taxonNodes = getChosenTaxonNodes();
|
216
|
for (TaxonNode taxonNode : taxonNodes) {
|
217
|
if(taxonNode.getTaxon()!=null){
|
218
|
allNodes.add(taxonNode);
|
219
|
}
|
220
|
allNodes.addAll(CdmSpringContextHelper.getTaxonNodeService().loadChildNodesOfTaxonNode(taxonNode, null, true, null));
|
221
|
}
|
222
|
return allNodes;
|
223
|
}
|
224
|
|
225
|
|
226
|
public List<TaxonNode> getChosenTaxonNodes() {
|
227
|
VaadinSession session = VaadinSession.getCurrent();
|
228
|
List<UUID> taxonNodeUUIDs = (List<UUID>) session.getAttribute(DistributionEditorUtil.SATTR_TAXON_NODES_UUID);
|
229
|
UUID classificationUuid = (UUID)session.getAttribute(DistributionEditorUtil.SATTR_CLASSIFICATION);
|
230
|
if((taxonNodeUUIDs==null || taxonNodeUUIDs.isEmpty()) && classificationUuid!=null){
|
231
|
Classification classification = CdmSpringContextHelper.getClassificationService().load(classificationUuid);
|
232
|
if(classification!=null){
|
233
|
taxonNodeUUIDs = Collections.singletonList(classification.getRootNode().getUuid());
|
234
|
}
|
235
|
}
|
236
|
List<TaxonNode> loadedNodes = CdmSpringContextHelper.getTaxonNodeService().load(taxonNodeUUIDs, null);
|
237
|
if(loadedNodes!=null){
|
238
|
return loadedNodes;
|
239
|
}
|
240
|
return Collections.emptyList();
|
241
|
}
|
242
|
|
243
|
public CdmSQLContainer getSQLContainer() throws SQLException{
|
244
|
List<Integer> nodeIds = new ArrayList<>();
|
245
|
for (TaxonNode taxonNode : getAllNodes()) {
|
246
|
nodeIds.add(taxonNode.getId());
|
247
|
}
|
248
|
Set<NamedArea> namedAreas = getNamedAreas();
|
249
|
if(namedAreas!=null){
|
250
|
return new CdmSQLContainer(CdmQueryFactory.generateTaxonDistributionQuery(nodeIds, namedAreas));
|
251
|
}
|
252
|
return null;
|
253
|
}
|
254
|
|
255
|
protected static final List<String> DESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
|
256
|
"$",
|
257
|
"elements.*",
|
258
|
"elements.sources.citation.authorship.$",
|
259
|
"elements.sources.nameUsedInSource.originalNameString",
|
260
|
"elements.area.level",
|
261
|
"elements.modifyingText",
|
262
|
"elements.states.*",
|
263
|
"elements.media",
|
264
|
"elements.multilanguageText",
|
265
|
"multilanguageText",
|
266
|
"stateData.$",
|
267
|
"annotations",
|
268
|
"markers",
|
269
|
"sources.citation.authorship",
|
270
|
"sources.nameUsedInSource",
|
271
|
"multilanguageText",
|
272
|
"media",
|
273
|
"name.$",
|
274
|
"name.rank.representations",
|
275
|
"name.status.type.representations",
|
276
|
"taxon2.name",
|
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
|
// VaadinSession.getCurrent().setAttribute(DistributionEditorUtil.SATTR_TAXON_NODES_UUID, taxonNodes);
|
304
|
// VaadinSession.getCurrent().setAttribute(DistributionEditorUtil.SATTR_SELECTED_AREA_VOCABULARY_UUID, voc.getUuid());
|
305
|
// VaadinSession.getCurrent().setAttribute(DistributionEditorUtil.SATTR_SELECTED_AREAS, selectedAreas);
|
306
|
// VaadinSession.getCurrent().setAttribute(DistributionEditorUtil.SATTR_CLASSIFICATION, classificationUuid);
|
307
|
if(userHelper.userIsAutheticated() && !userHelper.userIsAnnonymous()) {
|
308
|
getView().openDistributionSettings();
|
309
|
}
|
310
|
}
|
311
|
}
|