]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/NodeRequest.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / NodeRequest.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.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * @author Antti Villberg
33  */
34 public class NodeRequest extends BaseRequest2<Resource, IElement> {
35
36     private static final Logger LOGGER = LoggerFactory.getLogger(NodeRequest.class);
37
38     final IDiagram diagram;
39     final Listener<IElement> loadListener;
40
41     public NodeRequest(ICanvasContext canvas, IDiagram diagram, Resource resource, Listener<IElement> loadListener) {
42         super(canvas, resource);
43         this.diagram = diagram;
44         this.loadListener = loadListener;
45     }
46
47     @Override
48     public void perform(AsyncReadGraph graph, final AsyncProcedure<IElement> procedure) {
49         // Keep this code from crashing the whole DB client by unexpected
50         // throwing of NPE's somewhere in the following code that leads to
51         // procedure not getting called properly.
52         if (diagram == null) {
53             procedure.exception(graph, new NullPointerException("null diagram specified for resource " + data));
54             return;
55         }
56
57 //        System.out.println("NodeRequest2 " + data);
58 //        graph.asyncRequest(new SafeName(data), new Procedure<String>() {
59 //            @Override
60 //            public void exception(Throwable t) {
61 //            }
62 //            @Override
63 //            public void execute(String result) {
64 //                System.out.println("NodeRequest2 "  + result);
65 //            }
66 //        });
67
68         final ErrorHandler eh = ElementFactoryUtil.getErrorHandler(diagram);
69
70         graph.forHasStatement(data, new AsyncProcedure<Boolean>() {
71
72             @Override
73             public void exception(AsyncReadGraph graph, Throwable throwable) {
74                 eh.error("NodeRequest.forHasStatement failed", throwable);
75                 procedure.execute(graph, null);
76             }
77
78             @Override
79             public void execute(AsyncReadGraph graph, Boolean result) {
80
81                 if(!result) {
82                     procedure.execute(graph, null);
83                     return;
84                 }
85
86                 graph.asyncRequest(new Adapter<ElementFactory>(data, ElementFactory.class), new TransientCacheAsyncListener<ElementFactory>() {
87
88                     @Override
89                     public void exception(AsyncReadGraph graph, Throwable throwable) {
90                         eh.error("NodeRequest.asyncRequest(Adapter<ElementFactory>) failed", throwable);
91                         procedure.execute(graph, null);
92                     }
93
94                     @Override
95                     public void execute(AsyncReadGraph graph, final ElementFactory factory) {
96
97 //                        graph.asyncRequest(new ResourceToURI(data), new Procedure<String>() {
98 //                            @Override
99 //                            public void exception(Throwable t) {
100 //                            }
101 //                            @Override
102 //                            public void execute(String result) {
103 //                                System.out.println("NodeRequest2 factory for "  + result + " -> " + factory);
104 //                            }
105 //                        });
106
107                         graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener<ElementClass>() {
108
109                             @Override
110                             public void exception(AsyncReadGraph graph, Throwable throwable) {
111                                 LOGGER.error("Unexpected error in GetElementClassRequest", throwable);
112                                 procedure.execute(graph, null);
113                             }
114
115                             @Override
116                             public void execute(AsyncReadGraph graph, ElementClass mutableClazz) {
117                                 List<SubstituteElementClass> substitutes = diagram.getDiagramClass().getItemsByClass(SubstituteElementClass.class);
118                                 for (SubstituteElementClass subs : substitutes) {
119                                     mutableClazz = subs.substitute(diagram, mutableClazz);
120                                 }
121                                 final ElementClass clazz = mutableClazz;
122                                 graph.asyncRequest(new SpawnRequest(canvas, clazz, data), new TransientCacheAsyncListener<IElement>() {
123
124                                     @Override
125                                     public void exception(AsyncReadGraph graph, Throwable throwable) {
126                                         LOGGER.error("Unexpected error in SpawnRequest", throwable);
127                                         procedure.execute(graph, null);
128                                     }
129
130                                     @Override
131                                     public void execute(AsyncReadGraph graph, IElement element) {
132                                         procedure.execute(graph, element);
133
134                                         if (loadListener != null) {
135                                             //System.out.println("LoadRequest[" + (loadCounter++) + "] for " + data + ": " + element);
136                                             graph.asyncRequest(new LoadRequest(canvas, diagram, factory, clazz, data), loadListener);
137                                         } else {
138                                             //System.out.println("Spawn[" + (spawnCounter++) + "] for " + data + ": " + element);
139                                             factory.load(graph, canvas, diagram, data, element, new AsyncProcedure<IElement>() {
140                                                 @Override
141                                                 public void exception(AsyncReadGraph graph, Throwable throwable) {
142                                                     LOGGER.error("Unexpected error in ElementFactory.load (factory=" + factory + ")", throwable);
143                                                 }
144                                                 @Override
145                                                 public void execute(AsyncReadGraph graph, IElement result) {
146                                                     // Loading complete, don't care.
147                                                 }
148                                             });
149                                         }
150                                     }
151
152                                 });
153
154                             }
155
156                         });
157
158 //                        graph.asyncRequest(new SafeName(data), new Procedure<String>() {
159 //                            @Override
160 //                            public void exception(Throwable t) {
161 //                            }
162 //                            @Override
163 //                            public void execute(String result) {
164 //                                System.out.println("NodeRequest2 factory "  + result + " " + factory.getClass().getName());
165 //                            }
166 //                        });
167
168                     }
169
170                 });
171
172             }
173
174         });
175
176     }
177
178     static int loadCounter = 0;
179     static int spawnCounter = 0;
180
181 }