Project

General

Profile

Revision fe7f0131

IDfe7f013171b071f35f93c19c0c67bd46a52eba8b
Parent 41f187f1
Child f8b0de79

Added by Patrick Plitzner almost 4 years ago

ref #5458 Use service based on UuidAndTitleCache to build taxon tree

View differences:

src/main/java/eu/etaxonomy/cdm/vaadin/container/TaxonNodeContainer.java
1 1
package eu.etaxonomy.cdm.vaadin.container;
2 2

  
3
import java.util.ArrayList;
4 3
import java.util.Arrays;
5
import java.util.Collection;
4
import java.util.HashSet;
6 5
import java.util.List;
6
import java.util.Set;
7 7

  
8 8
import com.vaadin.data.Item;
9 9
import com.vaadin.data.util.HierarchicalContainer;
10 10

  
11 11
import eu.etaxonomy.cdm.api.service.IClassificationService;
12
import eu.etaxonomy.cdm.model.name.Rank;
13 12
import eu.etaxonomy.cdm.model.taxon.Classification;
14 13
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
14
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
15 15
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
16 16

  
17 17
public class TaxonNodeContainer extends HierarchicalContainer {
18 18

  
19 19
	private static final long serialVersionUID = 102401340698963360L;
20
	public static final String LABEL = "label";
20
	public static final String LABEL = "titleCache";
21
	private Set<Object> itemCache = new HashSet<Object>();
22

  
21 23

  
22 24
	private final List<String> nodeInitStrategy = Arrays.asList(new String[]{
23 25
	        "taxon.sec",
......
30 32
	 * @param parentNode the parent node which will <b>not</b> be included
31 33
	 * in the result but only its child nodes
32 34
	 */
33
	public TaxonNodeContainer(TaxonNode parentNode) {
35
	public TaxonNodeContainer(UuidAndTitleCache<TaxonNode> parentNode) {
34 36
		addContainerProperty(LABEL, String.class, "[no taxon]");
35 37
		if(parentNode==null){
36 38
            //just load classifications
......
47 49
		else{
48 50
		    addItem(parentNode);
49 51
		}
50
//		getTaxonNodeList(parentNode);
51 52
	}
52 53

  
54
	/**
55
	 * {@inheritDoc}
56
	 */
53 57
	@Override
54
	public Collection<?> getChildren(Object itemId) {
55
	    List<TaxonNode> childNodes = ((TaxonNode) itemId).getChildNodes();
56
	    for (TaxonNode taxonNode : childNodes) {
57
            if(!containsId(taxonNode)){
58
                setParent(taxonNode, itemId);
59
            }
58
	public Item addItem(Object itemId) {
59
	    if(itemId instanceof UuidAndTitleCache){
60
	        if(!itemCache.contains(((UuidAndTitleCache<TaxonNode>) itemId).getId())){
61
	            return super.addItem(itemId);
62
	        }
63
        } else {
64
            return super.addItem(itemId);
60 65
        }
61
        return childNodes;
66
	    return null;
62 67
	}
63 68

  
64 69
	/**
65 70
	 * {@inheritDoc}
66 71
	 */
67 72
	@Override
68
	public Object getParent(Object itemId) {
69
	    return ((TaxonNode) itemId).getParent();
70
	}
71

  
72
    /**
73
     * {@inheritDoc}
74
     */
75
    @Override
76
    public boolean hasChildren(Object itemId) {
77
        return !((TaxonNode)itemId).getChildNodes().isEmpty();
78
    }
79

  
80
	public void getTaxonNodeList(TaxonNode parentNode) {
81
		List<TaxonNode> nodes = new ArrayList<TaxonNode>();
82

  
83

  
84
		if(parentNode==null){
85
			//just load classifications
86
			IClassificationService classificationService = CdmSpringContextHelper.getClassificationService();
87
			List<Classification> classificationList = classificationService.listClassifications(null, null, null, nodeInitStrategy);
88
			for (Classification classification : classificationList) {
89
			    TaxonNode rootNode = classification.getRootNode();
90
			    Item item = addItem(rootNode);
91
				setChildrenAllowed(rootNode, false);
92
				nodes.add(rootNode);
93
                if(rootNode.getClassification()!=null){
94
                    item.getItemProperty(LABEL).setValue(rootNode.getClassification().getName().getText());
95
                }
96
			}
97
		}
98
		else{
99
			//load child nodes
100
	        addChildNodes(parentNode);
101
		}
102
	}
103

  
104
	private void addChildNodes(TaxonNode parentNode) {
105
		List<TaxonNode> childNodes = parentNode.getChildNodes();
106
		setChildrenAllowed(parentNode, !childNodes.isEmpty());
107
		boolean hasValidChildren = false;
108
		for (TaxonNode taxonNode : childNodes) {
109
			if(taxonNode!=null && taxonNode.getTaxon()!=null && taxonNode.getTaxon().getName()!=null){
110
				Rank rank = taxonNode.getTaxon().getName().getRank();
111
				if(rank!=null && rank.isHigher(Rank.SPECIES())){
112
				    Item item = addItem(taxonNode);
113
					setParent(taxonNode, parentNode);
114
					if(taxonNode.getTaxon()!=null){
115
					    item.getItemProperty(LABEL).setValue(taxonNode.getTaxon().getName().getTitleCache());
116
					}
117
					else if(taxonNode.getClassification()!=null){
118
                        item.getItemProperty(LABEL).setValue(taxonNode.getClassification().getName().getText());
119
                    }
120
					hasValidChildren = true;
121

  
122
					addChildNodes(taxonNode);
123
				}
124
			}
125
		}
126
		setChildrenAllowed(parentNode, hasValidChildren);
73
	public boolean setParent(Object itemId, Object newParentId) {
74
	    itemCache.add(((UuidAndTitleCache<TaxonNode>) itemId).getId());;
75
	    return super.setParent(itemId, newParentId);
127 76
	}
128 77

  
129 78
}
src/main/java/eu/etaxonomy/cdm/vaadin/container/TaxonTreeContainer.java
1
// $Id$
2
/**
3
* Copyright (C) 2016 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.vaadin.container;
11

  
12
import java.sql.SQLException;
13
import java.util.ArrayList;
14
import java.util.Collection;
15
import java.util.HashMap;
16
import java.util.HashSet;
17
import java.util.List;
18
import java.util.Map;
19

  
20
import com.vaadin.data.Container.Hierarchical;
21
import com.vaadin.data.util.filter.Compare;
22
import com.vaadin.data.util.sqlcontainer.RowId;
23

  
24
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
25
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
26

  
27
/**
28
 * @author pplitzner
29
 * @date 18.10.2016
30
 *
31
 */
32
public class TaxonTreeContainer extends CdmSQLContainer implements Hierarchical{
33

  
34
    private static final long serialVersionUID = 5488629563366944491L;
35

  
36
    private final Collection<RowId> rootItemIds = new HashSet<>();
37
    private final Map<Object, List<Object>> parentChildMap = new HashMap<>();
38
    private final Map<Object, Object> childParentMap = new HashMap<>();
39
    private CdmSQLContainer childrenContainer;
40

  
41
    public TaxonTreeContainer(TaxonNode parentNode) throws SQLException {
42
        super(CdmQueryFactory.generateTaxonTreeQuery("Name", Integer.toString(parentNode.getClassification().getId())));
43
        childrenContainer = new CdmSQLContainer(CdmQueryFactory.generateTaxonTreeQuery("Name", Integer.toString(parentNode.getClassification().getId())));
44
        List<TaxonNode> childNodes = parentNode.getChildNodes();
45
        for (TaxonNode taxonNode : childNodes) {
46
            rootItemIds.add(new RowId(taxonNode.getId()));
47
        }
48
    }
49

  
50
    /**
51
     * {@inheritDoc}
52
     */
53
    @Override
54
    public Collection<?> getChildren(Object itemId) {
55
        List<Object> children = parentChildMap.get(itemId);
56
        if(children==null){
57
            children = updateChildren(itemId);
58
        }
59
        return children;
60
    }
61

  
62
    private List<Object> updateChildren(Object itemId) {
63
        List<Object> children;
64
        Filter childrenOfTaxonFilter = new Compare.Equal("tn.parent_id", Integer.valueOf(itemId.toString()));
65
        childrenContainer.addContainerFilter(childrenOfTaxonFilter);
66
        children = new ArrayList<>();
67
        Collection<?> itemIds = childrenContainer.getItemIds();
68
        for (Object object : itemIds) {
69
            childParentMap.put(object, itemId);
70
            children.add(object);
71
        }
72
        childrenContainer.removeAllContainerFilters();
73

  
74
        parentChildMap.put(itemId, children);
75
        return children;
76
    }
77

  
78
    /**
79
     * {@inheritDoc}
80
     */
81
    @Override
82
    public Object getParent(Object itemId) {
83
        return childParentMap.get(itemId);
84
    }
85

  
86
    /**
87
     * {@inheritDoc}
88
     */
89
    @Override
90
    public boolean hasChildren(Object itemId) {
91
        List<Object> children = parentChildMap.get(itemId);
92
        if(children==null){
93
            children = updateChildren(itemId);
94
        }
95
        return !children.isEmpty();
96
    }
97

  
98
    /**
99
     * {@inheritDoc}
100
     */
101
    @Override
102
    public Collection<?> rootItemIds() {
103
        return rootItemIds;
104
    }
105

  
106
    /**
107
     * {@inheritDoc}
108
     */
109
    @Override
110
    public boolean setParent(Object itemId, Object newParentId) throws UnsupportedOperationException {
111
        return true;
112
    }
113

  
114
    /**
115
     * {@inheritDoc}
116
     */
117
    @Override
118
    public boolean areChildrenAllowed(Object itemId) {
119
        return hasChildren(itemId);
120
    }
121

  
122
    /**
123
     * {@inheritDoc}
124
     */
125
    @Override
126
    public boolean setChildrenAllowed(Object itemId, boolean areChildrenAllowed) throws UnsupportedOperationException {
127
        return true;
128
    }
129

  
130
    /**
131
     * {@inheritDoc}
132
     */
133
    @Override
134
    public boolean isRoot(Object itemId) {
135
        return itemId==null?true:rootItemIds.contains(itemId);
136
    }
137

  
138

  
139

  
140
}
src/main/java/eu/etaxonomy/cdm/vaadin/view/dbstatus/DistributionSettingsConfigWindow.java
1 1
// $Id$
2 2
/**
3
* Copyright (C) 2015 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
3
 * Copyright (C) 2015 EDIT
4
 * European Distributed Institute of Taxonomy
5
 * http://www.e-taxonomy.eu
6
 *
7
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8
 * See LICENSE.TXT at the top of this package for the full license terms.
9
 */
10 10
package eu.etaxonomy.cdm.vaadin.view.dbstatus;
11 11

  
12
import java.sql.SQLException;
12
import java.util.Collection;
13 13
import java.util.Set;
14
import java.util.UUID;
14 15

  
16
import com.vaadin.data.Item;
15 17
import com.vaadin.data.Property;
16 18
import com.vaadin.data.Property.ValueChangeEvent;
17 19
import com.vaadin.data.Property.ValueChangeListener;
......
25 27
import com.vaadin.ui.HorizontalLayout;
26 28
import com.vaadin.ui.ListSelect;
27 29
import com.vaadin.ui.Table.ColumnHeaderMode;
30
import com.vaadin.ui.Tree.ExpandEvent;
31
import com.vaadin.ui.Tree.ExpandListener;
28 32
import com.vaadin.ui.TreeTable;
29 33
import com.vaadin.ui.VerticalLayout;
30 34

  
35
import eu.etaxonomy.cdm.model.common.CdmBase;
31 36
import eu.etaxonomy.cdm.model.common.TermVocabulary;
32 37
import eu.etaxonomy.cdm.model.location.NamedArea;
33 38
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
39
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
34 40
import eu.etaxonomy.cdm.vaadin.container.NamedAreaContainer;
35 41
import eu.etaxonomy.cdm.vaadin.container.TaxonNodeContainer;
36
import eu.etaxonomy.cdm.vaadin.container.TaxonTreeContainer;
37 42
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
38 43
import eu.etaxonomy.cdm.vaadin.util.DistributionEditorUtil;
39 44

  
......
42 47
 * @author pplitzner
43 48
 *
44 49
 */
45
public class DistributionSettingsConfigWindow extends AbstractSettingsDialogWindow implements ValueChangeListener, ClickListener{
50
public class DistributionSettingsConfigWindow extends AbstractSettingsDialogWindow implements ValueChangeListener, ClickListener, ExpandListener{
46 51

  
47
	private static final long serialVersionUID = 1439411115014088780L;
48
	private ComboBox classificationBox;
52
    private static final long serialVersionUID = 1439411115014088780L;
53
    private ComboBox classificationBox;
49 54
    private ComboBox distAreaBox;
50 55
    private ListSelect namedAreaList;
51 56
    private TreeTable taxonTree;
......
58 63
     * @param distributionTableView
59 64
     */
60 65
    public DistributionSettingsConfigWindow(DistributionTableView distributionTableView) {
61
    	super();
66
        super();
62 67
    }
63 68

  
69
    @Override
64 70
    protected void init() {
65
    	//init classification and taxon selection
71
        //init classification and taxon selection
66 72
        TaxonNode chosenTaxonNode = presenter.getChosenTaxonNode();
67 73

  
68 74
        classificationBox.setContainerDataSource(new TaxonNodeContainer(null));
69 75
        Object classificationSelection = null;
70
		if(classificationBox.getItemIds().size()==1){
71
			//only one classification exists
72
		    classificationSelection = classificationBox.getItemIds().iterator().next();
73
		}
74
		else if(chosenTaxonNode!=null){
75
			//get the classification from the selected taxon node
76
			classificationSelection = chosenTaxonNode.getClassification().getRootNode();
77
		}
76
        if(classificationBox.getItemIds().size()==1){
77
            //only one classification exists
78
            classificationSelection = classificationBox.getItemIds().iterator().next();
79
        }
80
        else if(chosenTaxonNode!=null){
81
            //get the classification from the selected taxon node
82
            classificationSelection = chosenTaxonNode.getClassification().getRootNode();
83
        }
78 84
        if(classificationSelection!=null){
79
        	classificationBox.setValue(classificationSelection);
80
        	try {
81
                taxonTree.setContainerDataSource(new TaxonTreeContainer((TaxonNode) classificationSelection));
82
                taxonTree.setVisibleColumns("Name");
83
            } catch (SQLException e) {
84
    			DistributionEditorUtil.showSqlError(e);
85
            classificationBox.setValue(classificationSelection);
86
            taxonTree.addExpandListener(this);
87

  
88
            UUID uuid = ((CdmBase) classificationSelection).getUuid();
89
            int id = ((CdmBase) classificationSelection).getId();
90
            String titleCache = ((TaxonNode) classificationSelection).getClassification().getTitleCache();
91
            UuidAndTitleCache<TaxonNode> parent = new UuidAndTitleCache<>(uuid, id, titleCache);
92
            taxonTree.setContainerDataSource(new TaxonNodeContainer(parent));
93
            if(chosenTaxonNode!=null){
94
                taxonTree.select(new RowId(chosenTaxonNode.getId()));
85 95
            }
86
        	if(chosenTaxonNode!=null){
87
    			taxonTree.select(new RowId(chosenTaxonNode.getId()));
88
        	}
89 96
        }
90 97
        classificationBox.addValueChangeListener(this);
91
        
98

  
92 99
        TermVocabulary<NamedArea> chosenArea = presenter.getChosenArea();
93 100
        distAreaBox.setContainerDataSource(presenter.getDistributionContainer());
94 101
        distAreaBox.setValue(chosenArea);
95 102
        distAreaBox.addValueChangeListener(this);
96 103

  
97 104
        if(chosenArea!=null){
98
        	NamedAreaContainer container = new NamedAreaContainer(chosenArea);
99
        	namedAreaList.setContainerDataSource(container);
105
            NamedAreaContainer container = new NamedAreaContainer(chosenArea);
106
            namedAreaList.setContainerDataSource(container);
100 107
        }
101 108
        Object selectedAreas = VaadinSession.getCurrent().getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREAS);
102 109
        namedAreaList.setValue(selectedAreas);
......
106 113
        updateButtons();
107 114
    }
108 115

  
116
    @Override
109 117
    protected AbstractLayout buildMainLayout() {
110 118

  
111 119
        mainLayout = new VerticalLayout();
112 120
        mainLayout.setSizeFull();
113 121

  
114
    	HorizontalLayout leftAndRightContainer = new HorizontalLayout();
122
        HorizontalLayout leftAndRightContainer = new HorizontalLayout();
115 123
        leftAndRightContainer.setImmediate(false);
116 124
        leftAndRightContainer.setSizeFull();
117 125
        leftAndRightContainer.setMargin(true);
......
186 194
        return leftAndRightContainer;
187 195
    }
188 196

  
189
	@Override
190
	public void valueChange(ValueChangeEvent event) {
191
		Property property = event.getProperty();
192
		if(property==classificationBox){
193
			TaxonNode parentNode = (TaxonNode) event.getProperty().getValue();
194
			if(parentNode!=null){
195
			    try {
196
                    taxonTree.setContainerDataSource(new TaxonTreeContainer(parentNode));
197
                    taxonTree.setVisibleColumns("Name");
198
                } catch (SQLException e) {
199
        			DistributionEditorUtil.showSqlError(e);
200
                }
201
			}
202
			else{
203
				taxonTree.setContainerDataSource(null);
204
			}
205
		}
206

  
207
		else if(property==distAreaBox){
208
			TermVocabulary<NamedArea> vocabulary = (TermVocabulary<NamedArea>) event.getProperty().getValue();
209
			NamedAreaContainer container = new NamedAreaContainer(vocabulary);
210
			namedAreaList.setContainerDataSource(container);
211
		}
212
		updateButtons();
213
	}
214
	
215
	@Override
216
	protected boolean isValid() {
217
		return classificationBox.getValue()!=null && distAreaBox.getValue()!=null;
218
	}
219

  
220
	@Override
221
	public void buttonClick(ClickEvent event) {
222
		Object source = event.getSource();
223
		if(source==okButton){
224
			TaxonNode taxonNode = null;
225
			TermVocabulary<NamedArea> term = null;
226
			//TODO use field converter
227
			if(taxonTree.getValue()!=null){
228
			    taxonNode = CdmSpringContextHelper.getTaxonNodeService().find((Integer)((RowId) taxonTree.getValue()).getId()[0]);
229
			}
230
			if(taxonNode==null){
231
				taxonNode = (TaxonNode) classificationBox.getValue();
232
			}
233
			term = (TermVocabulary<NamedArea>) distAreaBox.getValue();
234
			Set<NamedArea> selectedAreas = (Set<NamedArea>) namedAreaList.getValue();
235
			DistributionEditorUtil.openDistributionView(taxonNode, term, selectedAreas);
236
			window.close();
237
		}
238
		else if(source==cancelButton){
239
			window.close();
240
		}
241
	}
197
    @Override
198
    public void valueChange(ValueChangeEvent event) {
199
        Property property = event.getProperty();
200
        if(property==classificationBox){
201
            TaxonNode parentNode = (TaxonNode) event.getProperty().getValue();
202
            if(parentNode!=null){
203
                UUID uuid = parentNode.getUuid();
204
                int id = parentNode.getId();
205
                String titleCache = parentNode.getClassification().getTitleCache();
206
                UuidAndTitleCache<TaxonNode> parent = new UuidAndTitleCache<>(uuid, id, titleCache);
207
                taxonTree.setContainerDataSource(new TaxonNodeContainer(parent));
208
            }
209
            else{
210
                taxonTree.setContainerDataSource(null);
211
            }
212
        }
213

  
214
        else if(property==distAreaBox){
215
            TermVocabulary<NamedArea> vocabulary = (TermVocabulary<NamedArea>) event.getProperty().getValue();
216
            NamedAreaContainer container = new NamedAreaContainer(vocabulary);
217
            namedAreaList.setContainerDataSource(container);
218
        }
219
        updateButtons();
220
    }
221

  
222
    @Override
223
    protected boolean isValid() {
224
        return classificationBox.getValue()!=null && distAreaBox.getValue()!=null;
225
    }
226

  
227
    @Override
228
    public void buttonClick(ClickEvent event) {
229
        Object source = event.getSource();
230
        if(source==okButton){
231
            TaxonNode taxonNode = null;
232
            TermVocabulary<NamedArea> term = null;
233
            //TODO use field converter
234
            if(taxonTree.getValue()!=null){
235
                taxonNode = CdmSpringContextHelper.getTaxonNodeService().find((Integer)((RowId) taxonTree.getValue()).getId()[0]);
236
            }
237
            if(taxonNode==null){
238
                taxonNode = (TaxonNode) classificationBox.getValue();
239
            }
240
            term = (TermVocabulary<NamedArea>) distAreaBox.getValue();
241
            Set<NamedArea> selectedAreas = (Set<NamedArea>) namedAreaList.getValue();
242
            DistributionEditorUtil.openDistributionView(taxonNode, term, selectedAreas);
243
            window.close();
244
        }
245
        else if(source==cancelButton){
246
            window.close();
247
        }
248
    }
249

  
250
    @Override
251
    public void nodeExpand(ExpandEvent event) {
252
        UuidAndTitleCache<TaxonNode> parent = (UuidAndTitleCache<TaxonNode>) event.getItemId();
253
        Collection<UuidAndTitleCache<TaxonNode>> children = CdmSpringContextHelper.getTaxonNodeService().listChildNodesAsUuidAndTitleCache(parent);
254
        taxonTree.setChildrenAllowed(parent, !children.isEmpty());
255
        for (UuidAndTitleCache<TaxonNode> child : children) {
256
            Item childItem = taxonTree.addItem(child);
257
            if(childItem!=null){
258
                taxonTree.setParent(child, parent);
259
                childItem.getItemProperty(TaxonNodeContainer.LABEL).setValue(child.getTitleCache());
260
            }
261
            Collection<UuidAndTitleCache<TaxonNode>> grandChildren = CdmSpringContextHelper.getTaxonNodeService().listChildNodesAsUuidAndTitleCache(child);
262
            taxonTree.setChildrenAllowed(child, !grandChildren.isEmpty());
263
        }
264
    }
242 265

  
243 266
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)