]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/primitiverequest/ForEachAssertedObject.java
First changes
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / primitiverequest / ForEachAssertedObject.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.primitiverequest;
13
14 import java.util.Collection;
15
16 import org.simantics.db.AsyncReadGraph;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.request.ResourceRead2;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.procedure.AsyncMultiProcedure;
22 import org.simantics.db.service.CollectionSupport;
23 import org.simantics.utils.DataContainer;
24
25 final public class ForEachAssertedObject extends ResourceRead2<Collection<Resource>> {
26
27     public ForEachAssertedObject(Resource subject, Resource relation) {
28         super(subject, relation);
29     }
30
31         @Override
32         public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
33                 CollectionSupport cs = graph.getService(CollectionSupport.class);
34                 Collection<Resource> result = cs.createSet();
35                 DataContainer<Throwable> throwable = new DataContainer<Throwable>(null);
36                 graph.forEachAssertedObject(resource, resource2, new AsyncMultiProcedure<Resource>() {
37                         
38                         @Override
39                         public void finished(AsyncReadGraph graph) {
40                         }
41                         
42                         @Override
43                         public void execute(AsyncReadGraph graph, Resource r) {
44                                 result.add(r);
45                         }
46                         
47                         @Override
48                         public void exception(AsyncReadGraph graph, Throwable t) {
49                                 throwable.set(t);
50                         }
51                 });
52                 Throwable t = throwable.get();
53                 if(t != null)
54                         if(t instanceof DatabaseException)
55                                 throw (DatabaseException)t;
56                         else throw new DatabaseException(t);
57                 return result;
58         }
59
60 }