]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ResourceHandleImpl.java
Merge commit '2a46c55'
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / ResourceHandleImpl.java
1 package fi.vtt.simantics.procore.internal;\r
2 \r
3 import java.io.IOException;\r
4 \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
18 \r
19 import fi.vtt.simantics.procore.internal.SessionImplSocket.WriteOnlySupport;\r
20 \r
21 final public class ResourceHandleImpl implements ResourceHandle {\r
22         \r
23         final private ClusterImpl cluster;\r
24         final public int resourceKey;\r
25         \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
30         }\r
31         \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
36         }\r
37 \r
38         @Override\r
39         public void applyPredicate(Object cluster) {\r
40                 \r
41                 ClusterImpl impl = (ClusterImpl)cluster;\r
42                 impl.change.addStatementIndex1(resourceKey, this.cluster.clusterUID, (byte)0, impl.foreignLookup);\r
43                 \r
44         }\r
45 \r
46         @Override\r
47         public void applyObject(Object cluster) {\r
48 \r
49                 ClusterImpl impl = (ClusterImpl)cluster;\r
50                 impl.change.addStatementIndex2(resourceKey, this.cluster.clusterUID, (byte)0, impl.foreignLookup);\r
51                 \r
52         }\r
53         \r
54         @Override\r
55         public void addStatement(StatementHandle handle) {\r
56 \r
57             if(cluster.getImmutable()) return;\r
58 \r
59                 cluster.change.addStatementIndex0(resourceKey, ClusterChange.ADD_OPERATION);\r
60         handle.apply(cluster);\r
61                 cluster.cc.addChange(cluster.change);\r
62                 \r
63         }\r
64         \r
65         @Override\r
66         public void addStatement(ResourceHandle predicate, ResourceHandle object) {\r
67                 \r
68             if(cluster.getImmutable()) return;\r
69             \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
75                 \r
76         }\r
77         \r
78         @Override\r
79         public void addStatement(WriteOnlyGraph graph, ResourceHandle predicate, ResourceHandle object) throws DatabaseException {\r
80                 if(cluster.isWriteOnly()) addStatement(predicate, object);\r
81                 else {\r
82             WriteGraphImpl impl = (WriteGraphImpl)graph;\r
83                     impl.writeSupport.claim(graph.getProvider(), resourceKey, ((ResourceHandleImpl)predicate).resourceKey, ((ResourceHandleImpl)object).resourceKey);\r
84                 }\r
85         }\r
86         \r
87         @Override\r
88         public void addValue(WriteOnlyGraph graph, byte[] bytes) throws DatabaseException {\r
89 \r
90                 if(cluster.isWriteOnly()) cluster.cc.setValue((short)(resourceKey & 0xFFFF), bytes);\r
91                 else {\r
92                     WriteGraphImpl impl = (WriteGraphImpl)graph;\r
93                     impl.writeSupport.claimValue(null, resourceKey, bytes, bytes.length);\r
94                 }\r
95                 \r
96         }\r
97         \r
98         @Override\r
99         public void addValue(Object value, Binding binding) {\r
100 \r
101                 try {\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
109                 }\r
110                 \r
111         }\r
112         \r
113         @Override\r
114         public void addValue(Object value, Serializer serializer) {\r
115 \r
116                 try {\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
123                 }\r
124                 \r
125         }\r
126         \r
127         @Override\r
128         public Resource resource(SerialisationSupport ss) {\r
129             try {\r
130             return ss.getResource(resourceKey);\r
131         } catch (DatabaseException e) {\r
132             throw new RuntimeDatabaseException(e);\r
133         }\r
134         }\r
135         \r
136 }\r