]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/URIToResource.java
Trying to remove synchronization problems
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / URIToResource.java
index 918f882f179c2acd4ba8b79677f473abacf67deb..547264f4b0c99fb75368e5c448d82965038994a1 100644 (file)
@@ -19,7 +19,7 @@ import org.simantics.db.exception.ResourceNotFoundException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
 
-public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
+public class URIToResource extends StringQuery<InternalProcedure<Integer>> implements InternalProcedure<Integer> {
 
     URIToResource(final String id) {
         super(id);
@@ -36,12 +36,13 @@ public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
         return getResult();
     }
 
-    static void computeForEach(ReadGraphImpl graph, String id, final URIToResource entry, final InternalProcedure<Integer> procedure) throws DatabaseException {
+    static void computeForEach(ReadGraphImpl graph, String id, final URIToResource entry, final InternalProcedure<Integer> procedure_) throws DatabaseException {
+        
+        InternalProcedure<Integer> procedure = entry != null ? entry : procedure_;
         
         if("http://".equals(id) || "http:/".equals(id)) {
             
                QueryProcessor processor = graph.processor;
-            if(entry != null) entry.addOrSet(graph, processor, processor.getRootLibrary());
             procedure.execute(graph, processor.getRootLibrary());
 
         } else {
@@ -49,8 +50,6 @@ public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
             final String[] parts = URIStringUtils.splitURI(id);
             if (parts != null) {
 
-               //Integer parentId = QueryCache.resultURIToResource(graph, parts[0], entry, null);
-               
                QueryCache.runnerURIToResource(graph, parts[0], entry, null, new InternalProcedure<Integer>() {
 
                     @Override
@@ -58,31 +57,17 @@ public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
                         
                         ObjectResourceIdMap<String> map = QueryCache.resultChildMap(graph, parentId, entry, null);
                         assert(map != null);
-//                      if(map == null) {
-//                          throw new DatabaseException("Internal Error, contact application support.");
-//                            if(entry != null) entry.except(e);
-//                            procedure.exception(graph, e);
-////                            procedure.execute(graph, 0);
-////                            if(entry != null) entry.addOrSet(graph, graph.processor, 0);
-//                      } else {
-                            int result = map.getId(URIStringUtils.unescape(parts[1]));
-                            if(result == 0) {
-                                ResourceNotFoundException e = new ResourceNotFoundException("No resource for URI: " + id);
-                                if(entry != null) entry.except(e);
-                                procedure.exception(graph, e);
-                            } else {
-                                if(entry != null) entry.addOrSet(graph, graph.processor, result);
-                                procedure.execute(graph, result);
-                            }
-//                      }
-                        
-                        // TODO Auto-generated method stub
-                        
+                        int result = map.getId(URIStringUtils.unescape(parts[1]));
+                        if (result == 0) {
+                            ResourceNotFoundException e = new ResourceNotFoundException("No resource for URI: " + id);
+                            procedure.exception(graph, e);
+                        } else {
+                            procedure.execute(graph, result);
+                        }
                     }
 
                     @Override
                     public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
-                        if(entry != null) entry.except(throwable);
                         procedure.exception(graph, throwable);
                     }
                     
@@ -90,18 +75,17 @@ public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
                
                        
             } else {
-               
                 ResourceNotFoundException e = new ResourceNotFoundException("No resource for URI: " + id);
-               if(entry != null) entry.except(e);
-               procedure.exception(graph, e);
-                
+                procedure.exception(graph, e);
             }
 
         }
         
+        if(entry != null) entry.performFromCache(graph, procedure_);
+        
     }
     
-    public void addOrSet(ReadGraphImpl graph, QueryProcessor provider, Integer result) {
+    public void addOrSet(Integer result) {
 
         assert(isPending());
 
@@ -148,5 +132,18 @@ public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
         });
         
     }
+
+    @Override
+    public void execute(ReadGraphImpl graph, Integer result) throws DatabaseException {
+        synchronized(this) {
+            setResult(result);
+            setReady();
+        }
+    }
+
+    @Override
+    public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
+        except(throwable);
+    }
        
 }