]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/GraphBackend.java
First step to enable reading of cache when not writing
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / GraphBackend.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.spreadsheet.graph;
13
14 import org.simantics.Simantics;
15 import org.simantics.db.AsyncReadGraph;
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.RequestProcessor;
18 import org.simantics.db.Resource;
19 import org.simantics.db.VirtualGraph;
20 import org.simantics.db.common.procedure.adapter.AsyncListenerSupport;
21 import org.simantics.db.common.procedure.adapter.ListenerSupport;
22 import org.simantics.db.common.procedure.adapter.SyncListenerSupport;
23 import org.simantics.db.common.processor.MergingDelayedWriteProcessor;
24 import org.simantics.db.common.processor.MergingWriteOnlyProcessor;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.layer0.variable.Variable;
27 import org.simantics.spreadsheet.Adaptable;
28 import org.simantics.spreadsheet.CellEditor;
29
30 public class GraphBackend implements Adaptable, ListenerSupport, AsyncListenerSupport, SyncListenerSupport {
31
32         final private RequestProcessor processor;
33
34         final private VirtualGraph virtualGraph;
35
36         private Resource model;
37 //      private Resource spreadsheet;
38 //      private Variable sheet;
39 //      private ModelCellManager creator;
40         private CellEditor editor;
41         private boolean disposed = false;
42
43         final private MergingDelayedWriteProcessor delayedMerger;
44         final private MergingWriteOnlyProcessor writeOnlyMerger;
45         
46         public GraphBackend(RequestProcessor processor, VirtualGraph virtualGraph) {
47                 this.processor = processor;
48                 this.virtualGraph = virtualGraph;
49                 delayedMerger = new MergingDelayedWriteProcessor(Simantics.getSessionContext().getSession(), 5);
50                 writeOnlyMerger = new MergingWriteOnlyProcessor(Simantics.getSessionContext().getSession(), virtualGraph, 5);
51         }
52         
53         public RequestProcessor getModifier() {
54                 return delayedMerger;
55         }
56
57         public RequestProcessor getWriteOnlyModifier() {
58                 return writeOnlyMerger;
59         }
60
61         public void load(RequestProcessor proc, final Resource model, final Variable sheet) throws DatabaseException {
62
63 //              final ITask task = ThreadLogger.getInstance().begin("GraphBackend.load");
64 //              
65 //              this.model = model;
66 //
67 //              processor.asyncRequest(new ReadRequest() {
68 //
69 //                      @Override
70 //                      public void run(ReadGraph graph) throws DatabaseException {
71 //
72 //                              Layer0 l0 = Layer0.getInstance(graph);
73 //
74 //                              SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
75 //
76 //                              final Resource realization = sheet.getInterface(graph, Resource.class);
77 //                              Resource config = graph.getPossibleObject(realization, L0X.Represents);
78 //
79 //                              // TODO : remove this and make the request persistent
80 //                              processor.asyncRequest(new Rows(config), new CacheListener<RowsColumnsIndex>(GraphBackend.this));
81 //
82 //                              creator = new DefaultCellCreator(processor, config);
83 //                              editor = new DefaultCellEditor(processor, config);
84 //
85 //                              graph.forObjectSet(config, sr.HasCell, new AsyncSetListenerDelegate<Resource>(GraphBackend.this) {
86 //
87 //                                      @Override
88 //                                      public void add(AsyncReadGraph graph, final Resource cellResource) {
89 //
90 //                                              graph.forAdapted(cellResource, ServerCell.class, new ResourceAsyncListenerDelegate<ServerCell>(cellResource, GraphBackend.this) {
91 //
92 //                                                      @Override
93 //                                                      public void execute(AsyncReadGraph graph, final ServerCell cell) {
94 //
95 //                                                              cell.register(graph.getSession(), delayedMerger, cellResource);
96 //
97 //                                                              graph.getSession().asyncRequest(new WriteRequest(virtualGraph) {
98 //
99 //                                                                      @Override
100 //                                                                      public void perform(WriteGraph graph) throws DatabaseException {
101 //
102 //                                                                              Layer0 L0 = Layer0.getInstance(graph);
103 //                                                                              SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
104 //
105 //                                                                              Map<Resource, Resource> map = graph.syncRequest(new MappedParts(realization));
106 //                                                                              Resource resource = map.get(cellResource);
107 //                                                                              Resource realizationType = graph.syncRequest(new RealizationType(cellResource, sr.Realization));
108 //
109 //                                                                              if(resource == null) {
110 //                                                                                      resource = graph.newResource();
111 //                                                                                      graph.claim(resource, L0.InstanceOf, null, realizationType);
112 //                                                                                      graph.claim(resource, L0X.Represents, null, cellResource);
113 //                                                                                      graph.addLiteral(resource, sr.Label, sr.LabelOf, L0.String, "", Bindings.STRING );
114 //                                                                                      graph.addLiteral(resource, sr.Content, sr.ContentOf, L0.String, "", Bindings.STRING );
115 //                                                                                      graph.claim(realization, L0.ConsistsOf, L0.PartOf, resource);
116 //                                                                              }
117 //
118 //                                                                              final AdaptionService service = graph.peekService(AdaptionService.class);
119 //
120 //                                                                              class Modifier implements ResourceAdapter<StringModifier> {
121 //
122 //                                                                                      public void adapt(AsyncReadGraph graph, Resource source, Resource r, AsyncProcedure<StringModifier> procedure) {
123 //
124 //                                                                                              procedure.execute(graph, new StringModifier() {
125 //
126 //                                                                                                      @Override
127 //                                                                                                      public String isValid(String newValue) {
128 //                                                                                                              return null;
129 //                                                                                                      }
130 //
131 //                                                                                                      @Override
132 //                                                                                                      public void modify(WriteGraph graph, String value) throws DatabaseException {
133 //
134 //                                                                                                              StringModifier modifier = graph.adapt(cellResource, StringModifier.class);
135 //                                                                                                              modifier.modify(graph, value);
136 //
137 //                                                                                                      }
138 //
139 //                                                                                              });
140 //
141 //                                                                                      }
142 //
143 //                                                                              };
144 //
145 //                                                                              service.addInstanceAdapter(resource, StringModifier.class, new Modifier());
146 //
147 //                                                                              cell.listen(GraphBackend.this, graph, resource);
148 //
149 //                                                                      }
150 //
151 //                                                              });
152 //
153 //                                                      }
154 //
155 //                                              });
156 //
157 //
158 //                                      }
159 //
160 //                                      @Override
161 //                                      public void remove(AsyncReadGraph graph, final Resource cellResource) {
162 //
163 //                                      }
164 //
165 //                              });
166 //                              
167 //                              task.finish();
168 //
169 //                      }
170 //
171 //              });
172
173         }
174
175         @Override
176         public <T> T getAdapter(Class<T> clazz) {
177
178 /*              if(ModelCellManager.class == clazz) return (T)creator;
179                 else if(CellEditor.class == clazz) return (T)editor;
180                 else*/ return null;
181
182         }
183
184         @Override
185         public void exception(Throwable t) {
186                 t.printStackTrace();
187         }
188
189         @Override
190         public boolean isDisposed() {
191                 return disposed;
192         }
193
194         @Override
195         public void exception(AsyncReadGraph graph, Throwable t) {
196                 t.printStackTrace();
197         }
198
199         @Override
200         public void exception(ReadGraph graph, Throwable t) {
201                 t.printStackTrace();
202         }
203
204         public VirtualGraph getVirtualGraph() {
205                 return virtualGraph;
206         }
207
208         public Resource getModel() {
209                 return model;
210         }
211         
212         public void dispose() {
213                 disposed = true;
214         }
215
216 }