]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusteringSupportImpl.java
isImmutable can NPE
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / ClusteringSupportImpl.java
1 package fi.vtt.simantics.procore.internal;
2
3 import org.simantics.db.Resource;
4 import org.simantics.db.exception.DatabaseException;
5 import org.simantics.db.exception.ResourceNotFoundException;
6 import org.simantics.db.exception.RuntimeDatabaseException;
7 import org.simantics.db.impl.ClusterI;
8 import org.simantics.db.impl.ResourceImpl;
9 import org.simantics.db.impl.TransientGraph;
10 import org.simantics.db.procore.cluster.ClusterImpl;
11 import org.simantics.db.service.ClusteringSupport;
12
13 public class ClusteringSupportImpl implements ClusteringSupport {
14
15     final private SessionImplSocket session;
16
17     ClusteringSupportImpl(SessionImplSocket session) {
18         this.session = session;
19     }
20
21     @Override
22     public long createCluster() {
23         long id;
24         try {
25             id = session.graphSession.newClusterId();
26         } catch (DatabaseException e) {
27             throw new RuntimeDatabaseException("Failed to get new cluster id.", e);
28         }
29         session.clusterTable.makeCluster(id, session.writeOnly);
30         return id;
31     }
32
33     @Override
34     public long getCluster(Resource r) {
35         int id = session.querySupport.getId(r);
36         if(id < 0) // Virtual resource
37             return TransientGraph.getVirtualClusterKey(id);
38         return session.clusterTable.getClusterIdByResourceKeyNoThrow(id);
39     }
40
41     @Override
42     public int getNumberOfResources(long clusterId) throws DatabaseException {
43         return session.clusterTable.getClusterByClusterId(clusterId).getNumberOfResources(session.clusterTranslator);
44     }
45
46     @Override
47     public Resource getResourceByKey(int resourceKey)
48             throws ResourceNotFoundException {
49         return session.getResourceByKey(resourceKey);
50     }
51
52     @Override
53     public Resource getResourceByIndexAndCluster(int resourceIndex, long clusterId)
54             throws DatabaseException, ResourceNotFoundException {
55         if (resourceIndex < 1)
56             throw new ResourceNotFoundException("Illegal resource index=" + resourceIndex + " cluster=" + clusterId);
57         ClusterI cluster = session.getClusterTable().getLoadOrThrow(clusterId);
58         int n = cluster.getNumberOfResources(null);
59         if (resourceIndex > n)
60             throw new ResourceNotFoundException("Illegal resource index=" + resourceIndex + " cluster=" + clusterId
61                     + " max index=" + n);
62         return session.getResource(resourceIndex, clusterId);
63     }
64 //    @Override
65 //    public Id getModifiedId(long clusterId) throws DatabaseException {
66 //        return session.clusterTable.getClusterByClusterId(clusterId).getModifiedId();
67 //    }
68
69     @Override
70     public Resource getClusterSetOfCluster(Resource r) throws DatabaseException {
71         if(!r.isPersistent()) return null;
72         ClusterImpl cluster = session.clusterTable.getClusterByResourceKey(((ResourceImpl)r).id);
73         Long rid = session.clusterSetsSupport.getSet(cluster.getClusterId());
74         if(rid == null || rid == 0) return null;
75         return session.resourceSerializer.getResource(rid);
76     }
77
78     @Override
79     public Resource getClusterSetOfCluster(long cluster) throws DatabaseException {
80         Long rid = session.clusterSetsSupport.getSet(cluster);
81         if(rid == null || rid == 0) return null;
82         return session.resourceSerializer.getResource(rid);
83     }
84
85     @Override
86     public boolean isClusterSet(Resource r) throws DatabaseException {
87         return session.containsClusterSet(r);
88     }
89
90 }