]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics/src/org/simantics/Simantics.java
DB query swapping to file system
[simantics/platform.git] / bundles / org.simantics / src / org / simantics / Simantics.java
index 9a1a354f17131b35e1fbfd7bfcc0565645b95616..35808bd929fa74d63f18f860bdc51680abeababf 100644 (file)
@@ -24,6 +24,7 @@ import org.simantics.SimanticsPlatform.RecoveryPolicy;
 import org.simantics.application.arguments.IArguments;
 import org.simantics.application.arguments.SimanticsArguments;
 import org.simantics.db.ReadGraph;
+import org.simantics.db.RequestProcessor;
 import org.simantics.db.Resource;
 import org.simantics.db.Session;
 import org.simantics.db.WriteGraph;
@@ -43,6 +44,7 @@ import org.simantics.db.management.SessionContextProvider;
 import org.simantics.db.management.SingleSessionContextProviderSource;
 import org.simantics.db.request.ReadInterface;
 import org.simantics.db.request.WriteInterface;
+import org.simantics.db.service.XSupport;
 import org.simantics.internal.FileServiceImpl;
 import org.simantics.layer0.Layer0;
 import org.simantics.project.IProject;
@@ -283,6 +285,14 @@ public class Simantics {
             throw new IllegalStateException("Session unavailable, no database session open");
         return ctx.getSession();
     }
+    
+    public static RequestProcessor getAvailableRequestProcessor() {
+        Object graph = SCLContext.getCurrent().get("graph");
+        if(graph instanceof ReadGraph)
+            return (RequestProcessor)graph;
+        else
+            return Simantics.getSession();
+    }
 
     /**
      * Returns the database Session bound to the currently active context.
@@ -458,22 +468,41 @@ public class Simantics {
         }
     }
 
+    public static void saveQueries(Session session) {
+        try {
+            XSupport xs = session.getService(XSupport.class);
+            xs.saveQueries();
+        } catch (Exception e) {
+            LOGGER.error("Saving database queries failed.", e);
+        }
+    }
 
     @SuppressWarnings({ "unchecked", "rawtypes" })
-       public static <T> T applySCL(String module, String function, ReadGraph graph, Object ... args) throws DatabaseException {
-        SCLContext sclContext = SCLContext.getCurrent();
-           Object oldGraph = sclContext.put("graph", graph);
-               try {
-                       T t = (T)((Function)SCLOsgi.MODULE_REPOSITORY.getValue(module, function)).applyArray(args);
-                       return t;
-               } catch (ValueNotFound e) {
-                       throw new DatabaseException("SCL Value not found: " + e.name);
-               } catch (Throwable t) {
-                       throw new DatabaseException(t);
-               } finally {
-                       sclContext.put("graph", oldGraph);
-               }
+    public static <T> T applySCL(String module, String function, Object ... args) throws DatabaseException {
+        try {
+            T t = (T)((Function)SCLOsgi.MODULE_REPOSITORY.getValue(module, function)).applyArray(args);
+            return t;
+        } catch (ValueNotFound e) {
+            throw new DatabaseException("SCL Value not found: " + e.name);
+        } catch (Throwable t) {
+            if (t instanceof DatabaseException)
+                throw (DatabaseException) t;
+            throw new DatabaseException(t);
+        }
+    }
 
+    public static <T> T applySCL(String module, String function, ReadGraph graph, Object ... args) throws DatabaseException {
+        SCLContext sclContext = SCLContext.getCurrent();
+        Object oldGraph = sclContext.put("graph", graph);
+        try {
+            return applySCL(module, function, args);
+        } catch (DatabaseException dbe) {
+            throw dbe;
+        } catch (Throwable t) {
+            throw new DatabaseException(t);
+        } finally {
+            sclContext.put("graph", oldGraph);
+        }
     }
 
     @SuppressWarnings("unchecked")
@@ -483,6 +512,8 @@ public class Simantics {
         try {
             return (T)f.applyArray(args);
         } catch (Throwable t) {
+            if (t instanceof DatabaseException)
+                throw (DatabaseException) t;
             throw new DatabaseException(t);
         } finally {
             sclContext.put("graph", oldGraph);
@@ -496,6 +527,8 @@ public class Simantics {
         try {
             return (T)f.applyArray(args);
         } catch (Throwable t) {
+            if (t instanceof DatabaseException)
+                throw (DatabaseException) t;
             throw new DatabaseException(t);
         } finally {
             sclContext.put("graph", oldGraph);
@@ -508,6 +541,8 @@ public class Simantics {
         try {
             return function.apply(p0);
         } catch (Throwable t) {
+            if (t instanceof DatabaseException)
+                throw (DatabaseException) t;
             throw new DatabaseException(t);
         } finally {
             sclContext.put("graph", oldGraph);
@@ -520,6 +555,8 @@ public class Simantics {
         try {
             return function.apply(p0);
         } catch (Throwable t) {
+            if (t instanceof DatabaseException)
+                throw (DatabaseException) t;
             throw new DatabaseException(t);
         } finally {
             sclContext.put("graph", oldGraph);
@@ -532,6 +569,8 @@ public class Simantics {
         try {
             return function.apply(p0, p1);
         } catch (Throwable t) {
+            if (t instanceof DatabaseException)
+                throw (DatabaseException) t;
             throw new DatabaseException(t);
         } finally {
             sclContext.put("graph", oldGraph);