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.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;
33 * Synchronous read without checked exceptions:
36 * Resource r = new SimpleReadGraphRequest<Resource>(session) {
38 * public Resource run(Graph g) {
39 * Resource r = doSomethingThatMayFail();
46 * Synchronous read with a selected checked exception that may be thrown:
49 * Resource r = new SimpleReadGraphRequest<Resource>(session) {
51 * public Resource run(Graph g) throws CheckedException {
52 * Resource r = doSomethingThatMayFailWithCheckedException();
55 * }.sync(CheckedException.class);
59 * Asynchronous read with a completed() method for examining the result:
62 * Resource r = new SimpleReadGraphRequest<Resource>(session) {
64 * public Resource run(Graph g) {
65 * Resource r = doSomethingThatMayFail();
68 * public void completed(Resource result) {
69 * // quickly dispatch the result somewhere, do nothing serious in this code.
74 * @param <T> the result type
76 public abstract class DelayedWriteRequest implements DelayedWrite, UndoTraits, WriteInterface<Object> {
79 public VirtualGraph getProvider() {
84 public UndoTraits getUndoTraits() {
89 public ChangeSetIdentifier getIdentifier() {
94 public void fillMetadata(Map<String, String> metadata) {
95 metadata.put("class=", this.getClass().getName());
99 public void commitOk(Operation op) {
103 public void request(AsyncRequestProcessor processor, final Procedure<Object> procedure) {
104 processor.asyncRequest(this, new Callback<DatabaseException>() {
107 public void run(DatabaseException parameter) {
108 if(parameter != null) procedure.exception(parameter);
109 else procedure.execute(null);
116 public Object request(RequestProcessor processor) throws DatabaseException {
117 processor.syncRequest(this);