Project

General

Profile

Download (17.2 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.util.DistributionStatusQuery;
61
import eu.etaxonomy.cdm.vaadin.view.AbstractPageView;
62

    
63
/**
64
 * The main view of the distribution status editor.
65
 * 
66
 * @author freimeier
67
 * @since 18.10.2017
68
 *
69
 */
70
@ViewScope
71
@SpringView(name=DistributionTableViewBean.NAME)
72
public class DistributionTableViewBean
73
            extends AbstractPageView<DistributionTablePresenter>
74
            implements IDistributionTableView, AccessRestrictedView {
75

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

    
79
    @Autowired
80
    private DistributionToolbar toolbar;
81

    
82
	private Table table;
83
	private Grid grid;
84
	private FooterRow footerRow;
85

    
86
    private CdmSQLContainer container;
87
    private LazyQueryContainer gridcontainer;
88
	private AreaAndTaxonSettingsConfigWindow areaAndTaxonConfigWindow;;
89
	private DistributionStatusSettingsConfigWindow distributionStatusConfigWindow;
90
	private HelpWindow helpWindow;
91

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

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

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

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

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

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

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

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

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

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

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

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

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

    
224
		return mainLayout;
225
	}
226

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

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

    
250
		table.setContainerDataSource(container);
251

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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