]> gerrit.simantics Code Review - simantics/platform.git/blob
79f32fcd2fee1e144bdd89e25e4317ecfdfc5ff7
[simantics/platform.git] /
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.DatabaseException;
24 import org.simantics.db.layer0.property.OrderedResource;
25 import org.simantics.operation.Layer0X;
26
27 /**
28  * @author Tuukka Lehtonen
29  */
30 public class ObtainedDoubleAdapter extends SimpleContextualAdapter<Double, RelationContext> {
31
32     @Override
33     public Double adapt(ReadGraph graph, Resource source, RelationContext context) throws DatabaseException {
34         Layer0X L0X = Layer0X.getInstance(graph);
35         ArrayList<OrderedResource> order = new ArrayList<OrderedResource>();
36         for (Statement stm : graph.getStatements(context.getSubject(), L0X.ObtainsProperty)) {
37             Integer position = graph.getRelatedValue(stm.getPredicate(), L0X.NaturalNumberOrderRelation,
38                     Bindings.INTEGER);
39             order.add(new OrderedResource(position, stm.getObject()));
40         }
41         if (order.size() > 1)
42             Collections.sort(order);
43         for (OrderedResource or : order) {
44             Double result = graph.getPossibleRelatedAdapter(or.r, context.getStatement().getPredicate(), Double.class);
45             if (result != null)
46                 return result;
47         }
48
49         return null;
50     }
51
52 }