]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.server/src/org/simantics/document/server/VariableStructuralContext.java
DocumentRequest performance enhancements (Simupedia)
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / VariableStructuralContext.java
1 package org.simantics.document.server;
2
3 import java.util.List;
4 import java.util.Map;
5
6 import org.simantics.databoard.adapter.AdaptException;
7 import org.simantics.databoard.binding.Binding;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.request.VariableRead;
12 import org.simantics.db.layer0.variable.Variable;
13 import org.simantics.db.service.CollectionSupport;
14 import org.simantics.structural2.scl.StructuralComponent;
15
16 public class VariableStructuralContext extends StructuralComponent<Variable> {
17
18         final private Variable selfVariable;
19         final private Variable variable;
20         
21         public VariableStructuralContext(ReadGraph graph, Variable selfVariable) throws DatabaseException {
22                 this.selfVariable = selfVariable;
23                 this.variable = selfVariable.getParent(graph).getParent(graph);
24         }
25         
26         @Override
27         public Resource getType(ReadGraph g) throws DatabaseException {
28                 throw new UnsupportedOperationException();
29         }
30         
31         static class MapRequest extends VariableRead<Map<Resource,Variable>> {
32
33                 public MapRequest(Variable component) {
34                         super(component);
35                 }
36
37                 @Override
38                 public Map<Resource, Variable> perform(ReadGraph graph) throws DatabaseException {
39                         
40                         CollectionSupport cs = graph.getService(CollectionSupport.class);
41                         Map<Resource, Variable> result = cs.createMap(Variable.class);
42                         for(Variable property : variable.getProperties(graph)) {
43                                 Resource predicate = property.getPossiblePredicateResource(graph);
44                                 if(predicate != null)
45                                         result.put(predicate, property);
46                         }
47                         return result;
48                         
49                 }
50                 
51         }
52
53         @Override
54         public Object getValue(ReadGraph g, Resource attribute, Binding binding) throws DatabaseException, AdaptException {
55                 
56                 Map<Resource,Variable> map = g.syncRequest(new MapRequest(variable));
57                 Variable p = map.get(attribute);
58                 if(p != null) {
59                         if(binding != null)
60                                 return p.getValue(g, binding);
61                         else
62                                 return p.getValue(g);
63                 }
64                 
65                 return null;
66                 
67         }
68
69         @Override
70         public boolean isParametrized(ReadGraph g) throws DatabaseException {
71                 throw new UnsupportedOperationException();
72         }
73
74         @Override
75         public Variable getConnection(ReadGraph g, Resource connectionPoint) throws DatabaseException {
76                 throw new UnsupportedOperationException();
77         }
78
79         @SuppressWarnings({ "unchecked", "rawtypes" })
80         @Override
81         public List getConnections(ReadGraph g, Resource connectionPoint) throws DatabaseException {
82                 throw new UnsupportedOperationException();
83         }
84
85         @Override
86         public Resource getResource() {
87                 throw new UnsupportedOperationException();
88         }
89
90     @Override
91     public StructuralComponent<Variable> getContext() {
92         return null;
93     }
94
95 }