--- /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.uri;\r
+\r
+import java.util.Map;\r
+import java.util.concurrent.ConcurrentHashMap;\r
+\r
+import org.simantics.databoard.util.URIStringUtils;\r
+import org.simantics.db.AsyncReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.ProcedureBarrier;\r
+import org.simantics.db.common.WriteBindings;\r
+import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;\r
+import org.simantics.db.common.request.ResourceAsyncRead;\r
+import org.simantics.db.procedure.AsyncProcedure;\r
+import org.simantics.layer0.Layer0;\r
+\r
+public class EscapedChildMapOfResource extends ResourceAsyncRead<Map<String, Resource>> {\r
+\r
+ public EscapedChildMapOfResource(Resource resource) {\r
+ super(resource);\r
+ }\r
+\r
+ @Override\r
+ public void perform(AsyncReadGraph g, final AsyncProcedure<Map<String, Resource>> procedure) {\r
+ \r
+ final Layer0 L0 = g.getService(Layer0.class);\r
+ final ConcurrentHashMap<String, Resource> map = new ConcurrentHashMap<String, Resource>();\r
+ final ProcedureBarrier<Map<String, Resource>> barrier = new ProcedureBarrier<Map<String, Resource>>(1);\r
+ \r
+ g.forEachObject(resource, L0.ConsistsOf, new AsyncMultiProcedureAdapter<Resource>() {\r
+\r
+ @Override\r
+ public void execute(AsyncReadGraph g, final Resource child) {\r
+ barrier.incrementAndGet();\r
+\r
+ g.forPossibleRelatedValue(child, L0.HasName, WriteBindings.STRING, new AsyncProcedure<Object>() {\r
+\r
+ @Override\r
+ public void execute(AsyncReadGraph g, Object name) {\r
+ if (name != null) {\r
+ String escapedName = URIStringUtils.escape((String)name); \r
+ if (map.put(escapedName, child) != null)\r
+ System.err.println(this + ": The database contains siblings with the same name " + escapedName + " (resource=" + resource.getResourceId() +").");\r
+ }\r
+ barrier.dec(g, procedure, map);\r
+ }\r
+ \r
+ @Override\r
+ public void exception(AsyncReadGraph graph, Throwable t) {\r
+ barrier.dec(graph, procedure, t);\r
+ }\r
+ \r
+ });\r
+ }\r
+ \r
+ @Override\r
+ public void exception(AsyncReadGraph graph, Throwable t) {\r
+ barrier.dec(graph, procedure, t);\r
+ }\r
+ \r
+ @Override\r
+ public void finished(AsyncReadGraph g) {\r
+ barrier.dec(g, procedure, map);\r
+ }\r
+ \r
+ });\r
+ }\r
+ \r
+}\r