]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/request/TransientResourceRead.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / request / TransientResourceRead.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in 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.db.common.request;
13
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.request.ReadExt;
18 import org.simantics.db.service.QueryControl;
19
20 abstract public class TransientResourceRead<R> extends BinaryRead<Object,Resource,R> implements ReadExt {
21
22         protected static final Object WITH_PARENT = new Object();
23         
24         public TransientResourceRead(ReadGraph graph, Resource parameter) throws DatabaseException {
25                 this(graph, graph.getService(QueryControl.class), parameter);
26         }
27         
28         public TransientResourceRead(ReadGraph graph, QueryControl qc, Resource parameter) throws DatabaseException {
29                 super(resolveFirstParameter(graph, qc), parameter);
30         }
31
32         final private static Object resolveFirstParameter(ReadGraph graph, QueryControl qc) throws DatabaseException {
33                 if(qc.hasParentRequest(graph)) return WITH_PARENT;
34                 else return graph.getModificationCounter();
35         }
36
37         @Override
38         final public R perform(ReadGraph _graph) throws DatabaseException {
39                 if(parameter == WITH_PARENT) {
40                         return perform(_graph, parameter2);
41                 } else {
42                         QueryControl qc = _graph.getService(QueryControl.class);
43                         ReadGraph graph = qc.getIndependentGraph(_graph);
44                         return perform(graph, parameter2);
45                 }
46         }
47
48         abstract public R perform(ReadGraph graph, Resource parameter) throws DatabaseException;
49     
50         @Override
51         public boolean isImmutable(ReadGraph graph) throws DatabaseException {
52                 return graph.isImmutable(parameter2);
53         }
54         
55 }