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