Sync git svn branch with SVN repository r33269.
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / Charts.java
1 package org.simantics.charts;\r
2 \r
3 import java.util.Collections;\r
4 import java.util.List;\r
5 \r
6 import org.simantics.charts.editor.ChartData;\r
7 import org.simantics.charts.editor.ChartKeys;\r
8 import org.simantics.databoard.binding.error.BindingException;\r
9 import org.simantics.databoard.util.Bean;\r
10 import org.simantics.db.ReadGraph;\r
11 import org.simantics.db.Resource;\r
12 import org.simantics.db.common.request.PossibleIndexRoot;\r
13 import org.simantics.db.exception.DatabaseException;\r
14 import org.simantics.db.layer0.variable.Variable;\r
15 import org.simantics.history.HistoryException;\r
16 import org.simantics.history.HistorySamplerItem;\r
17 import org.simantics.history.ItemManager;\r
18 import org.simantics.history.util.subscription.SamplingFormat;\r
19 import org.simantics.modeling.subscription.SubscriptionItem;\r
20 import org.simantics.modeling.subscription.SubscriptionItemQuery;\r
21 import org.simantics.project.IProject;\r
22 import org.simantics.simulation.experiment.IExperiment;\r
23 \r
24 /**\r
25  * Main facade for externally dealing with the trending system.\r
26  * \r
27  * @author Tuukka Lehtonen\r
28  * @author Antti Villberg\r
29  * \r
30  */\r
31 public final class Charts {\r
32 \r
33     public static void resetChartEditorData(IProject project, Resource model, ChartData editorData) {\r
34         if (editorData != null) {\r
35             project.setHint(ChartKeys.chartSourceKey(model), editorData);\r
36         } else {\r
37             project.removeHint(ChartKeys.chartSourceKey(model));\r
38         }\r
39     }\r
40 \r
41         public static HistorySamplerItem createHistorySamplerItem(ReadGraph graph, Variable run, Resource subscriptionItem) throws DatabaseException {\r
42                         IExperiment exp = (IExperiment) run.getPropertyValue(graph, "iExperiment");\r
43                         ITrendSupport support = exp.getService(ITrendSupport.class);\r
44                         ChartData data = support.getChartData();\r
45                         return createHistorySamplerItem(graph, subscriptionItem, data);\r
46         }\r
47 \r
48     public static HistorySamplerItem createHistorySamplerItem(ReadGraph graph, Resource subscriptionItem, ChartData data) throws DatabaseException {\r
49 \r
50         try {\r
51             Resource model = graph.syncRequest(new PossibleIndexRoot(subscriptionItem));\r
52             if (model == null) {\r
53                 throw new DatabaseException("There is no model for " + subscriptionItem);\r
54             }\r
55 \r
56             ItemManager im = new ItemManager(data.history.getItems());\r
57 \r
58             SubscriptionItem i = graph.syncRequest(new SubscriptionItemQuery(subscriptionItem));\r
59 \r
60             List<Bean> items = im.search("variableId", i.variableId);\r
61             Collections.sort(items, SamplingFormat.INTERVAL_COMPARATOR);\r
62             if (items.isEmpty())\r
63                 new DatabaseException("There is history item for " + i.variableId);\r
64             Bean config = items.get(0);\r
65             String historyId = (String) config.getFieldUnchecked("id");\r
66 \r
67             return new HistorySamplerItem(data.collector, data.history, historyId, System.identityHashCode(data));\r
68         } catch (HistoryException e) {\r
69             throw new DatabaseException(e);\r
70         } catch (BindingException e) {\r
71             throw new DatabaseException(e);\r
72         }\r
73     }\r
74 \r
75 }\r