]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.common/src/org/simantics/db/common/primitiverequest/ForEachAssertedObject.java
Multiple simultaneous readers
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / primitiverequest / ForEachAssertedObject.java
index 1cef1de4586757d3632709ac7187d41065665dd5..541aee94e10d31c50480f7a2508a3f432face183 100644 (file)
@@ -1,30 +1,60 @@
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.db.common.primitiverequest;\r
-\r
-import org.simantics.db.AsyncReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.request.ResourceAsyncMultiRead2;\r
-import org.simantics.db.procedure.AsyncMultiProcedure;\r
-\r
-final public class ForEachAssertedObject extends ResourceAsyncMultiRead2<Resource> {\r
-\r
-    public ForEachAssertedObject(Resource subject, Resource relation) {\r
-        super(subject, relation);\r
-    }\r
-\r
-       @Override\r
-       public void perform(AsyncReadGraph graph, AsyncMultiProcedure<Resource> procedure) {\r
-       graph.forEachAssertedObject(resource, resource2, procedure);\r
-       }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.common.primitiverequest;
+
+import java.util.Collection;
+
+import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.ResourceRead2;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.procedure.AsyncMultiProcedure;
+import org.simantics.db.service.CollectionSupport;
+import org.simantics.utils.DataContainer;
+
+final public class ForEachAssertedObject extends ResourceRead2<Collection<Resource>> {
+
+    public ForEachAssertedObject(Resource subject, Resource relation) {
+        super(subject, relation);
+    }
+
+       @Override
+       public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
+               CollectionSupport cs = graph.getService(CollectionSupport.class);
+               Collection<Resource> result = cs.createSet();
+               DataContainer<Throwable> throwable = new DataContainer<Throwable>(null);
+               graph.forEachAssertedObject(resource, resource2, new AsyncMultiProcedure<Resource>() {
+                       
+                       @Override
+                       public void finished(AsyncReadGraph graph) {
+                       }
+                       
+                       @Override
+                       public void execute(AsyncReadGraph graph, Resource r) {
+                               result.add(r);
+                       }
+                       
+                       @Override
+                       public void exception(AsyncReadGraph graph, Throwable t) {
+                               throwable.set(t);
+                       }
+               });
+               Throwable t = throwable.get();
+               if(t != null)
+                       if(t instanceof DatabaseException)
+                               throw (DatabaseException)t;
+                       else throw new DatabaseException(t);
+               return result;
+       }
+
+}