]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncMultiReadEntry.java
Generate parts of db client query code
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / AsyncMultiReadEntry.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 java.util.ArrayList;
15
16 import org.simantics.db.AsyncReadGraph;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.impl.graph.ReadGraphImpl;
19 import org.simantics.db.procedure.AsyncMultiProcedure;
20 import org.simantics.db.request.AsyncMultiRead;
21 import org.simantics.db.request.RequestFlags;
22
23 final public class AsyncMultiReadEntry<T> extends CacheEntryBase<AsyncMultiProcedure<T>> {
24
25     protected AsyncMultiRead<T> request;
26     
27     AsyncMultiReadEntry(AsyncMultiRead<T> request) {
28         this.request = request;
29     }
30     
31     @Override
32     int makeHash() {
33         return request.hashCode();
34     }
35     
36     @Override
37     public Object getOriginalRequest() {
38         return request;
39     }
40     
41     @Override
42     public void discard() {
43         super.discard();
44         request = null;
45         setResult(null);
46     }
47     
48     final synchronized public void finish(AsyncReadGraph graph) {
49         
50         assert(isPending());
51
52         synchronized(this) {
53                 setReady();
54         }
55         
56     }
57
58     final synchronized public void except(AsyncReadGraph graph, Throwable t) {
59
60         assert(isPending());
61
62         synchronized(this) {
63                 except(t);
64         }
65         
66     }
67
68     @SuppressWarnings("unchecked")
69         final synchronized public void addOrSet(Object item) {
70
71         assert(isPending());
72         
73         ArrayList<T> value = (ArrayList<T>)getResult(); 
74         value.add((T)item);
75         
76     }
77     
78     @Override
79     public void clearResult(QuerySupport support) {
80         setResult(new ArrayList<T>());
81     }
82     
83     @Override
84     final public Query getQuery() {
85         
86         return new Query() {
87
88                         @Override
89                         public void recompute(ReadGraphImpl graph) {
90
91                                 request.perform(graph , new AsyncMultiProcedure<T>() {
92
93                                         @Override
94                                         public void execute(AsyncReadGraph graph, T result) {
95                                                 addOrSet(result);
96                                         }
97
98                                         public void finished(AsyncReadGraph graph) {
99                                                 finish(graph);
100                                         };
101
102                                         @Override
103                                         public void exception(AsyncReadGraph graph, Throwable t) {
104                                                 except(t);
105                                         }
106
107                                 });
108                                 
109                         }
110
111                         @Override
112                         public void removeEntry(QueryProcessor processor) {
113                         processor.cache.remove(AsyncMultiReadEntry.this);
114                         }
115
116                         @Override
117                         public int type() {
118                                 return RequestFlags.INVALIDATE;
119                         }
120                         
121                         @Override
122                         public String toString() {
123                                 if(request == null) return "DISCARDED";
124                                 else return request.toString() + statusOrException;
125                         }
126                 
127         };
128         
129     }
130
131         @SuppressWarnings("unchecked")
132         @Override
133         public Object performFromCache(ReadGraphImpl graph, AsyncMultiProcedure<T> proc) {
134
135         if(isExcepted()) {
136
137             try {
138                 proc.exception(graph, (Throwable)getResult());
139             } catch (Throwable t) {
140                 t.printStackTrace();
141             }
142             
143             
144         } else {
145             
146             final ArrayList<T> values = (ArrayList<T>)getResult();
147             for(T value : values) {
148                 try {
149                     proc.execute(graph, value);
150                 } catch (Throwable t) {
151                     t.printStackTrace();
152                 }
153             }
154
155             try {
156                 proc.finished(graph);
157             } catch (Throwable t) {
158                 t.printStackTrace();
159             }
160
161         }
162                 
163                 return getResult();
164                 
165         }
166         
167         @Override
168         public String toString() {
169                 if(request == null) return "DISCARDED";
170                 else return request.toString() + statusOrException;
171         }
172
173         @Override
174         public Object compute(ReadGraphImpl graph, AsyncMultiProcedure<T> procedure) throws DatabaseException {
175         return graph.processor.cache.performQuery(graph, request, this, procedure);
176         }
177
178 }