Project

General

Profile

Download (13.6 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
        TermVocabulary<NamedArea> chosenArea = presenter.getChosenArea();
109
        distAreaBox.setContainerDataSource(presenter.getDistributionContainer());
110
        distAreaBox.setValue(chosenArea);
111
        distAreaBox.addValueChangeListener(this);
112

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

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

    
125
    @Override
126
    protected AbstractLayout buildMainLayout() {
127

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
212
        return leftAndRightContainer;
213
    }
214

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

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

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

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

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

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

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

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

    
319
    private class TreeUpdater extends Thread{
320

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

    
323

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

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

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

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