1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.spreadsheet.graph;
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;
30 public class GraphBackend implements Adaptable, ListenerSupport, AsyncListenerSupport, SyncListenerSupport {
32 final private RequestProcessor processor;
34 final private VirtualGraph virtualGraph;
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;
43 final private MergingDelayedWriteProcessor delayedMerger;
44 final private MergingWriteOnlyProcessor writeOnlyMerger;
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);
53 public RequestProcessor getModifier() {
57 public RequestProcessor getWriteOnlyModifier() {
58 return writeOnlyMerger;
61 public void load(RequestProcessor proc, final Resource model, final Variable sheet) throws DatabaseException {
63 // final ITask task = ThreadLogger.getInstance().begin("GraphBackend.load");
65 // this.model = model;
67 // processor.asyncRequest(new ReadRequest() {
70 // public void run(ReadGraph graph) throws DatabaseException {
72 // Layer0 l0 = Layer0.getInstance(graph);
74 // SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
76 // final Resource realization = sheet.getInterface(graph, Resource.class);
77 // Resource config = graph.getPossibleObject(realization, L0X.Represents);
79 // // TODO : remove this and make the request persistent
80 // processor.asyncRequest(new Rows(config), new CacheListener<RowsColumnsIndex>(GraphBackend.this));
82 // creator = new DefaultCellCreator(processor, config);
83 // editor = new DefaultCellEditor(processor, config);
85 // graph.forObjectSet(config, sr.HasCell, new AsyncSetListenerDelegate<Resource>(GraphBackend.this) {
88 // public void add(AsyncReadGraph graph, final Resource cellResource) {
90 // graph.forAdapted(cellResource, ServerCell.class, new ResourceAsyncListenerDelegate<ServerCell>(cellResource, GraphBackend.this) {
93 // public void execute(AsyncReadGraph graph, final ServerCell cell) {
95 // cell.register(graph.getSession(), delayedMerger, cellResource);
97 // graph.getSession().asyncRequest(new WriteRequest(virtualGraph) {
100 // public void perform(WriteGraph graph) throws DatabaseException {
102 // Layer0 L0 = Layer0.getInstance(graph);
103 // SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
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));
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);
118 // final AdaptionService service = graph.peekService(AdaptionService.class);
120 // class Modifier implements ResourceAdapter<StringModifier> {
122 // public void adapt(AsyncReadGraph graph, Resource source, Resource r, AsyncProcedure<StringModifier> procedure) {
124 // procedure.execute(graph, new StringModifier() {
127 // public String isValid(String newValue) {
132 // public void modify(WriteGraph graph, String value) throws DatabaseException {
134 // StringModifier modifier = graph.adapt(cellResource, StringModifier.class);
135 // modifier.modify(graph, value);
145 // service.addInstanceAdapter(resource, StringModifier.class, new Modifier());
147 // cell.listen(GraphBackend.this, graph, resource);
161 // public void remove(AsyncReadGraph graph, final Resource cellResource) {
176 public <T> T getAdapter(Class<T> clazz) {
178 /* if(ModelCellManager.class == clazz) return (T)creator;
179 else if(CellEditor.class == clazz) return (T)editor;
185 public void exception(Throwable t) {
190 public boolean isDisposed() {
195 public void exception(AsyncReadGraph graph, Throwable t) {
200 public void exception(ReadGraph graph, Throwable t) {
204 public VirtualGraph getVirtualGraph() {
208 public Resource getModel() {
212 public void dispose() {