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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.spreadsheet.graph;
\r
14 import org.simantics.db.AsyncReadGraph;
\r
15 import org.simantics.db.ReadGraph;
\r
16 import org.simantics.db.RequestProcessor;
\r
17 import org.simantics.db.Resource;
\r
18 import org.simantics.db.VirtualGraph;
\r
19 import org.simantics.db.common.procedure.adapter.AsyncListenerSupport;
\r
20 import org.simantics.db.common.procedure.adapter.ListenerSupport;
\r
21 import org.simantics.db.common.procedure.adapter.SyncListenerSupport;
\r
22 import org.simantics.db.common.processor.MergingDelayedWriteProcessor;
\r
23 import org.simantics.db.common.processor.MergingWriteOnlyProcessor;
\r
24 import org.simantics.db.exception.DatabaseException;
\r
25 import org.simantics.db.layer0.variable.Variable;
\r
26 import org.simantics.spreadsheet.Adaptable;
\r
27 import org.simantics.spreadsheet.CellEditor;
\r
28 import org.simantics.ui.SimanticsUI;
\r
30 public class GraphBackend implements Adaptable, ListenerSupport, AsyncListenerSupport, SyncListenerSupport {
\r
32 final private RequestProcessor processor;
\r
34 final private VirtualGraph virtualGraph;
\r
36 private Resource model;
\r
37 // private Resource spreadsheet;
\r
38 // private Variable sheet;
\r
39 // private ModelCellManager creator;
\r
40 private CellEditor editor;
\r
41 private boolean disposed = false;
\r
43 final private MergingDelayedWriteProcessor delayedMerger;
\r
44 final private MergingWriteOnlyProcessor writeOnlyMerger;
\r
46 public GraphBackend(RequestProcessor processor, VirtualGraph virtualGraph) {
\r
47 this.processor = processor;
\r
48 this.virtualGraph = virtualGraph;
\r
49 delayedMerger = new MergingDelayedWriteProcessor(SimanticsUI.getSessionContext().getSession(), 5);
\r
50 writeOnlyMerger = new MergingWriteOnlyProcessor(SimanticsUI.getSessionContext().getSession(), virtualGraph, 5);
\r
53 public RequestProcessor getModifier() {
\r
54 return delayedMerger;
\r
57 public RequestProcessor getWriteOnlyModifier() {
\r
58 return writeOnlyMerger;
\r
61 public void load(RequestProcessor proc, final Resource model, final Variable sheet) throws DatabaseException {
\r
63 // final ITask task = ThreadLogger.getInstance().begin("GraphBackend.load");
\r
65 // this.model = model;
\r
67 // processor.asyncRequest(new ReadRequest() {
\r
70 // public void run(ReadGraph graph) throws DatabaseException {
\r
72 // Layer0 l0 = Layer0.getInstance(graph);
\r
74 // SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
\r
76 // final Resource realization = sheet.getInterface(graph, Resource.class);
\r
77 // Resource config = graph.getPossibleObject(realization, L0X.Represents);
\r
79 // // TODO : remove this and make the request persistent
\r
80 // processor.asyncRequest(new Rows(config), new CacheListener<RowsColumnsIndex>(GraphBackend.this));
\r
82 // creator = new DefaultCellCreator(processor, config);
\r
83 // editor = new DefaultCellEditor(processor, config);
\r
85 // graph.forObjectSet(config, sr.HasCell, new AsyncSetListenerDelegate<Resource>(GraphBackend.this) {
\r
88 // public void add(AsyncReadGraph graph, final Resource cellResource) {
\r
90 // graph.forAdapted(cellResource, ServerCell.class, new ResourceAsyncListenerDelegate<ServerCell>(cellResource, GraphBackend.this) {
\r
93 // public void execute(AsyncReadGraph graph, final ServerCell cell) {
\r
95 // cell.register(graph.getSession(), delayedMerger, cellResource);
\r
97 // graph.getSession().asyncRequest(new WriteRequest(virtualGraph) {
\r
100 // public void perform(WriteGraph graph) throws DatabaseException {
\r
102 // Layer0 L0 = Layer0.getInstance(graph);
\r
103 // SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
\r
105 // Map<Resource, Resource> map = graph.syncRequest(new MappedParts(realization));
\r
106 // Resource resource = map.get(cellResource);
\r
107 // Resource realizationType = graph.syncRequest(new RealizationType(cellResource, sr.Realization));
\r
109 // if(resource == null) {
\r
110 // resource = graph.newResource();
\r
111 // graph.claim(resource, L0.InstanceOf, null, realizationType);
\r
112 // graph.claim(resource, L0X.Represents, null, cellResource);
\r
113 // graph.addLiteral(resource, sr.Label, sr.LabelOf, L0.String, "", Bindings.STRING );
\r
114 // graph.addLiteral(resource, sr.Content, sr.ContentOf, L0.String, "", Bindings.STRING );
\r
115 // graph.claim(realization, L0.ConsistsOf, L0.PartOf, resource);
\r
118 // final AdaptionService service = graph.peekService(AdaptionService.class);
\r
120 // class Modifier implements ResourceAdapter<StringModifier> {
\r
122 // public void adapt(AsyncReadGraph graph, Resource source, Resource r, AsyncProcedure<StringModifier> procedure) {
\r
124 // procedure.execute(graph, new StringModifier() {
\r
127 // public String isValid(String newValue) {
\r
132 // public void modify(WriteGraph graph, String value) throws DatabaseException {
\r
134 // StringModifier modifier = graph.adapt(cellResource, StringModifier.class);
\r
135 // modifier.modify(graph, value);
\r
145 // service.addInstanceAdapter(resource, StringModifier.class, new Modifier());
\r
147 // cell.listen(GraphBackend.this, graph, resource);
\r
161 // public void remove(AsyncReadGraph graph, final Resource cellResource) {
\r
176 public <T> T getAdapter(Class<T> clazz) {
\r
178 /* if(ModelCellManager.class == clazz) return (T)creator;
\r
179 else if(CellEditor.class == clazz) return (T)editor;
\r
180 else*/ return null;
\r
185 public void exception(Throwable t) {
\r
186 t.printStackTrace();
\r
190 public boolean isDisposed() {
\r
195 public void exception(AsyncReadGraph graph, Throwable t) {
\r
196 t.printStackTrace();
\r
200 public void exception(ReadGraph graph, Throwable t) {
\r
201 t.printStackTrace();
\r
204 public VirtualGraph getVirtualGraph() {
\r
205 return virtualGraph;
\r
208 public Resource getModel() {
\r
212 public void dispose() {
\r