--- /dev/null
+package fi.vtt.simantics.procore.internal;\r
+\r
+import java.io.IOException;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.serialization.SerializationException;\r
+import org.simantics.databoard.serialization.Serializer;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteOnlyGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.RuntimeDatabaseException;\r
+import org.simantics.db.impl.graph.WriteGraphImpl;\r
+import org.simantics.db.procore.cluster.ClusterImpl;\r
+import org.simantics.db.service.ClusterBuilder.ResourceHandle;\r
+import org.simantics.db.service.ClusterBuilder.StatementHandle;\r
+import org.simantics.db.service.SerialisationSupport;\r
+\r
+import fi.vtt.simantics.procore.internal.SessionImplSocket.WriteOnlySupport;\r
+\r
+final public class ResourceHandleImpl implements ResourceHandle {\r
+ \r
+ final private ClusterImpl cluster;\r
+ final public int resourceKey;\r
+ \r
+ public ResourceHandleImpl(WriteOnlySupport support) throws DatabaseException {\r
+ this.resourceKey = support.createResourceKey(1);\r
+ this.cluster = support.currentCluster;\r
+ if(cluster.cc == null) cluster.cc = new ClusterChange(support.stream, cluster);\r
+ }\r
+ \r
+ public ResourceHandleImpl(ClusterStream stream, ClusterImpl cluster, int resourceKey) {\r
+ this.resourceKey = resourceKey;\r
+ this.cluster = cluster;\r
+ if(cluster.cc == null) cluster.cc = new ClusterChange(stream, cluster);\r
+ }\r
+\r
+ @Override\r
+ public void applyPredicate(Object cluster) {\r
+ \r
+ ClusterImpl impl = (ClusterImpl)cluster;\r
+ impl.change.addStatementIndex1(resourceKey, this.cluster.clusterUID, (byte)0, impl.foreignLookup);\r
+ \r
+ }\r
+\r
+ @Override\r
+ public void applyObject(Object cluster) {\r
+\r
+ ClusterImpl impl = (ClusterImpl)cluster;\r
+ impl.change.addStatementIndex2(resourceKey, this.cluster.clusterUID, (byte)0, impl.foreignLookup);\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public void addStatement(StatementHandle handle) {\r
+\r
+ if(cluster.getImmutable()) return;\r
+\r
+ cluster.change.addStatementIndex0(resourceKey, ClusterChange.ADD_OPERATION);\r
+ handle.apply(cluster);\r
+ cluster.cc.addChange(cluster.change);\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public void addStatement(ResourceHandle predicate, ResourceHandle object) {\r
+ \r
+ if(cluster.getImmutable()) return;\r
+ \r
+ Change change = cluster.change;\r
+ change.addStatementIndex0(resourceKey, ClusterChange.ADD_OPERATION);\r
+ predicate.applyPredicate(cluster);\r
+ object.applyObject(cluster);\r
+ cluster.cc.addChange(change);\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public void addStatement(WriteOnlyGraph graph, ResourceHandle predicate, ResourceHandle object) throws DatabaseException {\r
+ if(cluster.isWriteOnly()) addStatement(predicate, object);\r
+ else {\r
+ WriteGraphImpl impl = (WriteGraphImpl)graph;\r
+ impl.writeSupport.claim(graph.getProvider(), resourceKey, ((ResourceHandleImpl)predicate).resourceKey, ((ResourceHandleImpl)object).resourceKey);\r
+ }\r
+ }\r
+ \r
+ @Override\r
+ public void addValue(WriteOnlyGraph graph, byte[] bytes) throws DatabaseException {\r
+\r
+ if(cluster.isWriteOnly()) cluster.cc.setValue((short)(resourceKey & 0xFFFF), bytes);\r
+ else {\r
+ WriteGraphImpl impl = (WriteGraphImpl)graph;\r
+ impl.writeSupport.claimValue(null, resourceKey, bytes, bytes.length);\r
+ }\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public void addValue(Object value, Binding binding) {\r
+\r
+ try {\r
+ Serializer ser = Bindings.getSerializerUnchecked(binding);\r
+ byte[] bytes = ser.serialize(value);\r
+ cluster.cc.setValue((short)(resourceKey & 0xFFFF), bytes);\r
+ } catch (SerializationException e) {\r
+ e.printStackTrace();\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public void addValue(Object value, Serializer serializer) {\r
+\r
+ try {\r
+ byte[] bytes = serializer.serialize(value);\r
+ cluster.cc.setValue((short)(resourceKey & 0xFFFF), bytes);\r
+ } catch (SerializationException e) {\r
+ e.printStackTrace();\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public Resource resource(SerialisationSupport ss) {\r
+ try {\r
+ return ss.getResource(resourceKey);\r
+ } catch (DatabaseException e) {\r
+ throw new RuntimeDatabaseException(e);\r
+ }\r
+ }\r
+ \r
+}\r