]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/SCL.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / SCL.java
diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/SCL.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/SCL.java
new file mode 100644 (file)
index 0000000..691d829
--- /dev/null
@@ -0,0 +1,151 @@
+package org.simantics.modeling;\r
+\r
+import java.io.File;\r
+import java.text.SimpleDateFormat;\r
+import java.util.Date;\r
+import java.util.List;\r
+\r
+import org.eclipse.core.runtime.NullProgressMonitor;\r
+import org.eclipse.core.runtime.Platform;\r
+import org.eclipse.core.runtime.jobs.IJobManager;\r
+import org.eclipse.core.runtime.jobs.Job;\r
+import org.simantics.DatabaseJob;\r
+import org.simantics.Logger;\r
+import org.simantics.Simantics;\r
+import org.simantics.SimanticsPlatform;\r
+import org.simantics.SimanticsPlatform.OntologyRecoveryPolicy;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.request.external.EclipsePreferencePrimitiveRead;\r
+import org.simantics.db.layer0.util.RemoverUtil;\r
+import org.simantics.db.layer0.util.SimanticsClipboard;\r
+import org.simantics.db.service.DebugSupport;\r
+\r
+public class SCL {\r
+    public static void killPlatformWrite(WriteGraph graph) throws DatabaseException {\r
+        // Currently not supported.\r
+        // Would be relatively easy to support the desired functionality.\r
+        // To implement this I would recommend something like:\r
+        // SimanticsPlatform.INSTANCE.breakWrite();\r
+        // And it's implementation with db.management.breakWrite() method.\r
+        // To be clear, at the moment this method (breakWrite) does not exist.\r
+    }\r
+    public static void killPlatformRead(ReadGraph graph) throws DatabaseException {\r
+        // See above.\r
+    }\r
+    public static void killPlatform() throws Exception {\r
+        SimanticsPlatform.INSTANCE.shutdown(null);\r
+    }\r
+    public static void shutdownPlatform() throws Exception {\r
+        SimanticsPlatform.INSTANCE.shutdown(null);\r
+    }\r
+    public static void reconnectPlatform() throws Exception {\r
+        SimanticsPlatform.INSTANCE.reconnect(null);\r
+    }\r
+    public static void synchronizeOntologies() throws Exception {\r
+       SimanticsPlatform.INSTANCE.synchronizeOntologies(new NullProgressMonitor(), OntologyRecoveryPolicy.Merge, true);\r
+       Simantics.getSession().getService(DebugSupport.class).query(Simantics.getSession(), "exec QueryControl.flush");\r
+    }\r
+\r
+    public static void sync() throws DatabaseException {\r
+       // Multiple bugs here:\r
+       // -Model performs activation in separate write transactions because API does not support changing the virtual graph\r
+       //  => activation & activation listener is delayed beyond this point\r
+       // -This should be fixed by the following code\r
+        //     TransactionSupport ts = session.getService(TransactionSupport.class);\r
+       //     ts.waitCompletion();\r
+       //  but unfortunately this does not work either...\r
+       // so we synchronize by a familiar write transaction\r
+        // And then wait still some more\r
+               for(int i=0;i<3;i++) {\r
+              Simantics.getSession().syncRequest(new WriteRequest() {\r
+                   @Override\r
+                   public void perform(WriteGraph graph) throws DatabaseException {\r
+                   }\r
+\r
+                       @Override\r
+                       public String toString() {\r
+                               return "Utils sync";\r
+                       }\r
+              });\r
+\r
+              // And then wait still some more\r
+              Simantics.getSession().syncRequest(new ReadRequest() {\r
+                  @Override\r
+                  public void run(ReadGraph graph) throws DatabaseException {\r
+                  }\r
+             });\r
+               }\r
+    }\r
+\r
+    public static void syncGraph() throws Exception {\r
+        sync();\r
+\r
+               // OK, now the experiment activate job should be scheduled\r
+               // Wait for the job to finish\r
+        IJobManager job = Job.getJobManager();\r
+        Job[] jobs = job.find(null);\r
+        for (Job j : jobs) {\r
+            if(j instanceof DatabaseJob) j.join();\r
+        }\r
+        sync();\r
+       }\r
+\r
+    public static boolean deleteMBNode(List<Resource> resources) throws DatabaseException {\r
+       boolean value = false;\r
+               try {\r
+               value = RemoverUtil.tryCollectionRemover(resources);\r
+       } catch (DatabaseException e){\r
+               return value;\r
+       }\r
+       return value;\r
+    }\r
+\r
+    public static void sleep(int ms) {\r
+       try {\r
+                       Thread.sleep(ms);\r
+               } catch (InterruptedException e) {\r
+                       Logger.defaultLogError(e);\r
+               }\r
+    }\r
+\r
+    public static boolean hasSomethingToPaste(ReadGraph graph, Resource resource) throws DatabaseException {\r
+\r
+       SimanticsClipboard clipboard = Simantics.getClipboard();\r
+       return !clipboard.getContents().isEmpty();\r
+\r
+    }\r
+    \r
+    public static boolean canDelete(ReadGraph graph, Resource resource) throws DatabaseException {\r
+\r
+       return RemoverUtil.canRemove(graph, resource);\r
+\r
+    }\r
+\r
+    public static boolean canRename(ReadGraph graph, Resource resource) throws DatabaseException {\r
+       return true;\r
+    }\r
+\r
+    public static String currentDate(String format) {\r
+\r
+               SimpleDateFormat sdf = new SimpleDateFormat(format);\r
+               return sdf.format(new Date(System.currentTimeMillis()));\r
+\r
+    }\r
+\r
+    public static File workspaceDirectory() {\r
+       return new File(Platform.getInstanceLocation().getURL().getFile());\r
+    }\r
+    \r
+    public static String queryPreference(ReadGraph graph, String pluginId, String preferenceKey) throws DatabaseException {\r
+       String result = graph.syncRequest(new EclipsePreferencePrimitiveRead(pluginId, preferenceKey));\r
+       if(result == null) return "";\r
+        return result;\r
+    }\r
+\r
+    \r
+}\r