]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/URIToResource.java
Fixing URIToResource
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / URIToResource.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.impl.query;
13
14 import org.simantics.databoard.util.URIStringUtils;
15 import org.simantics.db.ObjectResourceIdMap;
16 import org.simantics.db.common.exception.DebugException;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.exception.ResourceNotFoundException;
19 import org.simantics.db.impl.graph.ReadGraphImpl;
20 import org.simantics.db.impl.procedure.InternalProcedure;
21
22 public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
23
24     URIToResource(final String id) {
25         super(id);
26     }
27
28     @Override
29     final public void removeEntry(QueryProcessor provider) {
30         provider.cache.remove(this);
31     }
32     
33     //@Override
34     public Object compute(ReadGraphImpl graph, final InternalProcedure<Integer> procedure) throws DatabaseException {
35         computeForEach(graph, id, this, procedure);
36         return getResult();
37     }
38
39     static void computeForEach(ReadGraphImpl graph, String id, final URIToResource entry, final InternalProcedure<Integer> procedure) throws DatabaseException {
40         
41         if("http://".equals(id) || "http:/".equals(id)) {
42             
43                 QueryProcessor processor = graph.processor;
44             if(entry != null) entry.addOrSet(graph, processor, processor.getRootLibrary());
45             procedure.execute(graph, processor.getRootLibrary());
46
47         } else {
48             
49             final String[] parts = URIStringUtils.splitURI(id);
50             if (parts != null) {
51
52                 //Integer parentId = QueryCache.resultURIToResource(graph, parts[0], entry, null);
53                 
54                 QueryCache.runnerURIToResource(graph, parts[0], entry, null, new InternalProcedure<Integer>() {
55
56                     @Override
57                     public void execute(ReadGraphImpl graph, Integer parentId) throws DatabaseException {
58                         
59                         ObjectResourceIdMap<String> map = QueryCache.resultChildMap(graph, parentId, entry, null);
60                         assert(map != null);
61 //                      if(map == null) {
62 //                          throw new DatabaseException("Internal Error, contact application support.");
63 //                            if(entry != null) entry.except(e);
64 //                            procedure.exception(graph, e);
65 ////                            procedure.execute(graph, 0);
66 ////                            if(entry != null) entry.addOrSet(graph, graph.processor, 0);
67 //                      } else {
68                             int result = map.getId(URIStringUtils.unescape(parts[1]));
69                             if(result == 0) {
70                                 ResourceNotFoundException e = new ResourceNotFoundException("No resource for URI: " + id);
71                                 if(entry != null) entry.except(e);
72                                 procedure.exception(graph, e);
73                             } else {
74                                 if(entry != null) entry.addOrSet(graph, graph.processor, result);
75                                 procedure.execute(graph, result);
76                             }
77 //                      }
78                         
79                         // TODO Auto-generated method stub
80                         
81                     }
82
83                     @Override
84                     public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
85                         if(entry != null) entry.except(throwable);
86                         procedure.exception(graph, throwable);
87                     }
88                     
89                 });
90                 
91                         
92             } else {
93                 
94                 ResourceNotFoundException e = new ResourceNotFoundException("No resource for URI: " + id);
95                 if(entry != null) entry.except(e);
96                 procedure.exception(graph, e);
97                 
98             }
99
100         }
101         
102     }
103     
104     public void addOrSet(ReadGraphImpl graph, QueryProcessor provider, Integer result) {
105
106         assert(isPending());
107
108         synchronized(this) {
109             setResult(result);
110             setReady();
111         }
112         
113     }
114     
115     @Override
116     public String toString() {
117         return "URIToResource[" + id + "]";
118     }
119
120     @Override
121     public Object performFromCache(ReadGraphImpl graph, InternalProcedure<Integer> procedure) throws DatabaseException {
122         
123         assert(isReady());
124         
125         if(handleException(graph, procedure)) return (Throwable)getResult();
126
127         Integer result = (Integer)getResult();
128         procedure.execute(graph, result);
129         return result;
130         
131     }
132     
133     @Override
134     public void recompute(ReadGraphImpl graph) throws DatabaseException {
135         
136         compute(graph, new InternalProcedure<Integer>() {
137
138             @Override
139             public void execute(ReadGraphImpl graph, Integer result) {
140             }
141             
142             @Override
143             public void exception(ReadGraphImpl graph, Throwable t) {
144                 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
145                 throw new Error("Error in recompute.", t);
146             }
147
148         });
149         
150     }
151         
152 }