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