]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/request/TransientUnaryRead.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / request / TransientUnaryRead.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.exception.DatabaseException;
16 import org.simantics.db.service.QueryControl;
17
18 abstract public class TransientUnaryRead<P, R> extends BinaryRead<Object,P,R> {
19
20         protected static final Object WITH_PARENT = new Object();
21         
22         public TransientUnaryRead(ReadGraph graph, P parameter) throws DatabaseException {
23                 this(graph, graph.getService(QueryControl.class), parameter);
24         }
25         
26         public TransientUnaryRead(ReadGraph graph, QueryControl qc, P parameter) throws DatabaseException {
27                 super(resolveFirstParameter(graph, qc), parameter);
28         }
29
30         final private static Object resolveFirstParameter(ReadGraph graph, QueryControl qc) throws DatabaseException {
31                 if(qc.hasParentRequest(graph)) return WITH_PARENT;
32                 else return graph.getModificationCounter();
33         }
34
35         @Override
36         final public R perform(ReadGraph _graph) throws DatabaseException {
37                 if(parameter == WITH_PARENT) {
38                         return perform(_graph, parameter2);
39                 } else {
40                         QueryControl qc = _graph.getService(QueryControl.class);
41                         ReadGraph graph = qc.getIndependentGraph(_graph);
42                         return perform(graph, parameter2);
43                 }
44         }
45
46         abstract public R perform(ReadGraph graph, P parameter) throws DatabaseException;
47     
48 }