]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.common/src/org/simantics/db/common/utils/CommonDBUtils.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / utils / CommonDBUtils.java
index a07d42519c45741c1cd5c8c038502ca06c4fab4e..919f06b6847a51d876469bec47a5043ab65a6f77 100644 (file)
@@ -4,7 +4,6 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
@@ -16,8 +15,12 @@ import org.simantics.db.WriteGraph;
 import org.simantics.db.common.procedure.adapter.DirectStatementProcedure;
 import org.simantics.db.common.request.IsParent;
 import org.simantics.db.common.request.ObjectsWithType;
+import org.simantics.db.common.request.PossibleChild;
 import org.simantics.db.common.request.PossibleObjectWithType;
 import org.simantics.db.common.request.PossibleOwner;
+import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.common.request.RuntimeEnvironmentRequest;
+import org.simantics.db.exception.AdaptionException;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.InvalidResourceReferenceException;
 import org.simantics.db.service.ClusterUID;
@@ -26,6 +29,10 @@ import org.simantics.db.service.DirectQuerySupport;
 import org.simantics.db.service.SerialisationSupport;
 import org.simantics.db.service.XSupport;
 import org.simantics.layer0.Layer0;
+import org.simantics.scl.compiler.environment.Environments;
+import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
+import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
+import org.simantics.scl.compiler.types.Type;
 import org.simantics.utils.datastructures.collections.CollectionUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -313,5 +320,63 @@ public class CommonDBUtils {
        return xs.isClusterLoaded(clusterUID);
     }
     
-    
+    public static void setImmutable(ReadGraph graph, Resource resource, boolean value) throws DatabaseException {
+        XSupport xs = graph.getService(XSupport.class);
+        xs.setImmutable(resource, value);
+    }
+
+    public static Type getSCLType(ReadGraph graph, RuntimeEnvironment runtimeEnvironment, String typeText) throws DatabaseException {
+        try {
+            return Environments.getType(runtimeEnvironment.getEnvironment(), typeText);
+        } catch (SCLExpressionCompilationException e) {
+            throw new DatabaseException(e);
+        }
+    }
+
+    public static Type getSCLType(ReadGraph graph, Resource resource, String typeText) throws DatabaseException {
+        try {
+            RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(resource));
+            return Environments.getType(runtimeEnvironment.getEnvironment(), typeText);
+        } catch (SCLExpressionCompilationException e) {
+            throw new DatabaseException(e);
+        }
+    }
+
+    public static Resource getPossibleChild(ReadGraph graph, Resource resource, String name) throws DatabaseException {
+        return graph.sync(new PossibleChild(resource, name));
+    }
+
+    public static Resource getPossibleChild(ReadGraph graph, Resource resource, Resource type, String name) throws DatabaseException {
+        Resource child = graph.sync(new PossibleChild(resource, name));
+        if(child == null) return null;
+        if(!graph.isInstanceOf(child, type)) return null;
+        return child;
+    }
+
+    public static String getEnumerationValueName(ReadGraph graph, Resource resource) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        String label = graph.getPossibleRelatedValue(resource, L0.HasLabel, Bindings.STRING);
+        if(label != null)
+            return label;
+        return safeName(graph, resource);
+    }
+
+    private static String safeName(ReadGraph graph, Resource value) throws DatabaseException {
+        return graph.syncRequest(new StringAdapterRequest(value));
+    }
+
+    public static class StringAdapterRequest extends ResourceRead<String> {
+        public StringAdapterRequest(Resource resource) {
+            super(resource);
+        }
+        @Override
+        public String perform(ReadGraph graph) throws DatabaseException {
+            try {
+                return graph.adapt(resource, String.class);
+            } catch (AdaptionException e) {
+                return NameUtils.getSafeName(graph, resource);
+            }
+        }
+    }
+
 }