Sync git svn branch with SVN repository r33249.
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / SCL.java
1 package org.simantics.modeling;\r
2 \r
3 import java.io.File;\r
4 import java.text.SimpleDateFormat;\r
5 import java.util.Date;\r
6 import java.util.List;\r
7 \r
8 import org.eclipse.core.runtime.NullProgressMonitor;\r
9 import org.eclipse.core.runtime.Platform;\r
10 import org.eclipse.core.runtime.jobs.IJobManager;\r
11 import org.eclipse.core.runtime.jobs.Job;\r
12 import org.simantics.DatabaseJob;\r
13 import org.simantics.Logger;\r
14 import org.simantics.Simantics;\r
15 import org.simantics.SimanticsPlatform;\r
16 import org.simantics.SimanticsPlatform.OntologyRecoveryPolicy;\r
17 import org.simantics.db.ReadGraph;\r
18 import org.simantics.db.Resource;\r
19 import org.simantics.db.WriteGraph;\r
20 import org.simantics.db.common.request.WriteRequest;\r
21 import org.simantics.db.exception.DatabaseException;\r
22 import org.simantics.db.layer0.request.external.EclipsePreferencePrimitiveRead;\r
23 import org.simantics.db.layer0.util.RemoverUtil;\r
24 import org.simantics.db.layer0.util.SimanticsClipboard;\r
25 import org.simantics.db.service.DebugSupport;\r
26 import org.simantics.db.service.ServiceActivityMonitor;\r
27 \r
28 public class SCL {\r
29     public static void killPlatformWrite(WriteGraph graph) throws DatabaseException {\r
30         // Currently not supported.\r
31         // Would be relatively easy to support the desired functionality.\r
32         // To implement this I would recommend something like:\r
33         // SimanticsPlatform.INSTANCE.breakWrite();\r
34         // And it's implementation with db.management.breakWrite() method.\r
35         // To be clear, at the moment this method (breakWrite) does not exist.\r
36     }\r
37     public static void killPlatformRead(ReadGraph graph) throws DatabaseException {\r
38         // See above.\r
39     }\r
40     public static void killPlatform() throws Exception {\r
41         SimanticsPlatform.INSTANCE.shutdown(null);\r
42     }\r
43     public static void shutdownPlatform() throws Exception {\r
44         SimanticsPlatform.INSTANCE.shutdown(null);\r
45     }\r
46     public static void reconnectPlatform() throws Exception {\r
47         SimanticsPlatform.INSTANCE.reconnect(Simantics.getDefaultDatabaseDriver());\r
48     }\r
49     public static void synchronizeOntologies() throws Exception {\r
50         SimanticsPlatform.INSTANCE.synchronizeOntologies(new NullProgressMonitor(), OntologyRecoveryPolicy.Merge, true);\r
51         Simantics.getSession().getService(DebugSupport.class).query(Simantics.getSession(), "exec QueryControl.flush");\r
52     }\r
53 \r
54     public static void sync() throws DatabaseException {\r
55         // Multiple bugs here:\r
56         // -Model performs activation in separate write transactions because API does not support changing the virtual graph\r
57         //  => activation & activation listener is delayed beyond this point\r
58         // -This should be fixed by the following code\r
59         //     TransactionSupport ts = session.getService(TransactionSupport.class);\r
60         //     ts.waitCompletion();\r
61         //  but unfortunately this does not work either...\r
62         // so we synchronize by a familiar write transaction\r
63         // And then wait still some more\r
64 //              for(int i=0;i<3;i++) {\r
65        Simantics.getSession().syncRequest(new WriteRequest() {\r
66             @Override\r
67             public void perform(WriteGraph graph) throws DatabaseException {\r
68             }\r
69 \r
70                 @Override\r
71                 public String toString() {\r
72                         return "Utils sync";\r
73                 }\r
74        });\r
75 //\r
76 //             // And then wait still some more\r
77 //             Simantics.getSession().syncRequest(new ReadRequest() {\r
78 //                 @Override\r
79 //                 public void run(ReadGraph graph) throws DatabaseException {\r
80 //                 }\r
81 //            });\r
82 //              }\r
83     }\r
84 \r
85     public static void syncGraph() throws Exception {\r
86         try {\r
87             Simantics.getSession().getService(ServiceActivityMonitor.class).waitForCompletion();\r
88         } catch (InterruptedException e) {\r
89             throw new DatabaseException(e);\r
90         }\r
91 \r
92                 // OK, now the experiment activate job should be scheduled\r
93                 // Wait for the job to finish\r
94         IJobManager job = Job.getJobManager();\r
95         Job[] jobs = job.find(null);\r
96         for (Job j : jobs) {\r
97             if(j instanceof DatabaseJob) j.join();\r
98         }\r
99         sync();\r
100         }\r
101 \r
102     public static boolean deleteMBNode(List<Resource> resources) throws DatabaseException {\r
103         boolean value = false;\r
104                 try {\r
105                 value = RemoverUtil.tryCollectionRemover(resources);\r
106         } catch (DatabaseException e){\r
107                 return value;\r
108         }\r
109         return value;\r
110     }\r
111 \r
112     public static void sleep(int ms) {\r
113         try {\r
114                         Thread.sleep(ms);\r
115                 } catch (InterruptedException e) {\r
116                         Logger.defaultLogError(e);\r
117                 }\r
118     }\r
119 \r
120     public static boolean hasSomethingToPaste(ReadGraph graph, Resource resource) throws DatabaseException {\r
121 \r
122         SimanticsClipboard clipboard = Simantics.getClipboard();\r
123         return !clipboard.getContents().isEmpty();\r
124 \r
125     }\r
126     \r
127     public static boolean canDelete(ReadGraph graph, Resource resource) throws DatabaseException {\r
128 \r
129         return RemoverUtil.canRemove(graph, resource);\r
130 \r
131     }\r
132 \r
133     public static boolean canRename(ReadGraph graph, Resource resource) throws DatabaseException {\r
134         return true;\r
135     }\r
136 \r
137     public static String currentDate(String format) {\r
138 \r
139         SimpleDateFormat sdf = new SimpleDateFormat(format);\r
140         return sdf.format(new Date(System.currentTimeMillis()));\r
141 \r
142     }\r
143 \r
144     public static File workspaceDirectory() {\r
145         return new File(Platform.getInstanceLocation().getURL().getFile());\r
146     }\r
147     \r
148     public static String queryPreference(ReadGraph graph, String pluginId, String preferenceKey) throws DatabaseException {\r
149         String result = graph.syncRequest(new EclipsePreferencePrimitiveRead(pluginId, preferenceKey));\r
150         if(result == null) return "";\r
151         return result;\r
152     }\r
153 \r
154     \r
155 }\r