]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/uri/UnescapedChildMapOfResource.java
303afddd00da0dbdc657891f45282c2eb09b0665
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / uri / UnescapedChildMapOfResource.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.uri;\r
13 \r
14 import java.util.Collection;\r
15 import java.util.Map;\r
16 \r
17 import org.simantics.databoard.Bindings;\r
18 import org.simantics.db.ReadGraph;\r
19 import org.simantics.db.Resource;\r
20 import org.simantics.db.common.request.ResourceRead;\r
21 import org.simantics.db.exception.DatabaseException;\r
22 import org.simantics.db.service.CollectionSupport;\r
23 import org.simantics.layer0.Layer0;\r
24 import org.simantics.utils.Development;\r
25 \r
26 public class UnescapedChildMapOfResource extends ResourceRead<Map<String, Resource>> {\r
27 \r
28         public UnescapedChildMapOfResource(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             Layer0 L0 = Layer0.getInstance(graph);\r
35             Collection<Resource> objects = graph.getObjects(resource, L0.ConsistsOf);\r
36             CollectionSupport cs = graph.getService(CollectionSupport.class);\r
37             Map<String,Resource> result = cs.createObjectResourceMap(String.class, objects.size());\r
38             for(Resource r : objects) {\r
39                 String name = graph.getPossibleRelatedValue(r, L0.HasName, Bindings.STRING);\r
40                 if(name != null) {\r
41                     Resource old = result.put(name, r);\r
42                     if (old != null)\r
43                         System.err.println(this + ": The database contains siblings with the same name " + name + " (resource=$" + resource.getResourceId() + ", child=$" + r.getResourceId() + ", previous child=$" + old.getResourceId() + ").");\r
44                 } else {\r
45                         if(Development.DEVELOPMENT)\r
46                             System.err.println(this + ": The database contains a child with no unique name (resource=$" + resource.getResourceId() + ", child=$" + r.getResourceId() + ").");\r
47                 }\r
48             }\r
49             return result;\r
50         }       \r
51         \r
52 }\r