1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.common.uri;
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;
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)}.
27 * @author Antti Villberg
29 public class ResourceToPossibleURI extends ResourceRead<String> {
31 public ResourceToPossibleURI(Resource resource) {
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;