Hierarchical Container & SQLContainer

Hi:

I’m trying to build a tree or a hierarchical table from a MySQL database with the following format:

Column 1 Column 2 Column 3
A A1 A11
A A2 A21
B B1 B11
B B1 B12

in order to display something like:

Column 1 Column 2 Column 3
A A1 A11
A2 A21
B B1 B11
B12

How can I build a hierarchical container from a basic SQL container?

regards,

Hugo

Apparently I didn’t post correctly the spacing in the tables and trees, but I hope you got the idea…

This is basically what I want to obtain:

A A1 A11
—A2 A21
B B1 B11
-------B12

You can extend SQLContainer and implement the Hierarchical interface as is done
here for BeanItemContainer
.

The basic solutions are explained in Knowledge Base article
#254: How do I bind a Tree to an SQLContainer?
(Pro Account required).

Hi:

Thank you very much for your answer. I implemented the proposed solution to populate a tree from MySQL and it works, but not hierarchically…could you please tell me what I’m missing here??? In my DB table, “userId” is the main id, “parentId” is which determines the parent

package com.example.ver003.data;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import com.vaadin.addon.sqlcontainer.SQLContainer;
import com.vaadin.addon.sqlcontainer.connection.JDBCConnectionPool;
import com.vaadin.addon.sqlcontainer.connection.SimpleJDBCConnectionPool;
import com.vaadin.addon.sqlcontainer.query.TableQuery;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.util.filter.Compare;


public class UserHierarchicalContainer implements Container.Hierarchical   {
	private JDBCConnectionPool connectionPool = null;
	private SQLContainer personContainer = null;
	
	private Object itemId=null;

    public UserHierarchicalContainer() {
  	  initConnectionPool();
  	  initContainers();
    }

    private void initConnectionPool() {
      	try {
          	connectionPool = new SimpleJDBCConnectionPool(
                  	"com.mysql.jdbc.Driver",
                  	"jdbc:mysql://localhost:3306/data", "root", "xxx", 2, 5);
      	} catch (SQLException e) {
        	  e.printStackTrace();
        }
    }
 
 
    private void initContainers() {
        try {
            /* TableQuery and SQLContainer for personaddress -table */
            TableQuery q = new TableQuery("users", connectionPool);
            q.setVersionColumn("VERSION");
            personContainer = new SQLContainer(q);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    public SQLContainer getSQL(){
		return personContainer;
    	    }

	public Item getItem(Object itemId) {
		// TODO Auto-generated method stub
		return null;
	}

	public Collection<?> getContainerPropertyIds() {
		// TODO Auto-generated method stub
		return null;
	}

	public Collection<?> getItemIds() {
		// TODO Auto-generated method stub
		return null;
	}

	public Property getContainerProperty(Object userId, Object propertyId) {
		// TODO Auto-generated method stub
		return null;
	}

	public Class<?> getType(Object propertyId) {
		// TODO Auto-generated method stub
		return null;
	}

	public int size() {
		// TODO Auto-generated method stub
		return 0;
	}

	public boolean containsId(Object itemId) {
		// TODO Auto-generated method stub
		return false;
	}

	public Item addItem(Object itemId) throws UnsupportedOperationException {
		// TODO Auto-generated method stub
		return null;
	}

	public Object addItem() throws UnsupportedOperationException {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean addContainerProperty(Object propertyId, Class<?> type,
			Object defaultValue) throws UnsupportedOperationException {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean removeContainerProperty(Object propertyId)
			throws UnsupportedOperationException {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean removeAllItems() throws UnsupportedOperationException {
		// TODO Auto-generated method stub
		return false;
	}

	public Collection<?> getChildren(Object itemId) {
		// TODO Auto-generated method stub
		String filter = (String) getItem(itemId).getItemProperty("userid").getValue();
		personContainer.addContainerFilter("parentId", filter, true, false);
		ArrayList<Object> myList = new ArrayList<Object>();
		myList.addAll(personContainer.getItemIds());
		personContainer.removeContainerFilters("parentId");
        return myList;

	}

	public Object getParent(Object itemId) {
		// TODO Auto-generated method stub
		return getContainerProperty(itemId, "parentId");
	}

	public Collection<?> rootItemIds() {
		// TODO Auto-generated method stub
		String filter = (String) getItem(itemId).getItemProperty("userid").getValue();
		personContainer.addContainerFilter("parentId", "0", true, false);
		ArrayList<Object> myList = new ArrayList<Object>();
		myList.addAll(personContainer.getItemIds());
		personContainer.removeContainerFilters("parentId");
        return myList;
	}

	public boolean setParent(Object itemId, Object newParentId)
			throws UnsupportedOperationException {
		// TODO Auto-generated method stub
		return true;
	}

	public boolean areChildrenAllowed(Object itemId) {
        if ((Integer)getItem(itemId).getItemProperty("leaf").getValue()==1) {
            return false;
        }
        return true;

		// TODO Auto-generated method stub
	}

	public boolean setChildrenAllowed(Object itemId, boolean areChildrenAllowed)
			throws UnsupportedOperationException {
		// TODO Auto-generated method stub
		return true;
	}

	public boolean isRoot(Object itemId) {
		// TODO Auto-generated method stub
		return true;
	}

	public boolean hasChildren(Object itemId) { 
		// TODO Auto-generated method stub
		return true;
	}

	public boolean removeItem(Object itemId)
			throws UnsupportedOperationException {
		// TODO Auto-generated method stub
		return false;
	}
}

on the tree side, I have this:

package com.example.ver003.ui;

import com.example.ver003.Ver003Application;
import com.example.ver003.data.UserHierarchicalContainer;
import com.vaadin.addon.sqlcontainer.SQLContainer;
import com.vaadin.addon.treetable.TreeTable;

public class OrgChart extends TreeTable {
	
	private UserHierarchicalContainer userHierarchicalContainer=new UserHierarchicalContainer();
	private SQLContainer sqlc = userHierarchicalContainer.getSQL();
	
	

	public static final Object[] MOSTRAR = new Object[]
 {"completeName"};

	public OrgChart(Ver003Application app) {
		
		setContainerDataSource(sqlc);
		setVisibleColumns(MOSTRAR);
		setSizeFull();
		
		setSelectable(true);
                setImmediate(true);
                setNullSelectionAllowed(false);

	}
	
}

Thank you very much!!!

You are not extending SQLContainer there, just creating a container that holds an SQLContainer. That would be fine if you delegated most of the calls to that object. But you have to set the implemented hierarchical container as the container data source for the tree, not the SQLContainer as you do there.

Thank you very much, this really made me rethink all my connections and now it works much better…

regards,

Hugo