]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/ImmutableComponentVariableContentRequest.java
cef62c242633e7708131a47a7c92dc0b0148149d
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / ImmutableComponentVariableContentRequest.java
1 package org.simantics.modeling;
2
3 import java.util.HashMap;
4 import java.util.HashSet;
5 import java.util.Map;
6
7 import org.simantics.databoard.Bindings;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.common.request.ResourceRead;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.request.PropertyInfo;
13 import org.simantics.db.layer0.request.UnescapedPropertyMapOfResource;
14 import org.simantics.layer0.Layer0;
15 import org.simantics.structural.stubs.StructuralResource2;
16 import org.simantics.structural2.ConnectionImpl;
17 import org.simantics.structural2.Functions.StructuralChildMapOfResource;
18 import org.simantics.structural2.queries.ConnectionPointMapOfResource;
19
20 public class ImmutableComponentVariableContentRequest extends ResourceRead<ImmutableComponentVariableContent> {
21
22         protected ImmutableComponentVariableContentRequest(Resource resource) {
23                 super(resource);
24         }
25         
26         @Override
27         public ImmutableComponentVariableContent perform(ReadGraph graph) throws DatabaseException {
28                 
29                 Layer0 L0 = Layer0.getInstance(graph);
30                 String name = graph.getRelatedValue(resource, L0.HasName, Bindings.STRING);
31                 ImmutableComponentVariableContent result = new ImmutableComponentVariableContent(resource, name);
32
33                 StructuralResource2 STR = StructuralResource2.getInstance(graph);
34                 Resource componentType = graph.getPossibleType(resource, STR.Component);
35                 if(componentType != null)
36                         result.procedural = graph.isInstanceOf(componentType, STR.ProceduralComponentType); 
37                 
38                 Map<String,PropertyInfo> pis = graph.syncRequest(new UnescapedPropertyMapOfResource(resource));
39                 
40                 for(Map.Entry<String, PropertyInfo> e : pis.entrySet()) {
41                         PropertyInfo pi = e.getValue();
42                         if(pi.isHasProperty) {
43                                 ImmutableComponentPropertyContent pc = graph.syncRequest(new ImmutableComponentPropertyContentRequest(resource, pi));
44                                 if(pc != null) {
45                                         if(result.properties == null) result.properties = new HashMap<>();
46                                         result.properties.put(pc.pi.name, pc);
47                                 }
48                         }
49                 }
50
51                 Map<String,PropertyInfo> cps = graph.syncRequest(new ConnectionPointMapOfResource(graph, resource));
52                 for(Map.Entry<String, PropertyInfo> e : cps.entrySet()) {
53                         PropertyInfo pi = e.getValue();
54                         Resource conn = graph.getPossibleObject(resource, pi.predicate);
55                         if(conn != null) {
56                                 if(result.properties == null) result.properties = new HashMap<>();
57                                 result.properties.put(pi.name, new ImmutableComponentPropertyContent(pi, conn, new ConnectionImpl(pi.predicate), null));
58                         }
59                 }
60                 
61                 HashSet<Resource> childSet = null;
62                 for(Resource child : graph.syncRequest(new StructuralChildMapOfResource(resource)).values()) {
63                         if(childSet == null)
64                                 childSet = new HashSet<>();
65                         childSet.add(child);
66                 }
67                 
68                 if(childSet != null) {
69                         result.children = new HashMap<>();
70                         for(Resource child : childSet) {
71                                 ImmutableComponentVariableContent content = graph.syncRequest(new ImmutableComponentVariableContentRequest(child)); 
72                                 result.children.put(content.name, content);
73                         }
74                 }
75                 
76                 return result;
77                 
78         }
79
80         
81 }