From 489dcdf697f8752924199aca84d111e5d011d656 Mon Sep 17 00:00:00 2001 From: Antti Villberg Date: Tue, 18 Apr 2017 16:15:06 +0300 Subject: [PATCH] Generic Model Import refs #7144 Change-Id: I3bcf568b70e12b3493563901c7de228b9be35cd4 --- bundles/org.simantics.modeling.ui/plugin.xml | 9 + .../ui/sharedontology/wizard/Constants.java | 5 + .../wizard/ModelImportPage.java | 213 ++++++++++++++++ .../wizard/ModelImportWizard.java | 228 ++++++++++++++++++ .../wizard/WizardExtensionFactory.java | 4 + 5 files changed, 459 insertions(+) create mode 100644 bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImportPage.java create mode 100644 bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImportWizard.java diff --git a/bundles/org.simantics.modeling.ui/plugin.xml b/bundles/org.simantics.modeling.ui/plugin.xml index c0bdf8a08..c6596a7b0 100644 --- a/bundles/org.simantics.modeling.ui/plugin.xml +++ b/bundles/org.simantics.modeling.ui/plugin.xml @@ -1631,6 +1631,15 @@ + + + + diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/Constants.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/Constants.java index f90ff02f7..df6cdca92 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/Constants.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/Constants.java @@ -18,8 +18,13 @@ public class Constants { public static final String SHARED_LIBRARY_FORMAT = "sharedLibrary"; + public static final String MODEL_FORMAT = "model"; + public static final String SHARED_LIBRARY_FORMAT_V1 = SHARED_LIBRARY_FORMAT + ":1"; public static final int SHARED_LIBRARY_CURRENT_VERSION = 1; + + public static final String MODEL_FORMAT_V1 = MODEL_FORMAT + ":1"; + } diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImportPage.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImportPage.java new file mode 100644 index 000000000..f2c8423eb --- /dev/null +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImportPage.java @@ -0,0 +1,213 @@ +/******************************************************************************* + * Copyright (c) 2012 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.modeling.ui.sharedontology.wizard; + +import java.io.File; +import java.io.IOException; +import java.util.Collections; +import java.util.List; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.jface.wizard.WizardPage; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.CCombo; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.FileDialog; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Text; +import org.simantics.databoard.Bindings; +import org.simantics.databoard.adapter.AdaptException; +import org.simantics.databoard.binding.mutable.Variant; +import org.simantics.databoard.container.DataContainer; +import org.simantics.databoard.container.DataContainers; +import org.simantics.db.common.NamedResource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.util.DraftStatusBean; +import org.simantics.utils.ui.ErrorLogger; + +/** + * @author Tuukka Lehtonen + */ +public class ModelImportPage extends WizardPage { + + /** + * If non-null, the wizard cannot continue. This message tells why. + */ + String failure; + + ImportPlan importModel; + + Composite draft; + + Text importTarget; + CCombo importLocation; + + List models = Collections.emptyList(); + Label author; + Label status; + + protected ModelImportPage(ImportPlan model) { + super("Import Model", "Define Import Location", null); + this.importModel = model; + } + + @Override + public void createControl(Composite parent) { + Composite container = new Composite(parent, SWT.NONE); + { + GridLayout layout = new GridLayout(); + layout.horizontalSpacing = 20; + layout.verticalSpacing = 10; + layout.numColumns = 3; + container.setLayout(layout); + } + + draft = new Composite(container, SWT.NONE); + draft.setBackground(draft.getDisplay().getSystemColor(SWT.COLOR_RED)); + GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(draft); + GridLayoutFactory.swtDefaults().numColumns(0).margins(0, 0).applyTo(draft); + + Composite draft2 = new Composite(draft, SWT.NONE); + GridLayoutFactory.swtDefaults().applyTo(draft2); + GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(draft2); + new Label(draft2, SWT.NONE).setText("This model draft was not finished for publishing."); + + new Label(container, SWT.NONE).setText("&Model file:"); + importLocation = new CCombo(container, SWT.BORDER); + { + importLocation.setText(""); + GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(importLocation); + importLocation.addModifyListener(new ModifyListener(){ + @Override + public void modifyText(ModifyEvent e) { + validatePage(); + } + }); + } + Button browseFileButton = new Button(container, SWT.PUSH); + { + browseFileButton.setText("Br&owse..."); + browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); + browseFileButton.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + FileDialog dialog = new FileDialog(getShell(), SWT.OPEN); + dialog.setText("Choose Model to Import"); + String loc = importLocation.getText(); + dialog.setFilterPath(loc); + dialog.setFilterExtensions(new String[] { "*.tg" }); + dialog.setFilterNames(new String[] { "Model (*.tg)" }); + String file = dialog.open(); + if (file == null) + return; + importLocation.setText(file); + validatePage(); + } + }); + } + + author = new Label(container, SWT.NONE); + author.setText(""); + GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(author); + + status = new Label(container, SWT.NONE); + status.setText(""); + GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(status); + + try { + initializeData(); + } catch (DatabaseException e) { + ErrorLogger.defaultLogError(e); + } + + setControl(container); + validatePage(); + } + + private void initializeData() throws DatabaseException { + for (String path : importModel.recentLocations) { + importLocation.add(path); + } + if (importLocation.getItemCount() > 0) + importLocation.select(0); + } + + void validatePage() { + + if (failure != null) { + setErrorMessage(failure); + setPageComplete(false); + return; + } + String importLoc = importLocation.getText(); + if (importLoc.isEmpty()) { + setMessage("Select file to import."); + setErrorMessage(null); + setPageComplete(false); + return; + } + File file = new File(importLoc); + if (!file.exists() || !file.isFile()) { + setErrorMessage("Selected file is invalid."); + setPageComplete(false); + return; + } + importModel.importLocation = file; + + try { + + DataContainer container = DataContainers.readHeader(file); + Variant draftStatus = container.metadata.get(DraftStatusBean.EXTENSION_KEY); + if(draftStatus != null) { + GridLayoutFactory.swtDefaults().spacing(5, 5).applyTo(draft); + draft.getParent().layout(true); + } else { + GridLayoutFactory.swtDefaults().numColumns(0).margins(0, 0).applyTo(draft); + draft.getParent().layout(true); + } + + Variant authorVariant = container.metadata.get("author"); + Variant dateVariant = container.metadata.get("date"); + + if(authorVariant != null && dateVariant != null) { + String auth = (String)authorVariant.getValue(Bindings.STRING); + String date = (String)dateVariant.getValue(Bindings.STRING); + author.setText("Created by " + auth + " on " + date); + } else { + author.setText(""); + } + + } catch (IOException e) { + setErrorMessage("Could not read header information from " + file.getAbsolutePath()); + setPageComplete(false); + return; + } catch (AdaptException e) { + setErrorMessage("Could not read header information from " + file.getAbsolutePath()); + setPageComplete(false); + return; + } + + setErrorMessage(null); + setMessage("Ready to import " + file.getName() + ""); + setPageComplete(true); + + } + +} diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImportWizard.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImportWizard.java new file mode 100644 index 000000000..235c304e6 --- /dev/null +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImportWizard.java @@ -0,0 +1,228 @@ +/******************************************************************************* + * Copyright (c) 2012 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.modeling.ui.sharedontology.wizard; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.Collection; +import java.util.Deque; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.SubMonitor; +import org.eclipse.core.runtime.preferences.InstanceScope; +import org.eclipse.jface.operation.IRunnableWithProgress; +import org.eclipse.jface.preference.IPersistentPreferenceStore; +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.wizard.Wizard; +import org.eclipse.jface.wizard.WizardPage; +import org.eclipse.swt.SWT; +import org.eclipse.ui.IImportWizard; +import org.eclipse.ui.IWorkbench; +import org.eclipse.ui.preferences.ScopedPreferenceStore; +import org.simantics.Simantics; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.binding.mutable.Variant; +import org.simantics.databoard.container.DataContainer; +import org.simantics.databoard.container.DataContainers; +import org.simantics.databoard.container.FormatHandler; +import org.simantics.databoard.util.binary.BinaryFile; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.layer0.migration.MigratedImportResult; +import org.simantics.db.layer0.migration.MigrationState; +import org.simantics.db.layer0.migration.MigrationStateKeys; +import org.simantics.db.layer0.migration.MigrationUtils; +import org.simantics.db.layer0.migration.ModelImportAdvisor; +import org.simantics.db.layer0.util.DraftStatusBean; +import org.simantics.db.management.ISessionContext; +import org.simantics.graph.db.ImportResult; +import org.simantics.graph.db.MissingDependencyException; +import org.simantics.graph.representation.TransferableGraph1; +import org.simantics.modeling.ui.Activator; +import org.simantics.modeling.ui.utils.NoProjectPage; +import org.simantics.project.IProject; +import org.simantics.project.ProjectKeys; +import org.simantics.ui.SimanticsUI; +import org.simantics.ui.utils.ResourceAdaptionUtils; +import org.simantics.utils.strings.EString; +import org.simantics.utils.ui.ErrorLogger; +import org.simantics.utils.ui.ExceptionUtils; +import org.simantics.utils.ui.dialogs.InfoDialog; + +/** + * @author Tuukka Lehtonen + */ +public class ModelImportWizard extends Wizard implements IImportWizard { + + private static final int MAX_RECENT_IMPORT_PATHS = 10; + + ImportPlan importModel; + + private boolean readPreferences(IStructuredSelection selection) { + IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID); + + String recentPathsPref = store.getString(Preferences.RECENT_SHARED_LIBRARY_IMPORT_LOCATIONS); + Deque recentImportPaths = Preferences.decodePaths(recentPathsPref); + + ISessionContext ctx = SimanticsUI.getSessionContext(); + if (ctx == null) + return false; + IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT); + if (project == null) + return false; + + importModel = new ImportPlan(ctx, recentImportPaths); + importModel.project = project; + importModel.selection = selection.getFirstElement(); + + return true; + } + + private void writePreferences() throws IOException { + IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID); + + store.putValue(Preferences.RECENT_SHARED_LIBRARY_IMPORT_LOCATIONS, Preferences.encodePaths(importModel.recentLocations)); + + if (store.needsSaving()) + store.save(); + } + + public ModelImportWizard() { + setWindowTitle("Import Model"); + setNeedsProgressMonitor(true); + } + + @Override + public void init(IWorkbench workbench, IStructuredSelection selection) { + readPreferences(selection); + } + + @Override + public void addPages() { + super.addPages(); + if (importModel != null) { + addPage(new ModelImportPage(importModel)); + } else { + addPage(new NoProjectPage("Import Model")); + } + } + + @Override + public boolean performFinish() { + try { + importModel.recentLocations.addFirst(importModel.importLocation.getAbsolutePath()); + Preferences.removeDuplicates(importModel.recentLocations); + if (importModel.recentLocations.size() > MAX_RECENT_IMPORT_PATHS) + importModel.recentLocations.pollLast(); + + writePreferences(); + } catch (IOException e) { + ErrorLogger.defaultLogError("Failed to write preferences", e); + } + + try { + MigratedImportResult[] result = { null }; + getContainer().run(true, true, new IRunnableWithProgress() { + @Override + public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { + try { + Resource target = ResourceAdaptionUtils.toSingleResource(importModel.selection); + importModel.sessionContext.getSession().markUndoPoint(); + result[0] = doImport(monitor, importModel.importLocation, importModel.sessionContext.getSession(), target); + } catch (Exception e) { + throw new InvocationTargetException(e); + } finally { + monitor.done(); + } + } + }); + + if (result[0].hasMissingExternals()) { + InfoDialog.open(getShell(), "Missing Externals Created", + "The system was unable to find some of the external entities referenced by the imported material. Place-holders have been created for the missing entities.\nThe missing entities are:\n" + + EString.implode(result[0].tgResult.missingExternals), + SWT.SHEET); + } + } catch (InvocationTargetException e) { + Throwable cause = e.getCause(); + WizardPage cp = (WizardPage) getContainer().getCurrentPage(); + if (cause instanceof MissingDependencyException) { + cp.setErrorMessage("Failed to import model due to missing dependencies.\n" + cause.getMessage()); + ErrorLogger.defaultLogError("Model " + importModel.importLocation + " import failed due to missing database dependencies. See exception for details.", cause); + ExceptionUtils.showError("Failed to import model due to missing dependencies.\n\n" + cause.getMessage(), null); + } else { + cp.setErrorMessage("Unexpected problem importing model.\nMessage: " + cause.getMessage()); + ErrorLogger.defaultLogError("Model " + importModel.importLocation + " import failed unexpectedly. See exception for details.", cause); + ExceptionUtils.showError("Unexpected problem importing model.\n\n" + cause.getMessage(), cause); + } + return false; + } catch (InterruptedException e) { + WizardPage cp = (WizardPage) getContainer().getCurrentPage(); + cp.setErrorMessage("Import interrupted.\nMessage: " + e.getMessage()); + ErrorLogger.defaultLogError("Model " + importModel.importLocation + " import interrupted.", e); + ExceptionUtils.showError("Model import was interrupted.", e); + return false; + } + + return true; + } + + public static MigratedImportResult doImport(IProgressMonitor monitor, File modelFile, Session session, Resource target) + throws Exception + { + SubMonitor mon = SubMonitor.convert(monitor); + mon.beginTask("Loading model from disk", 1000); + + FormatHandler handler1 = new FormatHandler() { + @Override + public Binding getBinding() { + return TransferableGraph1.BINDING; + } + + @Override + public MigratedImportResult process(DataContainer container) throws Exception { + mon.worked(100); + mon.setTaskName("Importing model into database"); + + MigrationState state = MigrationUtils.newState(); + state.setProperty(MigrationStateKeys.UPDATE_DEPENDENCIES, false); + state.setProperty(MigrationStateKeys.MODEL_FILE, modelFile); + state.setProperty(MigrationStateKeys.SESSION, session); + state.setProperty(MigrationStateKeys.PROGRESS_MONITOR, monitor); + + MigrationUtils.importMigrated(monitor, session, modelFile, state, new ModelImportAdvisor(Simantics.getProjectResource()), Simantics.getProjectResource()); + + Collection resultRoots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES); + ImportResult result = state.getProperty(MigrationStateKeys.IMPORT_RESULT); + return new MigratedImportResult(resultRoots, result); + + } + }; + + Map> handlers = new HashMap<>(); + handlers.put(":1", handler1); + handlers.put(Constants.MODEL_FORMAT_V1, handler1); + + MigratedImportResult result = DataContainers.readFile(modelFile, handlers); + + mon.setTaskName("Postprocessing"); + mon.subTask(""); + mon.newChild(50).done(); + + return result; + } + +} diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/WizardExtensionFactory.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/WizardExtensionFactory.java index b67dee50e..83d062e36 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/WizardExtensionFactory.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/WizardExtensionFactory.java @@ -36,6 +36,7 @@ public class WizardExtensionFactory implements IExecutableExtensionFactory, IExe * Factory ID for the shared library import wizard. */ public static final String ANNOTATION_TYPE_IMPORT_WIZARD = "sharedOntologyImportWizard"; // $//$NON-NLS-1$ + public static final String MODEL_IMPORT_WIZARD = "modelImportWizard"; // $//$NON-NLS-1$ private IConfigurationElement config; @@ -65,6 +66,9 @@ public class WizardExtensionFactory implements IExecutableExtensionFactory, IExe if (ANNOTATION_TYPE_IMPORT_WIZARD.equals(id)) { return configure(new SharedOntologyImportWizard()); } + if (MODEL_IMPORT_WIZARD.equals(id)) { + return configure(new ModelImportWizard()); + } throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, "Unknown id in data argument for " + getClass(), null)); //$NON-NLS-1$ -- 2.45.2