]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/request/PropertyMapOfResource.java
Merge commit 'a2a4242'
[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 import org.slf4j.Logger;\r
26 import org.slf4j.LoggerFactory;\r
27 \r
28 public class PropertyMapOfResource extends ResourceRead<Map<String, Resource>> {\r
29 \r
30     private static final Logger LOGGER = LoggerFactory.getLogger(PropertyMapOfResource.class);\r
31 \r
32         public PropertyMapOfResource(Resource resource) {\r
33             super(resource);\r
34         }\r
35 \r
36         @Override\r
37         public Map<String,Resource> perform(ReadGraph graph) throws DatabaseException {\r
38 \r
39                 Layer0 L0 = Layer0.getInstance(graph);\r
40                 Collection<Resource> predicates = graph.getPredicates(resource); \r
41         THashMap<String, Resource> result = new THashMap<String, Resource>(predicates.size());\r
42                 for(Resource predicate : predicates) {\r
43                         if(graph.isSubrelationOf(predicate, L0.HasProperty)) {\r
44                         String name = graph.getPossibleRelatedValue(predicate, L0.HasName, Bindings.STRING);\r
45                         if(name != null) {\r
46                                         String escapedName = URIStringUtils.escape(name); \r
47                             if (result.put(escapedName, predicate) != null)\r
48                                 LOGGER.error("The database contains siblings with the same name " + name + " (resource=$" + resource.getResourceId() +").");\r
49                         }\r
50                         }                               \r
51                 }\r
52                 return result;\r
53                 \r
54         }\r
55         \r
56 }\r