]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/ProxyVariables.java
Merge "(refs #7307) Added features field to SCL module header"
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / ProxyVariables.java
1 package org.simantics.db.layer0.variable;
2
3 import org.simantics.db.ReadGraph;
4 import org.simantics.db.exception.DatabaseException;
5
6 public class ProxyVariables {
7         
8         public static Variable inputVariable(ReadGraph graph, Variable context) throws DatabaseException {
9         Variable session = graph.syncRequest(new ProxySessionRequest(context));
10         if(session == null) return null;
11         String uri = session.getPossiblePropertyValue(graph, "inputURI");
12         if(uri == null) {
13                 // TODO HAXX - Please fix this
14                 // we have no URI so this probably means that someone has inserted a non-session 
15                 // into the proxy variable => return that instead
16                 return session;
17         }
18         return Variables.getVariable(graph, uri);
19         }
20
21         public static Variable sessionVariable(ReadGraph graph, Variable self) throws DatabaseException {
22                 return graph.syncRequest(new ProxySessionRequest(self));
23         }
24
25         public static Variable makeProxyVariable(ReadGraph graph, Variable base, Variable input) throws DatabaseException {
26                 
27         base = base.getChild(graph, ProxyChildVariable.CONTEXT_BEGIN);
28         String path = input.getURI(graph).substring("http:/".length());
29         base = base.browse(graph, path);
30         return base.getChild(graph, ProxyChildVariable.CONTEXT_END);
31         
32         }
33         
34     public static Variable proxySessionVariable(ReadGraph graph, Variable variable) throws DatabaseException {
35         
36         try {
37                 
38                 Variable child = variable.getParent(graph);
39                 Variable parent = child.getParent(graph);
40
41                 if(parent instanceof ProxyChildVariable) {
42                         Variable var = ((ProxyChildVariable)parent).other();
43                         if(var.getParent(graph) == null) {
44                                 return null;
45                         }
46                         return var;
47                 } else {
48                         Variable input = parent.getPossiblePropertyValue(graph, "session");
49                         if(input == null) {
50                             System.out.println("null input for: " + parent.getURI(graph));
51                         }
52                         return input;
53                 }
54         } catch (Throwable t) {
55             t.printStackTrace();
56             return null;
57         }
58
59     }
60
61     public static Variable proxyVariableBase(ReadGraph graph, Variable variable) throws DatabaseException {
62         
63         try {
64                 
65                 Variable parent = variable.getParent(graph);
66                 if(parent == null) return null;
67                 
68                 if(parent instanceof ProxyChildVariable) {
69                         return ((ProxyChildVariable)parent).base();
70                 } else {
71                         return proxyVariableBase(graph, parent);
72                 }
73                 
74         } catch (Throwable t) {
75             t.printStackTrace();
76             return null;
77         }
78
79     }
80
81     public static Variable proxyVariableInput(ReadGraph graph, Variable variable) throws DatabaseException {
82
83                 if(variable instanceof ProxyChildVariable) {
84                         Variable var = ((ProxyChildVariable)variable).other();
85                         if(var.getParent(graph) == null) {
86                                 return null;
87                         }
88                         return var;
89                 } else {
90                         Variable parent = variable.getParent(graph);
91                         if(parent == null) return null;
92                         else return proxyVariableBase(graph, parent);
93                 }
94         
95     }
96     
97     public static Variable proxyVariableRoot(ReadGraph graph, Variable variable) throws DatabaseException {
98         
99         try {
100                 
101                 Variable parent = variable.getParent(graph);
102                 if(parent == null) return null;
103                 
104                 if(parent instanceof ProxyChildVariable) {
105                         return variable;
106                 } else {
107                         return proxyVariableRoot(graph, parent);
108                 }
109                 
110         } catch (Throwable t) {
111             t.printStackTrace();
112             return null;
113         }
114
115     }
116
117     public static boolean isProxy(ReadGraph graph, Variable variable) throws DatabaseException {
118         return proxyVariableRoot(graph, variable) != null;
119     }
120
121     
122 }