Project

General

Profile

Download (17.6 KB) Statistics
| Branch: | Tag: | Revision:
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
import org.vaadin.addons.lazyquerycontainer.LazyQueryContainer;
21

    
22
import com.vaadin.data.Item;
23
import com.vaadin.data.Property;
24
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
25
import com.vaadin.spring.annotation.SpringView;
26
import com.vaadin.spring.annotation.ViewScope;
27
import com.vaadin.ui.AbsoluteLayout;
28
import com.vaadin.ui.Button;
29
import com.vaadin.ui.Grid;
30
import com.vaadin.ui.Grid.FooterRow;
31
import com.vaadin.ui.ListSelect;
32
import com.vaadin.ui.Notification;
33
import com.vaadin.ui.Notification.Type;
34
import com.vaadin.ui.Table;
35
import com.vaadin.ui.UI;
36
import com.vaadin.ui.VerticalLayout;
37
import com.vaadin.ui.Window;
38

    
39
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
40
import eu.etaxonomy.cdm.i18n.Messages;
41
import eu.etaxonomy.cdm.model.common.CdmBase;
42
import eu.etaxonomy.cdm.model.common.Language;
43
import eu.etaxonomy.cdm.model.common.Representation;
44
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
45
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
46
import eu.etaxonomy.cdm.model.taxon.Taxon;
47
import eu.etaxonomy.cdm.vaadin.component.distributionStatus.AreaAndTaxonSettingsConfigWindow;
48
import eu.etaxonomy.cdm.vaadin.component.distributionStatus.DetailWindow;
49
import eu.etaxonomy.cdm.vaadin.component.distributionStatus.DistributionStatusSettingsConfigWindow;
50
import eu.etaxonomy.cdm.vaadin.component.distributionStatus.DistributionToolbar;
51
import eu.etaxonomy.cdm.vaadin.component.distributionStatus.HelpWindow;
52
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
53
import eu.etaxonomy.cdm.vaadin.event.error.DelegatingErrorHandler;
54
import eu.etaxonomy.cdm.vaadin.event.error.HibernateSystemErrorHandler;
55
import eu.etaxonomy.cdm.vaadin.security.AccessRestrictedView;
56
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
57
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
58
import eu.etaxonomy.cdm.vaadin.util.DistributionEditorUtil;
59
import eu.etaxonomy.cdm.vaadin.view.AbstractPageView;
60

    
61
/**
62
 * @author freimeier
63
 * @since 18.10.2017
64
 *
65
 */
66
@ViewScope
67
@SpringView(name=DistributionTableViewBean.NAME)
68
public class DistributionTableViewBean
69
            extends AbstractPageView<DistributionTablePresenter>
70
            implements IDistributionTableView, AccessRestrictedView {
71

    
72
	private static final long serialVersionUID = 1L;
73
    public static final String NAME = "distGrid"; //$NON-NLS-1$
74

    
75
    @Autowired
76
    private DistributionToolbar toolbar;
77

    
78
	private Table table;
79
	private Grid grid;
80
	private FooterRow footerRow;
81

    
82
    private CdmSQLContainer container;
83
    private LazyQueryContainer gridcontainer;
84
	private AreaAndTaxonSettingsConfigWindow areaAndTaxonConfigWindow;;
85
	private DistributionStatusSettingsConfigWindow distributionStatusConfigWindow;
86
	private HelpWindow helpWindow;
87

    
88
	public DistributionTableViewBean() {
89
		super();
90
	}
91

    
92
	private AbsoluteLayout initLayout() {
93
		AbsoluteLayout mainLayout = new AbsoluteLayout();
94
		mainLayout.setImmediate(false);
95
		mainLayout.setWidth("100%"); //$NON-NLS-1$
96
		mainLayout.setHeight("100%"); //$NON-NLS-1$
97

    
98
		setWidth("100.0%"); //$NON-NLS-1$
99
		setHeight("100.0%"); //$NON-NLS-1$
100

    
101
		//Horizontal Toolbar
102
		mainLayout.addComponent(toolbar, "top:0.0px;right:0.0px;"); //$NON-NLS-1$
103

    
104
		// table + formatting
105
		table = new Table(){
106
			private static final long serialVersionUID = -5148756917468804385L;
107

    
108
			@Override
109
			protected String formatPropertyValue(Object rowId, Object colId, Property<?> property) {
110
				String formattedValue = null;
111
				PresenceAbsenceTerm presenceAbsenceTerm = null;
112
				Object value = property.getValue();
113
				if(value instanceof String){
114
//					presenceAbsenceTerm = TermCacher.getInstance().getPresenceAbsenceTerm((String) value);
115
                    try {
116
                        presenceAbsenceTerm = (PresenceAbsenceTerm)CdmSpringContextHelper.getTermService().load(UUID.fromString((String)value));
117
                    }catch(IllegalArgumentException|ClassCastException e) {
118
                        // Not a PresenceAbsenceTerm Column
119
                    }
120
				}
121
				if(presenceAbsenceTerm != null){
122
					Representation representation = presenceAbsenceTerm.getRepresentation(Language.DEFAULT());
123
					if(representation!=null){
124
						if(DistributionEditorUtil.isAbbreviatedLabels()){
125
							formattedValue = representation.getAbbreviatedLabel();
126
						}
127
						else{
128
							formattedValue = representation.getLabel();
129
						}
130
					}
131
					if(formattedValue==null){
132
						formattedValue = presenceAbsenceTerm.getTitleCache();
133
					}
134
					return formattedValue;
135
				}
136
				return super.formatPropertyValue(rowId, colId, property);
137
			}
138
		};
139
		table.setImmediate(false);
140
		table.setWidth("100.0%");
141
		table.setHeight("100.0%");
142

    
143
        table.setColumnReorderingAllowed(true);
144
        table.setSortEnabled(false);
145

    
146
        table.setColumnCollapsingAllowed(true);
147
        table.setSelectable(true);
148
        table.setPageLength(20);
149
        table.setFooterVisible(true);
150
        table.setCacheRate(20);
151

    
152
		table.addItemClickListener(event -> {
153
            if(!(event.getPropertyId().toString().equalsIgnoreCase(CdmQueryFactory.TAXON_COLUMN))
154
            		&& !(event.getPropertyId().toString().equalsIgnoreCase(CdmQueryFactory.RANK_COLUMN))
155
            		// TODO: HACK FOR RL 2017, REMOVE AS SOON AS POSSIBLE
156
            		&& !(event.getPropertyId().toString().equalsIgnoreCase("DE"))
157
            		&& !(event.getPropertyId().toString().equalsIgnoreCase("Deutschland"))){
158
                final Item item = event.getItem();
159
                Property<?> itemProperty = item.getItemProperty("uuid");
160
                UUID uuid = UUID.fromString(itemProperty.getValue().toString());
161
                final Taxon taxon = CdmBase.deproxy(CdmSpringContextHelper.getTaxonService()
162
                		.load(uuid,Arrays.asList("descriptions.descriptionElements","name.taxonBases","updatedBy")), Taxon.class);
163
                final String areaID = (String)event.getPropertyId();
164
                PresenceAbsenceTerm presenceAbsenceTerm = null;
165
                Object statusValue = item.getItemProperty(areaID).getValue();
166
                if(statusValue instanceof String){
167
//                	presenceAbsenceTerm = TermCacher.getInstance().getPresenceAbsenceTerm((String) statusValue);
168
                    try {
169
                        presenceAbsenceTerm = (PresenceAbsenceTerm)CdmSpringContextHelper.getTermService().load(UUID.fromString((String)statusValue));
170
                    }catch(IllegalArgumentException|ClassCastException e) {
171
                        // Not a PresenceAbsenceTerm Column
172
                    }
173
                }
174
                //popup window
175
                final Window popup = new Window(Messages.getLocalizedString(Messages.DistributionTableViewBean_CHOOSE_DISTRIBUTION_STATUS));
176
                DelegatingErrorHandler errorHandler = new DelegatingErrorHandler();
177
                errorHandler.registerHandler(new HibernateSystemErrorHandler());
178
                popup.setErrorHandler(errorHandler);
179
                final ListSelect termSelect = new ListSelect();
180
                termSelect.setSizeFull();
181
                termSelect.setContainerDataSource(getPresenter().getPresenceAbsenceTermContainer());
182
                termSelect.setNullSelectionAllowed(presenceAbsenceTerm != null);
183
                if(presenceAbsenceTerm != null){
184
                	termSelect.setNullSelectionItemId(Messages.getLocalizedString(Messages.DistributionTableViewBean_NO_STATUS_SELECT));
185
                }else{
186
                    logger.debug("No distribution status exists yet for area");
187
                }
188
                termSelect.setValue(presenceAbsenceTerm);
189
                termSelect.addValueChangeListener(valueChangeEvent -> {
190
						Object distributionStatus = valueChangeEvent.getProperty().getValue();
191
						getPresenter().updateDistributionField(areaID, distributionStatus, taxon);
192
						container.refresh();
193
						popup.close();
194
				});
195
                VerticalLayout layout = new VerticalLayout(termSelect);
196
                popup.setContent(layout);
197
                popup.setModal(true);
198
                popup.center();
199
                UI.getCurrent().addWindow(popup);
200
            }
201
        });
202

    
203
		mainLayout.addComponent(table, "top:75px;right:10.0px;left:10.0px;");
204

    
205
//		grid = new Grid();
206
//		grid.setSizeFull();
207
//		grid.setEditorEnabled(true);
208
//      grid.setFooterVisible(true);
209
//		mainLayout.addComponent(grid, "top:75px;right:10.0px;left:10.0px;"); //$NON-NLS-1$
210

    
211
		return mainLayout;
212
	}
213

    
214
    /**
215
     * {@inheritDoc}
216
     */
217
	@Override
218
	public void enter(ViewChangeEvent event) {
219
	    update();
220
	}
221

    
222
    /**
223
     * {@inheritDoc}
224
     */
225
	@Override
226
	public void update(){
227
		try {
228
			container = getPresenter().getSQLContainer();
229
		} catch (SQLException e) {
230
			DistributionEditorUtil.showSqlError(e);
231
			return;
232
		}
233
		if(container==null){
234
			return;
235
		}
236

    
237
		table.setContainerDataSource(container);
238

    
239
		List<String> columnHeaders = new ArrayList<>(Arrays.asList(table.getColumnHeaders()));
240
		columnHeaders.remove(CdmQueryFactory.DTYPE_COLUMN);
241
		columnHeaders.remove(CdmQueryFactory.ID_COLUMN);
242
		columnHeaders.remove(CdmQueryFactory.UUID_COLUMN);
243
		columnHeaders.remove(CdmQueryFactory.CLASSIFICATION_COLUMN);
244
//		columnHeaders.sort(new Comparator<String>() {
245
//            @Override
246
//            public int compare(String o1, String o2) {
247
//                if(o1.equals(CdmQueryFactory.TAXON_COLUMN) || o2.equals(CdmQueryFactory.TAXON_COLUMN)) {
248
//                    return o1.equals(CdmQueryFactory.TAXON_COLUMN) ? -1 : 1;
249
//                }
250
//                if(o1.equals(CdmQueryFactory.RANK_COLUMN) || o2.equals(CdmQueryFactory.RANK_COLUMN)) {
251
//                    return o1.equals(CdmQueryFactory.RANK_COLUMN) ? -1 : 1;
252
//                }
253
//
254
//                // TODO: HACK FOR RL 2017, REMOVE AS SOON AS POSSIBLE
255
//                if(o1.equals("DE") || o1.equals("Deutschland")
256
//                        || o2.equals("DE") || o2.equals("Deutschland")) {
257
//                    return (o1.equals("DE") || o1.equals("Deutschland")) ? -1 : 1;
258
//                }
259
//
260
//                return o1.compareTo(o2);
261
//            }
262
//		});
263

    
264
		List<String> columnList = new ArrayList<>(columnHeaders);
265

    
266
		String[] string = new String[columnList.size()];
267

    
268
		table.setVisibleColumns(columnList.toArray());
269
		table.setColumnHeaders(columnList.toArray(string));
270
		table.setColumnFooter(CdmQueryFactory.TAXON_COLUMN, String.format(Messages.getLocalizedString(Messages.DistributionTableViewBean_TOTAL_TAXA), container.size()));
271

    
272
//        gridcontainer = getPresenter().getAreaDistributionStatusContainer();
273
//        if(gridcontainer==null){
274
//            return;
275
//        }
276
//
277
//        if(footerRow != null) {
278
//            grid.removeFooterRow(footerRow);
279
//        }
280
//		grid.removeAllColumns();
281
//
282
//        grid.setContainerDataSource(gridcontainer);
283
//
284
//        Column uuidColumn = grid.getColumn(DistributionStatusQuery.UUID_COLUMN);
285
//        uuidColumn.setEditable(false);
286
//        uuidColumn.setHidden(true);
287
//        Column taxonColumn = grid.getColumn(DistributionStatusQuery.TAXON_COLUMN);
288
//        taxonColumn.setEditable(false);
289
//        taxonColumn.setHeaderCaption(Messages.DistributionTableViewBean_TAXON);
290
//        taxonColumn.setLastFrozenColumn();
291
//
292
//        Converter<String, UUID> displayConverter = new PresenceAbsenceTermUuidTitleStringConverter();
293
//        Converter<Object, UUID> editorConverter = new PresenceAbsenceTermUuidObjectConverter();
294
//        for(Column c : grid.getColumns()) {
295
//            if(c.isEditable()) {
296
//                NamedArea namedArea = (NamedArea) CdmSpringContextHelper.getTermService().load((UUID.fromString(c.getHeaderCaption())));
297
//                String caption = DistributionEditorUtil.isAbbreviatedLabels() ?
298
//                        namedArea.getRepresentation(Language.DEFAULT()).getAbbreviatedLabel() : namedArea.getTitleCache();
299
//                c.setHeaderCaption(caption);
300
//                c.setConverter(displayConverter);
301
//
302
//                NativeSelect termSelect = new NativeSelect();
303
//                termSelect.setSizeFull();
304
//                termSelect.setContainerDataSource(getPresenter().getPresenceAbsenceTermContainer());
305
//                termSelect.setItemCaptionMode(ItemCaptionMode.PROPERTY);
306
//                termSelect.setItemCaptionPropertyId("titleCache"); //$NON-NLS-1$
307
//                termSelect.setConverter(editorConverter);
308
//                termSelect.setImmediate(true);
309
//                c.setEditorField(termSelect);
310
//            }
311
//        }
312
//        grid.getEditorFieldGroup().addCommitHandler(new CommitHandler() {
313
//            private static final long serialVersionUID = 7515807188410712420L;
314
//
315
//            @Override
316
//            public void preCommit(CommitEvent commitEvent) throws CommitException {
317
//
318
//            }
319
//
320
//            @Override
321
//            public void postCommit(CommitEvent commitEvent) throws CommitException {
322
//                gridcontainer.commit();
323
//            }
324
//        });
325
//
326
//        footerRow = grid.appendFooterRow();
327
//        Object[] cells = grid.getColumns().stream().map(c -> c.getPropertyId()).toArray(Object[]::new);
328
//        if(cells.length == 0) {
329
//            return;
330
//        }
331
//        FooterCell footerCell = null;
332
//        if(cells.length > 1) {
333
//            footerCell = footerRow.join(cells);
334
//        }else {
335
//            footerCell = footerRow.getCell(cells[0]);
336
//        }
337
//        footerCell.setText(String.format(Messages.DistributionTableViewBean_TOTAL_TAXA, gridcontainer.size()));
338
	}
339

    
340
	private void createEditClickListener(){
341
		//details
342
	    Button detailButton = toolbar.getDetailButton();
343
		detailButton.setCaption(Messages.getLocalizedString(Messages.DistributionTableViewBean_TAXON_DETAILS));
344
		detailButton.addClickListener(event -> {
345
				Object selectedItemId = DistributionTableViewBean.this.table.getValue();
346
//				Object selectedItemId = DistributionTableViewBean.this.grid.getSelectedRow();
347
				if(selectedItemId!=null){
348
				    final UUID uuid = UUID.fromString(table.getContainerDataSource().getItem(selectedItemId).getItemProperty("uuid").getValue().toString());
349
//					final UUID uuid = (UUID) selectedItemId;
350
					Taxon taxon = HibernateProxyHelper.deproxy(CdmSpringContextHelper.getTaxonService().load(uuid), Taxon.class);
351
//					Taxon taxon = (Taxon) CdmSpringContextHelper.getTaxonService().load(uuid);
352
					List<DescriptionElementBase> listDescriptions = getPresenter().listDescriptionElementsForTaxon(taxon, null);
353
					DetailWindow detailWindow = new DetailWindow(taxon, listDescriptions);
354
					Window window = detailWindow.createWindow();
355
					window.center();
356
					getUI().addWindow(window);
357
				}
358
				else{
359
					Notification.show(Messages.getLocalizedString(Messages.DistributionTableViewBean_SELECT_TAXON), Type.HUMANIZED_MESSAGE);
360
				}
361
			}
362
		);
363

    
364
		//area and taxon
365
		Button areaAndTaxonSettingsButton = toolbar.getDistributionSettingsButton();
366
		areaAndTaxonSettingsButton.addClickListener(event -> openAreaAndTaxonSettings());
367

    
368
		//distr status
369
		Button distrStatusButton = toolbar.getSettingsButton();
370
		distrStatusButton.addClickListener(event -> openStatusSettings());
371

    
372
	    //help
373
        Button helpButton = toolbar.getHelpButton();
374
        helpButton.addClickListener(event -> openHelpWindow());
375
	}
376

    
377
    /**
378
     * {@inheritDoc}
379
     */
380
	@Override
381
	public void openStatusSettings() {
382
        if(distributionStatusConfigWindow==null){
383
            distributionStatusConfigWindow = new DistributionStatusSettingsConfigWindow(this);
384
        }
385
        Window window  = distributionStatusConfigWindow.createWindow(Messages.getLocalizedString(Messages.DistributionTableViewBean_STATUS));
386
        window.setWidth("25%"); //$NON-NLS-1$
387
        window.setHeight("60%"); //$NON-NLS-1$
388
        UI.getCurrent().addWindow(window);
389
	}
390

    
391
    /**
392
     * {@inheritDoc}
393
     */
394
	@Override
395
	public void openAreaAndTaxonSettings() {
396
		if(areaAndTaxonConfigWindow==null){
397
			areaAndTaxonConfigWindow = new AreaAndTaxonSettingsConfigWindow(this);
398
		}
399
        Window window  = areaAndTaxonConfigWindow.createWindow(Messages.getLocalizedString(Messages.DistributionTableViewBean_AREAS_AND_TAXA));
400
        UI.getCurrent().addWindow(window);
401
	}
402

    
403
	public void openHelpWindow() {
404
	       if(helpWindow==null){
405
	           helpWindow = new HelpWindow(this);
406
	        }
407
	        Window window  = helpWindow.createWindow(Messages.getLocalizedString(Messages.DistributionToolbar_HELP));
408
	        UI.getCurrent().addWindow(window);
409
	}
410

    
411
    /**
412
     * {@inheritDoc}
413
     */
414
	@Override
415
	public boolean allowAnonymousAccess() {
416
		// TODO Auto-generated method stub
417
		return false;
418
	}
419

    
420
    /**
421
     * {@inheritDoc}
422
     */
423
	@Override
424
	public Collection<Collection<GrantedAuthority>> allowedGrantedAuthorities() {
425
		// TODO Auto-generated method stub
426
		return null;
427
	}
428

    
429
    /**
430
     * {@inheritDoc}
431
     */
432
	@Override
433
	protected String getHeaderText() {
434
		// TODO Auto-generated method stub
435
		return null;
436
	}
437

    
438
    /**
439
     * {@inheritDoc}
440
     */
441
	@Override
442
	protected String getSubHeaderText() {
443
		// TODO Auto-generated method stub
444
		return null;
445
	}
446

    
447
    /**
448
     * {@inheritDoc}
449
     */
450
	@Override
451
	protected void initContent() {
452
	    // initialize layout
453
        AbsoluteLayout mainLayout = initLayout();
454
        setCompositionRoot(mainLayout);
455
        // add click listener on DistributionToolbar-buttons
456
        createEditClickListener();
457
	}
458
}
(2-2/4)