]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/subscription/SubscriptionsQuery.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / subscription / SubscriptionsQuery.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 java.util.List;
15
16 import org.simantics.databoard.util.Bean;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.request.ResourceRead;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.layer0.Layer0;
22 import org.simantics.modeling.ModelingResources;
23
24 /**
25  * This query returns all subscriptions and their corersponding names.
26  *  
27  * @author toni.kalajainen
28  */
29 public class SubscriptionsQuery extends ResourceRead<SubscriptionsQuery.SubscriptionsResult> {
30
31         public static class SubscriptionsResult extends Bean {
32                 
33                 public List<Resource> resources;
34                 public List<String> names;
35                 
36                 public String[] toNames() {
37                         return names.toArray( new String[names.size()] );
38                 }
39                 
40                 public String getName(Resource resource)
41                 {
42                         if (resource==null) return null;
43                         for ( int i=0; i<resources.size(); i++ ) {                              
44                                 if ( resource.equals( resources.get(i) ) ) return names.get(i);
45                         }
46                         return null;
47                 }
48                 
49         }
50
51         public SubscriptionsQuery(Resource resource) {
52                 super(resource);
53         }
54
55
56         @Override
57         public SubscriptionsResult perform(ReadGraph graph) throws DatabaseException {
58                 Layer0 L0 = Layer0.getInstance(graph);
59                 ModelingResources MOD = ModelingResources.getInstance(graph);
60                 
61                 SubscriptionsResult result = new SubscriptionsResult();
62                 result.init();
63                 
64         for (Resource r : graph.getObjects(resource, L0.ConsistsOf)) {
65                 if ( !graph.isInstanceOf(r, MOD.Subscription) ) continue;
66                 String label = graph.getPossibleRelatedValue(r, L0.HasLabel);
67                 result.resources.add( r );
68                 result.names.add( label == null ? "" : label );
69         }
70                 
71                 return result;
72         }
73         
74 }