]> gerrit.simantics Code Review - simantics/platform.git/blob
07c8bc2aefbb65eb374a2b2b90f905b9e3dcfbd3
[simantics/platform.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 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.typicals;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 import java.util.HashSet;\r
17 import java.util.Set;\r
18 \r
19 import org.simantics.db.ReadGraph;\r
20 import org.simantics.db.Resource;\r
21 import org.simantics.db.common.NamedResource;\r
22 import org.simantics.db.common.request.ObjectsWithType;\r
23 import org.simantics.db.common.request.ResourceRead;\r
24 import org.simantics.db.common.utils.NameUtils;\r
25 import org.simantics.db.exception.DatabaseException;\r
26 import org.simantics.diagram.stubs.DiagramResource;\r
27 import org.simantics.layer0.Layer0;\r
28 import org.simantics.modeling.ModelingResources;\r
29 \r
30 public class AvailableSynchronizationRules extends ResourceRead<Collection<NamedResource>> {\r
31 \r
32         public AvailableSynchronizationRules(Resource resource) {\r
33                 super(resource);\r
34         }\r
35 \r
36         @Override\r
37         public Collection<NamedResource> perform(ReadGraph graph)\r
38                         throws DatabaseException {\r
39                 \r
40                 Layer0 L0 = Layer0.getInstance(graph);\r
41                 DiagramResource DIA = DiagramResource.getInstance(graph);\r
42                 ModelingResources MOD = ModelingResources.getInstance(graph);\r
43                 \r
44                 Set<Resource> rules = new HashSet<Resource>();\r
45                 for(Resource element : graph.syncRequest(new ObjectsWithType(resource, L0.ConsistsOf, DIA.Element))) {\r
46                         for (Resource rule : graph.getObjects(element, MOD.HasTypicalSynchronizationRule)) {\r
47                                 ITypicalSynchronizationRule r = graph.getPossibleAdapter(rule, ITypicalSynchronizationRule.class);\r
48                                 if (r != null) rules.add(rule);\r
49                         }\r
50                 }\r
51                 \r
52                 Collection<NamedResource> result = new ArrayList<NamedResource>();\r
53                 for(Resource rule : rules) {\r
54                         String name = NameUtils.getSafeLabel(graph, rule);\r
55                         result.add(new NamedResource(name, rule));\r
56                 }\r
57                 \r
58                 return result;\r
59                 \r
60         }\r
61 \r
62 }