Search in sources :

Example 11 with Resource

use of org.eclipse.emf.ecore.resource.Resource 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)

Example 12 with Resource

use of org.eclipse.emf.ecore.resource.Resource in project tdi-studio-se by Talend.

the class MetadataEmfFactory method getOutputStream.

@SuppressWarnings("unchecked")
public OutputStream getOutputStream() throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    //$NON-NLS-1$
    URI uri = URI.createURI("");
    Resource res = new TalendFileResourceImpl(uri);
    res.getContents().add(metadataType);
    res.save(out, null);
    return out;
}
Also used : TalendFileResourceImpl(org.talend.designer.core.model.utils.emf.talendfile.util.TalendFileResourceImpl) Resource(org.eclipse.emf.ecore.resource.Resource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URI(org.eclipse.emf.common.util.URI)

Example 13 with Resource

use of org.eclipse.emf.ecore.resource.Resource in project tdi-studio-se by Talend.

the class MetadataEmfFactory method setInputStream.

public void setInputStream(final InputStream istream) throws IOException {
    //$NON-NLS-1$
    URI uri = URI.createURI("");
    TalendFilePackage.eINSTANCE.getNsURI();
    Resource res = new TalendFileResourceImpl(uri);
    res.load(istream, null);
    setMetadataType((MetadataType) res.getContents().get(0));
}
Also used : TalendFileResourceImpl(org.talend.designer.core.model.utils.emf.talendfile.util.TalendFileResourceImpl) Resource(org.eclipse.emf.ecore.resource.Resource) URI(org.eclipse.emf.common.util.URI)

Example 14 with Resource

use of org.eclipse.emf.ecore.resource.Resource in project tdi-studio-se by Talend.

the class JSONRepositoryContentHandler method save.

private Resource save(JSONFileConnectionItem item) {
    Resource itemResource = xmiResourceManager.getItemResource(item);
    itemResource.getContents().clear();
    MetadataManager.addContents(item, itemResource);
    // add to the current resource all Document and Description instances because they are not reference in
    // containment references.
    Map<EObject, Collection<Setting>> externalCrossref = EcoreUtil.ExternalCrossReferencer.find(item.getConnection());
    Collection<Object> documents = EcoreUtil.getObjectsByType(externalCrossref.keySet(), BusinessinformationPackage.Literals.DOCUMENT);
    for (Object doc : documents) {
        itemResource.getContents().add((EObject) doc);
    }
    Collection<Object> descriptions = EcoreUtil.getObjectsByType(externalCrossref.keySet(), BusinessinformationPackage.Literals.DESCRIPTION);
    for (Object doc : descriptions) {
        itemResource.getContents().add((EObject) doc);
    }
    return itemResource;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) Resource(org.eclipse.emf.ecore.resource.Resource) Collection(java.util.Collection) EObject(org.eclipse.emf.ecore.EObject) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject)

Example 15 with Resource

use of org.eclipse.emf.ecore.resource.Resource in project tdi-studio-se by Talend.

the class JSONRepositoryContentHandler method create.

private Resource create(IProject project, JSONFileConnectionItem item, IPath path, ERepositoryObjectType type) throws PersistenceException {
    Resource itemResource = xmiResourceManager.createItemResource(project, item, path, type, false);
    MetadataManager.addContents(item, itemResource);
    return itemResource;
}
Also used : Resource(org.eclipse.emf.ecore.resource.Resource)

Aggregations

Resource (org.eclipse.emf.ecore.resource.Resource)58 URI (org.eclipse.emf.common.util.URI)23 IOException (java.io.IOException)18 IResource (org.eclipse.core.resources.IResource)11 EObject (org.eclipse.emf.ecore.EObject)10 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)10 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)9 PersistenceException (org.talend.commons.exception.PersistenceException)8 XmiResourceManager (org.talend.core.repository.utils.XmiResourceManager)8 ArrayList (java.util.ArrayList)7 CoreException (org.eclipse.core.runtime.CoreException)7 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)7 XMIResourceFactoryImpl (org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl)7 List (java.util.List)6 IProject (org.eclipse.core.resources.IProject)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IFile (org.eclipse.core.resources.IFile)5 TalendXMIResource (org.talend.commons.runtime.model.emf.TalendXMIResource)5 ByteArrayResource (org.talend.core.model.properties.helper.ByteArrayResource)5 CwmResource (org.talend.model.emf.CwmResource)5