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.Collection;
|
15
|
import java.util.List;
|
16
|
import java.util.UUID;
|
17
|
|
18
|
import org.springframework.beans.factory.annotation.Autowired;
|
19
|
import org.springframework.security.core.GrantedAuthority;
|
20
|
|
21
|
import com.vaadin.data.Item;
|
22
|
import com.vaadin.data.Property;
|
23
|
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
|
24
|
import com.vaadin.spring.annotation.SpringView;
|
25
|
import com.vaadin.ui.AbsoluteLayout;
|
26
|
import com.vaadin.ui.Button;
|
27
|
import com.vaadin.ui.Grid;
|
28
|
import com.vaadin.ui.ListSelect;
|
29
|
import com.vaadin.ui.Notification;
|
30
|
import com.vaadin.ui.Notification.Type;
|
31
|
import com.vaadin.ui.Table;
|
32
|
import com.vaadin.ui.UI;
|
33
|
import com.vaadin.ui.VerticalLayout;
|
34
|
import com.vaadin.ui.Window;
|
35
|
|
36
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
37
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
38
|
import eu.etaxonomy.cdm.model.common.Language;
|
39
|
import eu.etaxonomy.cdm.model.common.Representation;
|
40
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
41
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
42
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
43
|
import eu.etaxonomy.cdm.vaadin.component.DetailWindow;
|
44
|
import eu.etaxonomy.cdm.vaadin.component.DistributionToolbar;
|
45
|
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
|
46
|
import eu.etaxonomy.cdm.vaadin.container.PresenceAbsenceTermContainer;
|
47
|
import eu.etaxonomy.cdm.vaadin.security.AccessRestrictedView;
|
48
|
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
|
49
|
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
|
50
|
import eu.etaxonomy.cdm.vaadin.util.DistributionEditorUtil;
|
51
|
import eu.etaxonomy.cdm.vaadin.util.TermCacher;
|
52
|
import eu.etaxonomy.cdm.vaadin.view.AbstractPageView;
|
53
|
|
54
|
/**
|
55
|
* @author freimeier
|
56
|
* @since 18.10.2017
|
57
|
*
|
58
|
*/
|
59
|
@SpringView(name=DistributionTableViewBean.NAME)
|
60
|
public class DistributionTableViewBean
|
61
|
extends AbstractPageView<DistributionTablePresenter>
|
62
|
implements IDistributionTableView, AccessRestrictedView {
|
63
|
|
64
|
private static final long serialVersionUID = 1L;
|
65
|
public static final String NAME = "distTable";
|
66
|
|
67
|
@Autowired
|
68
|
private DistributionToolbar toolbar;
|
69
|
|
70
|
private Table table;
|
71
|
private Grid grid;
|
72
|
|
73
|
private CdmSQLContainer container;
|
74
|
private AreaAndTaxonSettingsConfigWindow distributionSettingConfigWindow;
|
75
|
|
76
|
public DistributionTableViewBean() {
|
77
|
super();
|
78
|
}
|
79
|
|
80
|
private AbsoluteLayout initLayout() {
|
81
|
AbsoluteLayout mainLayout = new AbsoluteLayout();
|
82
|
mainLayout.setImmediate(false);
|
83
|
mainLayout.setWidth("100%");
|
84
|
mainLayout.setHeight("100%");
|
85
|
|
86
|
setWidth("100.0%");
|
87
|
setHeight("100.0%");
|
88
|
|
89
|
//Horizontal Toolbar
|
90
|
mainLayout.addComponent(toolbar, "top:0.0px;right:0.0px;");
|
91
|
|
92
|
// table + formatting
|
93
|
table = new Table(){
|
94
|
private static final long serialVersionUID = -5148756917468804385L;
|
95
|
|
96
|
@Override
|
97
|
protected String formatPropertyValue(Object rowId, Object colId, Property<?> property) {
|
98
|
String formattedValue = null;
|
99
|
PresenceAbsenceTerm presenceAbsenceTerm = null;
|
100
|
Object value = property.getValue();
|
101
|
if(value instanceof String){
|
102
|
presenceAbsenceTerm = TermCacher.getInstance().getPresenceAbsenceTerm((String) value);
|
103
|
}
|
104
|
if(presenceAbsenceTerm != null){
|
105
|
Representation representation = presenceAbsenceTerm.getRepresentation(Language.DEFAULT());
|
106
|
if(representation!=null){
|
107
|
if(DistributionEditorUtil.isAbbreviatedLabels()){
|
108
|
formattedValue = representation.getAbbreviatedLabel();
|
109
|
}
|
110
|
else{
|
111
|
formattedValue = representation.getLabel();
|
112
|
}
|
113
|
}
|
114
|
if(formattedValue==null){
|
115
|
formattedValue = presenceAbsenceTerm.getTitleCache();
|
116
|
}
|
117
|
return formattedValue;
|
118
|
}
|
119
|
return super.formatPropertyValue(rowId, colId, property);
|
120
|
}
|
121
|
};
|
122
|
table.setImmediate(false);
|
123
|
table.setWidth("100.0%");
|
124
|
table.setHeight("100.0%");
|
125
|
|
126
|
table.setColumnReorderingAllowed(true);
|
127
|
table.setSortEnabled(false);
|
128
|
|
129
|
table.setColumnCollapsingAllowed(true);
|
130
|
table.setSelectable(true);
|
131
|
table.setPageLength(20);
|
132
|
table.setFooterVisible(true);
|
133
|
table.setCacheRate(20);
|
134
|
|
135
|
table.addItemClickListener(event -> {
|
136
|
if(!(event.getPropertyId().toString().equalsIgnoreCase(CdmQueryFactory.TAXON_COLUMN))
|
137
|
&& !(event.getPropertyId().toString().equalsIgnoreCase(CdmQueryFactory.RANK_COLUMN))){
|
138
|
final Item item = event.getItem();
|
139
|
Property<?> itemProperty = item.getItemProperty("uuid");
|
140
|
UUID uuid = UUID.fromString(itemProperty.getValue().toString());
|
141
|
final Taxon taxon = CdmBase.deproxy(CdmSpringContextHelper.getTaxonService()
|
142
|
.load(uuid,Arrays.asList("descriptions.descriptionElements","name.taxonBases","updatedBy")), Taxon.class);
|
143
|
final String areaID = (String)event.getPropertyId();
|
144
|
PresenceAbsenceTerm presenceAbsenceTerm = null;
|
145
|
Object statusValue = item.getItemProperty(areaID).getValue();
|
146
|
if(statusValue instanceof String){
|
147
|
presenceAbsenceTerm = TermCacher.getInstance().getPresenceAbsenceTerm((String) statusValue);
|
148
|
}
|
149
|
//popup window
|
150
|
final Window popup = new Window("Choose distribution status");
|
151
|
final ListSelect termSelect = new ListSelect();
|
152
|
termSelect.setSizeFull();
|
153
|
termSelect.setContainerDataSource(PresenceAbsenceTermContainer.getInstance());
|
154
|
termSelect.setNullSelectionAllowed(presenceAbsenceTerm != null);
|
155
|
if(presenceAbsenceTerm != null){
|
156
|
termSelect.setNullSelectionItemId("[no status]");
|
157
|
}else{
|
158
|
logger.debug("No distribution status exists yet for area");
|
159
|
}
|
160
|
termSelect.setValue(presenceAbsenceTerm);
|
161
|
termSelect.addValueChangeListener(valueChangeEvent -> {
|
162
|
// System.out.println(valueChangeEvent);
|
163
|
Object distributionStatus = valueChangeEvent.getProperty().getValue();
|
164
|
getPresenter().updateDistributionField(areaID, distributionStatus, taxon);
|
165
|
container.refresh();
|
166
|
popup.close();
|
167
|
});
|
168
|
VerticalLayout layout = new VerticalLayout(termSelect);
|
169
|
popup.setContent(layout);
|
170
|
popup.setModal(true);
|
171
|
popup.center();
|
172
|
UI.getCurrent().addWindow(popup);
|
173
|
}
|
174
|
});
|
175
|
|
176
|
mainLayout.addComponent(table, "top:75px;right:10.0px;left:10.0px;");
|
177
|
|
178
|
return mainLayout;
|
179
|
}
|
180
|
|
181
|
/**
|
182
|
* {@inheritDoc}
|
183
|
*/
|
184
|
@Override
|
185
|
public void enter(ViewChangeEvent event) {
|
186
|
update();
|
187
|
}
|
188
|
|
189
|
/**
|
190
|
* {@inheritDoc}
|
191
|
*/
|
192
|
@Override
|
193
|
public void update(){
|
194
|
try {
|
195
|
container = getPresenter().getSQLContainer();
|
196
|
} catch (SQLException e) {
|
197
|
DistributionEditorUtil.showSqlError(e);
|
198
|
return;
|
199
|
}
|
200
|
if(container==null){
|
201
|
return;
|
202
|
}
|
203
|
|
204
|
table.setContainerDataSource(container);
|
205
|
|
206
|
List<String> columnHeaders = new ArrayList<>(Arrays.asList(table.getColumnHeaders()));
|
207
|
columnHeaders.remove(CdmQueryFactory.DTYPE_COLUMN);
|
208
|
columnHeaders.remove(CdmQueryFactory.ID_COLUMN);
|
209
|
columnHeaders.remove(CdmQueryFactory.UUID_COLUMN);
|
210
|
columnHeaders.remove(CdmQueryFactory.CLASSIFICATION_COLUMN);
|
211
|
|
212
|
List<String> columnList = new ArrayList<>(columnHeaders);
|
213
|
|
214
|
String[] string = new String[columnList.size()];
|
215
|
|
216
|
table.setVisibleColumns(columnList.toArray());
|
217
|
table.setColumnHeaders(columnList.toArray(string));
|
218
|
table.setColumnFooter(CdmQueryFactory.TAXON_COLUMN, "Total amount of Taxa displayed: " + container.size());
|
219
|
}
|
220
|
|
221
|
private void createEditClickListener(){
|
222
|
//details
|
223
|
Button detailButton = toolbar.getDetailButton();
|
224
|
detailButton.setCaption("Taxon Details");
|
225
|
detailButton.addClickListener(event -> {
|
226
|
Object selectedItemId = DistributionTableViewBean.this.table.getValue();
|
227
|
// Object selectedItemId = DistributionTableViewBean.this.grid.getSelectedRow();
|
228
|
if(selectedItemId!=null){
|
229
|
final UUID uuid = UUID.fromString(table.getContainerDataSource().getItem(selectedItemId).getItemProperty("uuid").getValue().toString());
|
230
|
// final UUID uuid = UUID.fromString(grid.getContainerDataSource().getItem(selectedItemId).getItemProperty("uuid").getValue().toString());
|
231
|
Taxon taxon = HibernateProxyHelper.deproxy(CdmSpringContextHelper.getTaxonService().load(uuid), Taxon.class);
|
232
|
List<DescriptionElementBase> listDescriptions = getPresenter().listDescriptionElementsForTaxon(taxon, null);
|
233
|
DetailWindow detailWindow = new DetailWindow(taxon, listDescriptions);
|
234
|
Window window = detailWindow.createWindow();
|
235
|
window.center();
|
236
|
getUI().addWindow(window);
|
237
|
}
|
238
|
else{
|
239
|
Notification.show("Please select a taxon", Type.HUMANIZED_MESSAGE);
|
240
|
}
|
241
|
}
|
242
|
);
|
243
|
|
244
|
//area and taxon
|
245
|
Button areaAndTaxonSettingsButton = toolbar.getDistributionSettingsButton();
|
246
|
areaAndTaxonSettingsButton.addClickListener(event -> openAreaAndTaxonSettings());
|
247
|
|
248
|
//distr status
|
249
|
Button distrStatusButton = toolbar.getSettingsButton();
|
250
|
distrStatusButton.addClickListener(event -> openStatusSettings());
|
251
|
}
|
252
|
|
253
|
/**
|
254
|
* {@inheritDoc}
|
255
|
*/
|
256
|
@Override
|
257
|
public void openStatusSettings() {
|
258
|
DistributionStatusSettingsConfigWindow cw = new DistributionStatusSettingsConfigWindow(this);
|
259
|
Window window = cw.createWindow("Status");
|
260
|
UI.getCurrent().addWindow(window);
|
261
|
}
|
262
|
|
263
|
/**
|
264
|
* {@inheritDoc}
|
265
|
*/
|
266
|
@Override
|
267
|
public void openAreaAndTaxonSettings() {
|
268
|
if(distributionSettingConfigWindow==null){
|
269
|
distributionSettingConfigWindow = new AreaAndTaxonSettingsConfigWindow(this);
|
270
|
}
|
271
|
Window window = distributionSettingConfigWindow.createWindow("Areas and Taxa");
|
272
|
UI.getCurrent().addWindow(window);
|
273
|
}
|
274
|
|
275
|
/**
|
276
|
* {@inheritDoc}
|
277
|
*/
|
278
|
@Override
|
279
|
public boolean allowAnonymousAccess() {
|
280
|
// TODO Auto-generated method stub
|
281
|
return false;
|
282
|
}
|
283
|
|
284
|
/**
|
285
|
* {@inheritDoc}
|
286
|
*/
|
287
|
@Override
|
288
|
public Collection<Collection<GrantedAuthority>> allowedGrantedAuthorities() {
|
289
|
// TODO Auto-generated method stub
|
290
|
return null;
|
291
|
}
|
292
|
|
293
|
/**
|
294
|
* {@inheritDoc}
|
295
|
*/
|
296
|
@Override
|
297
|
protected String getHeaderText() {
|
298
|
// TODO Auto-generated method stub
|
299
|
return null;
|
300
|
}
|
301
|
|
302
|
/**
|
303
|
* {@inheritDoc}
|
304
|
*/
|
305
|
@Override
|
306
|
protected String getSubHeaderText() {
|
307
|
// TODO Auto-generated method stub
|
308
|
return null;
|
309
|
}
|
310
|
|
311
|
/**
|
312
|
* {@inheritDoc}
|
313
|
*/
|
314
|
@Override
|
315
|
protected void initContent() {
|
316
|
AbsoluteLayout mainLayout = initLayout();
|
317
|
setCompositionRoot(mainLayout);
|
318
|
createEditClickListener();
|
319
|
}
|
320
|
}
|