]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/uri/EscapedChildMapOfResource.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / uri / EscapedChildMapOfResource.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.common.uri;
13
14 import java.util.Map;
15 import java.util.concurrent.ConcurrentHashMap;
16
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;
29
30 public class EscapedChildMapOfResource extends ResourceAsyncRead<Map<String, Resource>> {
31
32     private static final Logger LOGGER = LoggerFactory.getLogger(EscapedChildMapOfResource.class);
33
34         public EscapedChildMapOfResource(Resource resource) {
35             super(resource);
36         }
37
38         @Override
39         public void perform(AsyncReadGraph g, final AsyncProcedure<Map<String, Resource>> procedure) {
40                 
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);
44                 
45                 g.forEachObject(resource, L0.ConsistsOf, new AsyncMultiProcedureAdapter<Resource>() {
46
47                         @Override
48                         public void execute(AsyncReadGraph g, final Resource child) {
49                                 barrier.incrementAndGet();
50
51                                 g.forPossibleRelatedValue(child, L0.HasName, WriteBindings.STRING, new AsyncProcedure<Object>() {
52
53                                         @Override
54                                         public void execute(AsyncReadGraph g, Object name) {
55                                                 if (name != null) {
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() +").");
59                                                 }
60                                                 barrier.dec(g, procedure, map);
61                                         }
62                                         
63                                         @Override
64                                         public void exception(AsyncReadGraph graph, Throwable t) {
65                                                 barrier.dec(graph, procedure, t);
66                                         }
67                                         
68                                 });
69                         }
70                         
71                         @Override
72                         public void exception(AsyncReadGraph graph, Throwable t) {
73                                 barrier.dec(graph, procedure, t);
74                         }
75                         
76                         @Override
77                         public void finished(AsyncReadGraph g) {
78                                 barrier.dec(g, procedure, map);
79                         }
80                         
81                 });
82         }
83         
84 }