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