Project

General

Profile

Download (11 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.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 areaAndTaxonConfigWindow;;
74
	private DistributionStatusSettingsConfigWindow distributionStatusConfigWindow;
75

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

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

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

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

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

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

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

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

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

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

    
178
		return mainLayout;
179
	}
180

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

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

    
204
		table.setContainerDataSource(container);
205

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

    
212
		List<String> columnList = new ArrayList<>(columnHeaders);
213

    
214
		String[] string = new String[columnList.size()];
215

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

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

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

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

    
253
    /**
254
     * {@inheritDoc}
255
     */
256
	@Override
257
	public void openStatusSettings() {
258
        if(distributionStatusConfigWindow==null){
259
            distributionStatusConfigWindow = new DistributionStatusSettingsConfigWindow(this);
260
        }
261
        Window window  = distributionStatusConfigWindow.createWindow("Status");
262
        window.setWidth("25%");
263
        window.setHeight("60%");
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
	    // initialize term cacher for table formatting
321
	    getPresenter().getPresenceAbsenceTermContainer();
322
	    // initialize layout
323
        AbsoluteLayout mainLayout = initLayout();
324
        setCompositionRoot(mainLayout);
325
        // add click listener on DistributionToolbar-buttons
326
        createEditClickListener();
327
	}
328
}
(4-4/7)