]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/URIToResource.java
Still working for multiple readers
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / URIToResource.java
index 5a44fc2eb608a273139501ac827a264265f1f5d3..794bcc8edc3258fdee965278321c41f8e3789156 100644 (file)
 package org.simantics.db.impl.query;
 
 import org.simantics.databoard.util.URIStringUtils;
+import org.simantics.db.ObjectResourceIdMap;
 import org.simantics.db.common.exception.DebugException;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.ResourceNotFoundException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
-import org.simantics.db.procedure.ListenerBase;
-
-import gnu.trove.map.hash.TObjectIntHashMap;
 
 public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
 
@@ -30,40 +29,8 @@ public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
     final public void removeEntry(QueryProcessor provider) {
        provider.cache.remove(this);
     }
-
-    private static void lookup(ReadGraphImpl graph, final URIToResource entry, final InternalProcedure<Integer> procedure, final String namespace, final String name) throws DatabaseException {
-        
-       QueryCache.runnerNamespaceIndex(graph, namespace, entry, null, new InternalProcedure<TObjectIntHashMap<String>>() {
-
-            @Override
-            public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) throws DatabaseException {
-
-                if(index != null) {
-                    int result = index.get(name);
-                    if(result != 0) {
-                        if(entry != null) entry.addOrSet(graph, graph.processor, result);
-                        procedure.execute(graph, result);
-                        return;
-                    }
-                }
-                
-                Integer zero = 0;
-                if(entry != null) entry.addOrSet(graph, graph.processor, zero);
-                procedure.execute(graph, zero);
-
-            }
-
-            @Override
-            public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                if(entry != null) entry.except(t);
-                procedure.exception(graph, t);
-            }
-
-        });
-
-    }
     
-    @Override
+    //@Override
     public Object compute(ReadGraphImpl graph, final InternalProcedure<Integer> procedure) throws DatabaseException {
        computeForEach(graph, id, this, procedure);
         return getResult();
@@ -81,9 +48,24 @@ public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
             
             final String[] parts = URIStringUtils.splitURI(id);
             if (parts != null) {
-                lookup(graph, entry, procedure, parts[0], parts[1]);
+
+               Integer parentId = QueryCache.resultURIToResource(graph, parts[0], entry, null);
+               ObjectResourceIdMap<String> map = QueryCache.resultChildMap(graph, parentId, entry, null);
+               if(map == null) {
+                       procedure.execute(graph, 0);
+                       if(entry != null) entry.addOrSet(graph, graph.processor, 0);
+               } else {
+                       int result = map.getId(URIStringUtils.unescape(parts[1]));
+                       if(entry != null) entry.addOrSet(graph, graph.processor, result);
+                       procedure.execute(graph, result);
+               }
+               
             } else {
-                lookup(graph, entry, procedure, "http://", id.replaceFirst("http://", ""));
+               
+                ResourceNotFoundException e = new ResourceNotFoundException("No resource for URI: " + id);
+               if(entry != null) entry.except(e);
+               procedure.exception(graph, e);
+                
             }
 
         }