--- /dev/null
+package org.simantics.db.impl.graph;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.NoSingleResultException;\r
+import org.simantics.db.impl.query.IntProcedure;\r
+import org.simantics.db.impl.query.QueryProcessor;\r
+\r
+public class SingleObjectProcedure implements IntProcedure {\r
+\r
+ final private QueryProcessor processor;\r
+ private DatabaseException exception;\r
+ private Resource result;\r
+ \r
+ public SingleObjectProcedure(QueryProcessor processor) {\r
+ this.processor = processor;\r
+ }\r
+ \r
+ @Override\r
+ public void execute(ReadGraphImpl graph, int i) {\r
+ Resource resource = processor.querySupport.getResource(i);\r
+ if(result != null) {\r
+ exception = new NoSingleResultException("");\r
+ } else {\r
+ result = resource;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void finished(ReadGraphImpl graph) {\r
+ if(result == null) exception = new NoSingleResultException("");\r
+ }\r
+\r
+ @Override\r
+ public void exception(ReadGraphImpl graph, Throwable throwable) {\r
+ if(throwable instanceof DatabaseException) exception = (DatabaseException)throwable;\r
+ else throwable = new DatabaseException(throwable);\r
+ }\r
+ \r
+ public Resource get() throws DatabaseException {\r
+ if(exception != null) throw exception;\r
+ else return result;\r
+ }\r
+\r
+}\r