Search in sources :

Example 1 with ConnectionSwitch

use of org.talend.core.model.metadata.builder.connection.util.ConnectionSwitch in project tdq-studio-se by Talend.

the class ReadItemConnectionFile method test.

@Test
public void test() {
    EMFUtil emfUtil = new EMFUtil();
    File file = new File("data/my_0.1.item");
    System.out.println("Loading file " + file.getAbsolutePath());
    ResourceSet rs = emfUtil.getResourceSet();
    Resource r = rs.getResource(URI.createFileURI(file.getAbsolutePath()), true);
    // TreeIterator<EObject> allContents = r.getAllContents();
    // while (allContents.hasNext()) {
    // EObject metadata = allContents.next();
    // System.out.println(metadata);
    // }
    // should contain the connection, then catalog/schema, then tables, then columns
    EList<EObject> contents = r.getContents();
    // switch class that returns a connection when it finds one.
    ConnectionSwitch<Connection> connectionFinder = new ConnectionSwitch<Connection>() {

        /*
             * (non-Javadoc)
             * 
             * @see
             * org.talend.core.model.metadata.builder.connection.util.ConnectionSwitch#caseConnection(org.talend.core
             * .model.metadata.builder.connection.Connection)
             */
        @Override
        public Connection caseConnection(Connection object) {
            return object;
        }
    };
    // loop on all the content of the resource
    for (EObject eObject : contents) {
        Connection connection = connectionFinder.doSwitch(eObject);
        if (connection != null) {
            Set<Catalog> allCatalogs = ConnectionHelper.getAllCatalogs(connection);
            for (Catalog catalog : allCatalogs) {
                System.out.println("## Catalog: " + catalog.getName());
                System.out.println("\t## Tables");
                List<TdTable> tables = CatalogHelper.getTables(catalog);
                for (TdTable tdTable : tables) {
                    System.out.println("\t\t" + tdTable.getName());
                    List<TdColumn> columns = TableHelper.getColumns(tdTable);
                    if (!columns.isEmpty()) {
                        System.out.println("\t\t\t## Columns");
                    }
                    for (TdColumn tdColumn : columns) {
                        System.out.println("\t\t\t\t" + tdColumn.getName());
                    }
                }
                System.out.println("\t## Views");
                List<TdView> views = CatalogHelper.getViews(catalog);
                for (TdView tdView : views) {
                    System.out.println("\t\t" + tdView.getName());
                    List<TdColumn> columns = ViewHelper.getColumns(tdView);
                    if (!columns.isEmpty()) {
                        System.out.println("\t\t\t## Columns");
                    }
                    for (TdColumn tdColumn : columns) {
                        System.out.println("\t\t\t\t" + tdColumn.getName());
                    }
                }
            }
        }
    }
}
Also used : TdTable(org.talend.cwm.relational.TdTable) Resource(org.eclipse.emf.ecore.resource.Resource) Connection(org.talend.core.model.metadata.builder.connection.Connection) ConnectionSwitch(org.talend.core.model.metadata.builder.connection.util.ConnectionSwitch) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) Catalog(orgomg.cwm.resource.relational.Catalog) TdColumn(org.talend.cwm.relational.TdColumn) EMFUtil(org.talend.commons.emf.EMFUtil) TdView(org.talend.cwm.relational.TdView) EObject(org.eclipse.emf.ecore.EObject) File(java.io.File) Test(org.junit.Test)

Aggregations

File (java.io.File)1 EObject (org.eclipse.emf.ecore.EObject)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 Test (org.junit.Test)1 EMFUtil (org.talend.commons.emf.EMFUtil)1 Connection (org.talend.core.model.metadata.builder.connection.Connection)1 ConnectionSwitch (org.talend.core.model.metadata.builder.connection.util.ConnectionSwitch)1 TdColumn (org.talend.cwm.relational.TdColumn)1 TdTable (org.talend.cwm.relational.TdTable)1 TdView (org.talend.cwm.relational.TdView)1 Catalog (orgomg.cwm.resource.relational.Catalog)1