Project

General

Profile

Download (10 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.security.core.GrantedAuthority;
19

    
20
import com.vaadin.data.Item;
21
import com.vaadin.data.Property;
22
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
23
import com.vaadin.spring.annotation.SpringView;
24
import com.vaadin.ui.AbsoluteLayout;
25
import com.vaadin.ui.Button;
26
import com.vaadin.ui.Grid;
27
import com.vaadin.ui.ListSelect;
28
import com.vaadin.ui.Notification;
29
import com.vaadin.ui.Notification.Type;
30
import com.vaadin.ui.Table;
31
import com.vaadin.ui.UI;
32
import com.vaadin.ui.VerticalLayout;
33
import com.vaadin.ui.Window;
34

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

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

    
60
	private static final long serialVersionUID = 1L;
61
    public static final String NAME = "distTable";
62

    
63
    private HorizontalToolbar toolbar;
64
	private Table table;
65
	private Grid grid;
66

    
67
    private CdmSQLContainer container;
68
	private DistributionSettingsConfigWindow distributionSettingConfigWindow;
69

    
70
	public DistributionTableViewBean() {
71
		super();
72
	}
73

    
74
	private AbsoluteLayout initLayout() {
75
		AbsoluteLayout mainLayout = new AbsoluteLayout();
76
		mainLayout.setImmediate(false);
77
		mainLayout.setWidth("100%");
78
		mainLayout.setHeight("100%");
79

    
80
		setWidth("100.0%");
81
		setHeight("100.0%");
82

    
83
		//Horizontal Toolbar
84
		toolbar = new HorizontalToolbar();
85
		mainLayout.addComponent(toolbar, "top:0.0px;right:0.0px;");
86

    
87
		// table + formatting
88
		table = new Table(){
89
			private static final long serialVersionUID = -5148756917468804385L;
90

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

    
121
        table.setColumnReorderingAllowed(true);
122
        table.setSortEnabled(false);
123

    
124
        table.setColumnCollapsingAllowed(true);
125
        table.setSelectable(true);
126
        table.setPageLength(20);
127
        table.setFooterVisible(true);
128
        table.setCacheRate(20);
129

    
130
		table.addItemClickListener(event -> {
131
            if(!(event.getPropertyId().toString().equalsIgnoreCase(CdmQueryFactory.TAXON_COLUMN))
132
            		&& !(event.getPropertyId().toString().equalsIgnoreCase(CdmQueryFactory.RANK_COLUMN))){
133
                final Item item = event.getItem();
134
                Property<?> itemProperty = item.getItemProperty("uuid");
135
                UUID uuid = UUID.fromString(itemProperty.getValue().toString());
136
                final Taxon taxon = HibernateProxyHelper.deproxy(CdmSpringContextHelper.getTaxonService()
137
                		.load(uuid,Arrays.asList("descriptions.descriptionElements","name.taxonBases","updatedBy")), Taxon.class);
138
                final String areaID = (String) event.getPropertyId();
139
                PresenceAbsenceTerm presenceAbsenceTerm = null;
140
                Object statusValue = item.getItemProperty(areaID).getValue();
141
                if(statusValue instanceof String){
142
                	presenceAbsenceTerm = TermCacher.getInstance().getPresenceAbsenceTerm((String) statusValue);
143
                }
144
                //popup window
145
                final Window popup = new Window("Choose distribution status");
146
                final ListSelect termSelect = new ListSelect();
147
                termSelect.setSizeFull();
148
                termSelect.setContainerDataSource(PresenceAbsenceTermContainer.getInstance());
149
                termSelect.setNullSelectionAllowed(presenceAbsenceTerm!=null);
150
                if(presenceAbsenceTerm!=null){
151
                	termSelect.setNullSelectionItemId("[no status]");
152
                }
153
                termSelect.setValue(presenceAbsenceTerm);
154
                termSelect.addValueChangeListener(valueChangeEvent -> {
155
						System.out.println(valueChangeEvent);
156
						Object distributionStatus = valueChangeEvent.getProperty().getValue();
157
						getPresenter().updateDistributionField(areaID, distributionStatus, taxon);
158
						container.refresh();
159
						popup.close();
160
				});
161
                VerticalLayout layout = new VerticalLayout(termSelect);
162
                popup.setContent(layout);
163
                popup.setModal(true);
164
                popup.center();
165
                UI.getCurrent().addWindow(popup);
166
            }
167
        });
168

    
169
		mainLayout.addComponent(table, "top:75px;right:0.0px;");
170

    
171
		return mainLayout;
172
	}
173

    
174
    /**
175
     * {@inheritDoc}
176
     */
177
	@Override
178
	public void enter(ViewChangeEvent event) {
179
	    update();
180
	}
181

    
182
    /**
183
     * {@inheritDoc}
184
     */
185
	@Override
186
	public void update(){
187
		try {
188
			container = getPresenter().getSQLContainer();
189
		} catch (SQLException e) {
190
			DistributionEditorUtil.showSqlError(e);
191
			return;
192
		}
193
		if(container==null){
194
			return;
195
		}
196

    
197
		table.setContainerDataSource(container);
198

    
199
		List<String> columnHeaders = new ArrayList<>(Arrays.asList(table.getColumnHeaders()));
200
		columnHeaders.remove(CdmQueryFactory.DTYPE_COLUMN);
201
		columnHeaders.remove(CdmQueryFactory.ID_COLUMN);
202
		columnHeaders.remove(CdmQueryFactory.UUID_COLUMN);
203
		columnHeaders.remove(CdmQueryFactory.CLASSIFICATION_COLUMN);
204

    
205
		List<String> columnList = new ArrayList<>(columnHeaders);
206

    
207
		String[] string = new String[columnList.size()];
208

    
209
		table.setVisibleColumns(columnList.toArray());
210
		table.setColumnHeaders(columnList.toArray(string));
211
		table.setColumnFooter(CdmQueryFactory.TAXON_COLUMN, "Total amount of Taxa displayed: " + container.size());
212
	}
213

    
214
	private void createEditClickListener(){
215
		Button detailButton = toolbar.getDetailButton();
216
		detailButton.setCaption("Detail View");
217
		detailButton.addClickListener(event -> {
218
				Object selectedItemId = DistributionTableViewBean.this.grid.getSelectedRow();
219
				if(selectedItemId!=null){
220
					final UUID uuid = UUID.fromString(grid.getContainerDataSource().getItem(selectedItemId).getItemProperty("uuid").getValue().toString());
221
					Taxon taxon = HibernateProxyHelper.deproxy(CdmSpringContextHelper.getTaxonService().load(uuid), Taxon.class);
222
					List<DescriptionElementBase> listDescriptions = getPresenter().listDescriptionElementsForTaxon(taxon, null);
223
					DetailWindow detailWindow = new DetailWindow(taxon, listDescriptions);
224
					Window window = detailWindow.createWindow();
225
					window.center();
226
					getUI().addWindow(window);
227
				}
228
				else{
229
					Notification.show("Please select a taxon", Type.HUMANIZED_MESSAGE);
230
				}
231
			}
232
		);
233

    
234
		Button distributionSettingsButton = toolbar.getDistributionSettingsButton();
235
		distributionSettingsButton.addClickListener(event -> openDistributionSettings());
236

    
237
		Button settingsButton = toolbar.getSettingsButton();
238
		settingsButton.addClickListener(event -> openSettings());
239
	}
240

    
241
    /**
242
     * {@inheritDoc}
243
     */
244
	@Override
245
	public void openSettings() {
246
		SettingsConfigWindow cw = new SettingsConfigWindow(this);
247
		Window window  = cw.createWindow();
248
		UI.getCurrent().addWindow(window);
249
	}
250

    
251
    /**
252
     * {@inheritDoc}
253
     */
254
	@Override
255
	public void openDistributionSettings() {
256
		if(distributionSettingConfigWindow==null){
257
			distributionSettingConfigWindow = new DistributionSettingsConfigWindow(this);
258
		}
259
        Window window  = distributionSettingConfigWindow.createWindow();
260
        UI.getCurrent().addWindow(window);
261
	}
262

    
263
    /**
264
     * {@inheritDoc}
265
     */
266
	@Override
267
	public boolean allowAnonymousAccess() {
268
		// TODO Auto-generated method stub
269
		return false;
270
	}
271

    
272
    /**
273
     * {@inheritDoc}
274
     */
275
	@Override
276
	public Collection<Collection<GrantedAuthority>> allowedGrantedAuthorities() {
277
		// TODO Auto-generated method stub
278
		return null;
279
	}
280

    
281
    /**
282
     * {@inheritDoc}
283
     */
284
	@Override
285
	protected String getHeaderText() {
286
		// TODO Auto-generated method stub
287
		return null;
288
	}
289

    
290
    /**
291
     * {@inheritDoc}
292
     */
293
	@Override
294
	protected String getSubHeaderText() {
295
		// TODO Auto-generated method stub
296
		return null;
297
	}
298

    
299
    /**
300
     * {@inheritDoc}
301
     */
302
	@Override
303
	protected void initContent() {
304
		AbsoluteLayout mainLayout = initLayout();
305
		setCompositionRoot(mainLayout);
306
		createEditClickListener();
307
	}
308
}
(5-5/6)