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