--- /dev/null
+/*******************************************************************************\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.request;\r
+\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.simantics.db.AsyncReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.ProcedureBarrier;\r
+import org.simantics.db.procedure.AsyncMultiProcedure;\r
+import org.simantics.db.procedure.AsyncProcedure;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.operation.Layer0X;\r
+\r
+public class MappedTypedPartsAsync extends ResourceAsyncRead2<Map<Resource, Resource>> {\r
+\r
+ public MappedTypedPartsAsync(Resource realization, Resource type) {\r
+ super(realization, type);\r
+ }\r
+ \r
+ @Override\r
+ public void perform(AsyncReadGraph graph, final AsyncProcedure<Map<Resource, Resource>> procedure) {\r
+\r
+ final Layer0 L0 = graph.getService(Layer0.class);\r
+ final Layer0X L0X = graph.getService(Layer0X.class);\r
+ final HashMap<Resource, Resource> result = new HashMap<Resource, Resource>();\r
+ final ProcedureBarrier<Map<Resource, Resource>> ready = new ProcedureBarrier<Map<Resource, Resource>>(1);\r
+ \r
+ graph.forEachObject(resource, L0.ConsistsOf, new AsyncMultiProcedure<Resource>() {\r
+\r
+ @Override\r
+ public void execute(AsyncReadGraph graph, final Resource object) {\r
+ \r
+ ready.incrementAndGet();\r
+ \r
+ graph.forTypes(object, new AsyncProcedure<Set<Resource>>() {\r
+ \r
+ @Override\r
+ public void execute(AsyncReadGraph graph, Set<Resource> types) {\r
+\r
+ if(types.contains(resource2)) {\r
+ \r
+ graph.forEachObject(object, L0X.Represents, new AsyncMultiProcedure<Resource>() {\r
+\r
+ @Override\r
+ public void execute(AsyncReadGraph graph, Resource mapped) {\r
+ \r
+ synchronized(result) {\r
+ result.put(mapped, object);\r
+ }\r
+ \r
+ }\r
+\r
+ @Override\r
+ public void finished(AsyncReadGraph graph) {\r
+\r
+ ready.dec(graph, procedure, result);\r
+ \r
+ }\r
+\r
+ @Override\r
+ public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+ \r
+ ready.except(throwable);\r
+ ready.dec(graph, procedure, result);\r
+ \r
+ }\r
+ \r
+ });\r
+ \r
+ } else {\r
+ \r
+ ready.dec(graph, procedure, result);\r
+\r
+ }\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+ \r
+ ready.except(throwable);\r
+ ready.dec(graph, procedure, result);\r
+ \r
+ }\r
+ \r
+ });\r
+ \r
+ }\r
+\r
+ @Override\r
+ public void finished(AsyncReadGraph graph) {\r
+ \r
+ ready.dec(graph, procedure, result);\r
+ \r
+ }\r
+\r
+ @Override\r
+ public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+ \r
+ ready.except(throwable);\r
+ ready.dec(graph, procedure, result);\r
+ \r
+ }\r
+ \r
+ });\r
+ \r
+ }\r
+ \r
+// @Override\r
+// public Map<Resource, Resource> perform(ReadGraph graph) throws DatabaseException {\r
+// \r
+// Builtins b = graph.getBuiltins();\r
+// Map<Resource, Resource> result = new HashMap<Resource, Resource>();\r
+// \r
+// for(Resource part : graph.syncRequest(new ObjectsWithType(resource, b.ConsistsOf, resource2))) {\r
+// Resource represents = graph.getPossibleObject(part, b.Represents);\r
+// if(represents != null) result.put(represents, part);\r
+// }\r
+// \r
+// return result;\r
+// \r
+// }\r
+\r
+}\r