]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusteringSupportImpl.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / ClusteringSupportImpl.java
diff --git a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusteringSupportImpl.java b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusteringSupportImpl.java
new file mode 100644 (file)
index 0000000..463e8c6
--- /dev/null
@@ -0,0 +1,90 @@
+package fi.vtt.simantics.procore.internal;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.ResourceNotFoundException;\r
+import org.simantics.db.exception.RuntimeDatabaseException;\r
+import org.simantics.db.impl.ClusterI;\r
+import org.simantics.db.impl.ResourceImpl;\r
+import org.simantics.db.impl.TransientGraph;\r
+import org.simantics.db.procore.cluster.ClusterImpl;\r
+import org.simantics.db.service.ClusteringSupport;\r
+\r
+public class ClusteringSupportImpl implements ClusteringSupport {\r
+\r
+    final private SessionImplSocket session;\r
+\r
+    ClusteringSupportImpl(SessionImplSocket session) {\r
+        this.session = session;\r
+    }\r
+\r
+    @Override\r
+    public long createCluster() {\r
+        long id;\r
+        try {\r
+            id = session.graphSession.newClusterId();\r
+        } catch (DatabaseException e) {\r
+            throw new RuntimeDatabaseException("Failed to get new cluster id.", e);\r
+        }\r
+        session.clusterTable.makeCluster(id, session.writeOnly);\r
+        return id;\r
+    }\r
+\r
+    @Override\r
+    public long getCluster(Resource r) {\r
+        int id = session.querySupport.getId(r);\r
+        if(id < 0) // Virtual resource\r
+            return TransientGraph.getVirtualClusterKey(id);\r
+        return session.clusterTable.getClusterIdByResourceKeyNoThrow(id);\r
+    }\r
+\r
+    @Override\r
+    public int getNumberOfResources(long clusterId) throws DatabaseException {\r
+        return session.clusterTable.getClusterByClusterId(clusterId).getNumberOfResources(session.clusterTranslator);\r
+    }\r
+\r
+    @Override\r
+    public Resource getResourceByKey(int resourceKey)\r
+            throws ResourceNotFoundException {\r
+        return session.getResourceByKey(resourceKey);\r
+    }\r
+\r
+    @Override\r
+    public Resource getResourceByIndexAndCluster(int resourceIndex, long clusterId)\r
+            throws DatabaseException, ResourceNotFoundException {\r
+        if (resourceIndex < 1)\r
+            throw new ResourceNotFoundException("Illegal resource index=" + resourceIndex + " cluster=" + clusterId);\r
+        ClusterI cluster = session.getClusterTable().getLoadOrThrow(clusterId);\r
+        int n = cluster.getNumberOfResources(null);\r
+        if (resourceIndex > n)\r
+            throw new ResourceNotFoundException("Illegal resource index=" + resourceIndex + " cluster=" + clusterId\r
+                    + " max index=" + n);\r
+        return session.getResource(resourceIndex, clusterId);\r
+    }\r
+//    @Override\r
+//    public Id getModifiedId(long clusterId) throws DatabaseException {\r
+//        return session.clusterTable.getClusterByClusterId(clusterId).getModifiedId();\r
+//    }\r
+\r
+    @Override\r
+    public Resource getClusterSetOfCluster(Resource r) throws DatabaseException {\r
+        if(!r.isPersistent()) return null;\r
+        ClusterImpl cluster = session.clusterTable.getClusterByResourceKey(((ResourceImpl)r).id);\r
+        Long rid = session.clusterSetsSupport.getSet(cluster.getClusterId());\r
+        if(rid == null || rid == 0) return null;\r
+        return session.resourceSerializer.getResource(rid);\r
+    }\r
+\r
+    @Override\r
+    public Resource getClusterSetOfCluster(long cluster) throws DatabaseException {\r
+        Long rid = session.clusterSetsSupport.getSet(cluster);\r
+        if(rid == null || rid == 0) return null;\r
+        return session.resourceSerializer.getResource(rid);\r
+    }\r
+\r
+    @Override\r
+    public boolean isClusterSet(Resource r) throws DatabaseException {\r
+        return session.containsClusterSet(r);\r
+    }\r
+\r
+}\r