package org.simantics.db.layer0.util;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Set;
+
import org.simantics.databoard.Bindings;
+import org.simantics.databoard.adapter.AdaptException;
import org.simantics.databoard.binding.Binding;
+import org.simantics.databoard.binding.mutable.Variant;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.ObjectsWithType;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.adapter.CopyHandler;
+import org.simantics.db.layer0.migration.MigrationState;
+import org.simantics.db.layer0.migration.MigrationStateKeys;
+import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
+import org.simantics.graph.representation.TransferableGraph1;
+import org.simantics.layer0.Layer0;
public class ModelDependenciesBean {
public static final String EXTENSION_KEY = ModelDependenciesBean.class.getSimpleName();
-
+
public static final Binding BINDING = Bindings.getBindingUnchecked(ModelDependenciesBean.class);
-
+
public ModelDependency[] dependencies;
-
+
public ModelDependenciesBean() {
dependencies = new ModelDependency[0];
}
public ModelDependenciesBean(ModelDependency[] dependencies) {
this.dependencies = dependencies;
}
+
+ private static void collectDependencies(ReadGraph graph, Resource resource, LinkedList<ModelDependency> modelDependencies) throws DatabaseException {
+
+ Layer0 L0 = Layer0.getInstance(graph);
+ libs: for(Resource library : graph.syncRequest(new ObjectsWithType(resource, L0.IsLinkedTo, L0.SharedOntology))) {
+ String uri = graph.getPossibleURI(library);
+ if(uri == null) continue;
+ for(ModelDependency dep : modelDependencies)
+ if(dep.uri.equals(uri)) continue libs;
+ CopyHandler ch = graph.adapt(library, CopyHandler.class);
+ SimanticsClipboardImpl clipboard = new SimanticsClipboardImpl();
+ ch.copyToClipboard(graph, clipboard);
+ for (Set<Representation> object : clipboard.getContents()) {
+ TransferableGraph1 tg = ClipboardUtils.accept(graph, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
+ if(tg != null) {
+ modelDependencies.addFirst(new ModelDependency(uri, tg));
+ }
+ }
+ collectDependencies(graph, library, modelDependencies);
+ }
+
+ }
+
+ public static ModelDependenciesBean create(ReadGraph graph, Resource resource) throws DatabaseException {
+
+ LinkedList<ModelDependency> dependencies = new LinkedList<>();
+ collectDependencies(graph, resource, dependencies);
+ return new ModelDependenciesBean(dependencies.toArray(new ModelDependency[dependencies.size()]));
+
+ }
+
+ public static ModelDependenciesBean fromMigrationState(MigrationState state) throws DatabaseException {
+ Map<String,Variant> extensions = state.getProperty(MigrationStateKeys.TG_EXTENSIONS);
+ final Variant variant = extensions.get(ModelDependenciesBean.EXTENSION_KEY);
+ if (variant != null) {
+ try {
+ return (ModelDependenciesBean) variant.getValue(ModelDependenciesBean.BINDING);
+ } catch (AdaptException e) {
+ e.printStackTrace();
+ }
+ }
+ return null;
+ }
}
* <code>true</code> if the model should be overwritten without a warning.
*/
boolean overwrite;
+ boolean includeDependencies;
boolean tgAndPgraph;
ExportPlan(ISessionContext sessionContext, Deque<String> recentLocations) {
// Output
File importLocation;
+
+ boolean includeDependencies;
public ImportPlan(ISessionContext sessionContext, Deque<String> recentLocations) {
this.sessionContext = sessionContext;
List<LibraryInfo> models = Collections.emptyList();
private Button overwrite;
+ private Button dependencies;
protected ModelExportPage(ExportPlan model) {
super("Export Model", "Define Export Location", null);
}
});
+ dependencies = new Button(container, SWT.CHECK);
+ dependencies.setText("&Export dependencies");
+ dependencies.setSelection(exportModel.includeDependencies);
+ GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(dependencies);
+ dependencies.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ validatePage();
+ }
+ });
+
try {
initializeData();
} catch (DatabaseException e) {
}
exportModel.exportLocation = file;
exportModel.overwrite = overwrite.getSelection();
+ exportModel.includeDependencies = dependencies.getSelection();
setErrorMessage(null);
setMessage("Export selected model to " + exportModel.exportLocation + ".");
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.simantics.Simantics;
import org.simantics.databoard.binding.error.BindingException;
+import org.simantics.databoard.binding.mutable.Variant;
import org.simantics.databoard.serialization.SerializationException;
import org.simantics.db.ReadGraph;
import org.simantics.db.common.request.ReadRequest;
import org.simantics.db.common.utils.Logger;
import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.util.ModelDependenciesBean;
+import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
import org.simantics.modeling.ModelingUtils;
import org.simantics.modeling.ModelingUtils.LibraryInfo;
import org.simantics.utils.ui.dialogs.ShowMessage;
void exportModel(SubMonitor mon) throws IOException, DatabaseException, SerializationException, BindingException{
try {
- doExport(mon, exportModel.exportLocation, exportModel.model);
-
+ doExport(mon, exportModel.exportLocation, exportModel.model, exportModel.includeDependencies);
} catch (DatabaseException e) {
e.printStackTrace();
Logger.defaultLogError(e);
}
}
- public static void doExport(IProgressMonitor monitor, final File location, final LibraryInfo info) throws DatabaseException, IOException {
+ public static void doExport(IProgressMonitor monitor, final File location, final LibraryInfo info, boolean includeDependencies) throws DatabaseException, IOException {
Simantics.sync(new ReadRequest() {
@Override
public void run(ReadGraph graph) throws DatabaseException {
- ModelingUtils.exportModel(graph, info.library.getResource(), location.getAbsolutePath(), "", 1);
+ TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, info.library.getResource(), true, false);
+ if(includeDependencies) {
+ ModelDependenciesBean bean = ModelDependenciesBean.create(graph, info.library.getResource());
+ conf.baseExtensions.put(ModelDependenciesBean.EXTENSION_KEY, new Variant(ModelDependenciesBean.BINDING, bean));
+ }
+ ModelingUtils.exportModel(graph, conf, location.getAbsolutePath(), "", 1);
}
});
CCombo importLocation;
List<NamedResource> models = Collections.emptyList();
+
+ private Button dependencies;
+
Label author;
Label status;
});
}
+ dependencies = new Button(container, SWT.CHECK);
+ dependencies.setText("&Import dependencies");
+ dependencies.setSelection(importModel.includeDependencies);
+ GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(dependencies);
+ dependencies.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ validatePage();
+ }
+ });
+
author = new Label(container, SWT.NONE);
author.setText("");
GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(author);
return;
}
importModel.importLocation = file;
+ importModel.includeDependencies = dependencies.getSelection();
try {
import org.simantics.databoard.util.URIStringUtils;
import org.simantics.db.Resource;
import org.simantics.db.Session;
+import org.simantics.db.common.primitiverequest.PossibleResource;
import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;
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.util.ModelDependenciesBean;
+import org.simantics.db.layer0.util.ModelDependency;
import org.simantics.db.management.ISessionContext;
import org.simantics.graph.db.ImportResult;
import org.simantics.graph.db.MissingDependencyException;
try {
Resource target = Simantics.getProjectResource();
importModel.sessionContext.getSession().markUndoPoint();
- result[0] = doImport(monitor, importModel.importLocation, importModel.sessionContext.getSession(), target);
+ result[0] = doImport(monitor, importModel.importLocation, importModel.sessionContext.getSession(), target, importModel.includeDependencies);
} catch (Exception e) {
throw new InvocationTargetException(e);
} finally {
return true;
}
- public static MigratedImportResult doImport(IProgressMonitor monitor, File modelFile, Session session, Resource target)
+ public static MigratedImportResult doImport(IProgressMonitor monitor, File modelFile, Session session, Resource target, boolean includeDependencies)
throws Exception
{
SubMonitor mon = SubMonitor.convert(monitor);
state.setProperty(MigrationStateKeys.SESSION, session);
state.setProperty(MigrationStateKeys.PROGRESS_MONITOR, monitor);
+ if(includeDependencies) {
+ final ModelDependenciesBean libraryDependenciesBean = ModelDependenciesBean.fromMigrationState(state);
+ if(libraryDependenciesBean != null) {
+ for(ModelDependency dependency : libraryDependenciesBean.dependencies) {
+ Resource existing = session.sync(new PossibleResource(dependency.uri));
+ if(existing == null) {
+ MigrationUtils.importSharedOntology(session, dependency.tg, false);
+ }
+ }
+ }
+ }
+
MigrationUtils.importMigrated(monitor, session, modelFile, state, new DefaultPasteImportAdvisor(target), target);
Collection<Resource> resultRoots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);