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.exception.InvalidResourceReferenceException;
8 import org.simantics.db.impl.ClusterI;
9 import org.simantics.db.impl.ClusterI.PredicateProcedure;
10 import org.simantics.db.impl.ResourceImpl;
11 import org.simantics.db.procore.cluster.ClusterImpl;
12 import org.simantics.db.procore.cluster.ClusterTraits;
13 import org.simantics.db.service.ResourceUID;
14 import org.simantics.db.service.SerialisationSupport;
16 import fi.vtt.simantics.procore.internal.SessionImplSocket.ResourceSerializerImpl;
18 public class SerialisationSupportImpl implements SerialisationSupport {
20 final private SessionImplSocket session;
21 final private ResourceSerializerImpl serializer;
23 SerialisationSupportImpl(SessionImplSocket session) {
24 this.session = session;
25 this.serializer = session.resourceSerializer;
29 public ResourceSerializer getResourceSerializer() {
34 public long getRandomAccessId(int id) {
36 return serializer.createRandomAccessId(id);
37 } catch (DatabaseException e) {
38 Logger.defaultLogError(e);
39 } catch (Throwable t) {
40 Logger.defaultLogError(t);
46 public int getTransientId(Resource resource) throws DatabaseException {
47 return serializer.getTransientId(resource);
51 public Resource getResource(long randomAccessId) throws DatabaseException {
52 return serializer.getResource(randomAccessId);
56 public int getTransientId(long randomAccessId) throws DatabaseException {
57 return serializer.getTransientId(randomAccessId);
61 public long getRandomAccessId(Resource resource) throws DatabaseException {
62 return serializer.getRandomAccessId(resource);
66 public Resource getResource(int transientId) throws DatabaseException {
67 return session.getResource(transientId);
71 public ResourceUID getUID(Resource resource) throws DatabaseException {
72 ResourceImpl impl = (ResourceImpl)resource;
73 int resourceKey = impl.id;
74 ClusterImpl cluster = session.clusterTable.getClusterByResourceKey(resourceKey);
75 return cluster.clusterUID.toRID(ClusterTraits.getResourceIndexFromResourceKey(resourceKey));
78 static class ExistsPredicateProcedure implements PredicateProcedure<Integer> {
80 boolean exists = false;
83 public boolean execute(Integer c, final int predicateKey, int objectIndex) {
91 public Resource getResource(ResourceUID uid) throws DatabaseException {
92 ClusterI cluster = session.clusterTable.getClusterByClusterUIDOrMakeProxy(uid.asCID());
93 int key = ClusterTraits.createResourceKey(cluster.getClusterKey(), (int) uid.getIndex());
94 if (cluster.hasResource(key, session.clusterTranslator)) {
95 ExistsPredicateProcedure pp = new ExistsPredicateProcedure();
96 cluster.forPredicates(key, pp, 0, session.clusterTranslator);
98 return new ResourceImpl(session.resourceSupport, key);
99 } else if (cluster.hasValue(key, session.clusterTranslator)) {
100 return new ResourceImpl(session.resourceSupport, key);
103 throw new InvalidResourceReferenceException("Resource with uid = " + uid + " does not exist.");