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