]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/request/WriteOnlyRequest.java
03a4b4211352ff91c2c0ba9ff1b955bcc62842f9
[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.UndoTraits;
20 import org.simantics.db.request.WriteInterface;
21 import org.simantics.db.request.WriteOnly;
22 import org.simantics.utils.datastructures.Callback;
23
24
25 /**
26  * TODO: fix this javadoc to document the correct class!!
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 WriteOnlyRequest implements WriteOnly, WriteInterface<Object> {
75
76     private final VirtualGraph provider;
77
78     public WriteOnlyRequest() {
79         this.provider = null;
80     }
81
82     public WriteOnlyRequest(VirtualGraph provider) {
83         this.provider = provider;
84     }
85
86     @Override
87     final public VirtualGraph getProvider() {
88         return provider;
89     }
90
91     @Override
92     public UndoTraits getUndoTraits() {
93         return null;
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 }