]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/children/ChildContribution.java
Added transient caching for BrowseContext construction
[simantics/platform.git] / bundles / org.simantics.browsing.ui.model / src / org / simantics / browsing / ui / model / children / ChildContribution.java
1 /*******************************************************************************
2  * Copyright (c) 2010, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.model.children;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17
18 import org.simantics.browsing.ui.BuiltinKeys;
19 import org.simantics.browsing.ui.NodeContext;
20 import org.simantics.browsing.ui.model.InvalidContribution;
21 import org.simantics.browsing.ui.model.nodetypes.NodeType;
22 import org.simantics.databoard.Bindings;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.Resource;
25 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
26 import org.simantics.db.common.request.ResourceRead;
27 import org.simantics.db.common.utils.NameUtils;
28 import org.simantics.db.exception.DatabaseException;
29 import org.simantics.db.layer0.exception.PendingVariableException;
30 import org.simantics.viewpoint.ontology.ViewpointResource;
31
32 /**
33  * A child contribution adds child nodes to the instances of a specific node type.
34  * @author Hannu Niemistö
35  */
36 public class ChildContribution {
37         
38         double priority;
39         String identifier;
40     NodeType parentNodeType;
41     NodeType childNodeType;
42     ChildRule childRule;
43         
44     public ChildContribution(String identifier, double priority, NodeType parentNodeType, NodeType childNodeType,
45             ChildRule childRule) throws InvalidContribution {
46         if(!childRule.isCompatible(
47                 parentNodeType.getContentType()
48                 ))
49             throw new InvalidContribution("Child rule is not compatible with the parent content type.");
50         this.parentNodeType = parentNodeType;
51         this.childNodeType = childNodeType;
52         this.childRule = childRule;
53         this.identifier = identifier;
54         this.priority = priority;
55     }
56
57     public static ChildContribution createCached(ReadGraph g, Resource childContributionResource) throws DatabaseException, InvalidContribution {
58         try {
59             return g.syncRequest(new ResourceRead<ChildContribution>(childContributionResource) {
60                 @Override
61                 public ChildContribution perform(ReadGraph graph) throws DatabaseException {
62                     try {
63                         return create(g, resource);
64                     } catch (InvalidContribution e) {
65                         throw new DatabaseException(e);
66                     }
67                 }
68             }, TransientCacheAsyncListener.instance());
69         } catch (DatabaseException e) {
70             Throwable c = e.getCause();
71             if (c instanceof InvalidContribution)
72                 throw (InvalidContribution) c;
73             throw e;
74         }
75     }
76
77     public static ChildContribution create(ReadGraph g, Resource childContributionResource) throws DatabaseException, InvalidContribution {
78         ViewpointResource vr = ViewpointResource.getInstance(g);
79         
80         Resource parentNodeTypeResource = g.getSingleObject(childContributionResource, vr.ChildContribution_HasParentNodeType);
81         NodeType parentNodeType = g.adapt(parentNodeTypeResource, NodeType.class);
82         
83         Resource childNodeTypeResource = g.getSingleObject(childContributionResource, vr.ChildContribution_HasChildNodeType);
84         NodeType childNodeType = g.adapt(childNodeTypeResource, NodeType.class);
85         
86         Resource childRuleResource = g.getSingleObject(childContributionResource, vr.ChildContribution_HasRule);
87         ChildRule childRule = g.adapt(childRuleResource, ChildRule.class);
88         
89         String identifier = g.getPossibleRelatedValue(childContributionResource, vr.ChildContribution_identifier, Bindings.STRING);
90         if(identifier == null) identifier = "";
91         if("".equals(identifier)) {
92                 identifier = g.getPossibleURI(childContributionResource);
93             if(identifier == null) identifier = NameUtils.getSafeName(g, childContributionResource, true);
94         }
95         
96         Double priority = g.getPossibleRelatedValue(childContributionResource, vr.ChildContribution_priority, Bindings.DOUBLE);
97         if(priority == null) priority = 0.0;
98         
99         return new ChildContribution(identifier, priority, parentNodeType, childNodeType, childRule);
100         
101     }
102
103     public NodeType getParentNodeType() {
104         return parentNodeType;
105     }
106     
107     public NodeType getChildNodeType() {
108         return childNodeType;
109     }
110     
111     public String getIdentifier() {
112         return identifier;
113     }
114
115     public double getPriority() {
116         return priority;
117     }
118     
119     /**
120      * Given a parent node context returns a child node contexts contributed by
121      * this contribution.
122      */
123     public Collection<NodeContext> getChildren(ReadGraph graph, NodeContext parent) {
124         try {
125             Object parentContent = parent.getConstant(BuiltinKeys.INPUT);
126             Collection<?> childContents = childRule.getChildren(graph, parentContent);
127             if(childContents.isEmpty())
128                 return Collections.emptyList();
129             ArrayList<NodeContext> children = new ArrayList<NodeContext>(childContents.size());
130             for(Object childContent : childContents) {
131                 NodeContext child = childNodeType.createNodeContext(graph, childContent);
132                 if(child != null)
133                     children.add(child);
134             }
135             return children;
136         } catch(PendingVariableException e) {
137             return Collections.emptyList();
138         } catch(DatabaseException e) {
139             e.printStackTrace();
140             // TODO return some kind of error node
141             return Collections.emptyList();
142         }
143     }
144     
145     /**
146      * Given a child node context returns a collection of possible parent node contexts.
147      */
148     public Collection<NodeContext> getParents(ReadGraph graph, NodeContext child) {
149         try {
150             Object childContent = child.getConstant(BuiltinKeys.INPUT);
151             Collection<?> parentContents = childRule.getParents(graph, childContent);
152             if(parentContents.isEmpty())
153                 return Collections.emptyList();
154             ArrayList<NodeContext> parents = new ArrayList<NodeContext>(parentContents.size());
155             for(Object parentContent : parentContents) {
156                 NodeContext parent = parentNodeType.createNodeContext(graph, parentContent);
157                 if(parent != null)
158                     parents.add(parent);
159             }
160             return parents;
161         } catch(DatabaseException e) {
162             e.printStackTrace();
163             return Collections.emptyList();
164         }
165     }
166
167     public boolean hasChildren(ReadGraph graph, NodeContext parent) {
168         try {
169             Object parentContent = parent.getConstant(BuiltinKeys.INPUT);
170             Collection<?> childContents = childRule.getChildren(graph, parentContent);
171             if(childContents.isEmpty())
172                 return false;
173             for(Object childContent : childContents) {
174                 NodeContext child = childNodeType.createNodeContext(graph, childContent);
175                 if(child != null)
176                     return true;
177             }
178         } catch(DatabaseException e) {
179             e.printStackTrace();
180         }
181         return false;
182     }
183     
184     @Override
185     public String toString() {
186         return identifier;
187     }
188     
189 }