--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * 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
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.subscription;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.UniqueRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.utils.ObjectUtils;\r
+\r
+/**\r
+ * This query finds subscription using label as keyword.\r
+ * If multiple subscriptions have the \r
+ * \r
+ * @author toni.kalajainen\r
+ */\r
+public class FindSubscription extends UniqueRead<Resource> {\r
+\r
+ Resource model;\r
+ String id, label;\r
+ \r
+ public FindSubscription( Resource model, String id, String label )\r
+ {\r
+ this.model = model;\r
+ this.id = id;\r
+ this.label = label;\r
+ }\r
+ \r
+ @Override\r
+ public Resource perform(ReadGraph graph) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ ModelingResources MOD = ModelingResources.getInstance(graph);\r
+\r
+ for (Resource r : graph.getObjects(model, L0.ConsistsOf))\r
+ {\r
+ if ( !graph.isInstanceOf(r, MOD.Subscription) ) continue;\r
+ \r
+ if ( this.id!=null ) {\r
+ String name = graph.getPossibleRelatedValue(r, L0.HasName);\r
+ if ( !name.equals(this.id)) continue;\r
+ }\r
+ \r
+ if ( this.label!=null ) {\r
+ String label = graph.getPossibleRelatedValue(r, L0.HasLabel);\r
+ if ( !label.equals(this.label)) continue;\r
+ }\r
+ \r
+ return r;\r
+ }\r
+ \r
+ return null;\r
+ }\r
+ \r
+ @Override\r
+ public int hashCode() {\r
+ return model.hashCode() + (id!=null?id.hashCode():0) + (label!=null?label.hashCode():0);\r
+ }\r
+\r
+ @Override\r
+ public boolean equals(Object object) {\r
+ if (this == object) return true;\r
+ if (object == null) return false;\r
+ if (getClass() != object.getClass()) return false;\r
+ FindSubscription r = (FindSubscription) object;\r
+ \r
+ if ( !ObjectUtils.objectEquals(id, r.id) ) return false;\r
+ if ( !ObjectUtils.objectEquals(label, r.label)) return false;\r
+ \r
+ return true;\r
+ }\r
+ \r
+\r
+}\r