Project

General

Profile

Download (11.9 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
						Object distributionStatus = valueChangeEvent.getProperty().getValue();
167
						getPresenter().updateDistributionField(areaID, distributionStatus, taxon);
168
						container.refresh();
169
						popup.close();
170
				});
171
                VerticalLayout layout = new VerticalLayout(termSelect);
172
                popup.setContent(layout);
173
                popup.setModal(true);
174
                popup.center();
175
                UI.getCurrent().addWindow(popup);
176
            }
177
        });
178

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

    
181
		return mainLayout;
182
	}
183

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

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

    
207
		table.setContainerDataSource(container);
208

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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