package org.simantics.charts; import java.util.Collections; import java.util.List; import org.simantics.Simantics; import org.simantics.charts.editor.ChartData; import org.simantics.charts.editor.ChartKeys; import org.simantics.databoard.binding.error.BindingException; import org.simantics.databoard.util.Bean; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.request.PossibleIndexRoot; import org.simantics.db.exception.DatabaseException; import org.simantics.history.HistoryException; import org.simantics.history.HistorySamplerItem; import org.simantics.history.ItemManager; import org.simantics.history.util.subscription.SamplingFormat; import org.simantics.modeling.subscription.SubscriptionItem; import org.simantics.modeling.subscription.SubscriptionItemQuery; import org.simantics.project.IProject; import org.simantics.utils.datastructures.hints.IHintContext.Key; /** * Main facade for externally dealing with the trending system. * * @author Tuukka Lehtonen * @author Antti Villberg * */ public final class Charts { public static void resetChartEditorData(IProject project, Resource model, ChartData editorData) { if (editorData != null) { project.setHint(ChartKeys.chartSourceKey(model), editorData); } else { project.removeHint(ChartKeys.chartSourceKey(model)); } } public static HistorySamplerItem createHistorySamplerItem(ReadGraph graph, Resource subscriptionItem) throws DatabaseException { try { Resource model = graph.syncRequest(new PossibleIndexRoot(subscriptionItem)); if ( model == null ) { throw new DatabaseException("There is no model for " + subscriptionItem); } Key chartDataKey = ChartKeys.chartSourceKey(model); final ChartData data = Simantics.getProject().getHint(chartDataKey); if ( data == null ) { throw new DatabaseException("There is no chart data for " + model); } ItemManager im = new ItemManager( data.history.getItems() ); SubscriptionItem i = graph.syncRequest(new SubscriptionItemQuery(subscriptionItem)); List items = im.search("variableId", i.variableId); Collections.sort(items, SamplingFormat.INTERVAL_COMPARATOR); if (items.isEmpty()) new DatabaseException("There is history item for " + i.variableId); Bean config = items.get(0); String historyId = (String) config.getFieldUnchecked("id"); return new HistorySamplerItem(data.collector, data.history, historyId, System.identityHashCode(data)); } catch (HistoryException e) { throw new DatabaseException(e); } catch (BindingException e) { throw new DatabaseException(e); } } }