1 package fi.vtt.simantics.procore.internal;
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;
14 import fi.vtt.simantics.procore.internal.SessionImplSocket.ResourceSerializerImpl;
16 public class SerialisationSupportImpl implements SerialisationSupport {
18 final private SessionImplSocket session;
19 final private ResourceSerializerImpl serializer;
21 SerialisationSupportImpl(SessionImplSocket session) {
22 this.session = session;
23 this.serializer = session.resourceSerializer;
27 public ResourceSerializer getResourceSerializer() {
32 public long getRandomAccessId(int id) {
34 return serializer.createRandomAccessId(id);
35 } catch (DatabaseException e) {
36 Logger.defaultLogError(e);
37 } catch (Throwable t) {
38 Logger.defaultLogError(t);
44 public int getTransientId(Resource resource) throws DatabaseException {
45 return serializer.getTransientId(resource);
49 public Resource getResource(long randomAccessId) throws DatabaseException {
50 return serializer.getResource(randomAccessId);
54 public int getTransientId(long randomAccessId) throws DatabaseException {
55 return serializer.getTransientId(randomAccessId);
59 public long getRandomAccessId(Resource resource) throws DatabaseException {
60 return serializer.getRandomAccessId(resource);
64 public Resource getResource(int transientId) throws DatabaseException {
65 return session.getResource(transientId);
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));
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);