]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterBuilderImpl.java
Merge "Better emptying of trash bin"
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / ClusterBuilderImpl.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.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;
11
12 import fi.vtt.simantics.procore.internal.SessionImplSocket.WriteOnlySupport;
13
14 public class ClusterBuilderImpl implements ClusterBuilder {
15     
16     final private ClusterTable clusterTable;
17     final private ClusterStream stream;
18     final private WriteOnlySupport support;
19     
20     ClusterBuilderImpl(SessionImplSocket session, WriteOnlySupport support) {
21         this.support = support;
22         this.stream = support.stream;
23         this.clusterTable = session.clusterTable;
24     }
25
26     @Override
27     public void newCluster() throws DatabaseException {
28         support.flushCluster();
29     }
30     
31     @Override
32     public ResourceHandle newResource() throws DatabaseException {
33         return new ResourceHandleImpl(support);
34     }
35     
36     @Override
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);
42     }
43
44     @Override
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);
55     }
56
57 }