]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/NodeRequest2.java
DB threading still work in progress
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / NodeRequest2.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.diagram.adapter;
13
14 import java.util.List;
15
16 import org.simantics.db.AsyncReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.common.primitiverequest.Adapter;
19 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
20 import org.simantics.db.procedure.AsyncProcedure;
21 import org.simantics.db.procedure.Listener;
22 import org.simantics.diagram.synchronization.ErrorHandler;
23 import org.simantics.g2d.canvas.ICanvasContext;
24 import org.simantics.g2d.diagram.IDiagram;
25 import org.simantics.g2d.diagram.handler.SubstituteElementClass;
26 import org.simantics.g2d.element.ElementClass;
27 import org.simantics.g2d.element.IElement;
28 import org.simantics.scl.runtime.tuple.Tuple3;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * @author Antti Villberg
34  */
35 public class NodeRequest2 extends BaseRequest2<Resource, Tuple3> {
36
37     private static final Logger LOGGER = LoggerFactory.getLogger(NodeRequest2.class);
38
39     final IDiagram diagram;
40
41     public NodeRequest2(ICanvasContext canvas, IDiagram diagram, Resource resource) {
42         super(canvas, resource);
43         this.diagram = diagram;
44     }
45
46     @Override
47     public void perform(AsyncReadGraph graph, final AsyncProcedure<Tuple3> procedure) {
48         // Keep this code from crashing the whole DB client by unexpected
49         // throwing of NPE's somewhere in the following code that leads to
50         // procedure not getting called properly.
51         if (diagram == null) {
52             procedure.exception(graph, new NullPointerException("null diagram specified for resource " + data));
53             return;
54         }
55
56         final ErrorHandler eh = ElementFactoryUtil.getErrorHandler(diagram);
57
58         graph.forHasStatement(data, new AsyncProcedure<Boolean>() {
59
60             @Override
61             public void exception(AsyncReadGraph graph, Throwable throwable) {
62                 eh.error("NodeRequest.forHasStatement failed", throwable);
63                 procedure.execute(graph, null);
64             }
65
66             @Override
67             public void execute(AsyncReadGraph graph, Boolean result) {
68
69                 if(!result) {
70                     procedure.execute(graph, null);
71                     return;
72                 }
73
74                 graph.asyncRequest(new Adapter<ElementFactory>(data, ElementFactory.class), new TransientCacheAsyncListener<ElementFactory>() {
75
76                     @Override
77                     public void exception(AsyncReadGraph graph, Throwable throwable) {
78                         eh.error("NodeRequest.asyncRequest(Adapter<ElementFactory>) failed", throwable);
79                         procedure.execute(graph, null);
80                     }
81
82                     @Override
83                     public void execute(AsyncReadGraph graph, final ElementFactory factory) {
84
85                         graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener<ElementClass>() {
86
87                             @Override
88                             public void exception(AsyncReadGraph graph, Throwable throwable) {
89                                 LOGGER.error("Unexpected error in GetElementClassRequest", throwable);
90                                 procedure.execute(graph, null);
91                             }
92
93                             @Override
94                             public void execute(AsyncReadGraph graph, ElementClass mutableClazz) {
95                                 List<SubstituteElementClass> substitutes = diagram.getDiagramClass().getItemsByClass(SubstituteElementClass.class);
96                                 for (SubstituteElementClass subs : substitutes) {
97                                     mutableClazz = subs.substitute(diagram, mutableClazz);
98                                 }
99                                 final ElementClass clazz = mutableClazz;
100                                 graph.asyncRequest(new SpawnRequest(canvas, clazz, data), new TransientCacheAsyncListener<IElement>() {
101
102                                     @Override
103                                     public void exception(AsyncReadGraph graph, Throwable throwable) {
104                                         LOGGER.error("Unexpected error in SpawnRequest", throwable);
105                                         procedure.execute(graph, null);
106                                     }
107
108                                     @Override
109                                     public void execute(AsyncReadGraph graph, IElement element) {
110                                         procedure.execute(graph, new Tuple3(element, clazz, factory));
111                                     }
112
113                                 });
114
115                             }
116
117                         });
118
119                     }
120
121                 });
122
123             }
124
125         });
126
127     }
128
129 }