Project

General

Profile

Download (17.4 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.location.NamedArea;
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.HibernateExceptionHandler;
56
import eu.etaxonomy.cdm.vaadin.permission.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
 * The main view of the distribution status editor.
64
 *
65
 * @author freimeier
66
 * @since 18.10.2017
67
 *
68
 */
69
@ViewScope
70
@SpringView(name=DistributionTableViewBean.NAME)
71
public class DistributionTableViewBean extends AbstractPageView<DistributionTablePresenter>
72
            implements IDistributionTableView, AccessRestrictedView {
73

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

    
77
    @Autowired
78
    private DistributionToolbar toolbar;
79

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

    
84
    private CdmSQLContainer container;
85
    private LazyQueryContainer gridcontainer;
86
	private AreaAndTaxonSettingsConfigWindow areaAndTaxonConfigWindow;;
87
	private DistributionStatusSettingsConfigWindow distributionStatusConfigWindow;
88
	private HelpWindow helpWindow;
89
    private String accessDeniedMessage;
90

    
91
	/**
92
	 * Creates a new distribution status editor view.
93
	 */
94
	public DistributionTableViewBean() {
95
		super();
96
	}
97

    
98
	/**
99
	 * Initializes the layout of the view,
100
	 * adds the {@link DistributionToolbar} and
101
	 * creates the distribution status table and adds click listener for editing to it.
102
	 * @return Layout of the view.
103
	 */
104
	private AbsoluteLayout initLayout() {
105
		AbsoluteLayout mainLayout = new AbsoluteLayout();
106
		mainLayout.setImmediate(false);
107
		mainLayout.setWidth("100%"); //$NON-NLS-1$
108
		mainLayout.setHeight("100%"); //$NON-NLS-1$
109

    
110
		setWidth("100.0%"); //$NON-NLS-1$
111
		setHeight("100.0%"); //$NON-NLS-1$
112

    
113
		//Horizontal Toolbar
114
		mainLayout.addComponent(toolbar, "top:0.0px;right:0.0px;"); //$NON-NLS-1$
115

    
116
		// table + formatting
117
		table = new Table(){
118
			private static final long serialVersionUID = -5148756917468804385L;
119

    
120
			@Override
121
			protected String formatPropertyValue(Object rowId, Object colId, Property<?> property) {
122
				String formattedValue = null;
123
				PresenceAbsenceTerm presenceAbsenceTerm = null;
124
				Object value = property.getValue();
125
				if(value instanceof String){
126
                    try {
127
                        presenceAbsenceTerm = (PresenceAbsenceTerm)CdmSpringContextHelper.getTermService().load(UUID.fromString((String)value));
128
                    }catch(IllegalArgumentException|ClassCastException e) {
129
                        // Not a PresenceAbsenceTerm Column
130
                    }
131
				}
132
				if(presenceAbsenceTerm != null){
133
					Representation representation = presenceAbsenceTerm.getRepresentation(Language.DEFAULT());
134
					if(representation!=null){
135
						if(DistributionEditorUtil.isAbbreviatedLabels()){
136
							formattedValue = representation.getAbbreviatedLabel();
137
						}
138
						else{
139
							formattedValue = representation.getLabel();
140
						}
141
					}
142
					if(formattedValue==null){
143
						formattedValue = presenceAbsenceTerm.getTitleCache();
144
					}
145
					return formattedValue;
146
				}
147
				return super.formatPropertyValue(rowId, colId, property);
148
			}
149
		};
150
		table.setImmediate(false);
151
		table.setWidth("100.0%");
152
		table.setHeight("100.0%");
153

    
154
        table.setColumnReorderingAllowed(true);
155
        table.setSortEnabled(false);
156

    
157
        table.setColumnCollapsingAllowed(true);
158
        table.setSelectable(true);
159
        table.setPageLength(20);
160
        table.setFooterVisible(true);
161
        table.setCacheRate(20);
162

    
163
		table.addItemClickListener(event -> {
164
            if(!(event.getPropertyId().toString().equalsIgnoreCase(CdmQueryFactory.TAXON_COLUMN))
165
            		&& !(event.getPropertyId().toString().equalsIgnoreCase(CdmQueryFactory.RANK_COLUMN))){
166

    
167
                final String areaString = (String)event.getPropertyId();
168
                final NamedArea area = getPresenter().getAreaFromString(areaString);
169

    
170
                if(!getPresenter().getReadOnlyAreas().contains(area)) {
171
                    final Item item = event.getItem();
172
                    Property<?> itemProperty = item.getItemProperty(CdmQueryFactory.UUID_COLUMN);
173
                    UUID uuid = UUID.fromString(itemProperty.getValue().toString());
174
                    final Taxon taxon = CdmBase.deproxy(CdmSpringContextHelper.getTaxonService()
175
                    		.load(uuid,Arrays.asList("descriptions.descriptionElements","name.taxonBases","updatedBy")), Taxon.class);
176
                    PresenceAbsenceTerm presenceAbsenceTerm = null;
177
                    Object statusValue = item.getItemProperty(areaString).getValue();
178
                    if(statusValue instanceof String){
179
                        try {
180
                            presenceAbsenceTerm = (PresenceAbsenceTerm)CdmSpringContextHelper.getTermService().load(UUID.fromString((String)statusValue));
181
                        }catch(IllegalArgumentException|ClassCastException e) {
182
                            // Not a PresenceAbsenceTerm Column
183
                        }
184
                    }
185
                    //popup window
186
                    final Window popup = new Window(Messages.getLocalizedString(Messages.DistributionTableViewBean_CHOOSE_DISTRIBUTION_STATUS));
187
                    DelegatingErrorHandler errorHandler = new DelegatingErrorHandler();
188
                    errorHandler.registerHandler(new HibernateExceptionHandler());
189
                    popup.setErrorHandler(errorHandler);
190
                    final ListSelect termSelect = new ListSelect();
191
                    termSelect.setSizeFull();
192
                    termSelect.setContainerDataSource(getPresenter().getPresenceAbsenceTermContainer());
193
                    termSelect.setNullSelectionAllowed(presenceAbsenceTerm != null);
194
                    if(presenceAbsenceTerm != null){
195
                    	termSelect.setNullSelectionItemId(Messages.getLocalizedString(Messages.DistributionTableViewBean_NO_STATUS_SELECT));
196
                    }else{
197
                        logger.debug("No distribution status exists yet for area");
198
                    }
199
                    termSelect.setValue(presenceAbsenceTerm);
200
                    termSelect.addValueChangeListener(valueChangeEvent -> {
201
    						PresenceAbsenceTerm distributionStatus = (PresenceAbsenceTerm) valueChangeEvent.getProperty().getValue();
202
    						getPresenter().updateDistributionField(area, distributionStatus, taxon);
203
    						container.refresh();
204
    						popup.close();
205
    				});
206
                    VerticalLayout layout = new VerticalLayout(termSelect);
207
                    popup.setContent(layout);
208
                    popup.setModal(true);
209
                    popup.center();
210
                    UI.getCurrent().addWindow(popup);
211
                }
212
            }
213
        });
214

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

    
217
//		grid = new Grid();
218
//		grid.setSizeFull();
219
//		grid.setEditorEnabled(true);
220
//      grid.setFooterVisible(true);
221
//		mainLayout.addComponent(grid, "top:75px;right:10.0px;left:10.0px;"); //$NON-NLS-1$
222

    
223
		return mainLayout;
224
	}
225

    
226
    /**
227
     * {@inheritDoc}
228
     */
229
	@Override
230
	public void enter(ViewChangeEvent event) {
231
	    update();
232
	}
233

    
234
    /**
235
     * {@inheritDoc}
236
     */
237
	@Override
238
	public void update(){
239
		try {
240
			container = getPresenter().getSQLContainer();
241
		} catch (SQLException e) {
242
			DistributionEditorUtil.showSqlError(e);
243
			return;
244
		}
245
		if(container==null){
246
			return;
247
		}
248

    
249
		table.setContainerDataSource(container);
250

    
251
		List<String> columnHeaders = new ArrayList<>(Arrays.asList(table.getColumnHeaders()));
252
		columnHeaders.remove(CdmQueryFactory.DTYPE_COLUMN);
253
		columnHeaders.remove(CdmQueryFactory.ID_COLUMN);
254
		columnHeaders.remove(CdmQueryFactory.UUID_COLUMN);
255
		columnHeaders.remove(CdmQueryFactory.CLASSIFICATION_COLUMN);
256

    
257
		List<String> columnList = new ArrayList<>(columnHeaders);
258

    
259
		String[] string = new String[columnList.size()];
260

    
261
		table.setVisibleColumns(columnList.toArray());
262
		table.setColumnHeaders(columnList.toArray(string));
263
		table.setColumnFooter(CdmQueryFactory.TAXON_COLUMN, String.format(Messages.getLocalizedString(Messages.DistributionTableViewBean_TOTAL_TAXA), container.size()));
264

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

    
333
	/**
334
	 * Adds click listener to the buttons defined in the {@link DistributionToolbar}.
335
	 */
336
	private void createEditClickListener(){
337
		//details
338
	    Button detailButton = toolbar.getDetailButton();
339
		detailButton.setCaption(Messages.getLocalizedString(Messages.DistributionTableViewBean_TAXON_DETAILS));
340
		detailButton.addClickListener(event -> {
341
				Object selectedItemId = DistributionTableViewBean.this.table.getValue();
342
//				Object selectedItemId = DistributionTableViewBean.this.grid.getSelectedRow();
343
				if(selectedItemId!=null){
344
				    final UUID uuid = UUID.fromString(table.getContainerDataSource().getItem(selectedItemId).getItemProperty(CdmQueryFactory.UUID_COLUMN).getValue().toString());
345
//					final UUID uuid = (UUID) selectedItemId;
346
					Taxon taxon = HibernateProxyHelper.deproxy(CdmSpringContextHelper.getTaxonService().load(uuid), Taxon.class);
347
//					Taxon taxon = (Taxon) CdmSpringContextHelper.getTaxonService().load(uuid);
348
					List<DescriptionElementBase> listDescriptions = getPresenter().listDescriptionElementsForTaxon(taxon, null);
349
					DetailWindow detailWindow = new DetailWindow(taxon, listDescriptions);
350
					Window window = detailWindow.createWindow();
351
					window.center();
352
					getUI().addWindow(window);
353
				}
354
				else{
355
					Notification.show(Messages.getLocalizedString(Messages.DistributionTableViewBean_SELECT_TAXON), Type.HUMANIZED_MESSAGE);
356
				}
357
			}
358
		);
359

    
360
		//area and taxon
361
		Button areaAndTaxonSettingsButton = toolbar.getDistributionSettingsButton();
362
		areaAndTaxonSettingsButton.addClickListener(event -> openAreaAndTaxonSettings());
363

    
364
		//distr status
365
		Button distrStatusButton = toolbar.getSettingsButton();
366
		distrStatusButton.addClickListener(event -> openStatusSettings());
367

    
368
	    //help
369
        Button helpButton = toolbar.getHelpButton();
370
        helpButton.addClickListener(event -> openHelpWindow());
371
	}
372

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

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

    
399
	public void openHelpWindow() {
400
	       if(helpWindow==null){
401
	           helpWindow = new HelpWindow(this);
402
	        }
403
	        Window window  = helpWindow.createWindow(Messages.getLocalizedString(Messages.DistributionToolbar_HELP));
404
	        UI.getCurrent().addWindow(window);
405
	}
406

    
407
    /**
408
     * {@inheritDoc}
409
     */
410
	@Override
411
	public boolean allowAnonymousAccess() {
412
		// TODO Auto-generated method stub
413
		return false;
414
	}
415

    
416
    /**
417
     * {@inheritDoc}
418
     */
419
	@Override
420
	public Collection<Collection<GrantedAuthority>> allowedGrantedAuthorities() {
421
		// TODO Auto-generated method stub
422
		return null;
423
	}
424

    
425
    @Override
426
    public String getAccessDeniedMessage() {
427
        return accessDeniedMessage;
428
    }
429

    
430
    @Override
431
    public void setAccessDeniedMessage(String accessDeniedMessage) {
432
        this.accessDeniedMessage = accessDeniedMessage;
433

    
434
    }
435

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

    
445
    /**
446
     * {@inheritDoc}
447
     */
448
	@Override
449
	protected String getSubHeaderText() {
450
		// TODO Auto-generated method stub
451
		return null;
452
	}
453

    
454
    /**
455
     * {@inheritDoc}
456
     */
457
	@Override
458
	protected void initContent() {
459
	    // initialize layout
460
        AbsoluteLayout mainLayout = initLayout();
461
        setCompositionRoot(mainLayout);
462
        // add click listener on DistributionToolbar-buttons
463
        createEditClickListener();
464
	}
465
}
(2-2/4)