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