]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/request/MappedRelated.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / request / MappedRelated.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.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.operation.Layer0X;
21
22 /**
23  * Given an input resource that consists of resources representing other
24  * resources, return a map: represented resource -> representing resource.
25  * 
26  * @author Antti Villberg
27  */
28 public class MappedRelated extends ResourceRead2<Map<Resource, Resource>> {
29
30     public MappedRelated(Resource flat, Resource relation) {
31         super(flat, relation);
32     }
33
34     @Override
35     public Map<Resource, Resource> perform(ReadGraph graph) throws DatabaseException {
36
37         Layer0X L0X = Layer0X.getInstance(graph);
38         Map<Resource, Resource> result = new HashMap<Resource, Resource>();
39
40         for (Resource part : graph.getObjects(resource, resource2)) {
41             Resource represents = graph.getPossibleObject(part, L0X.Represents);
42             if (represents != null)
43                 result.put(represents, part);
44         }
45
46         return result;
47
48     }
49
50 }