1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.common.request;
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;
32 * Synchronous read without checked exceptions:
35 * Resource r = new SimpleReadGraphRequest<Resource>(session) {
37 * public Resource run(Graph g) {
38 * Resource r = doSomethingThatMayFail();
45 * Synchronous read with a selected checked exception that may be thrown:
48 * Resource r = new SimpleReadGraphRequest<Resource>(session) {
50 * public Resource run(Graph g) throws CheckedException {
51 * Resource r = doSomethingThatMayFailWithCheckedException();
54 * }.sync(CheckedException.class);
58 * Asynchronous read with a completed() method for examining the result:
61 * Resource r = new SimpleReadGraphRequest<Resource>(session) {
63 * public Resource run(Graph g) {
64 * Resource r = doSomethingThatMayFail();
67 * public void completed(Resource result) {
68 * // quickly dispatch the result somewhere, do nothing serious in this code.
73 * @param <T> the result type
75 public abstract class DelayedWriteRequest implements DelayedWrite, UndoTraits, WriteInterface<Object> {
78 public ChangeSetIdentifier getIdentifier() {
83 public void fillMetadata(Map<String, String> metadata) {
84 metadata.put("class=", this.getClass().getName());
88 public void commitOk(Operation op) {
92 public void request(AsyncRequestProcessor processor, final Procedure<Object> procedure) {
93 processor.asyncRequest(this, new Callback<DatabaseException>() {
96 public void run(DatabaseException parameter) {
97 if(parameter != null) procedure.exception(parameter);
98 else procedure.execute(null);
105 public Object request(RequestProcessor processor) throws DatabaseException {
106 processor.syncRequest(this);