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