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