1 package fi.vtt.simantics.procore.internal;
3 import org.simantics.db.Resource;
4 import org.simantics.db.exception.DatabaseException;
5 import org.simantics.db.impl.ClusterI;
6 import org.simantics.db.impl.ClusterTraitsBase;
7 import org.simantics.db.impl.ResourceImpl;
8 import org.simantics.db.procore.cluster.ClusterImpl;
9 import org.simantics.db.service.ClusterBuilder;
10 import org.simantics.db.service.ClusterUID;
12 import fi.vtt.simantics.procore.internal.SessionImplSocket.WriteOnlySupport;
14 public class ClusterBuilderImpl implements ClusterBuilder {
16 final private ClusterTable clusterTable;
17 final private ClusterStream stream;
18 final private WriteOnlySupport support;
20 ClusterBuilderImpl(SessionImplSocket session, WriteOnlySupport support) {
21 this.support = support;
22 this.stream = support.stream;
23 this.clusterTable = session.clusterTable;
27 public void newCluster() throws DatabaseException {
28 support.flushCluster();
32 public ResourceHandle newResource() throws DatabaseException {
33 return new ResourceHandleImpl(support);
37 public ResourceHandle resource(Resource res) throws DatabaseException {
38 ResourceImpl r = (ResourceImpl)res;
39 int clusterKey = ClusterTraitsBase.getClusterKeyFromResourceKey(r.id);
40 ClusterI cluster = clusterTable.getClusterByClusterKey(clusterKey);
41 return new ResourceHandleImpl(stream, (ClusterImpl)cluster, r.id);
45 public StatementHandle newStatement(ResourceHandle predicate, ResourceHandle object) throws DatabaseException {
46 ResourceHandleImpl p = (ResourceHandleImpl)predicate;
47 ResourceHandleImpl o = (ResourceHandleImpl)object;
48 int pResourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKey(p.resourceKey);
49 ClusterI pCluster = clusterTable.getClusterByClusterKey(pResourceIndex);
50 ClusterUID pClusterUID = pCluster.getClusterUID();
51 int oResourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKey(p.resourceKey);
52 ClusterI oCluster = clusterTable.getClusterByClusterKey(oResourceIndex);
53 ClusterUID oClusterUID = oCluster.getClusterUID();
54 return new StatementHandleImpl(p.resourceKey, (byte)0, o.resourceKey, (byte)0, pClusterUID, oClusterUID);