]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/SerialisationSupportImpl.java
Utilities for listing of statements in DB
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / SerialisationSupportImpl.java
index e0e871bb9ad154ae3a6ba93a274b3c86dd9e319b..0eaf0ecf0ef7bff56f9851b0fbca774f24c9bcb4 100644 (file)
@@ -4,7 +4,9 @@ import org.simantics.db.Resource;
 import org.simantics.db.ResourceSerializer;
 import org.simantics.db.common.utils.Logger;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.InvalidResourceReferenceException;
 import org.simantics.db.impl.ClusterI;
+import org.simantics.db.impl.ClusterI.PredicateProcedure;
 import org.simantics.db.impl.ResourceImpl;
 import org.simantics.db.procore.cluster.ClusterImpl;
 import org.simantics.db.procore.cluster.ClusterTraits;
@@ -73,11 +75,32 @@ public class SerialisationSupportImpl implements SerialisationSupport {
                return cluster.clusterUID.toRID(ClusterTraits.getResourceIndexFromResourceKey(resourceKey));
        }
 
+       static class ExistsPredicateProcedure implements PredicateProcedure<Integer> {
+               
+               boolean exists = false;
+
+               @Override
+               public boolean execute(Integer c, final int predicateKey, int objectIndex) {
+                       exists = true;
+                       return false;
+               }
+
+       }
+       
        @Override
        public Resource getResource(ResourceUID uid) throws DatabaseException {
         ClusterI cluster = session.clusterTable.getClusterByClusterUIDOrMakeProxy(uid.asCID());
         int key = ClusterTraits.createResourceKey(cluster.getClusterKey(), (int) uid.getIndex());
-        return new ResourceImpl(session.resourceSupport, key);
+        if (cluster.hasResource(key, session.clusterTranslator)) {
+               ExistsPredicateProcedure pp = new ExistsPredicateProcedure();
+                       cluster.forPredicates(key, pp, 0, session.clusterTranslator);
+                       if(pp.exists) {
+                               return new ResourceImpl(session.resourceSupport, key);
+                       } else if (cluster.hasValue(key, session.clusterTranslator)) {
+                               return new ResourceImpl(session.resourceSupport, key);
+                       }
+        }
+        throw new InvalidResourceReferenceException("Resource with uid = " + uid + " does not exist.");
        }
     
 }