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.changeset;
14 import java.lang.reflect.Constructor;
15 import java.lang.reflect.InvocationTargetException;
17 import org.simantics.db.ChangeSet;
18 import org.simantics.db.MetadataI;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Session;
21 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
22 import org.simantics.db.common.utils.Logger;
23 import org.simantics.db.event.ChangeEvent;
24 import org.simantics.db.event.ChangeListener;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.request.Read;
27 import org.simantics.utils.ReflectionUtils;
30 * A listener added to a Session for receiving notifications for completed
33 * @see GraphChangeEvent
36 abstract public class GenericChangeListener<Request, Result> implements ChangeListener {
38 private final Constructor<Read<Result>> constructor;
40 public GenericChangeListener() {
43 Class<Read<Result>> clazz = ReflectionUtils.getSingleParameterTypeExtending(getClass());
44 this.constructor = clazz.getConstructor(ChangeSet.class);
46 } catch (SecurityException e) {
47 Logger.defaultLogError(e);
48 } catch (NoSuchMethodException e) {
49 Logger.defaultLogError(e);
51 throw new IllegalArgumentException();
55 final public void graphChanged(ChangeEvent e) throws DatabaseException {
58 if (!preEventRequest())
61 Result event = e.getGraph().syncRequest(constructor.newInstance(e.getChanges()), TransientCacheListener.<Result>instance());
62 onEvent(e.getGraph(), e.getMetadataI(), event);
63 } catch (IllegalArgumentException e1) {
64 Logger.defaultLogError(e1);
65 } catch (InstantiationException e1) {
66 Logger.defaultLogError(e1);
67 } catch (IllegalAccessException e1) {
68 Logger.defaultLogError(e1);
69 } catch (InvocationTargetException e1) {
70 Logger.defaultLogError(e1.getCause());
76 * Invoked before performing the event request with the received change
77 * event and sending the result to {@link #onEvent(ReadGraph, Object)}. Can
78 * be used to veto processing of an event.
80 * @return <code>true</code> if the event request shall be performed and the
81 * result sent to {@link #onEvent(ReadGraph, Object)},
82 * <code>false</code> to disable any further processing of the
83 * received db {@link ChangeEvent}
86 public boolean preEventRequest() {
90 public abstract void onEvent(ReadGraph graph, MetadataI metadata, Result event) throws DatabaseException;