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