]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/ProxyVariables.java
Variable optimizations for documents (Simupedia)
[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                 
58                 Variable child = variable.getParent(graph);
59                 Variable parent = child.getParent(graph);
60
61                 if(parent instanceof ProxyChildVariable) {
62                         Variable var = ((ProxyChildVariable)parent).other();
63                         if(var.getParent(graph) == null) {
64                                 return null;
65                         }
66                         return var;
67                 } else {
68                         Variable input = parent.getPossiblePropertyValue(graph, "session");
69                         if(input == null) {
70                             System.out.println("null input for: " + parent.getURI(graph));
71                         }
72                         return input;
73                 }
74         } catch (Throwable t) {
75             LOGGER.error("proxySessionVariable failed", t);
76             return null;
77         }
78
79     }
80
81     public static Variable proxyVariableBase(ReadGraph graph, Variable variable) throws DatabaseException {
82         
83         try {
84                 
85                 Variable parent = variable.getParent(graph);
86                 if(parent == null) return null;
87                 
88                 if(parent instanceof ProxyChildVariable) {
89                         return ((ProxyChildVariable)parent).base();
90                 } else {
91                         return proxyVariableBase(graph, parent);
92                 }
93                 
94         } catch (Throwable t) {
95             LOGGER.error("proxyVariableBase failed", t);
96             return null;
97         }
98
99     }
100
101     public static Variable proxyVariableInput(ReadGraph graph, Variable variable) throws DatabaseException {
102
103                 if(variable instanceof ProxyChildVariable) {
104                         Variable var = ((ProxyChildVariable)variable).other();
105                         if(var.getParent(graph) == null) {
106                                 return null;
107                         }
108                         return var;
109                 } else {
110                         Variable parent = variable.getParent(graph);
111                         if(parent == null) return null;
112                         else return proxyVariableBase(graph, parent);
113                 }
114         
115     }
116     
117     public static Variable proxyVariableRoot(ReadGraph graph, Variable variable) throws DatabaseException {
118         
119         try {
120                 
121                 Variable parent = variable.getParent(graph);
122                 if(parent == null) return null;
123                 
124                 if(parent instanceof ProxyChildVariable) {
125                         return variable;
126                 } else {
127                         return proxyVariableRoot(graph, parent);
128                 }
129                 
130         } catch (Throwable t) {
131             LOGGER.error("proxyVariableRoot failed", t);
132             return null;
133         }
134
135     }
136
137     public static boolean isProxy(ReadGraph graph, Variable variable) throws DatabaseException {
138         return proxyVariableRoot(graph, variable) != null;
139     }
140
141 }