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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.common.uri;
\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
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
28 * @author Antti Villberg
\r
30 public class ResourceToURI extends ResourceRead<String> {
\r
32 public ResourceToURI(Resource resource) {
\r
37 public String perform(ReadGraph graph) throws DatabaseException {
\r
39 if (resource.equals(graph.getRootLibrary())) return "http:/";
\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
45 String escapedName = URIStringUtils.escape((String) name);
\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
50 String parentURI = graph.syncRequest(new ResourceToURI(parent));
\r
51 if(parentURI == null) throw new AssumptionException("parent resource " + parent + " has no URI");
\r
53 return parentURI + "/" + escapedName;
\r