]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/uri/URIToResource.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / uri / URIToResource.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.common.uri;\r
13 \r
14 import java.util.Arrays;\r
15 import java.util.Map;\r
16 \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
23 \r
24 /**\r
25  * TODO: should this be considered deprecated and favor {@link Queries#resource(String)} ?\r
26  */\r
27 public class URIToResource implements AsyncRead<Resource> {\r
28         String[] nameSequence;\r
29         \r
30         public URIToResource(String[] nameSequence) {\r
31                 this.nameSequence = nameSequence;\r
32         }\r
33         \r
34         public URIToResource(String uri) {\r
35                 this.nameSequence = URIEscape.splitURI(uri);\r
36         }\r
37 \r
38         @Override\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
42                 else\r
43                         g.asyncRequest(new URIToResource(Arrays.copyOf(nameSequence, nameSequence.length-1)),\r
44                                 new AsyncProcedure<Resource>() {\r
45 \r
46                                         @Override\r
47                                         public void execute(AsyncReadGraph g, Resource result) {\r
48                                                 g.asyncRequest(new EscapedChildMapOfResource(result),\r
49                                                         new AsyncProcedure<Map<String, Resource>>() {\r
50 \r
51                                                                 @Override\r
52                                                                 public void execute(AsyncReadGraph g, Map<String, Resource> map) {\r
53                                                                         Resource r = map.get(nameSequence[nameSequence.length-1]);\r
54                                                                         if(r != null)\r
55                                                                                 procedure.execute(g, r);\r
56                                                                         else\r
57                                                                                 procedure.exception(g, new ResourceNotFoundException(\r
58                                                                                                 URIEscape.joinURI(nameSequence)));\r
59                                                                 }\r
60                                                         \r
61                                                                 @Override\r
62                                                                 public void exception(AsyncReadGraph g, Throwable t) {\r
63                                                                         procedure.exception(g, t);                                                                                                                      \r
64                                                                 }\r
65                                                         \r
66                                                         }\r
67                                                 );\r
68                                         }\r
69                                 \r
70                                         @Override\r
71                                         public void exception(AsyncReadGraph g, Throwable t) {\r
72                                                 procedure.exception(g, t);                                              \r
73                                         }\r
74                                 \r
75                                 }\r
76                         );\r
77                 \r
78         }\r
79         \r
80         @Override\r
81         public int hashCode() {\r
82                 return Arrays.hashCode(nameSequence);\r
83         }\r
84 \r
85     @Override\r
86     public int threadHash() {\r
87         return hashCode();\r
88     }\r
89 \r
90         @Override\r
91         public boolean equals(Object obj) {\r
92                 if(this == obj)\r
93                         return true;\r
94                 if(!(obj instanceof URIToResource))\r
95                         return false;\r
96                 return Arrays.equals(nameSequence, ((URIToResource)obj).nameSequence);\r
97         }\r
98     \r
99     @Override\r
100     public int getFlags() {\r
101         return 0;\r
102     }\r
103 \r
104 }\r