]> gerrit.simantics Code Review - simantics/platform.git/blob - monitor/ObtainedExpressionAdapter.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / monitor / ObtainedExpressionAdapter.java
1 package org.simantics.modeling.ui.diagram.monitor;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collections;\r
5 \r
6 import org.simantics.databoard.Bindings;\r
7 import org.simantics.db.ReadGraph;\r
8 import org.simantics.db.RelationContext;\r
9 import org.simantics.db.Resource;\r
10 import org.simantics.db.Statement;\r
11 import org.simantics.db.common.adaption.SimpleContextualAdapter;\r
12 import org.simantics.db.exception.DatabaseException;\r
13 import org.simantics.db.layer0.property.OrderedResource;\r
14 import org.simantics.operation.Layer0X;\r
15 \r
16 public class ObtainedExpressionAdapter extends SimpleContextualAdapter<String, RelationContext> {\r
17 \r
18         @Override\r
19         public String adapt(ReadGraph graph, Resource source, RelationContext context) throws DatabaseException {\r
20                 \r
21                 Layer0X L0X = Layer0X.getInstance(graph);\r
22                 ArrayList<OrderedResource> order = new ArrayList<OrderedResource>();\r
23                 for(Statement stm : graph.getStatements(context.getSubject(), L0X.ObtainsProperty)) {\r
24                         Integer position = graph.getRelatedValue(stm.getPredicate(), L0X.NaturalNumberOrderRelation, Bindings.INTEGER);\r
25                         order.add(new OrderedResource(position, stm.getObject()));\r
26                 }\r
27                 if (order.size() > 1)\r
28                         Collections.sort(order);\r
29                 for(OrderedResource or : order) {\r
30                         String result = graph.getPossibleRelatedAdapter(or.r, L0X.HasExpression, String.class);\r
31                         if(result != null) return result;\r
32                 }\r
33                 \r
34                 return null;\r
35                 \r
36         }\r
37 \r
38 }\r