]> gerrit.simantics Code Review - simantics/platform.git/blob
2dff0a74006baa38fe9f1669ff5a037f60e2fde0
[simantics/platform.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.structural2.queries;\r
13 \r
14 import gnu.trove.map.hash.THashMap;\r
15 \r
16 import java.util.Collection;\r
17 import java.util.Collections;\r
18 import java.util.Map;\r
19 \r
20 import org.simantics.db.ReadGraph;\r
21 import org.simantics.db.Resource;\r
22 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;\r
23 import org.simantics.db.common.request.TransientResourceRead;\r
24 import org.simantics.db.exception.DatabaseException;\r
25 import org.simantics.db.layer0.request.PropertyInfo;\r
26 import org.simantics.db.request.RequestFlags;\r
27 import org.simantics.db.service.QueryControl;\r
28 \r
29 public class ConnectionPointMapOfResource extends TransientResourceRead<Map<String, Resource>> {\r
30 \r
31         public ConnectionPointMapOfResource(ReadGraph graph, Resource resource) throws DatabaseException {\r
32             super(graph, resource);\r
33         }\r
34         \r
35         public ConnectionPointMapOfResource(ReadGraph graph, QueryControl qc, Resource resource) throws DatabaseException {\r
36                 super(graph, qc, resource);\r
37         }\r
38 \r
39         @Override\r
40         public Map<String,Resource> perform(ReadGraph graph, Resource resource) throws DatabaseException {\r
41             \r
42                 Collection<Resource> predicates = graph.getPredicates(resource);\r
43                 \r
44                 THashMap<String,Resource> result = null;\r
45                 \r
46                 for(Resource predicate : predicates) {\r
47                         \r
48                         PropertyInfo info = graph.syncRequest(new PossibleConnectionPointInfo(predicate), TransientCacheAsyncListener.<PropertyInfo>instance());\r
49                         if(info != null) {\r
50                                 if (result == null) result = new THashMap<String,Resource>(predicates.size());\r
51                                 if (result.put(info.name, predicate) != null)\r
52                                         System.err.println(this + ": The database contains siblings with the same name " + info.name + " (resource=$" + resource.getResourceId() + ").");\r
53                         }                               \r
54                         \r
55                 }\r
56 \r
57                 if(result != null) return result;\r
58                 \r
59                 else return Collections.emptyMap();\r
60                         \r
61         }\r
62         \r
63         @Override\r
64         public int getType() {\r
65                 return RequestFlags.INVALIDATE;\r
66         }\r
67         \r
68 }\r