Project

General

Profile

Download (12 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.Comparator;
16
import java.util.List;
17
import java.util.UUID;
18

    
19
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.security.core.GrantedAuthority;
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.ui.AbsoluteLayout;
27
import com.vaadin.ui.Button;
28
import com.vaadin.ui.Grid;
29
import com.vaadin.ui.ListSelect;
30
import com.vaadin.ui.Notification;
31
import com.vaadin.ui.Notification.Type;
32
import com.vaadin.ui.Table;
33
import com.vaadin.ui.UI;
34
import com.vaadin.ui.VerticalLayout;
35
import com.vaadin.ui.Window;
36

    
37
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
38
import eu.etaxonomy.cdm.model.common.CdmBase;
39
import eu.etaxonomy.cdm.model.common.Language;
40
import eu.etaxonomy.cdm.model.common.Representation;
41
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
42
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
43
import eu.etaxonomy.cdm.model.taxon.Taxon;
44
import eu.etaxonomy.cdm.vaadin.component.DetailWindow;
45
import eu.etaxonomy.cdm.vaadin.component.DistributionToolbar;
46
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
47
import eu.etaxonomy.cdm.vaadin.security.AccessRestrictedView;
48
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
49
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
50
import eu.etaxonomy.cdm.vaadin.util.DistributionEditorUtil;
51
import eu.etaxonomy.cdm.vaadin.util.TermCacher;
52
import eu.etaxonomy.cdm.vaadin.view.AbstractPageView;
53

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

    
64
	private static final long serialVersionUID = 1L;
65
    public static final String NAME = "distTable";
66

    
67
    @Autowired
68
    private DistributionToolbar toolbar;
69

    
70
	private Table table;
71
	private Grid grid;
72

    
73
    private CdmSQLContainer container;
74
	private AreaAndTaxonSettingsConfigWindow areaAndTaxonConfigWindow;;
75
	private DistributionStatusSettingsConfigWindow distributionStatusConfigWindow;
76

    
77
	public DistributionTableViewBean() {
78
		super();
79
	}
80

    
81
	private AbsoluteLayout initLayout() {
82
		AbsoluteLayout mainLayout = new AbsoluteLayout();
83
		mainLayout.setImmediate(false);
84
		mainLayout.setWidth("100%");
85
		mainLayout.setHeight("100%");
86

    
87
		setWidth("100.0%");
88
		setHeight("100.0%");
89

    
90
		//Horizontal Toolbar
91
		mainLayout.addComponent(toolbar, "top:0.0px;right:0.0px;");
92

    
93
		// table + formatting
94
		table = new Table(){
95
			private static final long serialVersionUID = -5148756917468804385L;
96

    
97
			@Override
98
			protected String formatPropertyValue(Object rowId, Object colId, Property<?> property) {
99
				String formattedValue = null;
100
				PresenceAbsenceTerm presenceAbsenceTerm = null;
101
				Object value = property.getValue();
102
				if(value instanceof String){
103
					presenceAbsenceTerm = TermCacher.getInstance().getPresenceAbsenceTerm((String) value);
104
				}
105
				if(presenceAbsenceTerm != null){
106
					Representation representation = presenceAbsenceTerm.getRepresentation(Language.DEFAULT());
107
					if(representation!=null){
108
						if(DistributionEditorUtil.isAbbreviatedLabels()){
109
							formattedValue = representation.getAbbreviatedLabel();
110
						}
111
						else{
112
							formattedValue = representation.getLabel();
113
						}
114
					}
115
					if(formattedValue==null){
116
						formattedValue = presenceAbsenceTerm.getTitleCache();
117
					}
118
					return formattedValue;
119
				}
120
				return super.formatPropertyValue(rowId, colId, property);
121
			}
122
		};
123
		table.setImmediate(false);
124
		table.setWidth("100.0%");
125
		table.setHeight("100.0%");
126

    
127
        table.setColumnReorderingAllowed(true);
128
        table.setSortEnabled(false);
129

    
130
        table.setColumnCollapsingAllowed(true);
131
        table.setSelectable(true);
132
        table.setPageLength(20);
133
        table.setFooterVisible(true);
134
        table.setCacheRate(20);
135

    
136
		table.addItemClickListener(event -> {
137
            if(!(event.getPropertyId().toString().equalsIgnoreCase(CdmQueryFactory.TAXON_COLUMN))
138
            		&& !(event.getPropertyId().toString().equalsIgnoreCase(CdmQueryFactory.RANK_COLUMN))
139
            		// TODO: HACK FOR RL 2017, REMOVE AS SOON AS POSSIBLE
140
            		&& !(event.getPropertyId().toString().equalsIgnoreCase("DE"))
141
            		&& !(event.getPropertyId().toString().equalsIgnoreCase("Deutschland"))){
142
                final Item item = event.getItem();
143
                Property<?> itemProperty = item.getItemProperty("uuid");
144
                UUID uuid = UUID.fromString(itemProperty.getValue().toString());
145
                final Taxon taxon = CdmBase.deproxy(CdmSpringContextHelper.getTaxonService()
146
                		.load(uuid,Arrays.asList("descriptions.descriptionElements","name.taxonBases","updatedBy")), Taxon.class);
147
                final String areaID = (String)event.getPropertyId();
148
                PresenceAbsenceTerm presenceAbsenceTerm = null;
149
                Object statusValue = item.getItemProperty(areaID).getValue();
150
                if(statusValue instanceof String){
151
                	presenceAbsenceTerm = TermCacher.getInstance().getPresenceAbsenceTerm((String) statusValue);
152
                }
153
                //popup window
154
                final Window popup = new Window("Choose distribution status");
155
                final ListSelect termSelect = new ListSelect();
156
                termSelect.setSizeFull();
157
                termSelect.setContainerDataSource(getPresenter().getPresenceAbsenceTermContainer());
158
                termSelect.setNullSelectionAllowed(presenceAbsenceTerm != null);
159
                if(presenceAbsenceTerm != null){
160
                	termSelect.setNullSelectionItemId("[no status]");
161
                }else{
162
                    logger.debug("No distribution status exists yet for area");
163
                }
164
                termSelect.setValue(presenceAbsenceTerm);
165
                termSelect.addValueChangeListener(valueChangeEvent -> {
166
//						System.out.println(valueChangeEvent);
167
						Object distributionStatus = valueChangeEvent.getProperty().getValue();
168
						getPresenter().updateDistributionField(areaID, distributionStatus, taxon);
169
						container.refresh();
170
						popup.close();
171
				});
172
                VerticalLayout layout = new VerticalLayout(termSelect);
173
                popup.setContent(layout);
174
                popup.setModal(true);
175
                popup.center();
176
                UI.getCurrent().addWindow(popup);
177
            }
178
        });
179

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

    
182
		return mainLayout;
183
	}
184

    
185
    /**
186
     * {@inheritDoc}
187
     */
188
	@Override
189
	public void enter(ViewChangeEvent event) {
190
	    update();
191
	}
192

    
193
    /**
194
     * {@inheritDoc}
195
     */
196
	@Override
197
	public void update(){
198
		try {
199
			container = getPresenter().getSQLContainer();
200
		} catch (SQLException e) {
201
			DistributionEditorUtil.showSqlError(e);
202
			return;
203
		}
204
		if(container==null){
205
			return;
206
		}
207

    
208
		table.setContainerDataSource(container);
209

    
210
		List<String> columnHeaders = new ArrayList<>(Arrays.asList(table.getColumnHeaders()));
211
		columnHeaders.remove(CdmQueryFactory.DTYPE_COLUMN);
212
		columnHeaders.remove(CdmQueryFactory.ID_COLUMN);
213
		columnHeaders.remove(CdmQueryFactory.UUID_COLUMN);
214
		columnHeaders.remove(CdmQueryFactory.CLASSIFICATION_COLUMN);
215
		columnHeaders.sort(new Comparator<String>() {
216
            @Override
217
            public int compare(String o1, String o2) {
218
                if(o1.equals(CdmQueryFactory.TAXON_COLUMN) || o2.equals(CdmQueryFactory.TAXON_COLUMN)) {
219
                    return o1.equals(CdmQueryFactory.TAXON_COLUMN) ? -1 : 1;
220
                }
221
                if(o1.equals(CdmQueryFactory.RANK_COLUMN) || o2.equals(CdmQueryFactory.RANK_COLUMN)) {
222
                    return o1.equals(CdmQueryFactory.RANK_COLUMN) ? -1 : 1;
223
                }
224

    
225
                // TODO: HACK FOR RL 2017, REMOVE AS SOON AS POSSIBLE
226
                if(o1.equals("DE") || o1.equals("Deutschland")
227
                        || o2.equals("DE") || o2.equals("Deutschland")) {
228
                    return (o1.equals("DE") || o1.equals("Deutschland")) ? -1 : 1;
229
                }
230

    
231
                return o1.compareTo(o2);
232
            }
233
		});
234

    
235
		List<String> columnList = new ArrayList<>(columnHeaders);
236

    
237
		String[] string = new String[columnList.size()];
238

    
239
		table.setVisibleColumns(columnList.toArray());
240
		table.setColumnHeaders(columnList.toArray(string));
241
		table.setColumnFooter(CdmQueryFactory.TAXON_COLUMN, "Total amount of Taxa displayed: " + container.size());
242
	}
243

    
244
	private void createEditClickListener(){
245
		//details
246
	    Button detailButton = toolbar.getDetailButton();
247
		detailButton.setCaption("Taxon Details");
248
		detailButton.addClickListener(event -> {
249
				Object selectedItemId = DistributionTableViewBean.this.table.getValue();
250
//				Object selectedItemId = DistributionTableViewBean.this.grid.getSelectedRow();
251
				if(selectedItemId!=null){
252
				    final UUID uuid = UUID.fromString(table.getContainerDataSource().getItem(selectedItemId).getItemProperty("uuid").getValue().toString());
253
//					final UUID uuid = UUID.fromString(grid.getContainerDataSource().getItem(selectedItemId).getItemProperty("uuid").getValue().toString());
254
					Taxon taxon = HibernateProxyHelper.deproxy(CdmSpringContextHelper.getTaxonService().load(uuid), Taxon.class);
255
					List<DescriptionElementBase> listDescriptions = getPresenter().listDescriptionElementsForTaxon(taxon, null);
256
					DetailWindow detailWindow = new DetailWindow(taxon, listDescriptions);
257
					Window window = detailWindow.createWindow();
258
					window.center();
259
					getUI().addWindow(window);
260
				}
261
				else{
262
					Notification.show("Please select a taxon", Type.HUMANIZED_MESSAGE);
263
				}
264
			}
265
		);
266

    
267
		//area and taxon
268
		Button areaAndTaxonSettingsButton = toolbar.getDistributionSettingsButton();
269
		areaAndTaxonSettingsButton.addClickListener(event -> openAreaAndTaxonSettings());
270

    
271
		//distr status
272
		Button distrStatusButton = toolbar.getSettingsButton();
273
		distrStatusButton.addClickListener(event -> openStatusSettings());
274
	}
275

    
276
    /**
277
     * {@inheritDoc}
278
     */
279
	@Override
280
	public void openStatusSettings() {
281
        if(distributionStatusConfigWindow==null){
282
            distributionStatusConfigWindow = new DistributionStatusSettingsConfigWindow(this);
283
        }
284
        Window window  = distributionStatusConfigWindow.createWindow("Status");
285
        window.setWidth("25%");
286
        window.setHeight("60%");
287
        UI.getCurrent().addWindow(window);
288
	}
289

    
290
    /**
291
     * {@inheritDoc}
292
     */
293
	@Override
294
	public void openAreaAndTaxonSettings() {
295
		if(areaAndTaxonConfigWindow==null){
296
			areaAndTaxonConfigWindow = new AreaAndTaxonSettingsConfigWindow(this);
297
		}
298
        Window window  = areaAndTaxonConfigWindow.createWindow("Areas and Taxa");
299
        UI.getCurrent().addWindow(window);
300
	}
301

    
302
    /**
303
     * {@inheritDoc}
304
     */
305
	@Override
306
	public boolean allowAnonymousAccess() {
307
		// TODO Auto-generated method stub
308
		return false;
309
	}
310

    
311
    /**
312
     * {@inheritDoc}
313
     */
314
	@Override
315
	public Collection<Collection<GrantedAuthority>> allowedGrantedAuthorities() {
316
		// TODO Auto-generated method stub
317
		return null;
318
	}
319

    
320
    /**
321
     * {@inheritDoc}
322
     */
323
	@Override
324
	protected String getHeaderText() {
325
		// TODO Auto-generated method stub
326
		return null;
327
	}
328

    
329
    /**
330
     * {@inheritDoc}
331
     */
332
	@Override
333
	protected String getSubHeaderText() {
334
		// TODO Auto-generated method stub
335
		return null;
336
	}
337

    
338
    /**
339
     * {@inheritDoc}
340
     */
341
	@Override
342
	protected void initContent() {
343
	    // initialize layout
344
        AbsoluteLayout mainLayout = initLayout();
345
        setCompositionRoot(mainLayout);
346
        // add click listener on DistributionToolbar-buttons
347
        createEditClickListener();
348
	}
349
}
(4-4/7)