]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/request/ObjectsWithTypeAsync.java
Automatically import also SCLMain modules of dependent index roots
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / request / ObjectsWithTypeAsync.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.Collection;
15 import java.util.Set;
16 import java.util.concurrent.ConcurrentLinkedQueue;
17
18 import org.simantics.db.AsyncReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.common.ProcedureBarrier;
21 import org.simantics.db.common.utils.Logger;
22 import org.simantics.db.procedure.AsyncMultiProcedure;
23 import org.simantics.db.procedure.AsyncProcedure;
24
25 final public class ObjectsWithTypeAsync extends ResourceAsyncRead3<Collection<Resource>> {
26
27     public ObjectsWithTypeAsync(Resource subject, Resource subrelationOf, Resource instanceOf) {
28         super(subject, subrelationOf, instanceOf);
29     }
30
31     @Override
32     public void perform(AsyncReadGraph graph, final AsyncProcedure<Collection<Resource>> procedure) {
33
34         final Collection<Resource> result = new ConcurrentLinkedQueue<Resource>();
35         final ProcedureBarrier<Collection<Resource>> ready = new ProcedureBarrier<Collection<Resource>>(1);
36         
37         graph.forEachObject(resource, resource2, new AsyncMultiProcedure<Resource>() {
38
39                         @Override
40                         public void execute(AsyncReadGraph graph, final Resource object) {
41                                 
42                                 ready.incrementAndGet();
43                                 
44                                 graph.forTypes(object, new AsyncProcedure<Set<Resource>>() {
45                                         
46                                         @Override
47                                         public void execute(AsyncReadGraph graph, Set<Resource> types) {
48
49                                                 if(types.contains(resource3)) {
50                                                         result.add(object);
51                                                 }
52                                                 
53                                                 ready.dec(graph, procedure, result);
54                                                 
55                                         }
56                                         
57                                         @Override
58                                         public void exception(AsyncReadGraph graph, Throwable throwable) {
59                                                 
60                                                 throwable.printStackTrace();
61                                         Logger.defaultLogError(throwable);
62                                                 
63                                                 ready.except(throwable);
64                                                 ready.dec(graph, procedure, result);
65                                                 
66                                         }
67                                         
68                                 });
69                                 
70                         }
71
72                         @Override
73                         public void finished(AsyncReadGraph graph) {
74                                 
75                                 ready.dec(graph, procedure, result);
76                                 
77                         }
78
79                         @Override
80                         public void exception(AsyncReadGraph graph, Throwable throwable) {
81                                 
82                                 throwable.printStackTrace();
83                         Logger.defaultLogError(throwable);
84                                 
85                                 ready.except(throwable);
86                                 ready.dec(graph, procedure, result);
87                                 
88                         }
89                 
90         });
91         
92     }
93     
94 //    @Override
95 //    public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
96 //        
97 //      Set<Resource> result = new HashSet<Resource>();
98 //      for(Resource object : graph.getObjects(resource, resource2)) {
99 //              if(graph.isInstanceOf(object, resource3)) result.add(object);
100 //      }
101 //      return result;
102 //        
103 //    }
104
105 }