Search in sources :

Example 1 with EDatabaseSchemaOrCatalogMapping

use of org.talend.core.model.metadata.builder.database.EDatabaseSchemaOrCatalogMapping in project tdi-studio-se by Talend.

the class MergeTosMetadataMigrationTask method execute.

@Override
public ExecutionResult execute(Item item) {
    if (item instanceof ConnectionItem) {
        try {
            URI itemURI = getItemURI(item);
            if (itemURI != null) {
                URI itemResourceURI = getItemResourceURI(itemURI);
                if (metadata400to410 == null) {
                    metadata400to410 = new TosMetadataMigrationFrom400to410();
                }
                Resource migratedResource = metadata400to410.migrate(itemResourceURI.toString(), new NullProgressMonitor());
                HashMap<String, Object> xmlSaveOtions = XML_SAVE_OTIONS_1_0;
                if (migratedResource != null) {
                    // check for DB connection caus we need to setup Schema and Catalog properly
                    EObject content = migratedResource.getContents().get(0);
                    if (content != null && "DatabaseConnection".equals(content.eClass().getName())) {
                        // resource is dynamic EMF so convert it to static model by serialising it and reloading it
                        ByteArrayOutputStream tempStream = new ByteArrayOutputStream();
                        try {
                            // serialize into memory
                            try {
                                migratedResource.save(tempStream, XML_SAVE_OTIONS_1_0);
                            } catch (Exception e) {
                                // try with version 1.1
                                tempStream = new ByteArrayOutputStream();
                                xmlSaveOtions = XML_SAVE_OTIONS_1_1;
                                migratedResource.save(tempStream, xmlSaveOtions);
                            } finally {
                                tempStream.close();
                            }
                            // create a resource to laod the inmemory resource that should be a static EMF model
                            migratedResource = resourceSet.createResource(URI.createURI(//$NON-NLS-1$
                            "http://talend/dummy_static.metadata"));
                            migratedResource.load(new ByteArrayInputStream(tempStream.toByteArray()), xmlSaveOtions);
                            // check that DBConnection is firdt element
                            DatabaseConnection databaseConnection = SwitchHelpers.DATABASECONNECTION_SWITCH.doSwitch(migratedResource.getContents().get(0));
                            // do not check for null caus DB connection is already check above
                            String databaseType = databaseConnection.getDatabaseType();
                            databaseConnection.setDriverClass(ExtractMetaDataUtils.getInstance().getDriverClassByDbType(databaseType));
                            EDatabaseTypeName currentType = EDatabaseTypeName.getTypeFromDbType(databaseType);
                            EDatabaseSchemaOrCatalogMapping curCatalog = currentType.getCatalogMappingField();
                            EDatabaseSchemaOrCatalogMapping curSchema = currentType.getSchemaMappingField();
                            // all the DB connection are migrated with a Schema by default
                            if (!curCatalog.equals(EDatabaseSchemaOrCatalogMapping.None)) {
                                List<Schema> schemas = ConnectionHelper.getSchema(databaseConnection);
                                if (!curSchema.equals(EDatabaseSchemaOrCatalogMapping.None)) {
                                    // we need to place the current schemas into a catalogs
                                    ConnectionHelper.removeSchemas(schemas, databaseConnection);
                                    for (Schema schema : schemas) {
                                        // compute the name of the schema and the catalogs
                                        String schemaName = computeSchemaName(schema, databaseConnection, curSchema);
                                        String catalogName = computeCatalogName(databaseConnection, curCatalog);
                                        schema.setName(schemaName);
                                        Catalog catalog = RelationalFactory.eINSTANCE.createCatalog();
                                        // catalogs are not in a contained reference
                                        migratedResource.getContents().add(catalog);
                                        catalog.setName(catalogName);
                                        // add the schema to the catalog and the the catalog to the connection
                                        CatalogHelper.addSchemas(Collections.singleton(schema), catalog);
                                        ConnectionHelper.addCatalog(catalog, databaseConnection);
                                    }
                                } else {
                                    // we need to replace the Schemas with a Catalogs
                                    for (Schema schema : schemas) {
                                        // compute the name the catalog
                                        String catalogName = computeCatalogName(databaseConnection, curCatalog);
                                        // use owned elements to get everything regardless of tables or views or
                                        // else
                                        Catalog catalog = RelationalFactory.eINSTANCE.createCatalog();
                                        // catalogs are not in a contained reference
                                        migratedResource.getContents().add(catalog);
                                        catalog.setName(catalogName);
                                        catalog.getOwnedElement().addAll(schema.getOwnedElement());
                                        ConnectionHelper.addCatalog(catalog, databaseConnection);
                                        ConnectionHelper.removeSchemas(Collections.singleton(schema), databaseConnection);
                                    }
                                }
                            } else if (!curSchema.equals(EDatabaseSchemaOrCatalogMapping.None)) {
                                List<Schema> schemas = ConnectionHelper.getSchema(databaseConnection);
                                for (Schema schema : schemas) {
                                    String schemaName = computeSchemaName(schema, databaseConnection, curSchema);
                                    schema.setName(schemaName);
                                }
                            }
                        // else no catalog so we keep the schema as is
                        } catch (Exception e) {
                            // we have an exception finalising the migration but we trap it caus we still try to
                            // save it
                            log.error("Cannot complete merge metadata migration on file:" + itemResourceURI.toString(), e);
                            ExceptionHandler.process(e);
                        } finally {
                            tempStream.close();
                        }
                    }
                    // else not a DB connection so persist
                    OutputStream outputStream = item.eResource().getResourceSet().getURIConverter().createOutputStream(itemResourceURI, null);
                    try {
                        migratedResource.save(outputStream, xmlSaveOtions);
                    } finally {
                        outputStream.close();
                    }
                }
                return ExecutionResult.SUCCESS_WITH_ALERT;
            }
        } catch (ATLCoreException e) {
            log.error(e);
            ExceptionHandler.process(e);
            return ExecutionResult.SUCCESS_NO_ALERT;
        } catch (IOException e) {
            log.error(e);
            ExceptionHandler.process(e);
            return ExecutionResult.SUCCESS_NO_ALERT;
        } finally {
            resourceSet.getResources().clear();
        }
    }
    return ExecutionResult.NOTHING_TO_DO;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ConnectionItem(org.talend.core.model.properties.ConnectionItem) EDatabaseSchemaOrCatalogMapping(org.talend.core.model.metadata.builder.database.EDatabaseSchemaOrCatalogMapping) Schema(orgomg.cwm.resource.relational.Schema) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource) Resource(org.eclipse.emf.ecore.resource.Resource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) URI(org.eclipse.emf.common.util.URI) ATLCoreException(org.eclipse.m2m.atl.core.ATLCoreException) IOException(java.io.IOException) Catalog(orgomg.cwm.resource.relational.Catalog) ATLCoreException(org.eclipse.m2m.atl.core.ATLCoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) TosMetadataMigrationFrom400to410(org.talend.model.migration.TosMetadataMigrationFrom400to410) EObject(org.eclipse.emf.ecore.EObject) EObject(org.eclipse.emf.ecore.EObject) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) List(java.util.List) EDatabaseTypeName(org.talend.core.database.EDatabaseTypeName)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 List (java.util.List)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 XMLResource (org.eclipse.emf.ecore.xmi.XMLResource)1 ATLCoreException (org.eclipse.m2m.atl.core.ATLCoreException)1 EDatabaseTypeName (org.talend.core.database.EDatabaseTypeName)1 DatabaseConnection (org.talend.core.model.metadata.builder.connection.DatabaseConnection)1 EDatabaseSchemaOrCatalogMapping (org.talend.core.model.metadata.builder.database.EDatabaseSchemaOrCatalogMapping)1 ConnectionItem (org.talend.core.model.properties.ConnectionItem)1 TosMetadataMigrationFrom400to410 (org.talend.model.migration.TosMetadataMigrationFrom400to410)1 Catalog (orgomg.cwm.resource.relational.Catalog)1 Schema (orgomg.cwm.resource.relational.Schema)1