]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/request/DelayedWriteRequest.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / request / DelayedWriteRequest.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 java.util.Map;
15
16 import org.simantics.db.AsyncRequestProcessor;
17 import org.simantics.db.ChangeSetIdentifier;
18 import org.simantics.db.Operation;
19 import org.simantics.db.RequestProcessor;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.procedure.Procedure;
22 import org.simantics.db.request.DelayedWrite;
23 import org.simantics.db.request.UndoTraits;
24 import org.simantics.db.request.WriteInterface;
25
26
27 /**
28  * Usage samples:
29  * 
30  * <p>
31  * Synchronous read without checked exceptions:
32  * </p>
33  * <pre>
34  * Resource r = new SimpleReadGraphRequest&lt;Resource&gt;(session) {
35  *     {@code @Override}
36  *     public Resource run(Graph g) {
37  *         Resource r = doSomethingThatMayFail();
38  *         return r;
39  *     }
40  * }.sync();
41  * </pre>
42  * 
43  * <p>
44  * Synchronous read with a selected checked exception that may be thrown:
45  * </p>
46  * <pre>
47  * Resource r = new SimpleReadGraphRequest&lt;Resource&gt;(session) {
48  *     {@code @Override}
49  *     public Resource run(Graph g) throws CheckedException {
50  *         Resource r = doSomethingThatMayFailWithCheckedException();
51  *         return r;
52  *     }
53  * }.sync(CheckedException.class);
54  * </pre>
55  * 
56  * <p>
57  * Asynchronous read with a completed() method for examining the result:
58  * </p>
59  * <pre>
60  * Resource r = new SimpleReadGraphRequest&lt;Resource&gt;(session) {
61  *     {@code @Override}
62  *     public Resource run(Graph g) {
63  *         Resource r = doSomethingThatMayFail();
64  *         return r;
65  *     }
66  *     public void completed(Resource result) {
67  *         // quickly dispatch the result somewhere, do nothing serious in this code.
68  *     }
69  * }.async();
70  * </pre>
71  * 
72  * @param <T> the result type
73  */
74 public abstract class DelayedWriteRequest implements DelayedWrite, UndoTraits, WriteInterface<Object> {
75
76     @Override
77     public ChangeSetIdentifier getIdentifier() {
78         return null;
79     }
80
81     @Override
82     public void fillMetadata(Map<String, String> metadata) {
83         metadata.put("class=", this.getClass().getName());
84     }
85     
86     @Override
87     public void commitOk(Operation op) {
88     }
89
90     @Override
91     public void request(AsyncRequestProcessor processor, final Procedure<Object> procedure) {
92         processor.asyncRequest(this, parameter -> {
93                 if(parameter != null) procedure.exception(parameter);
94                 else procedure.execute(null);
95                 });
96     }
97     
98     @Override
99     public Object request(RequestProcessor processor) throws DatabaseException {
100         processor.syncRequest(this);
101         return null;
102     }
103     
104 }