Merge commit 'ffdf837'
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / SerialisationSupportImpl.java
1 package fi.vtt.simantics.procore.internal;\r
2 \r
3 import org.simantics.db.Resource;\r
4 import org.simantics.db.ResourceSerializer;\r
5 import org.simantics.db.common.utils.Logger;\r
6 import org.simantics.db.exception.DatabaseException;\r
7 import org.simantics.db.impl.ClusterI;\r
8 import org.simantics.db.impl.ResourceImpl;\r
9 import org.simantics.db.procore.cluster.ClusterImpl;\r
10 import org.simantics.db.procore.cluster.ClusterTraits;\r
11 import org.simantics.db.service.ResourceUID;\r
12 import org.simantics.db.service.SerialisationSupport;\r
13 \r
14 import fi.vtt.simantics.procore.internal.SessionImplSocket.ResourceSerializerImpl;\r
15 \r
16 public class SerialisationSupportImpl implements SerialisationSupport {\r
17 \r
18         final private SessionImplSocket session;\r
19         final private ResourceSerializerImpl serializer;\r
20         \r
21         SerialisationSupportImpl(SessionImplSocket session) {\r
22                 this.session = session;\r
23                 this.serializer = session.resourceSerializer;\r
24         }\r
25         \r
26     @Override\r
27     public ResourceSerializer getResourceSerializer() {\r
28         return serializer;\r
29     }\r
30 \r
31         @Override\r
32         public long getRandomAccessId(int id) {\r
33                 try {\r
34                         return serializer.createRandomAccessId(id);\r
35                 } catch (DatabaseException e) {\r
36                         Logger.defaultLogError(e);\r
37                 } catch (Throwable t) {\r
38                         Logger.defaultLogError(t);\r
39                 }\r
40                 return 0;\r
41         }\r
42         \r
43         @Override\r
44         public int getTransientId(Resource resource) throws DatabaseException {\r
45                 return serializer.getTransientId(resource);\r
46         }\r
47         \r
48         @Override\r
49         public Resource getResource(long randomAccessId) throws DatabaseException {\r
50                 return serializer.getResource(randomAccessId);\r
51         }\r
52         \r
53         @Override\r
54         public int getTransientId(long randomAccessId) throws DatabaseException {\r
55                 return serializer.getTransientId(randomAccessId);\r
56         }\r
57 \r
58         @Override\r
59         public long getRandomAccessId(Resource resource) throws DatabaseException {\r
60                 return serializer.getRandomAccessId(resource);\r
61         }\r
62 \r
63         @Override\r
64         public Resource getResource(int transientId) throws DatabaseException {\r
65                 return session.getResource(transientId);\r
66         }\r
67 \r
68         @Override\r
69         public ResourceUID getUID(Resource resource) throws DatabaseException {\r
70                 ResourceImpl impl = (ResourceImpl)resource;\r
71                 int resourceKey = impl.id;\r
72                 ClusterImpl cluster = session.clusterTable.getClusterByResourceKey(resourceKey);\r
73                 return cluster.clusterUID.toRID(ClusterTraits.getResourceIndexFromResourceKey(resourceKey));\r
74         }\r
75 \r
76         @Override\r
77         public Resource getResource(ResourceUID uid) throws DatabaseException {\r
78         ClusterI cluster = session.clusterTable.getClusterByClusterUIDOrMakeProxy(uid.asCID());\r
79         int key = ClusterTraits.createResourceKey(cluster.getClusterKey(), (int) uid.getIndex());\r
80         return new ResourceImpl(session.resourceSupport, key);\r
81         }\r
82     \r
83 }\r