Contextual adapters must throw instead of returning null
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagram / monitor / ObtainedExpressionAdapter.java
1 package org.simantics.modeling.ui.diagram.monitor;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5
6 import org.simantics.databoard.Bindings;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.RelationContext;
9 import org.simantics.db.Resource;
10 import org.simantics.db.Statement;
11 import org.simantics.db.common.adaption.SimpleContextualAdapter;
12 import org.simantics.db.exception.AdaptionException;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.layer0.property.OrderedResource;
15 import org.simantics.operation.Layer0X;
16
17 public class ObtainedExpressionAdapter extends SimpleContextualAdapter<String, RelationContext> {
18
19         @Override
20         public String adapt(ReadGraph graph, Resource source, RelationContext context) throws DatabaseException {
21                 
22                 Layer0X L0X = Layer0X.getInstance(graph);
23                 ArrayList<OrderedResource> order = new ArrayList<OrderedResource>();
24                 for(Statement stm : graph.getStatements(context.getSubject(), L0X.ObtainsProperty)) {
25                         Integer position = graph.getRelatedValue(stm.getPredicate(), L0X.NaturalNumberOrderRelation, Bindings.INTEGER);
26                         order.add(new OrderedResource(position, stm.getObject()));
27                 }
28                 if (order.size() > 1)
29                         Collections.sort(order);
30                 for(OrderedResource or : order) {
31                         String result = graph.getPossibleRelatedAdapter(or.r, L0X.HasExpression, String.class);
32                         if(result != null) return result;
33                 }
34                 
35                 throw new AdaptionException("No obtained expression available");
36                 
37         }
38
39 }