Skip to content

Commit

Permalink
Merge pull request #119 from mdrillin/TDES-1628
Browse files Browse the repository at this point in the history
TEIIDDES-1628
  • Loading branch information
blafond committed Mar 27, 2013
2 parents 8859f7b + fb6948a commit dc841e3
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.teiid.designer.core.resource.xmi.MtkXmiResourceImpl;
import org.teiid.designer.metamodels.core.ModelAnnotation;


/**
Expand All @@ -21,23 +22,38 @@
* @since 8.0
*/
public class TransientModelSelector extends URIModelSelector {

ModelAnnotation mdlAnnotation;

//============================================================================================================================
// Constructors

/**
* @since 4.1
* @param uri the uri string
*/
public TransientModelSelector(final String uri) {
this(URI.createURI(uri));
}

/**
* @since 4.1
* @param uri the model URI
*/
public TransientModelSelector(final URI uri) {
super(uri);
}

/**
* @since 8.1
* @param uri the model URI
* @param mdlAnnotation model annotation
*/
public TransientModelSelector(final URI uri,ModelAnnotation mdlAnnotation) {
super(uri);
this.mdlAnnotation=mdlAnnotation;
}

//============================================================================================================================
// Overridden Methods

Expand All @@ -55,6 +71,12 @@ public void open() {
set.getResources().add(resrc);
this.contents = resrc.getModelContents();
this.resource = resrc;

if(mdlAnnotation!=null) {
ModelAnnotation annotation = resrc.getModelAnnotation();
annotation.setPrimaryMetamodelUri(this.mdlAnnotation.getPrimaryMetamodelUri());
annotation.setModelType(this.mdlAnnotation.getModelType());
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.modeshape.graph.JcrLexicon;
import org.modeshape.graph.property.Name;
Expand All @@ -42,14 +43,17 @@
import org.teiid.core.designer.CoreModelerPlugin;
import org.teiid.core.designer.I18n;
import org.teiid.core.designer.exception.EmptyArgumentException;
import org.teiid.core.designer.util.CoreArgCheck;
import org.teiid.core.designer.util.FileUtils;
import org.teiid.core.designer.util.OperationUtil;
import org.teiid.core.designer.util.OperationUtil.Unreliable;
import org.teiid.core.designer.util.FileUtils;
import org.teiid.designer.compare.DifferenceProcessor;
import org.teiid.designer.compare.DifferenceReport;
import org.teiid.designer.compare.MergeProcessor;
import org.teiid.designer.compare.ModelerComparePlugin;
import org.teiid.designer.compare.processor.DifferenceProcessorImpl;
import org.teiid.designer.compare.selector.ModelResourceSelector;
import org.teiid.designer.compare.selector.ModelSelector;
import org.teiid.designer.compare.selector.TransientModelSelector;
import org.teiid.designer.core.ModelerCore;
import org.teiid.designer.core.validation.rules.StringNameValidator;
Expand Down Expand Up @@ -91,7 +95,7 @@ public class DdlImporter {
private String modelName;
private IFile modelFile;
private ModelType modelType;
private DifferenceProcessorImpl chgProcessor;
private DifferenceProcessor chgProcessor;
private ModelResource model;
private boolean optToCreateModelEntitiesForUnsupportedDdl;
private boolean optToSetModelEntityDescription;
Expand Down Expand Up @@ -377,28 +381,76 @@ private void handleStatus( final IStatus status ) {
}
}

/**
* @param messages
* @param monitor
* @param totalWork
*/
public void importDdl( final List<String> messages,
final IProgressMonitor monitor,
final int totalWork ) {
if (chgProcessor != null) return;
OperationUtil.perform(new Unreliable() {

private FileReader reader = null;

@Override
public void doIfFails() {
}

@Override
public void finallyDo() throws Exception {
if (reader != null) reader.close();
}

@Override
public void tryToDo() throws Exception {
reader = new FileReader(ddlFileName());
importDdl(reader, messages, monitor, totalWork);
}
});
}

void importDdl( final FileReader reader,
final List<String> messages,
final IProgressMonitor monitor,
final int totalWork ) throws IOException, CoreException {

final int workUnit = totalWork / 3;

// ------------------------------------------------------------------------------
// Parse the DDL from the file
// ------------------------------------------------------------------------------
monitor.subTask(DdlImporterI18n.PARSING_DDL_MSG);
// Read the file contents
final char[] buf = new char[FileUtils.DEFAULT_BUFFER_SIZE];
final StringBuilder builder = new StringBuilder();
for (int charTot = reader.read(buf); charTot >= 0; charTot = reader.read(buf))
builder.append(buf, 0, charTot);
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
final String ddlString = builder.toString();

// Parse the DDL
final DdlParsers parsers = new DdlParsers();
final AstNode rootNode = parsers.parse(builder.toString(), ddlFileName);
final AstNode rootNode = parsers.parse(ddlString, ddlFileName);
if (monitor.isCanceled()) throw new OperationCanceledException();
monitor.worked(workUnit);

// ------------------------------------------------------------------------------
// Set up DifferenceProcessor
// - startingSelector -- existing model
// - endingSelector -- generated from parsed ddl nodes
// ------------------------------------------------------------------------------
monitor.subTask(DdlImporterI18n.CREATING_MODEL_MSG);
// TODO: cleanup old model in case import called multiple times
model = ModelerCore.create(modelFile);
final TransientModelSelector endSelector = new TransientModelSelector(model.getEmfResource().getURI());
final DifferenceProcessorImpl processor = new DifferenceProcessorImpl(new ModelResourceSelector(model), endSelector);
processor.addEObjectMatcherFactories(ModelerComparePlugin.createEObjectMatcherFactories());
final List<EObject> roots = endSelector.getRootObjects();
final ModelResourceSelector startingSelector = new ModelResourceSelector(model);
final URI mdlUri = URI.createFileURI(model.getPath().toFile().getAbsolutePath());
final ModelAnnotation mdlAnnotation = model.getModelAnnotation();

final TransientModelSelector endingSelector = new TransientModelSelector(mdlUri,mdlAnnotation);

final DifferenceProcessor diffProcessor = createDifferenceProcessor(startingSelector,endingSelector);
final List<EObject> roots = endingSelector.getRootObjects();
for (final AstNode node : rootNode) {
final Name mixinType = mixinType(node);
if (StandardDdlLexicon.TYPE_CREATE_SCHEMA_STATEMENT.equals(mixinType)) {
Expand All @@ -412,41 +464,32 @@ void importDdl( final FileReader reader,
}
if (monitor.isCanceled()) throw new OperationCanceledException();
monitor.worked(workUnit);


// ------------------------------------------------------------------------------
// Execute generates the differenceReport
// ------------------------------------------------------------------------------
monitor.subTask(DdlImporterI18n.CREATING_CHANGE_REPORT_MSG);
handleStatus(processor.execute(monitor));
handleStatus(diffProcessor.execute(monitor));
if (monitor.isCanceled()) throw new OperationCanceledException();
monitor.worked(workUnit);
chgProcessor = processor;

chgProcessor = diffProcessor;
}

/**
* @param messages
* @param monitor
* @param totalWork

/*
* Create DifferenceProcessor using starting and ending ModelSelector
* @param startingSelector the starting selector
* @param endingSelector the ending selector
* @return the difference processor
*/
public void importDdl( final List<String> messages,
final IProgressMonitor monitor,
final int totalWork ) {
if (chgProcessor != null) return;
OperationUtil.perform(new Unreliable() {

private FileReader reader = null;

@Override
public void doIfFails() {
}

@Override
public void finallyDo() throws Exception {
if (reader != null) reader.close();
}

@Override
public void tryToDo() throws Exception {
reader = new FileReader(ddlFileName());
importDdl(reader, messages, monitor, totalWork);
}
});
private DifferenceProcessor createDifferenceProcessor( final ModelSelector startingSelector,
final ModelSelector endingSelector ) {
CoreArgCheck.isNotNull(startingSelector);
CoreArgCheck.isNotNull(endingSelector);
final DifferenceProcessor processor = new DifferenceProcessorImpl(startingSelector, endingSelector);
processor.addEObjectMatcherFactories(ModelerComparePlugin.createEObjectMatcherFactories());
return processor;
}

private void initialize( final RelationalEntity entity,
Expand Down Expand Up @@ -741,4 +784,4 @@ private class Info<T extends RelationalEntity> {
}
}
}
}
}

0 comments on commit dc841e3

Please sign in to comment.