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 java.util.Arrays;
\r
15 import java.util.Map;
\r
17 import org.simantics.db.AsyncReadGraph;
\r
18 import org.simantics.db.Resource;
\r
19 import org.simantics.db.common.request.Queries;
\r
20 import org.simantics.db.exception.ResourceNotFoundException;
\r
21 import org.simantics.db.procedure.AsyncProcedure;
\r
22 import org.simantics.db.request.AsyncRead;
\r
25 * TODO: should this be considered deprecated and favor {@link Queries#resource(String)} ?
\r
27 public class URIToResource implements AsyncRead<Resource> {
\r
28 String[] nameSequence;
\r
30 public URIToResource(String[] nameSequence) {
\r
31 this.nameSequence = nameSequence;
\r
34 public URIToResource(String uri) {
\r
35 this.nameSequence = URIEscape.splitURI(uri);
\r
39 public void perform(AsyncReadGraph g, final AsyncProcedure<Resource> procedure) {
\r
40 if(nameSequence.length == 0)
\r
41 procedure.execute(g, g.getRootLibrary());
\r
43 g.asyncRequest(new URIToResource(Arrays.copyOf(nameSequence, nameSequence.length-1)),
\r
44 new AsyncProcedure<Resource>() {
\r
47 public void execute(AsyncReadGraph g, Resource result) {
\r
48 g.asyncRequest(new EscapedChildMapOfResource(result),
\r
49 new AsyncProcedure<Map<String, Resource>>() {
\r
52 public void execute(AsyncReadGraph g, Map<String, Resource> map) {
\r
53 Resource r = map.get(nameSequence[nameSequence.length-1]);
\r
55 procedure.execute(g, r);
\r
57 procedure.exception(g, new ResourceNotFoundException(
\r
58 URIEscape.joinURI(nameSequence)));
\r
62 public void exception(AsyncReadGraph g, Throwable t) {
\r
63 procedure.exception(g, t);
\r
71 public void exception(AsyncReadGraph g, Throwable t) {
\r
72 procedure.exception(g, t);
\r
81 public int hashCode() {
\r
82 return Arrays.hashCode(nameSequence);
\r
86 public int threadHash() {
\r
91 public boolean equals(Object obj) {
\r
94 if(!(obj instanceof URIToResource))
\r
96 return Arrays.equals(nameSequence, ((URIToResource)obj).nameSequence);
\r
100 public int getFlags() {
\r