X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.layer0%2Fsrc%2Forg%2Fsimantics%2Fdb%2Flayer0%2Fmigration%2FMigrationUtils.java;h=e1421df90a89054f23d0a4625665f90ccf8be527;hb=ccb253462b54a0e036f0906ec8ac11a9021a968c;hp=5f20036a54e1ea75638fba8699a1f7044e8cb26c;hpb=c26409b1caf2f1e560d37c5befd11b442399c3fe;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/MigrationUtils.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/MigrationUtils.java index 5f20036a5..e1421df90 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/MigrationUtils.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/MigrationUtils.java @@ -50,9 +50,11 @@ import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler; import org.simantics.db.layer0.adapter.impl.SharedOntologyImportAdvisor; import org.simantics.db.layer0.adapter.impl.TrashBinRemover; import org.simantics.db.layer0.internal.SimanticsInternal; +import org.simantics.db.layer0.request.ActivateModel; import org.simantics.db.layer0.util.ExternalDownloadBean; import org.simantics.db.layer0.util.Layer0Utils; import org.simantics.db.layer0.util.TGTransferableGraphSource; +import org.simantics.db.request.Write; import org.simantics.db.service.XSupport; import org.simantics.graph.db.IImportAdvisor; import org.simantics.graph.db.ImportResult; @@ -63,6 +65,8 @@ import org.simantics.graph.representation.Root; import org.simantics.graph.representation.TransferableGraph1; import org.simantics.graph.representation.TransferableGraphUtils; import org.simantics.layer0.Layer0; +import org.simantics.operation.Layer0X; +import org.simantics.simulation.ontology.SimulationResource; import org.simantics.utils.datastructures.Pair; import org.simantics.utils.datastructures.collections.CollectionUtils; @@ -90,19 +94,26 @@ public class MigrationUtils { // return getTG(session, state, true, false); // } - public static void clearTempResource(Session session, final Resource resource) { - session.asyncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - graph.deny(resource, Layer0.getInstance(graph).PartOf); - } + public static void clearTempResource(Session session, Resource resource) { + session.asyncRequest((Write) graph -> { + graph.deny(resource, Layer0.getInstance(graph).PartOf); }); } + /** + * Activate the imported resource, if there are no other active models and the resource is a Model. + */ + private static void activateIfNoActiveModel(WriteGraph graph, Resource root, Resource parent) throws DatabaseException { + Layer0X L0X = Layer0X.getInstance(graph); + SimulationResource SIMU = SimulationResource.getInstance(graph); + if (!graph.hasStatement(parent, L0X.Activates) && graph.isInstanceOf(root, SIMU.Model)) { + new ActivateModel(parent, root).perform(graph); + } + } + public static Collection importTo(IProgressMonitor monitor, Session session, MigrationState state, final Resource parent, final IImportAdvisor advisor) throws DatabaseException, TransferableGraphException { final Resource resource = getResource(monitor, session, state); - final ArrayList result = new ArrayList(); + final ArrayList result = new ArrayList<>(); if(resource != null) { session.syncRequest(new WriteRequest() { @@ -118,11 +129,11 @@ public class MigrationUtils { if(version != null) { VersionMap map = graph.syncRequest(new VersionMapRequest(parent)); if(map.contains(baseName, version)) { - String newName = graph.syncRequest(new FreshEscapedName(parent, Layer0.getInstance(graph).ConsistsOf, baseName)); + String newName = graph.syncRequest(new FreshEscapedName(parent, L0.ConsistsOf, baseName)); graph.claimLiteral(root, L0.HasName, newName + "@1", Bindings.STRING); } } else { - String newName = graph.syncRequest(new FreshEscapedName(parent, Layer0.getInstance(graph).ConsistsOf, baseName)); + String newName = graph.syncRequest(new FreshEscapedName(parent, L0.ConsistsOf, baseName)); if(!newName.equals(baseName)) { graph.claimLiteral(root, L0.HasName, newName, Bindings.STRING); } @@ -131,6 +142,10 @@ public class MigrationUtils { graph.deny(root, L0.PartOf); graph.claim(root, L0.PartOf, parent); + if (Boolean.TRUE.equals( state.getProperty(MigrationKeys.ACTIVATE_ROOT_IF_NONE_ACTIVE) )) { + activateIfNoActiveModel(graph, root, parent); + } + CommentMetadata cm = graph.getMetadata(CommentMetadata.class); graph.addMetadata(cm.add("Imported " + graph.getURI(root) + ", resource " + root)); @@ -173,9 +188,9 @@ public class MigrationUtils { public Collection perform(ReadGraph graph) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); - ArrayList> steps = new ArrayList>(); + ArrayList> steps = new ArrayList<>(); Instances query = graph.adapt(L0.Migration, Instances.class); - Set migrations = new HashSet(); + Set migrations = new HashSet<>(); for(Resource ontology : Layer0Utils.listOntologies(graph)) { migrations.addAll(Layer0Utils.sortByCluster(graph, query.find(graph, ontology))); }