]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/URIToResource.java
Some enhancements made by Antti for multiple readers
[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                 ObjectResourceIdMap<String> map = QueryCache.resultChildMap(graph, parentId, entry, null);
54                 if(map == null) {
55                         procedure.execute(graph, 0);
56                         if(entry != null) entry.addOrSet(graph, graph.processor, 0);
57                 } else {
58                         int result = map.getId(URIStringUtils.unescape(parts[1]));
59                         if(entry != null) entry.addOrSet(graph, graph.processor, result);
60                         procedure.execute(graph, result);
61                 }
62                 
63             } else {
64                 
65                 ResourceNotFoundException e = new ResourceNotFoundException("No resource for URI: " + id);
66                 if(entry != null) entry.except(e);
67                 procedure.exception(graph, e);
68                 
69             }
70
71         }
72         
73     }
74     
75     public void addOrSet(ReadGraphImpl graph, QueryProcessor provider, Integer result) {
76
77         assert(isPending());
78
79         synchronized(this) {
80             setResult(result);
81             setReady();
82         }
83         
84     }
85     
86     @Override
87     public String toString() {
88         return "URIToResource[" + id + "]";
89     }
90
91     @Override
92     public Object performFromCache(ReadGraphImpl graph, InternalProcedure<Integer> procedure) throws DatabaseException {
93         
94         assert(isReady());
95         
96         if(handleException(graph, procedure)) return (Throwable)statusOrException;
97
98         Integer result = (Integer)getResult();
99         procedure.execute(graph, result);
100         return result;
101         
102     }
103     
104     @Override
105     public void recompute(ReadGraphImpl graph) throws DatabaseException {
106         
107         compute(graph, new InternalProcedure<Integer>() {
108
109             @Override
110             public void execute(ReadGraphImpl graph, Integer result) {
111             }
112             
113             @Override
114             public void exception(ReadGraphImpl graph, Throwable t) {
115                 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
116                 throw new Error("Error in recompute.", t);
117             }
118
119         });
120         
121     }
122         
123 }