Project

General

Profile

Download (11.3 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.CdmBase;
38
import eu.etaxonomy.cdm.model.common.Language;
39
import eu.etaxonomy.cdm.model.common.Representation;
40
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
41
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
42
import eu.etaxonomy.cdm.model.taxon.Taxon;
43
import eu.etaxonomy.cdm.vaadin.component.DetailWindow;
44
import eu.etaxonomy.cdm.vaadin.component.DistributionToolbar;
45
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
46
import eu.etaxonomy.cdm.vaadin.container.PresenceAbsenceTermContainer;
47
import eu.etaxonomy.cdm.vaadin.security.AccessRestrictedView;
48
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
49
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
50
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
51
import eu.etaxonomy.cdm.vaadin.util.DistributionEditorUtil;
52
import eu.etaxonomy.cdm.vaadin.util.TermCacher;
53
import eu.etaxonomy.cdm.vaadin.view.AbstractPageView;
54

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

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

    
68
    @Autowired
69
    private DistributionToolbar toolbar;
70

    
71
	private Table table;
72
	private Grid grid;
73

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

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

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

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

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

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

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

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

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

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

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

    
180
		return mainLayout;
181
	}
182

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

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

    
206
		table.setContainerDataSource(container);
207

    
208
		List<String> columnHeaders = new ArrayList<>(Arrays.asList(table.getColumnHeaders()));
209
		columnHeaders.remove(CdmQueryFactory.DTYPE_COLUMN);
210
		columnHeaders.remove(CdmQueryFactory.ID_COLUMN);
211
		columnHeaders.remove(CdmQueryFactory.UUID_COLUMN);
212
		columnHeaders.remove(CdmQueryFactory.CLASSIFICATION_COLUMN);
213

    
214
		List<String> columnList = new ArrayList<>(columnHeaders);
215

    
216
		String[] string = new String[columnList.size()];
217

    
218
		table.setVisibleColumns(columnList.toArray());
219
		table.setColumnHeaders(columnList.toArray(string));
220
		table.setColumnFooter(CdmQueryFactory.TAXON_COLUMN, "Total amount of Taxa displayed: " + container.size());
221
	}
222

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

    
246
		//area and taxon
247
		Button areaAndTaxonSettingsButton = toolbar.getDistributionSettingsButton();
248
		areaAndTaxonSettingsButton.addClickListener(event -> openAreaAndTaxonSettings());
249

    
250
		//distr status
251
		Button distrStatusButton = toolbar.getSettingsButton();
252
		distrStatusButton.addClickListener(event -> openStatusSettings());
253
	}
254

    
255
    /**
256
     * {@inheritDoc}
257
     */
258
	@Override
259
	public void openStatusSettings() {
260
        if(distributionStatusConfigWindow==null){
261
            distributionStatusConfigWindow = new DistributionStatusSettingsConfigWindow(this);
262
        }
263
        Window window  = distributionStatusConfigWindow.createWindow("Status");
264
        UI.getCurrent().addWindow(window);
265
	}
266

    
267
    /**
268
     * {@inheritDoc}
269
     */
270
	@Override
271
	public void openAreaAndTaxonSettings() {
272
		if(areaAndTaxonConfigWindow==null){
273
			areaAndTaxonConfigWindow = new AreaAndTaxonSettingsConfigWindow(this);
274
		}
275
        Window window  = areaAndTaxonConfigWindow.createWindow("Areas and Taxa");
276
        UI.getCurrent().addWindow(window);
277
	}
278

    
279
    /**
280
     * {@inheritDoc}
281
     */
282
	@Override
283
	public boolean allowAnonymousAccess() {
284
		// TODO Auto-generated method stub
285
		return false;
286
	}
287

    
288
    /**
289
     * {@inheritDoc}
290
     */
291
	@Override
292
	public Collection<Collection<GrantedAuthority>> allowedGrantedAuthorities() {
293
		// TODO Auto-generated method stub
294
		return null;
295
	}
296

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

    
306
    /**
307
     * {@inheritDoc}
308
     */
309
	@Override
310
	protected String getSubHeaderText() {
311
		// TODO Auto-generated method stub
312
		return null;
313
	}
314

    
315
    /**
316
     * {@inheritDoc}
317
     */
318
	@Override
319
	protected void initContent() {
320
	    /*
321
	     * This method is called twice. One time before and one time after login.
322
	     * Initializing the layout and click listeners twice is unnecessary and produces
323
	     * strange behavior (e.g. one click on settings-button opens settings-window several times),
324
	     * so we check for user authentication first.
325
	     */
326
	    if(UserHelper.fromSession().userIsAutheticated() && !UserHelper.fromSession().userIsAnnonymous()) {
327
	        AbsoluteLayout mainLayout = initLayout();
328
    		setCompositionRoot(mainLayout);
329
    		createEditClickListener();
330
	    }
331
	}
332
}
(4-4/7)