]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/chart/property/ObtainedDoubleAdapter.java
ff257169ca01e1272b57e62ff1dd259d82aa6601
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / chart / property / ObtainedDoubleAdapter.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.chart.property;
13
14 import java.util.ArrayList;
15 import java.util.Collections;
16
17 import org.simantics.databoard.Bindings;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.RelationContext;
20 import org.simantics.db.Resource;
21 import org.simantics.db.Statement;
22 import org.simantics.db.common.adaption.SimpleContextualAdapter;
23 import org.simantics.db.exception.AdaptionException;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.property.OrderedResource;
26 import org.simantics.operation.Layer0X;
27
28 /**
29  * @author Tuukka Lehtonen
30  */
31 public class ObtainedDoubleAdapter extends SimpleContextualAdapter<Double, RelationContext> {
32
33     @Override
34     public Double adapt(ReadGraph graph, Resource source, RelationContext context) throws DatabaseException {
35         Layer0X L0X = Layer0X.getInstance(graph);
36         ArrayList<OrderedResource> order = new ArrayList<OrderedResource>();
37         for (Statement stm : graph.getStatements(context.getSubject(), L0X.ObtainsProperty)) {
38             Integer position = graph.getRelatedValue(stm.getPredicate(), L0X.NaturalNumberOrderRelation,
39                     Bindings.INTEGER);
40             order.add(new OrderedResource(position, stm.getObject()));
41         }
42         if (order.size() > 1)
43             Collections.sort(order);
44         for (OrderedResource or : order) {
45             Double result = graph.getPossibleRelatedAdapter(or.r, context.getStatement().getPredicate(), Double.class);
46             if (result != null)
47                 return result;
48         }
49
50         throw new AdaptionException("No obtained double available");
51     }
52
53 }