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