]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
index 76aa59392263b5938bcc046ca284a837a07bd7c7..09a15fefcb6bd5a4235fd2a4433130057c54b170 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2010, 2011 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.browsing.ui.model.children;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-\r
-import org.simantics.browsing.ui.BuiltinKeys;\r
-import org.simantics.browsing.ui.NodeContext;\r
-import org.simantics.browsing.ui.model.InvalidContribution;\r
-import org.simantics.browsing.ui.model.nodetypes.NodeType;\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.exception.PendingVariableException;\r
-import org.simantics.viewpoint.ontology.ViewpointResource;\r
-\r
-/**\r
- * A child contribution adds child nodes to the instances of a specific node type.\r
- * @author Hannu Niemistö\r
- */\r
-public class ChildContribution {\r
-       \r
-       double priority;\r
-       String identifier;\r
-    NodeType parentNodeType;\r
-    NodeType childNodeType;\r
-    ChildRule childRule;\r
-        \r
-    public ChildContribution(String identifier, double priority, NodeType parentNodeType, NodeType childNodeType,\r
-            ChildRule childRule) throws InvalidContribution {\r
-        if(!childRule.isCompatible(\r
-                parentNodeType.getContentType()\r
-                ))\r
-            throw new InvalidContribution("Child rule is not compatible with the parent content type.");\r
-        this.parentNodeType = parentNodeType;\r
-        this.childNodeType = childNodeType;\r
-        this.childRule = childRule;\r
-        this.identifier = identifier;\r
-        this.priority = priority;\r
-    }\r
-\r
-    public static ChildContribution create(ReadGraph g, Resource childContributionResource) throws DatabaseException, InvalidContribution {\r
-        ViewpointResource vr = ViewpointResource.getInstance(g);\r
-        \r
-        Resource parentNodeTypeResource = g.getSingleObject(childContributionResource, vr.ChildContribution_HasParentNodeType);\r
-        NodeType parentNodeType = g.adapt(parentNodeTypeResource, NodeType.class);\r
-        \r
-        Resource childNodeTypeResource = g.getSingleObject(childContributionResource, vr.ChildContribution_HasChildNodeType);\r
-        NodeType childNodeType = g.adapt(childNodeTypeResource, NodeType.class);\r
-        \r
-        Resource childRuleResource = g.getSingleObject(childContributionResource, vr.ChildContribution_HasRule);\r
-        ChildRule childRule = g.adapt(childRuleResource, ChildRule.class);\r
-        \r
-        String identifier = g.getPossibleRelatedValue(childContributionResource, vr.ChildContribution_identifier, Bindings.STRING);\r
-        if(identifier == null) identifier = "";\r
-        if("".equals(identifier)) {\r
-               identifier = g.getPossibleURI(childContributionResource);\r
-            if(identifier == null) identifier = NameUtils.getSafeName(g, childContributionResource, true);\r
-        }\r
-        \r
-        Double priority = g.getPossibleRelatedValue(childContributionResource, vr.ChildContribution_priority, Bindings.DOUBLE);\r
-        if(priority == null) priority = 0.0;\r
-        \r
-        return new ChildContribution(identifier, priority, parentNodeType, childNodeType, childRule);\r
-        \r
-    }\r
-\r
-    public NodeType getParentNodeType() {\r
-        return parentNodeType;\r
-    }\r
-    \r
-    public NodeType getChildNodeType() {\r
-        return childNodeType;\r
-    }\r
-    \r
-    public String getIdentifier() {\r
-       return identifier;\r
-    }\r
-\r
-    public double getPriority() {\r
-       return priority;\r
-    }\r
-    \r
-    /**\r
-     * Given a parent node context returns a child node contexts contributed by\r
-     * this contribution.\r
-     */\r
-    public Collection<NodeContext> getChildren(ReadGraph graph, NodeContext parent) {\r
-        try {\r
-            Object parentContent = parent.getConstant(BuiltinKeys.INPUT);\r
-            Collection<?> childContents = childRule.getChildren(graph, parentContent);\r
-            if(childContents.isEmpty())\r
-                return Collections.emptyList();\r
-            ArrayList<NodeContext> children = new ArrayList<NodeContext>(childContents.size());\r
-            for(Object childContent : childContents) {\r
-                NodeContext child = childNodeType.createNodeContext(graph, childContent);\r
-                if(child != null)\r
-                    children.add(child);\r
-            }\r
-            return children;\r
-        } catch(PendingVariableException e) {\r
-            return Collections.emptyList();\r
-        } catch(DatabaseException e) {\r
-            e.printStackTrace();\r
-            // TODO return some kind of error node\r
-            return Collections.emptyList();\r
-        }\r
-    }\r
-    \r
-    /**\r
-     * Given a child node context returns a collection of possible parent node contexts.\r
-     */\r
-    public Collection<NodeContext> getParents(ReadGraph graph, NodeContext child) {\r
-        try {\r
-            Object childContent = child.getConstant(BuiltinKeys.INPUT);\r
-            Collection<?> parentContents = childRule.getParents(graph, childContent);\r
-            if(parentContents.isEmpty())\r
-                return Collections.emptyList();\r
-            ArrayList<NodeContext> parents = new ArrayList<NodeContext>(parentContents.size());\r
-            for(Object parentContent : parentContents) {\r
-                NodeContext parent = parentNodeType.createNodeContext(graph, parentContent);\r
-                if(parent != null)\r
-                    parents.add(parent);\r
-            }\r
-            return parents;\r
-        } catch(DatabaseException e) {\r
-            e.printStackTrace();\r
-            return Collections.emptyList();\r
-        }\r
-    }\r
-\r
-    public boolean hasChildren(ReadGraph graph, NodeContext parent) {\r
-        try {\r
-            Object parentContent = parent.getConstant(BuiltinKeys.INPUT);\r
-            Collection<?> childContents = childRule.getChildren(graph, parentContent);\r
-            if(childContents.isEmpty())\r
-                return false;\r
-            for(Object childContent : childContents) {\r
-                NodeContext child = childNodeType.createNodeContext(graph, childContent);\r
-                if(child != null)\r
-                    return true;\r
-            }\r
-        } catch(DatabaseException e) {\r
-            e.printStackTrace();\r
-        }\r
-        return false;\r
-    }\r
-    \r
-    @Override\r
-    public String toString() {\r
-       return identifier;\r
-    }\r
-    \r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2010, 2011 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.browsing.ui.model.children;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+
+import org.simantics.browsing.ui.BuiltinKeys;
+import org.simantics.browsing.ui.NodeContext;
+import org.simantics.browsing.ui.model.InvalidContribution;
+import org.simantics.browsing.ui.model.nodetypes.NodeType;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
+import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.exception.PendingVariableException;
+import org.simantics.viewpoint.ontology.ViewpointResource;
+
+/**
+ * A child contribution adds child nodes to the instances of a specific node type.
+ * @author Hannu Niemistö
+ */
+public class ChildContribution {
+       
+       double priority;
+       String identifier;
+    NodeType parentNodeType;
+    NodeType childNodeType;
+    ChildRule childRule;
+        
+    public ChildContribution(String identifier, double priority, NodeType parentNodeType, NodeType childNodeType,
+            ChildRule childRule) throws InvalidContribution {
+        if(!childRule.isCompatible(
+                parentNodeType.getContentType()
+                ))
+            throw new InvalidContribution("Child rule is not compatible with the parent content type.");
+        this.parentNodeType = parentNodeType;
+        this.childNodeType = childNodeType;
+        this.childRule = childRule;
+        this.identifier = identifier;
+        this.priority = priority;
+    }
+
+    public static ChildContribution createCached(ReadGraph g, Resource childContributionResource) throws DatabaseException, InvalidContribution {
+        try {
+            return g.syncRequest(new ResourceRead<ChildContribution>(childContributionResource) {
+                @Override
+                public ChildContribution perform(ReadGraph graph) throws DatabaseException {
+                    try {
+                        return create(g, resource);
+                    } catch (InvalidContribution e) {
+                        throw new DatabaseException(e);
+                    }
+                }
+            }, TransientCacheAsyncListener.instance());
+        } catch (DatabaseException e) {
+            Throwable c = e.getCause();
+            if (c instanceof InvalidContribution)
+                throw (InvalidContribution) c;
+            throw e;
+        }
+    }
+
+    public static ChildContribution create(ReadGraph g, Resource childContributionResource) throws DatabaseException, InvalidContribution {
+        ViewpointResource vr = ViewpointResource.getInstance(g);
+        
+        Resource parentNodeTypeResource = g.getSingleObject(childContributionResource, vr.ChildContribution_HasParentNodeType);
+        NodeType parentNodeType = g.adapt(parentNodeTypeResource, NodeType.class);
+        
+        Resource childNodeTypeResource = g.getSingleObject(childContributionResource, vr.ChildContribution_HasChildNodeType);
+        NodeType childNodeType = g.adapt(childNodeTypeResource, NodeType.class);
+        
+        Resource childRuleResource = g.getSingleObject(childContributionResource, vr.ChildContribution_HasRule);
+        ChildRule childRule = g.adapt(childRuleResource, ChildRule.class);
+        
+        String identifier = g.getPossibleRelatedValue(childContributionResource, vr.ChildContribution_identifier, Bindings.STRING);
+        if(identifier == null) identifier = "";
+        if("".equals(identifier)) {
+               identifier = g.getPossibleURI(childContributionResource);
+            if(identifier == null) identifier = NameUtils.getSafeName(g, childContributionResource, true);
+        }
+        
+        Double priority = g.getPossibleRelatedValue(childContributionResource, vr.ChildContribution_priority, Bindings.DOUBLE);
+        if(priority == null) priority = 0.0;
+        
+        return new ChildContribution(identifier, priority, parentNodeType, childNodeType, childRule);
+        
+    }
+
+    public NodeType getParentNodeType() {
+        return parentNodeType;
+    }
+    
+    public NodeType getChildNodeType() {
+        return childNodeType;
+    }
+    
+    public String getIdentifier() {
+       return identifier;
+    }
+
+    public double getPriority() {
+       return priority;
+    }
+    
+    /**
+     * Given a parent node context returns a child node contexts contributed by
+     * this contribution.
+     */
+    public Collection<NodeContext> getChildren(ReadGraph graph, NodeContext parent) {
+        try {
+            Object parentContent = parent.getConstant(BuiltinKeys.INPUT);
+            Collection<?> childContents = childRule.getChildren(graph, parentContent);
+            if(childContents.isEmpty())
+                return Collections.emptyList();
+            ArrayList<NodeContext> children = new ArrayList<NodeContext>(childContents.size());
+            for(Object childContent : childContents) {
+                NodeContext child = childNodeType.createNodeContext(graph, childContent);
+                if(child != null)
+                    children.add(child);
+            }
+            return children;
+        } catch(PendingVariableException e) {
+            return Collections.emptyList();
+        } catch(DatabaseException e) {
+            e.printStackTrace();
+            // TODO return some kind of error node
+            return Collections.emptyList();
+        }
+    }
+    
+    /**
+     * Given a child node context returns a collection of possible parent node contexts.
+     */
+    public Collection<NodeContext> getParents(ReadGraph graph, NodeContext child) {
+        try {
+            Object childContent = child.getConstant(BuiltinKeys.INPUT);
+            Collection<?> parentContents = childRule.getParents(graph, childContent);
+            if(parentContents.isEmpty())
+                return Collections.emptyList();
+            ArrayList<NodeContext> parents = new ArrayList<NodeContext>(parentContents.size());
+            for(Object parentContent : parentContents) {
+                NodeContext parent = parentNodeType.createNodeContext(graph, parentContent);
+                if(parent != null)
+                    parents.add(parent);
+            }
+            return parents;
+        } catch(DatabaseException e) {
+            e.printStackTrace();
+            return Collections.emptyList();
+        }
+    }
+
+    public boolean hasChildren(ReadGraph graph, NodeContext parent) {
+        try {
+            Object parentContent = parent.getConstant(BuiltinKeys.INPUT);
+            Collection<?> childContents = childRule.getChildren(graph, parentContent);
+            if(childContents.isEmpty())
+                return false;
+            for(Object childContent : childContents) {
+                NodeContext child = childNodeType.createNodeContext(graph, childContent);
+                if(child != null)
+                    return true;
+            }
+        } catch(DatabaseException e) {
+            e.printStackTrace();
+        }
+        return false;
+    }
+    
+    @Override
+    public String toString() {
+       return identifier;
+    }
+    
+}