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