]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/request/DelayedWriteRequest.java
044e91acc7e6e9922158f3b18d833f99846daae1
[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 import org.simantics.utils.datastructures.Callback;
26
27
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 DelayedWriteRequest implements DelayedWrite, UndoTraits, WriteInterface<Object> {
76
77     @Override
78     public ChangeSetIdentifier getIdentifier() {
79         return null;
80     }
81
82     @Override
83     public void fillMetadata(Map<String, String> metadata) {
84         metadata.put("class=", this.getClass().getName());
85     }
86     
87     @Override
88     public void commitOk(Operation op) {
89     }
90
91     @Override
92     public void request(AsyncRequestProcessor processor, final Procedure<Object> procedure) {
93         processor.asyncRequest(this, new Callback<DatabaseException>() {
94                         
95                         @Override
96                         public void run(DatabaseException parameter) {
97                                 if(parameter != null) procedure.exception(parameter);
98                                 else procedure.execute(null);
99                         }
100                         
101                 });
102     }
103     
104     @Override
105     public Object request(RequestProcessor processor) throws DatabaseException {
106         processor.syncRequest(this);
107         return null;
108     }
109     
110 }