]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/MigrationUtils.java
Disable model activation during model migration
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / migration / MigrationUtils.java
index 7af76baf330b23fb476da913f165d1f929f59a9a..58197619a7a25f8c002ac6460e166ff299b1919d 100644 (file)
@@ -55,6 +55,7 @@ import org.simantics.db.layer0.util.Layer0Utils;
 import org.simantics.db.layer0.util.TGTransferableGraphSource;
 import org.simantics.db.service.XSupport;
 import org.simantics.graph.db.IImportAdvisor;
+import org.simantics.graph.db.ImportResult;
 import org.simantics.graph.db.MissingDependencyException;
 import org.simantics.graph.db.TransferableGraphException;
 import org.simantics.graph.representation.Identity;
@@ -62,6 +63,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;
 
@@ -99,6 +102,16 @@ public class MigrationUtils {
         });
     }
 
+    /**
+     * 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))
+            graph.claim(parent, L0X.Activates, root);
+    }
+
     public static Collection<Resource> 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<Resource> result = new ArrayList<Resource>();
@@ -129,6 +142,10 @@ public class MigrationUtils {
 
                            graph.deny(root, L0.PartOf);
                            graph.claim(root, L0.PartOf, parent);
+                           
+                           // FIXME : Model activation must be done in "activations" virtual graph.
+                           // FIXME : This step should be optional, not all the products use active models.
+                           //activateIfNoActiveModel(graph, root, parent);
 
                            CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
                            graph.addMetadata(cm.add("Imported " + graph.getURI(root) + ", resource " + root));
@@ -164,7 +181,6 @@ public class MigrationUtils {
     }
     
     
-    @SuppressWarnings("deprecation")
        public static Collection<MigrationStep> getMigrationSteps(DataContainer header) throws DatabaseException {
        
        return SimanticsInternal.sync(new BinaryRead<String,Integer,Collection<MigrationStep>>(header.format, header.version) {
@@ -308,11 +324,11 @@ public class MigrationUtils {
         return t != null ? t : defaultValue;
     }
 
-    public static Resource importSharedOntology(Session session, TransferableGraph1 tg, boolean published) throws DatabaseException {
+    public static MigratedImportResult importSharedOntology(Session session, TransferableGraph1 tg, boolean published) throws DatabaseException {
        return importSharedOntology(null, session, tg, published);
     }
 
-    public static Resource importSharedOntology(IProgressMonitor monitor, Session session, TransferableGraph1 tg, boolean published) throws DatabaseException {
+    public static MigratedImportResult importSharedOntology(IProgressMonitor monitor, Session session, TransferableGraph1 tg, boolean published) throws DatabaseException {
        
        if(monitor == null) monitor = new NullProgressMonitor();
        
@@ -344,7 +360,6 @@ public class MigrationUtils {
         Collection<Identity> roots = TransferableGraphUtils.getRoots(tg);
         if(roots.size() == 1) {
             try {
-               
                 TGTransferableGraphSource tgSource = new TGTransferableGraphSource(tg);
                 SharedOntologyImportAdvisor advisor = new SharedOntologyImportAdvisor(published);
 
@@ -355,8 +370,11 @@ public class MigrationUtils {
                 state.setProperty(MigrationStateKeys.PROGRESS_MONITOR, monitor);
                 state.setProperty(MigrationStateKeys.CURRENT_DATA_CONTAINER, new DataContainer("sharedLibrary", 1, new Variant(TransferableGraph1.BINDING, tg)));
 
-                return MigrationUtils.importMigrated(monitor, session, null, state, advisor, null);
-                
+                MigrationUtils.importMigrated(monitor, session, null, state, advisor, null);
+
+                Collection<Resource> resultRoots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
+                ImportResult result = state.getProperty(MigrationStateKeys.IMPORT_RESULT);
+                return new MigratedImportResult(resultRoots, result);
                        } catch (TransferableGraphException e) {
                                throw new DatabaseException(e);
                        } catch (MissingDependencyException e) {