1 package fi.vtt.simantics.procore.internal;
3 import java.io.IOException;
5 import org.simantics.databoard.Bindings;
6 import org.simantics.databoard.binding.Binding;
7 import org.simantics.databoard.serialization.SerializationException;
8 import org.simantics.databoard.serialization.Serializer;
9 import org.simantics.db.Resource;
10 import org.simantics.db.WriteOnlyGraph;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.exception.RuntimeDatabaseException;
13 import org.simantics.db.impl.graph.WriteGraphImpl;
14 import org.simantics.db.procore.cluster.ClusterImpl;
15 import org.simantics.db.service.ClusterBuilder.ResourceHandle;
16 import org.simantics.db.service.ClusterBuilder.StatementHandle;
17 import org.simantics.db.service.SerialisationSupport;
19 import fi.vtt.simantics.procore.internal.SessionImplSocket.WriteOnlySupport;
21 final public class ResourceHandleImpl implements ResourceHandle {
23 final private ClusterImpl cluster;
24 final public int resourceKey;
26 public ResourceHandleImpl(WriteOnlySupport support) throws DatabaseException {
27 this.resourceKey = support.createResourceKey(1);
28 this.cluster = support.currentCluster;
29 if(cluster.cc == null) cluster.cc = new ClusterChange(support.stream, cluster);
32 public ResourceHandleImpl(ClusterStream stream, ClusterImpl cluster, int resourceKey) {
33 this.resourceKey = resourceKey;
34 this.cluster = cluster;
35 if(cluster.cc == null) cluster.cc = new ClusterChange(stream, cluster);
39 public void applyPredicate(Object cluster) {
41 ClusterImpl impl = (ClusterImpl)cluster;
42 impl.change.addStatementIndex1(resourceKey, this.cluster.clusterUID, (byte)0, impl.foreignLookup);
47 public void applyObject(Object cluster) {
49 ClusterImpl impl = (ClusterImpl)cluster;
50 impl.change.addStatementIndex2(resourceKey, this.cluster.clusterUID, (byte)0, impl.foreignLookup);
55 public void addStatement(StatementHandle handle) {
57 if(cluster.getImmutable()) return;
59 cluster.change.addStatementIndex0(resourceKey, ClusterChange.ADD_OPERATION);
60 handle.apply(cluster);
61 cluster.cc.addChange(cluster.change);
66 public void addStatement(ResourceHandle predicate, ResourceHandle object) {
68 if(cluster.getImmutable()) return;
70 Change change = cluster.change;
71 change.addStatementIndex0(resourceKey, ClusterChange.ADD_OPERATION);
72 predicate.applyPredicate(cluster);
73 object.applyObject(cluster);
74 cluster.cc.addChange(change);
79 public void addStatement(WriteOnlyGraph graph, ResourceHandle predicate, ResourceHandle object) throws DatabaseException {
80 if(cluster.isWriteOnly()) addStatement(predicate, object);
82 WriteGraphImpl impl = (WriteGraphImpl)graph;
83 impl.writeSupport.claim(graph.getProvider(), resourceKey, ((ResourceHandleImpl)predicate).resourceKey, ((ResourceHandleImpl)object).resourceKey);
88 public void addValue(WriteOnlyGraph graph, byte[] bytes) throws DatabaseException {
90 if(cluster.isWriteOnly()) cluster.cc.setValue((short)(resourceKey & 0xFFFF), bytes);
92 WriteGraphImpl impl = (WriteGraphImpl)graph;
93 impl.writeSupport.claimValue(null, resourceKey, bytes, bytes.length);
99 public void addValue(Object value, Binding binding) {
102 Serializer ser = Bindings.getSerializerUnchecked(binding);
103 byte[] bytes = ser.serialize(value);
104 cluster.cc.setValue((short)(resourceKey & 0xFFFF), bytes);
105 } catch (SerializationException e) {
107 } catch (IOException e) {
114 public void addValue(Object value, Serializer serializer) {
117 byte[] bytes = serializer.serialize(value);
118 cluster.cc.setValue((short)(resourceKey & 0xFFFF), bytes);
119 } catch (SerializationException e) {
121 } catch (IOException e) {
128 public Resource resource(SerialisationSupport ss) {
130 return ss.getResource(resourceKey);
131 } catch (DatabaseException e) {
132 throw new RuntimeDatabaseException(e);