Project

General

Profile

Download (16.5 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.DetailWindow;
48
import eu.etaxonomy.cdm.vaadin.component.DistributionToolbar;
49
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
50
import eu.etaxonomy.cdm.vaadin.security.AccessRestrictedView;
51
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
52
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
53
import eu.etaxonomy.cdm.vaadin.util.DistributionEditorUtil;
54
import eu.etaxonomy.cdm.vaadin.view.AbstractPageView;
55

    
56
/**
57
 * @author freimeier
58
 * @since 18.10.2017
59
 *
60
 */
61
@ViewScope
62
@SpringView(name=DistributionTableViewBean.NAME)
63
public class DistributionTableViewBean
64
            extends AbstractPageView<DistributionTablePresenter>
65
            implements IDistributionTableView, AccessRestrictedView {
66

    
67
	private static final long serialVersionUID = 1L;
68
    public static final String NAME = "distGrid"; //$NON-NLS-1$
69

    
70
    @Autowired
71
    private DistributionToolbar toolbar;
72

    
73
	private Table table;
74
	private Grid grid;
75
	private FooterRow footerRow;
76

    
77
    private CdmSQLContainer container;
78
    private LazyQueryContainer gridcontainer;
79
	private AreaAndTaxonSettingsConfigWindow areaAndTaxonConfigWindow;;
80
	private DistributionStatusSettingsConfigWindow distributionStatusConfigWindow;
81

    
82
	public DistributionTableViewBean() {
83
		super();
84
	}
85

    
86
	private AbsoluteLayout initLayout() {
87
		AbsoluteLayout mainLayout = new AbsoluteLayout();
88
		mainLayout.setImmediate(false);
89
		mainLayout.setWidth("100%"); //$NON-NLS-1$
90
		mainLayout.setHeight("100%"); //$NON-NLS-1$
91

    
92
		setWidth("100.0%"); //$NON-NLS-1$
93
		setHeight("100.0%"); //$NON-NLS-1$
94

    
95
		//Horizontal Toolbar
96
		mainLayout.addComponent(toolbar, "top:0.0px;right:0.0px;"); //$NON-NLS-1$
97

    
98
		// table + formatting
99
		table = new Table(){
100
			private static final long serialVersionUID = -5148756917468804385L;
101

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

    
137
        table.setColumnReorderingAllowed(true);
138
        table.setSortEnabled(false);
139

    
140
        table.setColumnCollapsingAllowed(true);
141
        table.setSelectable(true);
142
        table.setPageLength(20);
143
        table.setFooterVisible(true);
144
        table.setCacheRate(20);
145

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

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

    
196
//		grid = new Grid();
197
//		grid.setSizeFull();
198
//		grid.setEditorEnabled(true);
199
//      grid.setFooterVisible(true);
200
//		mainLayout.addComponent(grid, "top:75px;right:10.0px;left:10.0px;"); //$NON-NLS-1$
201

    
202
		return mainLayout;
203
	}
204

    
205
    /**
206
     * {@inheritDoc}
207
     */
208
	@Override
209
	public void enter(ViewChangeEvent event) {
210
	    update();
211
	}
212

    
213
    /**
214
     * {@inheritDoc}
215
     */
216
	@Override
217
	public void update(){
218
		try {
219
			container = getPresenter().getSQLContainer();
220
		} catch (SQLException e) {
221
			DistributionEditorUtil.showSqlError(e);
222
			return;
223
		}
224
		if(container==null){
225
			return;
226
		}
227

    
228
		table.setContainerDataSource(container);
229

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

    
255
		List<String> columnList = new ArrayList<>(columnHeaders);
256

    
257
		String[] string = new String[columnList.size()];
258

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

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

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

    
355
		//area and taxon
356
		Button areaAndTaxonSettingsButton = toolbar.getDistributionSettingsButton();
357
		areaAndTaxonSettingsButton.addClickListener(event -> openAreaAndTaxonSettings());
358

    
359
		//distr status
360
		Button distrStatusButton = toolbar.getSettingsButton();
361
		distrStatusButton.addClickListener(event -> openStatusSettings());
362
	}
363

    
364
    /**
365
     * {@inheritDoc}
366
     */
367
	@Override
368
	public void openStatusSettings() {
369
        if(distributionStatusConfigWindow==null){
370
            distributionStatusConfigWindow = new DistributionStatusSettingsConfigWindow(this);
371
        }
372
        Window window  = distributionStatusConfigWindow.createWindow(Messages.getLocalizedString(Messages.DistributionTableViewBean_STATUS));
373
        window.setWidth("25%"); //$NON-NLS-1$
374
        window.setHeight("60%"); //$NON-NLS-1$
375
        UI.getCurrent().addWindow(window);
376
	}
377

    
378
    /**
379
     * {@inheritDoc}
380
     */
381
	@Override
382
	public void openAreaAndTaxonSettings() {
383
		if(areaAndTaxonConfigWindow==null){
384
			areaAndTaxonConfigWindow = new AreaAndTaxonSettingsConfigWindow(this);
385
		}
386
        Window window  = areaAndTaxonConfigWindow.createWindow(Messages.getLocalizedString(Messages.DistributionTableViewBean_AREAS_AND_TAXA));
387
        UI.getCurrent().addWindow(window);
388
	}
389

    
390
    /**
391
     * {@inheritDoc}
392
     */
393
	@Override
394
	public boolean allowAnonymousAccess() {
395
		// TODO Auto-generated method stub
396
		return false;
397
	}
398

    
399
    /**
400
     * {@inheritDoc}
401
     */
402
	@Override
403
	public Collection<Collection<GrantedAuthority>> allowedGrantedAuthorities() {
404
		// TODO Auto-generated method stub
405
		return null;
406
	}
407

    
408
    /**
409
     * {@inheritDoc}
410
     */
411
	@Override
412
	protected String getHeaderText() {
413
		// TODO Auto-generated method stub
414
		return null;
415
	}
416

    
417
    /**
418
     * {@inheritDoc}
419
     */
420
	@Override
421
	protected String getSubHeaderText() {
422
		// TODO Auto-generated method stub
423
		return null;
424
	}
425

    
426
    /**
427
     * {@inheritDoc}
428
     */
429
	@Override
430
	protected void initContent() {
431
	    // initialize layout
432
        AbsoluteLayout mainLayout = initLayout();
433
        setCompositionRoot(mainLayout);
434
        // add click listener on DistributionToolbar-buttons
435
        createEditClickListener();
436
	}
437
}
(4-4/7)