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