1 package org.simantics.db.layer0.variable;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
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;
20 public class ExtendedGraphChildVariable extends StandardGraphChildVariable {
22 private Map<String, Variable> extension;
23 private List<Listener<Map<String, Variable>>> listeners = new ArrayList<Listener<Map<String, Variable>>>();
25 public ExtendedGraphChildVariable(Variable parent, Resource resource) throws DatabaseException {
26 super(parent, null, resource);
29 Map<String, Variable> getExtension(ReadGraph graph) throws DatabaseException {
31 if(extension == null) {
33 extension = new HashMap<String, Variable>();
35 QueryControl qc = graph.getService(QueryControl.class);
37 qc.getIndependentGraph(graph).syncRequest(new ResourceRead<Collection<VariableFactory>>(resource) {
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);
51 }, new SingleSetSyncListener<VariableFactory>() {
54 public void add(ReadGraph graph, final VariableFactory factory) throws DatabaseException {
56 graph.syncRequest(new VariableFactoryRequest(ExtendedGraphChildVariable.this, factory), new SingleSetSyncListener<Variable>() {
59 public void add(ReadGraph graph, final Variable child) throws DatabaseException {
60 String name = child.getPropertyValue(graph, Variables.NAME);
61 extension.put(name, child);
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);
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);
77 public boolean isDisposed() {
86 public boolean isDisposed() {
94 return graph.syncRequest(new ParametrizedPrimitiveRead<Variable, Map<String, Variable>>(this) {
97 public void register(ReadGraph graph, Listener<Map<String, Variable>> procedure) {
99 listeners.add(procedure);
100 procedure.execute(getClone());
108 private Map<String, Variable> getClone() {
109 Map<String, Variable> clone = new HashMap<String, Variable>();
110 clone.putAll(extension);
114 public Variable getPossibleSpecialChild(ReadGraph graph, String name) throws DatabaseException {
115 return getExtension(graph).get(name);
118 public void collectSpecialChildren(ReadGraph graph, Collection<Variable> children) throws DatabaseException {
119 children.addAll(getExtension(graph).values());