]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/subscription/FindSubscription.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / subscription / FindSubscription.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.subscription;\r
13 \r
14 import org.simantics.db.ReadGraph;\r
15 import org.simantics.db.Resource;\r
16 import org.simantics.db.common.request.UniqueRead;\r
17 import org.simantics.db.exception.DatabaseException;\r
18 import org.simantics.layer0.Layer0;\r
19 import org.simantics.modeling.ModelingResources;\r
20 import org.simantics.utils.ObjectUtils;\r
21 \r
22 /**\r
23  * This query finds subscription using label as keyword.\r
24  * If multiple subscriptions have the \r
25  * \r
26  * @author toni.kalajainen\r
27  */\r
28 public class FindSubscription extends UniqueRead<Resource> {\r
29 \r
30         Resource model;\r
31         String id, label;\r
32         \r
33         public FindSubscription( Resource model, String id, String label )\r
34         {\r
35                 this.model = model;\r
36                 this.id = id;\r
37                 this.label = label;\r
38         }\r
39         \r
40         @Override\r
41         public Resource perform(ReadGraph graph) throws DatabaseException {\r
42         Layer0 L0 = Layer0.getInstance(graph);\r
43         ModelingResources MOD = ModelingResources.getInstance(graph);\r
44 \r
45         for (Resource r : graph.getObjects(model, L0.ConsistsOf))\r
46         {\r
47                 if ( !graph.isInstanceOf(r, MOD.Subscription) ) continue;\r
48                 \r
49                 if ( this.id!=null ) {\r
50                 String name = graph.getPossibleRelatedValue(r, L0.HasName);\r
51                 if ( !name.equals(this.id)) continue;\r
52                 }\r
53                 \r
54                 if ( this.label!=null ) {\r
55                 String label = graph.getPossibleRelatedValue(r, L0.HasLabel);\r
56                 if ( !label.equals(this.label)) continue;\r
57                 }\r
58                 \r
59                 return r;\r
60         }\r
61         \r
62                 return null;\r
63         }\r
64         \r
65     @Override\r
66     public int hashCode() {\r
67         return model.hashCode() + (id!=null?id.hashCode():0) + (label!=null?label.hashCode():0);\r
68     }\r
69 \r
70     @Override\r
71     public boolean equals(Object object) {\r
72         if (this == object) return true;\r
73         if (object == null) return false;\r
74         if (getClass() != object.getClass()) return false;\r
75         FindSubscription r = (FindSubscription) object;\r
76         \r
77         if ( !ObjectUtils.objectEquals(id, r.id) ) return false;\r
78         if ( !ObjectUtils.objectEquals(label, r.label)) return false;\r
79         \r
80         return true;\r
81     }\r
82         \r
83 \r
84 }\r