]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/request/TransientResourceRead.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / request / TransientResourceRead.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.common.request;\r
13 \r
14 import org.simantics.db.ReadGraph;\r
15 import org.simantics.db.Resource;\r
16 import org.simantics.db.exception.DatabaseException;\r
17 import org.simantics.db.request.ReadExt;\r
18 import org.simantics.db.service.QueryControl;\r
19 \r
20 abstract public class TransientResourceRead<R> extends BinaryRead<Object,Resource,R> implements ReadExt {\r
21 \r
22         protected static final Object WITH_PARENT = new Object();\r
23         \r
24         public TransientResourceRead(ReadGraph graph, Resource parameter) throws DatabaseException {\r
25                 this(graph, graph.getService(QueryControl.class), parameter);\r
26         }\r
27         \r
28         public TransientResourceRead(ReadGraph graph, QueryControl qc, Resource parameter) throws DatabaseException {\r
29                 super(resolveFirstParameter(graph, qc), parameter);\r
30         }\r
31 \r
32         final private static Object resolveFirstParameter(ReadGraph graph, QueryControl qc) throws DatabaseException {\r
33                 if(qc.hasParentRequest(graph)) return WITH_PARENT;\r
34                 else return graph.getModificationCounter();\r
35         }\r
36 \r
37         @Override\r
38         final public R perform(ReadGraph _graph) throws DatabaseException {\r
39                 if(parameter == WITH_PARENT) {\r
40                         return perform(_graph, parameter2);\r
41                 } else {\r
42                         QueryControl qc = _graph.getService(QueryControl.class);\r
43                         ReadGraph graph = qc.getIndependentGraph(_graph);\r
44                         return perform(graph, parameter2);\r
45                 }\r
46         }\r
47 \r
48         abstract public R perform(ReadGraph graph, Resource parameter) throws DatabaseException;\r
49     \r
50         @Override\r
51         public boolean isImmutable(ReadGraph graph) throws DatabaseException {\r
52                 return graph.isImmutable(parameter2);\r
53         }\r
54         \r
55 }\r