]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/property/Properties.java
Fail safe import fixes made by Antti
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / property / Properties.java
1 package org.simantics.db.layer0.property;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5
6 import org.simantics.databoard.Bindings;
7 import org.simantics.databoard.binding.Binding;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.Statement;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.operation.Layer0X;
13
14 /**
15  * @author Antti Villberg
16  * TODO: consider removing since this doesn't seem to be used.
17  */
18 public class Properties {
19         
20         public static <T> T getPossibleRelatedValue(ReadGraph graph, Resource resource, Resource relation, Binding binding) throws DatabaseException {
21                 
22                 T result = graph.getPossibleRelatedValue(resource, relation, binding);
23                 if(result != null) return result;
24                 else {
25                         
26                     Layer0X L0X = Layer0X.getInstance(graph);
27                         ArrayList<OrderedResource> order = new ArrayList<OrderedResource>();
28                         for(Statement stm : graph.getStatements(resource, L0X.ObtainsProperty)) {
29                                 Integer position = graph.getRelatedValue(stm.getPredicate(), L0X.NaturalNumberOrderRelation, Bindings.INTEGER);
30                                 order.add(new OrderedResource(position, stm.getObject()));
31                         }
32                         Collections.sort(order);
33                         for(OrderedResource or : order) {
34                                 result = getPossibleRelatedValue(graph, or.r, relation, binding);
35                                 if(result != null) return result;
36                         }
37                         
38                         return null;
39                         
40                 }
41                 
42         }
43
44         public static <T> T getPossibleRelatedAdapter(ReadGraph graph, Resource resource, Resource relation, Class<T> clazz) throws DatabaseException {
45
46                 T result = graph.getPossibleRelatedAdapter(resource, relation, clazz);
47                 if(result != null) return result;
48                 else {
49                         
50                     Layer0X L0X = Layer0X.getInstance(graph);
51                         ArrayList<OrderedResource> order = new ArrayList<OrderedResource>();
52                         for(Statement stm : graph.getStatements(resource, L0X.ObtainsProperty)) {
53                                 Integer position = graph.getRelatedValue(stm.getPredicate(), L0X.NaturalNumberOrderRelation, Bindings.INTEGER);
54                                 order.add(new OrderedResource(position, stm.getObject()));
55                         }
56                         Collections.sort(order);
57                         for(OrderedResource or : order) {
58                                 result = getPossibleRelatedAdapter(graph, or.r, relation, clazz);
59                                 if(result != null) return result;
60                         }
61                         
62                         return null;
63                         
64                 }
65                 
66         }
67
68 }