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