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