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.AssumptionException;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.layer0.Layer0;
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)}.
28 * @author Antti Villberg
30 public class ResourceToURI extends ResourceRead<String> {
32 public ResourceToURI(Resource resource) {
37 public String perform(ReadGraph graph) throws DatabaseException {
39 if (resource.equals(graph.getRootLibrary())) return "http:/";
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");
45 String escapedName = URIStringUtils.escape((String) name);
47 Resource parent = graph.getPossibleObject(resource, L0.PartOf);
48 if(parent == null) throw new AssumptionException("resource " + resource + " does not have a parent");
50 String parentURI = graph.syncRequest(new ResourceToURI(parent));
51 if(parentURI == null) throw new AssumptionException("parent resource " + parent + " has no URI");
53 return parentURI + "/" + escapedName;