]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/uri/ResourceToPossibleURI.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / uri / ResourceToPossibleURI.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 org.simantics.databoard.Bindings;\r
15 import org.simantics.databoard.util.URIStringUtils;\r
16 import org.simantics.db.ReadGraph;\r
17 import org.simantics.db.Resource;\r
18 import org.simantics.db.common.request.ResourceRead;\r
19 import org.simantics.db.exception.DatabaseException;\r
20 import org.simantics.layer0.Layer0;\r
21 \r
22 /**\r
23  * Tries to retrieve URIs for specified resources. Returns <code>null</code> if\r
24  * URI is undefined. This query follows the semantics of\r
25  * {@link ReadGraph#getPossibleURI(Resource)}.\r
26  * \r
27  * @author Antti Villberg\r
28  */\r
29 public class ResourceToPossibleURI extends ResourceRead<String> {\r
30 \r
31     public ResourceToPossibleURI(Resource resource) {\r
32         super(resource);\r
33     }\r
34 \r
35     @Override\r
36     public String perform(ReadGraph g) throws DatabaseException {\r
37         if(g.getRootLibrary().equals(resource)) return "http:/";\r
38         Layer0 L0 = Layer0.getInstance(g);\r
39         String name = g.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING);\r
40         if(name == null) return null;\r
41         String escapedName = URIStringUtils.escape((String) name);\r
42         Resource parent = g.getPossibleObject(resource, L0.PartOf);\r
43         if(parent == null) return null;\r
44         String parentURI = g.syncRequest(new ResourceToPossibleURI(parent));\r
45         if(parentURI == null) return null;\r
46         return parentURI + "/" + escapedName;\r
47     }\r
48 \r
49 }\r