]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/request/MappedPartsAsync.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / request / MappedPartsAsync.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.db.common.request;
13
14 import java.util.HashMap;
15 import java.util.Map;
16
17 import org.simantics.db.AsyncReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.ProcedureBarrier;
20 import org.simantics.db.procedure.AsyncMultiProcedure;
21 import org.simantics.db.procedure.AsyncProcedure;
22 import org.simantics.layer0.Layer0;
23 import org.simantics.operation.Layer0X;
24
25 public class MappedPartsAsync extends ResourceAsyncRead<Map<Resource, Resource>> {
26
27     public MappedPartsAsync(Resource realization) {
28         super(realization);
29     }
30     
31     @Override
32     public void perform(AsyncReadGraph graph, final AsyncProcedure<Map<Resource, Resource>> procedure) {
33
34         final Layer0 L0 = graph.getService(Layer0.class);
35         final Layer0X L0X = graph.getService(Layer0X.class);
36         final HashMap<Resource, Resource> result = new HashMap<Resource, Resource>();
37         final ProcedureBarrier<Map<Resource, Resource>> ready = new ProcedureBarrier<Map<Resource, Resource>>(1);
38         
39         graph.forEachObject(resource, L0.ConsistsOf, new AsyncMultiProcedure<Resource>() {
40
41                         @Override
42                         public void execute(AsyncReadGraph graph, final Resource object) {
43                                 
44                                 ready.incrementAndGet();
45                                 
46                                 graph.forEachObject(object, L0X.Represents, new AsyncMultiProcedure<Resource>() {
47
48                                         @Override
49                                         public void execute(AsyncReadGraph graph, Resource mapped) {
50
51                                                 synchronized(result) {
52                                                         result.put(mapped, object);
53                                                 }
54
55                                         }
56
57                                         @Override
58                                         public void finished(AsyncReadGraph graph) {
59
60                                                 ready.dec(graph, procedure, result);
61
62                                         }
63
64                                         @Override
65                                         public void exception(AsyncReadGraph graph, Throwable throwable) {
66
67                                                 ready.except(throwable);
68                                                 ready.dec(graph, procedure, result);
69
70                                         }
71
72                                 });
73                                 
74                         }
75
76                         @Override
77                         public void finished(AsyncReadGraph graph) {
78                                 
79                                 ready.dec(graph, procedure, result);
80                                 
81                         }
82
83                         @Override
84                         public void exception(AsyncReadGraph graph, Throwable throwable) {
85                                 
86                                 ready.except(throwable);
87                                 ready.dec(graph, procedure, result);
88                                 
89                         }
90                 
91         });
92         
93     }
94     
95 //    @Override
96 //    public Map<Resource, Resource> perform(ReadGraph graph) throws DatabaseException {
97 //        
98 //        Builtins b = graph.getBuiltins();
99 //        Map<Resource, Resource> result = new HashMap<Resource, Resource>();
100 //        
101 //        for(Resource part : graph.syncRequest(new ObjectsWithType(resource, b.ConsistsOf, resource2))) {
102 //              Resource represents = graph.getPossibleObject(part, b.Represents);
103 //              if(represents != null) result.put(represents, part);
104 //        }
105 //        
106 //        return result;
107 //        
108 //    }
109
110 }