]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/variables/BrowseConnectionDescriptor.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / variables / BrowseConnectionDescriptor.java
1 package org.simantics.structural2.variables;
2
3 import org.simantics.db.ReadGraph;
4 import org.simantics.db.Resource;
5 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
6 import org.simantics.db.common.request.UnaryRead;
7 import org.simantics.db.exception.DatabaseException;
8 import org.simantics.db.layer0.variable.Variable;
9
10 public class BrowseConnectionDescriptor extends AbstractVariableConnectionPointDescriptor {
11         
12         public Variable base;
13         public String rel;
14         
15         public BrowseConnectionDescriptor(Variable base, String rel) {
16                 this.base = base;
17                 this.rel = rel;
18         }
19         @Override
20         public int hashCode() {
21                 return base.hashCode() + 31*rel.hashCode();
22         }
23         @Override
24         public boolean equals(Object object) {
25         if (this == object)
26             return true;
27         else if (object == null)
28             return false;
29         else if (!(object instanceof BrowseConnectionDescriptor))
30             return false;
31         BrowseConnectionDescriptor r = (BrowseConnectionDescriptor)object;
32         if(!r.rel.equals(rel)) return false;
33         if(!r.base.equals(base)) return false;
34         return true;
35         }
36         static class ComputeVariable extends UnaryRead<BrowseConnectionDescriptor, Variable> {
37
38                 public ComputeVariable(BrowseConnectionDescriptor desc) {
39                         super(desc);
40                 }
41
42                 @Override
43                 public Variable perform(ReadGraph graph) throws DatabaseException {
44                         return parameter.base.browse(graph, parameter.rel);
45                 }
46                 
47         }
48         @Override
49         public Variable getVariable(ReadGraph graph) throws DatabaseException {
50                 return graph.syncRequest(new ComputeVariable(this), TransientCacheAsyncListener.<Variable>instance());
51         }
52         public String getURI(ReadGraph graph) throws DatabaseException {
53                 return getVariable(graph).getURI(graph);
54         }
55         @Override
56         public Resource getConnectionPointResource(ReadGraph graph) throws DatabaseException {
57                 return getVariable(graph).getPossiblePredicateResource(graph);
58         }
59         
60         @Override
61         public boolean isFlattenedFrom(ReadGraph graph, Variable possiblyStructuralCp) throws DatabaseException {
62                 
63                 Variable baseParent = base.getParent(graph);
64                 Variable parent = possiblyStructuralCp.getParent(graph);
65                 Variable parentParent = parent.getParent(graph);
66                 
67                 if(baseParent.equals(parentParent)) {
68                         if(!parent.equals(base)) return false;
69                 }
70
71                 String descURI = getURI(graph);
72                 String parentURI = parent.getURI(graph);
73                 if(descURI.startsWith(parentURI)) {
74                         // Children are in substructure
75                         if('/' == descURI.charAt(parentURI.length())) return true;
76                         // Otherwise require exact match
77                         return descURI.endsWith(possiblyStructuralCp.getName(graph));
78                 } else {
79                         return false;
80                 }
81                 
82         }
83         
84 }