]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/ExtendedGraphChildVariable.java
New implementation NearestOwnerFinder of CommonDBUtils.getNearestOwner
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / ExtendedGraphChildVariable.java
1 package org.simantics.db.layer0.variable;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.common.procedure.single.SingleSetSyncListener;
12 import org.simantics.db.common.request.ParametrizedPrimitiveRead;
13 import org.simantics.db.common.request.ResourceRead;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.request.VariableFactoryRequest;
16 import org.simantics.db.procedure.Listener;
17 import org.simantics.db.service.QueryControl;
18 import org.simantics.operation.Layer0X;
19
20 public class ExtendedGraphChildVariable extends StandardGraphChildVariable {
21
22         private Map<String, Variable> extension;
23         private List<Listener<Map<String, Variable>>> listeners = new ArrayList<Listener<Map<String, Variable>>>();
24         
25         public ExtendedGraphChildVariable(Variable parent, Resource resource) throws DatabaseException {
26                 super(parent, null, resource);
27         }
28
29         Map<String, Variable> getExtension(ReadGraph graph) throws DatabaseException {
30
31                 if(extension == null) {
32
33                         extension = new HashMap<String, Variable>();
34
35                         QueryControl qc = graph.getService(QueryControl.class);
36                         
37                         qc.getIndependentGraph(graph).syncRequest(new ResourceRead<Collection<VariableFactory>>(resource) {
38
39                                 @Override
40                                 public Collection<VariableFactory> perform(ReadGraph graph) throws DatabaseException {
41                                         Layer0X L0X = Layer0X.getInstance(graph);
42                                         ArrayList<VariableFactory> result = new ArrayList<VariableFactory>();
43                                         for(Resource r : graph.getObjects(resource, L0X.HasChildVariables)) {
44                                 VariableFactory factory = graph.adapt(r, VariableFactory.class);
45                                 assert(factory != null);
46                                                 result.add(factory);
47                                         }
48                                         return result;
49                                 }
50                         
51                     }, new SingleSetSyncListener<VariableFactory>() {
52
53                     @Override
54                     public void add(ReadGraph graph, final VariableFactory factory) throws DatabaseException {
55                         
56                         graph.syncRequest(new VariableFactoryRequest(ExtendedGraphChildVariable.this, factory), new SingleSetSyncListener<Variable>() {
57
58                             @Override
59                             public void add(ReadGraph graph, final Variable child) throws DatabaseException {
60                                 String name = child.getPropertyValue(graph, Variables.NAME);
61                                 extension.put(name, child);
62                             }
63                             
64                             @Override
65                             public void remove(ReadGraph graph, Variable child) throws DatabaseException {
66                                 String name = child.getPropertyValue(graph, Variables.NAME);
67                                 Variable exist = extension.get(name);
68                                 if(exist == child) extension.remove(name);
69                             }
70                             
71                             public void finished(ReadGraph graph) throws DatabaseException {
72                                 Map<String, Variable> clone = getClone();
73                                 for(Listener<Map<String, Variable>> listener : listeners) listener.execute(clone);
74                             }
75
76                             @Override
77                             public boolean isDisposed() {
78                                 return false;
79                             }
80
81                         });
82                         
83                     }
84                     
85                     @Override
86                     public boolean isDisposed() {
87                         return false;
88                     }
89                     
90                     });
91                         
92                 }
93                 
94                 return graph.syncRequest(new ParametrizedPrimitiveRead<Variable, Map<String, Variable>>(this) {
95                         
96                         @Override
97                         public void register(ReadGraph graph, Listener<Map<String, Variable>> procedure) {
98
99                                 listeners.add(procedure);
100                                 procedure.execute(getClone());
101                                 
102                         }
103                         
104                 });
105                 
106         }
107         
108         private Map<String, Variable> getClone() {
109                 Map<String, Variable> clone = new HashMap<String, Variable>();
110                 clone.putAll(extension);
111                 return clone;
112         }
113         
114         public Variable getPossibleSpecialChild(ReadGraph graph, String name) throws DatabaseException {
115                 return getExtension(graph).get(name);
116         }
117
118         public void collectSpecialChildren(ReadGraph graph, Collection<Variable> children) throws DatabaseException {
119                 children.addAll(getExtension(graph).values());
120         }
121         
122 }