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