--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2013 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.subscription;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.List;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.VariableReference;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ModelingResources;\r
+\r
+/**\r
+ * @author Jani Simomaa <jani.simomaa@semantum.fi>\r
+ */\r
+public class SCLSubscription {\r
+ \r
+ /**\r
+ * Creates new folder under given models subscription folder\r
+ * \r
+ * @param model resource of the target model\r
+ * @param name name of the folder\r
+ * @return resource of the folder\r
+ * @throws DatabaseException on database failures\r
+ */\r
+ public static Resource addSubscriptionFolder (WriteGraph graph, Resource model, String name) throws DatabaseException {\r
+ \r
+ NewSubscription folder = new NewSubscription(model, name);\r
+ folder.perform(graph);\r
+ return folder.subscription;\r
+ }\r
+ \r
+ /**\r
+ * Adds subscription item with default plotting values to the given subscription resource (folder). \r
+ * \r
+ * @param subscriptionresource resource of the subscription folder\r
+ * @param variable variable of the item to be created\r
+ * @return resource of the subscription item\r
+ * @throws DatabaseException on database failures\r
+ */\r
+ public static Resource addSubscriptionItems (WriteGraph graph, Resource subscriptionresource, Variable variable) throws DatabaseException {\r
+ \r
+ VariableReference ref = new VariableReference(variable.getRVI(graph), variable.getDatatype(graph), null);\r
+ List<VariableReference> references = new ArrayList<VariableReference>(1);\r
+ references.add(ref);\r
+ AddSubscriptionItems subitems = new AddSubscriptionItems(subscriptionresource, references);\r
+ \r
+ subitems.perform(graph);\r
+ return subitems.result.get(subitems.result.size() -1);\r
+ }\r
+ \r
+ /**\r
+ * Adds subscription item to the given subscription resource (folder) with advanced plotting values that user can define.\r
+ * \r
+ * @param subscriptionresource resource of the subscription folder\r
+ * @param variable variable of the item to be created\r
+ * @param interval\r
+ * @param deadband\r
+ * @param gain\r
+ * @param bias\r
+ * @param unit\r
+ * @param label\r
+ * @return resource of the subscription item\r
+ * @throws DatabaseException on database failures\r
+ */\r
+ public static Resource newSubscriptionItem (WriteGraph graph, Resource subscriptionresource, Variable variable, double interval, double deadband, double gain, double bias, String unit, String label) throws DatabaseException {\r
+ \r
+ NewSubscriptionItem ns = new NewSubscriptionItem(\r
+ subscriptionresource, \r
+ interval, \r
+ deadband, \r
+ gain, \r
+ bias, \r
+ unit, \r
+ label, \r
+ variable.getRVI(graph),\r
+ variable.getDatatype(graph)\r
+ );\r
+ \r
+ ns.perform(graph);\r
+ \r
+ return ns.subscriptionItem;\r
+ }\r
+ \r
+ /**\r
+ * Returns the default subscription folder of the given model. If folder with label Default is not found, returns some other subscription folder\r
+ * \r
+ * @param model resource of the target model\r
+ * @return resource of the subscription folder\r
+ * @throws DatabaseException on database failures\r
+ */\r
+ public static Resource defaultSubscriptionFolder (ReadGraph graph, Resource model) throws DatabaseException {\r
+ \r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ ModelingResources MR = ModelingResources.getInstance(graph);\r
+ Collection<Resource> childrens = graph.getObjects(model, l0.ConsistsOf);\r
+ Resource folder = null;\r
+ for (final Resource children : childrens) {\r
+ \r
+ if (graph.isInstanceOf(children, MR.Subscription)) {\r
+ folder = children;\r
+ String label = graph.getRelatedValue2(folder, l0.HasLabel);\r
+ if (label.equals("Default")) {\r
+ return folder;\r
+ }\r
+ } \r
+ }\r
+ return folder;\r
+ }\r
+ \r
+ public static String getLabel(ReadGraph graph, Resource subscription) throws DatabaseException {\r
+ \r
+ return SubscriptionItemLabel.resolveLabel(graph, subscription, true); \r
+ }\r
+}\r