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