Utilities for listing of statements in DB
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / SerialisationSupportImpl.java
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.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;
15
16 import fi.vtt.simantics.procore.internal.SessionImplSocket.ResourceSerializerImpl;
17
18 public class SerialisationSupportImpl implements SerialisationSupport {
19
20         final private SessionImplSocket session;
21         final private ResourceSerializerImpl serializer;
22         
23         SerialisationSupportImpl(SessionImplSocket session) {
24                 this.session = session;
25                 this.serializer = session.resourceSerializer;
26         }
27         
28     @Override
29     public ResourceSerializer getResourceSerializer() {
30         return serializer;
31     }
32
33         @Override
34         public long getRandomAccessId(int id) {
35                 try {
36                         return serializer.createRandomAccessId(id);
37                 } catch (DatabaseException e) {
38                         Logger.defaultLogError(e);
39                 } catch (Throwable t) {
40                         Logger.defaultLogError(t);
41                 }
42                 return 0;
43         }
44         
45         @Override
46         public int getTransientId(Resource resource) throws DatabaseException {
47                 return serializer.getTransientId(resource);
48         }
49         
50         @Override
51         public Resource getResource(long randomAccessId) throws DatabaseException {
52                 return serializer.getResource(randomAccessId);
53         }
54         
55         @Override
56         public int getTransientId(long randomAccessId) throws DatabaseException {
57                 return serializer.getTransientId(randomAccessId);
58         }
59
60         @Override
61         public long getRandomAccessId(Resource resource) throws DatabaseException {
62                 return serializer.getRandomAccessId(resource);
63         }
64
65         @Override
66         public Resource getResource(int transientId) throws DatabaseException {
67                 return session.getResource(transientId);
68         }
69
70         @Override
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));
76         }
77
78         static class ExistsPredicateProcedure implements PredicateProcedure<Integer> {
79                 
80                 boolean exists = false;
81
82                 @Override
83                 public boolean execute(Integer c, final int predicateKey, int objectIndex) {
84                         exists = true;
85                         return false;
86                 }
87
88         }
89         
90         @Override
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);
97                         if(pp.exists) {
98                                 return new ResourceImpl(session.resourceSupport, key);
99                         } else if (cluster.hasValue(key, session.clusterTranslator)) {
100                                 return new ResourceImpl(session.resourceSupport, key);
101                         }
102         }
103         throw new InvalidResourceReferenceException("Resource with uid = " + uid + " does not exist.");
104         }
105     
106 }