1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.charts.query;
14 import java.util.Arrays;
16 import org.simantics.charts.ontology.ChartResource;
17 import org.simantics.databoard.Bindings;
18 import org.simantics.databoard.Databoard;
19 import org.simantics.databoard.binding.Binding;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.common.request.ResourceRead;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.layer0.request.PossibleModel;
25 import org.simantics.db.layer0.variable.RVI;
26 import org.simantics.db.layer0.variable.Variable;
27 import org.simantics.db.layer0.variable.Variables;
28 import org.simantics.layer0.Layer0;
29 import org.simantics.modeling.ModelingResources;
30 import org.simantics.modeling.subscription.SubscriptionsQuery;
31 import org.simantics.modeling.subscription.SubscriptionsQuery.SubscriptionsResult;
32 import org.simantics.trend.configuration.TrendItem;
33 import org.simantics.trend.configuration.TrendItem.Renderer;
36 * This query reads chart and item data.
38 * @author toni.kalajainen
40 public class ChartAndSubscriptionItemReadQuery extends ResourceRead<ChartAndSubscriptionItemData> {
42 public ChartAndSubscriptionItemReadQuery(Resource chartItem) {
47 public ChartAndSubscriptionItemData perform(ReadGraph g) throws DatabaseException {
48 ChartAndSubscriptionItemData data = new ChartAndSubscriptionItemData();
50 Layer0 L0 = Layer0.getInstance(g);
51 ModelingResources MOD = ModelingResources.getInstance(g);
52 ChartResource CHART = ChartResource.getInstance(g);
53 Resource chartItem = resource;
54 Resource chart = g.getPossibleObject(chartItem, L0.PartOf);
55 Resource model = g.syncRequest(new PossibleModel( chart ));
56 Resource subscriptionItem = g.getPossibleObject(chartItem, CHART.Chart_Item_HasSubscriptionItem);
57 Resource subscription = subscriptionItem==null ? null : g.getPossibleObject(subscriptionItem, L0.PartOf);
58 SubscriptionsResult subscriptions = g.sync( new SubscriptionsQuery(model) );
60 TrendItem trendItem = g.sync( new TrendItemQuery(chartItem) );
62 if ( subscriptionItem != null ) {
63 Variable configuration = Variables.getConfigurationContext(g, subscription);
64 Binding rviBinding = g.getService(Databoard.class).getBindingUnchecked( RVI.class );
65 RVI rvi = g.getRelatedValue(subscriptionItem, MOD.Subscription_Item_VariableId, rviBinding);
66 data.variableReference = rvi.toPossibleString(g, configuration);
69 data.index = trendItem.index;
70 data.chartName = g.getPossibleRelatedValue(chart, L0.HasName, Bindings.STRING);
71 data.subscriptions = subscriptions.toNames();
72 data.subscriptions = subscriptions.names.toArray( new String[subscriptions.names.size()]);
73 data.drawmode = trendItem.drawMode;
74 data.binaryMode = trendItem.renderer == Renderer.Binary;
75 data.subscription = subscriptions.getName(subscription);
76 data.scale = trendItem.scale;
77 if ( data.subscription==null ) data.subscription="";
79 data.min = g.getPossibleRelatedValue(chartItem, CHART.Chart_Item_ScaleMode_Min, Bindings.DOUBLE);
80 data.max = g.getPossibleRelatedValue(chartItem, CHART.Chart_Item_ScaleMode_Max, Bindings.DOUBLE);
82 data.strokeWidth = trendItem.customStrokeWidth;
83 data.color = trendItem.customColor != null ? Arrays.copyOf(trendItem.customColor, trendItem.customColor.length) : null;
85 data.hasSubscriptionItem = subscriptionItem != null;
86 if ( data.hasSubscriptionItem ) {
87 data.label = g.getPossibleRelatedValue(subscriptionItem, L0.HasLabel, Bindings.STRING);
88 data.bias = g.getPossibleRelatedValue(subscriptionItem, MOD.Subscription_Item_Bias, Bindings.DOUBLE);
89 data.gain = g.getPossibleRelatedValue(subscriptionItem, MOD.Subscription_Item_Gain, Bindings.DOUBLE);
90 data.deadband = g.getPossibleRelatedValue(subscriptionItem, MOD.Subscription_Item_Deadband, Bindings.DOUBLE);
91 data.interval = g.getPossibleRelatedValue(subscriptionItem, MOD.Subscription_Item_SamplingInterval, Bindings.DOUBLE);
92 data.unit = g.getPossibleRelatedValue(subscriptionItem, MOD.Subscription_Item_Unit, Bindings.STRING);
94 data.variableReference = trendItem.label;