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