Project

General

Profile

Download (10.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

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

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

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

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

    
66
    @Autowired
67
    private DistributionToolbar toolbar;
68

    
69
	private Table table;
70
	private Grid grid;
71

    
72
    private CdmSQLContainer container;
73
	private AreaAndTaxonSettingsConfigWindow distributionSettingConfigWindow;
74

    
75
	public DistributionTableViewBean() {
76
		super();
77
	}
78

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

    
85
		setWidth("100.0%");
86
		setHeight("100.0%");
87

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

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

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

    
125
        table.setColumnReorderingAllowed(true);
126
        table.setSortEnabled(false);
127

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

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

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

    
175
		return mainLayout;
176
	}
177

    
178
    /**
179
     * {@inheritDoc}
180
     */
181
	@Override
182
	public void enter(ViewChangeEvent event) {
183
	    update();
184
	}
185

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

    
201
		table.setContainerDataSource(container);
202

    
203
		List<String> columnHeaders = new ArrayList<>(Arrays.asList(table.getColumnHeaders()));
204
		columnHeaders.remove(CdmQueryFactory.DTYPE_COLUMN);
205
		columnHeaders.remove(CdmQueryFactory.ID_COLUMN);
206
		columnHeaders.remove(CdmQueryFactory.UUID_COLUMN);
207
		columnHeaders.remove(CdmQueryFactory.CLASSIFICATION_COLUMN);
208

    
209
		List<String> columnList = new ArrayList<>(columnHeaders);
210

    
211
		String[] string = new String[columnList.size()];
212

    
213
		table.setVisibleColumns(columnList.toArray());
214
		table.setColumnHeaders(columnList.toArray(string));
215
		table.setColumnFooter(CdmQueryFactory.TAXON_COLUMN, "Total amount of Taxa displayed: " + container.size());
216
	}
217

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

    
240
		Button distributionSettingsButton = toolbar.getDistributionSettingsButton();
241
		distributionSettingsButton.addClickListener(event -> openDistributionSettings());
242

    
243
		Button settingsButton = toolbar.getSettingsButton();
244
		settingsButton.addClickListener(event -> openSettings());
245
	}
246

    
247
    /**
248
     * {@inheritDoc}
249
     */
250
	@Override
251
	public void openSettings() {
252
		DistributionStatusSettingsConfigWindow cw = new DistributionStatusSettingsConfigWindow(this);
253
		Window window  = cw.createWindow("Status");
254
		UI.getCurrent().addWindow(window);
255
	}
256

    
257
    /**
258
     * {@inheritDoc}
259
     */
260
	@Override
261
	public void openDistributionSettings() {
262
		if(distributionSettingConfigWindow==null){
263
			distributionSettingConfigWindow = new AreaAndTaxonSettingsConfigWindow(this);
264
		}
265
        Window window  = distributionSettingConfigWindow.createWindow("Areas and Taxa");
266
        UI.getCurrent().addWindow(window);
267
	}
268

    
269
    /**
270
     * {@inheritDoc}
271
     */
272
	@Override
273
	public boolean allowAnonymousAccess() {
274
		// TODO Auto-generated method stub
275
		return false;
276
	}
277

    
278
    /**
279
     * {@inheritDoc}
280
     */
281
	@Override
282
	public Collection<Collection<GrantedAuthority>> allowedGrantedAuthorities() {
283
		// TODO Auto-generated method stub
284
		return null;
285
	}
286

    
287
    /**
288
     * {@inheritDoc}
289
     */
290
	@Override
291
	protected String getHeaderText() {
292
		// TODO Auto-generated method stub
293
		return null;
294
	}
295

    
296
    /**
297
     * {@inheritDoc}
298
     */
299
	@Override
300
	protected String getSubHeaderText() {
301
		// TODO Auto-generated method stub
302
		return null;
303
	}
304

    
305
    /**
306
     * {@inheritDoc}
307
     */
308
	@Override
309
	protected void initContent() {
310
		AbsoluteLayout mainLayout = initLayout();
311
		setCompositionRoot(mainLayout);
312
		createEditClickListener();
313
	}
314
}
(4-4/7)