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