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