1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.common.uri;
15 import java.util.concurrent.ConcurrentHashMap;
17 import org.simantics.databoard.util.URIStringUtils;
18 import org.simantics.db.AsyncReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.common.ProcedureBarrier;
21 import org.simantics.db.common.WriteBindings;
22 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;
23 import org.simantics.db.common.request.PropertyMapOfResource;
24 import org.simantics.db.common.request.ResourceAsyncRead;
25 import org.simantics.db.procedure.AsyncProcedure;
26 import org.simantics.layer0.Layer0;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class EscapedChildMapOfResource extends ResourceAsyncRead<Map<String, Resource>> {
32 private static final Logger LOGGER = LoggerFactory.getLogger(EscapedChildMapOfResource.class);
34 public EscapedChildMapOfResource(Resource resource) {
39 public void perform(AsyncReadGraph g, final AsyncProcedure<Map<String, Resource>> procedure) {
41 final Layer0 L0 = g.getService(Layer0.class);
42 final ConcurrentHashMap<String, Resource> map = new ConcurrentHashMap<String, Resource>();
43 final ProcedureBarrier<Map<String, Resource>> barrier = new ProcedureBarrier<Map<String, Resource>>(1);
45 g.forEachObject(resource, L0.ConsistsOf, new AsyncMultiProcedureAdapter<Resource>() {
48 public void execute(AsyncReadGraph g, final Resource child) {
49 barrier.incrementAndGet();
51 g.forPossibleRelatedValue(child, L0.HasName, WriteBindings.STRING, new AsyncProcedure<Object>() {
54 public void execute(AsyncReadGraph g, Object name) {
56 String escapedName = URIStringUtils.escape((String)name);
57 if (map.put(escapedName, child) != null)
58 LOGGER.error("The database contains siblings with the same name " + escapedName + " (resource=" + resource.getResourceId() +").");
60 barrier.dec(g, procedure, map);
64 public void exception(AsyncReadGraph graph, Throwable t) {
65 barrier.dec(graph, procedure, t);
72 public void exception(AsyncReadGraph graph, Throwable t) {
73 barrier.dec(graph, procedure, t);
77 public void finished(AsyncReadGraph g) {
78 barrier.dec(g, procedure, map);