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;
}
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;
}
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));
}
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;
}
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;
}
Aggregations