Project

General

Profile

Download (13.7 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2015 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.Collection;
14
import java.util.List;
15
import java.util.Set;
16
import java.util.UUID;
17

    
18
import com.vaadin.data.Property;
19
import com.vaadin.data.Property.ValueChangeEvent;
20
import com.vaadin.data.Property.ValueChangeListener;
21
import com.vaadin.data.util.BeanItemContainer;
22
import com.vaadin.data.util.sqlcontainer.RowId;
23
import com.vaadin.server.Page;
24
import com.vaadin.server.VaadinSession;
25
import com.vaadin.ui.AbstractLayout;
26
import com.vaadin.ui.Alignment;
27
import com.vaadin.ui.Button.ClickEvent;
28
import com.vaadin.ui.Button.ClickListener;
29
import com.vaadin.ui.ComboBox;
30
import com.vaadin.ui.HorizontalLayout;
31
import com.vaadin.ui.ListSelect;
32
import com.vaadin.ui.Notification;
33
import com.vaadin.ui.Table.ColumnHeaderMode;
34
import com.vaadin.ui.TextField;
35
import com.vaadin.ui.Tree.ExpandEvent;
36
import com.vaadin.ui.Tree.ExpandListener;
37
import com.vaadin.ui.TreeTable;
38
import com.vaadin.ui.UI;
39
import com.vaadin.ui.VerticalLayout;
40

    
41
import eu.etaxonomy.cdm.common.CdmUtils;
42
import eu.etaxonomy.cdm.model.common.TermVocabulary;
43
import eu.etaxonomy.cdm.model.location.NamedArea;
44
import eu.etaxonomy.cdm.model.taxon.Classification;
45
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
46
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
47
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
48
import eu.etaxonomy.cdm.vaadin.container.NamedAreaContainer;
49
import eu.etaxonomy.cdm.vaadin.container.TaxonNodeContainer;
50
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
51
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
52
import eu.etaxonomy.cdm.vaadin.util.DistributionEditorUtil;
53

    
54
/**
55
 *
56
 * @author pplitzner
57
 *
58
 */
59
public class DistributionSettingsConfigWindow extends AbstractSettingsDialogWindow implements ValueChangeListener, ClickListener, ExpandListener{
60

    
61
    private static final long serialVersionUID = 1439411115014088780L;
62
    private ComboBox classificationBox;
63
    private TextField taxonFilter;
64
    private ComboBox distAreaBox;
65
    private ListSelect namedAreaList;
66
    private TreeTable taxonTree;
67
    DistributionTableView distributionTableView;
68

    
69
    /**
70
     * The constructor should first build the main layout, set the
71
     * composition root and then do any custom initialization.
72
     *
73
     * The constructor will not be automatically regenerated by the
74
     * visual editor.
75
     * @param distributionTableView
76
     */
77
    public DistributionSettingsConfigWindow(DistributionTableView distributionTableView) {
78
        super();
79
        this.distributionTableView = distributionTableView;
80
    }
81

    
82
    @Override
83
    protected void init() {
84
        //init classification
85
        Classification classification = presenter.getChosenClassification();
86
        try {
87
            classificationBox.setContainerDataSource(new CdmSQLContainer(CdmQueryFactory.generateTableQuery("Classification")));
88
        } catch (SQLException e) {
89
            DistributionEditorUtil.showSqlError(e);
90
        }
91
        RowId parent = null;
92
        if(classification!=null){
93
        	parent = new RowId(classification.getRootNode().getId());
94
        }
95
        else if(classificationBox.getItemIds().size()==1){
96
            //only one classification exists
97
            parent = (RowId) classificationBox.getItemIds().iterator().next();
98
        }
99
        if(parent!=null){
100
            classificationBox.setValue(new RowId(parent.getId()));
101
            showClassificationTaxa(getUuidAndTitleCacheFromRowId(parent));
102
        }
103

    
104
        classificationBox.addValueChangeListener(this);
105
        taxonFilter.addValueChangeListener(this);
106
        taxonTree.addExpandListener(this);
107

    
108
        //init areas
109
        TermVocabulary<NamedArea> chosenAreaVoc = presenter.getChosenAreaVoc();
110
        distAreaBox.setContainerDataSource(presenter.getDistributionContainer());
111
        distAreaBox.setValue(chosenAreaVoc);
112
        distAreaBox.addValueChangeListener(this);
113

    
114
        if(chosenAreaVoc!=null){
115
            NamedAreaContainer container = new NamedAreaContainer(chosenAreaVoc);
116
            namedAreaList.setContainerDataSource(container);
117
        }
118
        Object selectedAreas = VaadinSession.getCurrent().getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREAS);
119
        namedAreaList.setValue(selectedAreas);
120

    
121
        okButton.addClickListener(this);
122
        cancelButton.addClickListener(this);
123
        updateButtons();
124
    }
125

    
126
    @Override
127
    protected AbstractLayout buildMainLayout() {
128

    
129
        mainLayout = new VerticalLayout();
130
        mainLayout.setSizeFull();
131

    
132
        HorizontalLayout leftAndRightContainer = new HorizontalLayout();
133
        leftAndRightContainer.setImmediate(false);
134
        leftAndRightContainer.setSizeFull();
135
        leftAndRightContainer.setMargin(true);
136
        leftAndRightContainer.setSpacing(true);
137

    
138
        VerticalLayout leftContainer = new VerticalLayout();
139
        leftContainer.setImmediate(false);
140
        leftContainer.setSpacing(true);
141
        leftContainer.setSizeFull();
142

    
143
        VerticalLayout rightContainer = new VerticalLayout();
144
        rightContainer.setImmediate(false);
145
        rightContainer.setSpacing(true);
146
        rightContainer.setSizeFull();
147

    
148
        //classification
149
        classificationBox = new ComboBox("Classification");
150
        classificationBox.setItemCaptionPropertyId(TaxonNodeContainer.LABEL);
151
        classificationBox.setInputPrompt("Please select a classification...");
152
        classificationBox.setImmediate(true);
153
        classificationBox.setNewItemsAllowed(false);
154
        classificationBox.setNullSelectionAllowed(false);
155
        classificationBox.setSizeFull();
156
        classificationBox.setWidth("100%");
157

    
158
        //taxonFilter
159
        taxonFilter = new TextField("Filter");
160
        taxonFilter.setInputPrompt("Filter taxa by name...");
161
        taxonFilter.setSizeFull();
162
        taxonFilter.setImmediate(true);
163

    
164
        //distribution area box
165
        distAreaBox = new ComboBox("Distribution Area:");
166
        distAreaBox.setInputPrompt("Please select a distribution area...");
167
        distAreaBox.setImmediate(true);
168
        distAreaBox.setNullSelectionAllowed(false);
169
        distAreaBox.setNewItemsAllowed(false);
170
        distAreaBox.setSizeFull();
171
        distAreaBox.setWidth("100%");
172

    
173
        // named areas
174
        namedAreaList = new ListSelect();
175
        namedAreaList.setCaption("Areas");
176
        namedAreaList.setSizeFull();
177
        namedAreaList.setMultiSelect(true);
178

    
179
        //taxonomy
180
        taxonTree = new TreeTable("Taxonomy");
181
        taxonTree.setSelectable(true);
182
        taxonTree.setSizeFull();
183
        taxonTree.setImmediate(true);
184
        taxonTree.setCacheRate(20);
185
        taxonTree.setColumnHeaderMode(ColumnHeaderMode.HIDDEN);
186
        taxonTree.setMultiSelect(true);
187

    
188
        leftContainer.addComponent(distAreaBox);
189
        leftContainer.addComponent(namedAreaList);
190
        leftContainer.setExpandRatio(distAreaBox, 0.1f);
191
        leftContainer.setExpandRatio(namedAreaList, 0.9f);
192
        leftContainer.setSizeFull();
193

    
194
        rightContainer.addComponent(classificationBox);
195
        rightContainer.addComponent(taxonFilter);
196
        rightContainer.addComponent(taxonTree);
197
        rightContainer.setExpandRatio(classificationBox, 0.1f);
198
        rightContainer.setExpandRatio(taxonFilter, 0.1f);
199
        rightContainer.setExpandRatio(taxonTree, 0.8f);
200

    
201
        leftAndRightContainer.addComponent(leftContainer);
202
        leftAndRightContainer.addComponent(rightContainer);
203

    
204
        //button toolbar
205
        HorizontalLayout buttonToolBar = createOkCancelButtons();
206

    
207
        mainLayout.addComponent(leftAndRightContainer);
208
        mainLayout.addComponent(buttonToolBar);
209
        mainLayout.setExpandRatio(leftAndRightContainer, 0.9f);
210
        mainLayout.setExpandRatio(buttonToolBar, 0.1f);
211
        mainLayout.setComponentAlignment(buttonToolBar, Alignment.BOTTOM_RIGHT);
212

    
213
        return leftAndRightContainer;
214
    }
215

    
216
    @Override
217
    public void valueChange(ValueChangeEvent event) {
218
        Property property = event.getProperty();
219
        if(property==classificationBox){
220
        	UuidAndTitleCache<TaxonNode> parent = getUuidAndTitleCacheFromRowId(classificationBox.getValue());
221
            showClassificationTaxa(parent);
222
        }
223
        else if(property==taxonFilter){
224
            String filterText = taxonFilter.getValue();
225
            Property uuidProperty = classificationBox.getContainerProperty(classificationBox.getValue(),"uuid");
226
            if(uuidProperty==null){
227
            	Notification.show("Please select a classification");
228
            }
229
            else{
230
            	if(CdmUtils.isNotBlank(filterText)){
231
            		UUID classificationUuid = UUID.fromString((String) uuidProperty.getValue());
232
            		List<UuidAndTitleCache<TaxonNode>> taxa = CdmSpringContextHelper.getTaxonNodeService().getUuidAndTitleCache(null, filterText, classificationUuid);
233
            		BeanItemContainer<UuidAndTitleCache<TaxonNode>> container = new BeanItemContainer<>(UuidAndTitleCache.class);
234
            		taxonTree.setContainerDataSource(container);
235
            		for (UuidAndTitleCache<TaxonNode> taxon : taxa) {
236
            			container.addItem(taxon);
237
            			taxonTree.setChildrenAllowed(taxon, false);
238
            		}
239
            		taxonTree.setVisibleColumns("titleCache");
240
            	}
241
            	else{
242
            		UuidAndTitleCache<TaxonNode> parent = getUuidAndTitleCacheFromRowId(classificationBox.getValue());
243
            		showClassificationTaxa(parent);
244
            	}
245
            }
246
        }
247
        else if(property==distAreaBox){
248
            TermVocabulary<NamedArea> vocabulary = (TermVocabulary<NamedArea>) event.getProperty().getValue();
249
            NamedAreaContainer container = new NamedAreaContainer(vocabulary);
250
            namedAreaList.setContainerDataSource(container);
251
        }
252
        updateButtons();
253
    }
254

    
255
    @Override
256
    protected boolean isValid() {
257
        return classificationBox.getValue()!=null && distAreaBox.getValue()!=null;
258
    }
259

    
260
    @Override
261
    public void buttonClick(ClickEvent event) {
262
        Object source = event.getSource();
263
        if(source==okButton){
264
            List<UUID> taxonNodes = new ArrayList<>();
265
            TermVocabulary<NamedArea> areaVoc = null;
266
            String uuidString = (String) classificationBox.getContainerProperty(classificationBox.getValue(),"uuid").getValue();
267
            UUID classificationUuid = UUID.fromString(uuidString);
268
            Set<UuidAndTitleCache<TaxonNode>> treeSelection = (Set<UuidAndTitleCache<TaxonNode>>) taxonTree.getValue();
269
			if(!treeSelection.isEmpty()){
270
				for (UuidAndTitleCache<TaxonNode> uuidAndTitleCache : treeSelection) {
271
					taxonNodes.add(uuidAndTitleCache.getUuid());
272
				}
273
            }
274
            areaVoc = (TermVocabulary<NamedArea>) distAreaBox.getValue();
275
            Set<NamedArea> selectedAreas = (Set<NamedArea>) namedAreaList.getValue();
276
            DistributionEditorUtil.updateDistributionView(distributionTableView, taxonNodes, areaVoc, selectedAreas, classificationUuid);
277
            window.close();
278
        }
279
        else if(source==cancelButton){
280
            window.close();
281
        }
282
    }
283

    
284
    @Override
285
    public void nodeExpand(ExpandEvent event) {
286
        UuidAndTitleCache<TaxonNode> parent = (UuidAndTitleCache<TaxonNode>) event.getItemId();
287
        ((TaxonNodeContainer) taxonTree.getContainerDataSource()).addChildItems(parent);
288
    }
289

    
290
    private void showClassificationTaxa(UuidAndTitleCache<TaxonNode> parent) {
291
        final Collection<UuidAndTitleCache<TaxonNode>> children = CdmSpringContextHelper.getTaxonNodeService().listChildNodesAsUuidAndTitleCache(parent);
292
        // Enable polling and set frequency to 0.5 seconds
293
        UI.getCurrent().setPollInterval(500);
294
        taxonTree.setEnabled(false);
295
        taxonTree.removeAllItems();
296
        Notification.show("Loading taxa...");
297

    
298
        new TreeUpdater(children).start();
299
    }
300

    
301
    private UuidAndTitleCache<TaxonNode> getUuidAndTitleCacheFromRowId(Object classificationSelection) {
302
        String uuidString = (String) classificationBox.getContainerProperty(classificationSelection, "uuid").getValue();
303
        Property rootNodeContainerProperty = null;
304

    
305
        Collection<?> ids = classificationBox.getContainerPropertyIds();
306
        //use for loop here because the case of the root node id columns differs between some DBs
307
        for (Object id : ids) {
308
			if(id instanceof String && ((String) id).toLowerCase().equals("rootnode_id")){
309
				rootNodeContainerProperty = classificationBox.getContainerProperty(classificationSelection, id);
310
				break;
311
			}
312
		}
313
		int id = (int) rootNodeContainerProperty.getValue();
314
        String titleCache = (String) classificationBox.getContainerProperty(classificationSelection, "titleCache").getValue();
315
        UUID uuid = UUID.fromString(uuidString);
316
        UuidAndTitleCache<TaxonNode> parent = new UuidAndTitleCache<>(uuid, id, titleCache);
317
        return parent;
318
    }
319

    
320
    private class TreeUpdater extends Thread{
321

    
322
    	private Collection<UuidAndTitleCache<TaxonNode>> children;
323

    
324

    
325
		public TreeUpdater(Collection<UuidAndTitleCache<TaxonNode>> children) {
326
			this.children = children;
327
		}
328

    
329
		@Override
330
    	public void run() {
331
			UI.getCurrent().access(new Runnable() {
332
				@Override
333
				public void run() {
334
					taxonTree.setContainerDataSource(new TaxonNodeContainer(children));
335

    
336
			        Notification notification = new Notification("Loading complete");
337
			        notification.setDelayMsec(500);
338
			        notification.show(Page.getCurrent());
339
			        taxonTree.setEnabled(true);
340

    
341
			        //disable polling when all taxa are loaded
342
			        UI.getCurrent().setPollInterval(-1);
343
				}
344
			});
345
    	}
346
    }
347
}
(2-2/7)