]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/query/TrendSpecQuery.java
Statements request is missing dec
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / query / TrendSpecQuery.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management
3  * in 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.UUID;
15
16 import org.simantics.charts.ontology.ChartResource;
17 import org.simantics.databoard.Bindings;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.common.request.BinaryRead;
21 import org.simantics.db.common.request.ObjectsWithType;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.trend.configuration.TrendItem;
25 import org.simantics.trend.configuration.TrendSpec;
26 import org.simantics.trend.configuration.ViewProfile;
27 import org.simantics.trend.configuration.YAxisMode;
28
29 /**
30  * This query reads trend configuration (Resource) and returns TrendSpec.
31  * 
32  * @author Tuukka Lehtonen
33  */
34 public class TrendSpecQuery extends BinaryRead<UUID, Resource, TrendSpec> {
35
36     public TrendSpecQuery(UUID id, Resource resource) {
37         super(id, resource);
38     }
39
40     @Override
41     public TrendSpec perform(ReadGraph graph) throws DatabaseException {
42         Resource chart = parameter2;
43
44         ChartResource CHART = ChartResource.getInstance(graph);
45         Layer0 L0 = Layer0.getInstance(graph);
46         
47         TrendSpec spec = new TrendSpec();
48         spec.init();
49         if ( !graph.hasStatement(chart) ) return spec;
50         spec.name = graph.getPossibleRelatedValue(chart, L0.HasName, Bindings.STRING);
51         if (spec.name == null)
52             spec.name = "Unnamed Chart";
53
54         spec.axisMode = YAxisMode.MultiAxis;
55         Resource axisMode = graph.getPossibleObject(chart, CHART.Chart_YAxisMode);
56         if (CHART.YAxisMode_SingleAxis.equals(axisMode))
57             spec.axisMode = YAxisMode.SingleAxis;
58         else if (CHART.YAxisMode_MultiAxis.equals(axisMode))
59             spec.axisMode = YAxisMode.MultiAxis;
60         
61         readProfile(graph, chart, spec.viewProfile);
62
63         for (Resource plot : graph.syncRequest(new ObjectsWithType(chart, L0.ConsistsOf, CHART.Chart_Item))) {
64                 TrendItem item = graph.sync( new TrendItemQuery(plot) );
65 //              if ( item.subscriptionId.equals("") ) continue;
66             spec.items.add( item );
67         }
68
69         spec.sortItems();
70         return spec;
71     }
72
73     private void readProfile(ReadGraph graph, Resource profile, ViewProfile viewProfile) throws DatabaseException {
74         viewProfile.profileName = "Custom Profile";
75         viewProfile.showMilestones = false;
76 //        viewProfile.timeWindow.timeWindowStart = 0.0;
77 //        viewProfile.timeWindow.timeWindowLength = 60.0;
78         viewProfile.timeWindow.timeWindowIncrement = 50.0;
79
80         if (profile != null) {
81             Layer0 L0 = Layer0.getInstance(graph);
82             ChartResource CHART = ChartResource.getInstance(graph);
83             String pn = graph.getPossibleRelatedValue(profile, L0.HasName);
84             if (pn != null)
85                 viewProfile.profileName = pn;
86
87             viewProfile.showMilestones = Boolean.TRUE.equals( graph.getPossibleRelatedValue(profile, CHART.Chart_ShowMilestones, Bindings.BOOLEAN) );
88             viewProfile.showGrid       = Boolean.TRUE.equals( graph.getPossibleRelatedValue(profile, CHART.Chart_showGrid, Bindings.BOOLEAN) );
89
90             Double windowStart = graph.getPossibleRelatedAdapter(profile, CHART.Chart_TimeWindowStart, Double.class);
91             if (windowStart != null && !Double.isNaN(windowStart))
92                 viewProfile.timeWindow.timeWindowStart = windowStart;
93             Double windowLength = graph.getPossibleRelatedAdapter(profile, CHART.Chart_TimeWindowLength, Double.class);
94             if (windowLength != null)
95                 viewProfile.timeWindow.timeWindowLength = windowLength;
96             Double windowIncrement = graph.getPossibleRelatedAdapter(profile, CHART.Chart_TimeWindowIncrement, Double.class);
97             if (windowIncrement != null)
98                 viewProfile.timeWindow.timeWindowIncrement = windowIncrement;
99
100             Boolean trackExperimentTime = graph.getPossibleRelatedValue(profile, CHART.Chart_trackExperimentTime, Bindings.BOOLEAN);
101             if (trackExperimentTime != null)
102                 viewProfile.trackExperimentTime = trackExperimentTime;
103             double[] valueViewPosition = graph.getPossibleRelatedValue(profile, CHART.Chart_valueViewPosition, Bindings.DOUBLE_ARRAY);
104             if (valueViewPosition != null && valueViewPosition.length >= 2) {
105                 viewProfile.valueViewPositionX = valueViewPosition[0];
106                 viewProfile.valueViewPositionY = valueViewPosition[1];
107             }
108
109             float[] bgColor = graph.getPossibleRelatedValue(profile, CHART.Chart_backgroundColor, Bindings.FLOAT_ARRAY);
110             if (bgColor != null && bgColor.length >= 3) {
111                 viewProfile.backgroundColor = bgColor;
112             }
113
114             float[] gridColor = graph.getPossibleRelatedValue(profile, CHART.Chart_gridColor, Bindings.FLOAT_ARRAY);
115             if (gridColor != null && gridColor.length >= 3) {
116                 viewProfile.gridColor = gridColor;
117             }
118         }
119     }
120
121 }