Project

General

Profile

Download (17.7 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.orm.hibernate5.HibernateSystemException;
20
import org.springframework.security.core.GrantedAuthority;
21
import org.vaadin.addons.lazyquerycontainer.LazyQueryContainer;
22

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

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

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

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

    
76
    @Autowired
77
    private DistributionToolbar toolbar;
78

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

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

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

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

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

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

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

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

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

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

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

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

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

    
212
		return mainLayout;
213
	}
214

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

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

    
238
		table.setContainerDataSource(container);
239

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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