]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/request/PropertyMapOfResource.java
5a92ef62c894717a878058bcdfe9d1eb8757f318
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / request / PropertyMapOfResource.java
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.db.common.request;\r
13 \r
14 import gnu.trove.map.hash.THashMap;\r
15 \r
16 import java.util.Collection;\r
17 import java.util.Map;\r
18 \r
19 import org.simantics.databoard.Bindings;\r
20 import org.simantics.databoard.util.URIStringUtils;\r
21 import org.simantics.db.ReadGraph;\r
22 import org.simantics.db.Resource;\r
23 import org.simantics.db.exception.DatabaseException;\r
24 import org.simantics.layer0.Layer0;\r
25 \r
26 public class PropertyMapOfResource extends ResourceRead<Map<String, Resource>> {\r
27 \r
28         public PropertyMapOfResource(Resource resource) {\r
29             super(resource);\r
30         }\r
31 \r
32         @Override\r
33         public Map<String,Resource> perform(ReadGraph graph) throws DatabaseException {\r
34 \r
35                 Layer0 L0 = Layer0.getInstance(graph);\r
36                 Collection<Resource> predicates = graph.getPredicates(resource); \r
37         THashMap<String, Resource> result = new THashMap<String, Resource>(predicates.size());\r
38                 for(Resource predicate : predicates) {\r
39                         if(graph.isSubrelationOf(predicate, L0.HasProperty)) {\r
40                         String name = graph.getPossibleRelatedValue(predicate, L0.HasName, Bindings.STRING);\r
41                         if(name != null) {\r
42                                         String escapedName = URIStringUtils.escape(name); \r
43                             if (result.put(escapedName, predicate) != null)\r
44                                 System.err.println(this + ": The database contains siblings with the same name " + name + " (resource=$" + resource.getResourceId() +").");\r
45                         }\r
46                         }                               \r
47                 }\r
48                 return result;\r
49                 \r
50         }\r
51         \r
52 }\r