]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/scl/FindComponentTypeRequest.java
Merge "Remove unnecessary getComparableKey from HashMapBinding"
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / scl / FindComponentTypeRequest.java
1 package org.simantics.structural2.scl;
2
3 import org.simantics.db.ReadGraph;
4 import org.simantics.db.Resource;
5 import org.simantics.db.common.request.ResourceRead;
6 import org.simantics.db.exception.DatabaseException;
7 import org.simantics.layer0.Layer0;
8 import org.simantics.structural.stubs.StructuralResource2;
9 import org.simantics.utils.datastructures.Pair;
10
11 /**
12  * Given component finds a component type it belongs to
13  */
14 public class FindComponentTypeRequest extends ResourceRead<Pair<Resource,Integer>> {
15
16         public FindComponentTypeRequest(Resource resource) {
17                 super(resource);
18         }
19
20         @Override
21         public Pair<Resource,Integer> perform(ReadGraph graph) throws DatabaseException {               
22                 StructuralResource2 STR = StructuralResource2.getInstance(graph);               
23                 Resource type = graph.getPossibleObject(resource, STR.Defines);
24                 if(type != null)
25                         return Pair.make(type, 0);              
26
27                 Layer0 L0 = Layer0.getInstance(graph);
28                 Resource parent = graph.getPossibleObject(resource, L0.PartOf);
29                 if(parent != null) {
30                     Pair<Resource,Integer> result = 
31                             graph.syncRequest(new FindComponentTypeRequest(parent));
32                         return Pair.make(result.first, result.second+1);
33                 }
34
35                 throw new DatabaseException("Unexpected graph structure at " + resource + ".");
36         }
37
38 }