Project

General

Profile

Download (13.5 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

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

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

    
102
        classificationBox.addValueChangeListener(this);
103
        taxonFilter.addValueChangeListener(this);
104
        taxonTree.addExpandListener(this);
105

    
106
        TermVocabulary<NamedArea> chosenArea = presenter.getChosenArea();
107
        distAreaBox.setContainerDataSource(presenter.getDistributionContainer());
108
        distAreaBox.setValue(chosenArea);
109
        distAreaBox.addValueChangeListener(this);
110

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

    
118
        okButton.addClickListener(this);
119
        cancelButton.addClickListener(this);
120
        updateButtons();
121
    }
122

    
123
    @Override
124
    protected AbstractLayout buildMainLayout() {
125

    
126
        mainLayout = new VerticalLayout();
127
        mainLayout.setSizeFull();
128

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

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

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

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

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

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

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

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

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

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

    
198
        leftAndRightContainer.addComponent(leftContainer);
199
        leftAndRightContainer.addComponent(rightContainer);
200

    
201
        //button toolbar
202
        HorizontalLayout buttonToolBar = createOkCancelButtons();
203

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

    
210
        return leftAndRightContainer;
211
    }
212

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

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

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

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

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

    
295
        new TreeUpdater(children).start();
296
    }
297

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

    
317
    private class TreeUpdater extends Thread{
318

    
319
    	private Collection<UuidAndTitleCache<TaxonNode>> children;
320

    
321

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

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

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

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