]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/ContextualRelatedValue.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / ContextualRelatedValue.java
1 /*******************************************************************************
2  * Copyright (c) 2018 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  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.layer0.adapter;
13
14 import org.simantics.db.ConverterComputationalValue;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.exception.RuntimeDatabaseException;
19 import org.simantics.db.layer0.variable.Variable;
20 import org.simantics.scl.runtime.SCLContext;
21 import org.simantics.scl.runtime.function.Function1;
22 import org.simantics.scl.runtime.function.FunctionImpl3;
23
24 /**
25  * @author Antti Villberg
26  * @since 1.36.0
27  */
28 public abstract class ContextualRelatedValue implements ConverterComputationalValue {
29
30     private final FunctionImpl3<ReadGraph, Resource, Object, Object> function = new FunctionImpl3<ReadGraph, Resource, Object, Object>() {
31         @Override
32         public Object apply(ReadGraph graph, Resource converter, Object context) {
33             SCLContext sclContext = SCLContext.getCurrent();
34             Object oldGraph = sclContext.get("graph");
35             try {
36                 if (context instanceof Variable) {
37                     Variable variable = (Variable)context;
38                     try {
39                         Function1<Object,Object> fn = getFunction(graph, variable.getParent(graph).getRepresents(graph), variable.getRepresents(graph), variable.getPredicateResource(graph));
40                         sclContext.put("graph", graph);
41                         return fn.apply(variable);
42                     } catch (DatabaseException e) {
43                         throw new RuntimeDatabaseException(e);
44                     }
45                 } if (context instanceof Resource) {
46                     Resource resource = (Resource)context;
47                     try {
48                         // Here converter is the object and context is the subject
49                         Function1<Object,Object> fn = getFunction(graph, resource, converter, null);
50                         return fn.apply(resource);
51                     } catch (DatabaseException e) {
52                         throw new RuntimeDatabaseException(e);
53                     }
54                 } else {
55                     throw new IllegalStateException("Unknown context " + context);
56                 }
57             } finally {
58                 sclContext.put("graph", oldGraph);
59             }
60         }
61     };
62
63     @SuppressWarnings("unchecked")
64     @Override
65     public <T> T getValue(ReadGraph graph, Resource resource) throws DatabaseException {
66         return (T) function;
67     }
68
69 }