]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/query/ChartAndSubscriptionItemReadQuery.java
Statements request is missing dec
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / query / ChartAndSubscriptionItemReadQuery.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.charts.query;
13
14 import java.util.Arrays;
15
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;
34
35 /**
36  * This query reads chart and item data. 
37  *  
38  * @author toni.kalajainen
39  */
40 public class ChartAndSubscriptionItemReadQuery extends ResourceRead<ChartAndSubscriptionItemData> {
41
42         public ChartAndSubscriptionItemReadQuery(Resource chartItem) {
43                 super(chartItem);
44         }
45
46         @Override
47         public ChartAndSubscriptionItemData perform(ReadGraph g) throws DatabaseException {
48                 ChartAndSubscriptionItemData data = new ChartAndSubscriptionItemData();
49
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) );
59
60                 TrendItem trendItem = g.sync( new TrendItemQuery(chartItem) );
61
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);
67                 }
68
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="";
78
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);
81
82         data.strokeWidth = trendItem.customStrokeWidth;
83         data.color = trendItem.customColor != null ? Arrays.copyOf(trendItem.customColor, trendItem.customColor.length) : null;
84
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);
93         } else {
94                 data.variableReference = trendItem.label;
95         }
96
97                 return data;
98         }
99
100 }