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