]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/changeset/GenericChangeListener.java
a971e02ca77bd87494084f7b1d567ebd6dd9c2e1
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / changeset / GenericChangeListener.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.changeset;
13
14 import java.lang.reflect.Constructor;
15 import java.lang.reflect.InvocationTargetException;
16
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;
28
29 /**
30  * A listener added to a Session for receiving notifications for completed
31  * write transactions.
32  * 
33  * @see GraphChangeEvent
34  * @see Session
35  */
36 abstract public class GenericChangeListener<Request, Result> implements ChangeListener {
37
38     private final Constructor<Read<Result>> constructor;
39
40     public GenericChangeListener() {
41
42         try {
43             Class<Read<Result>> clazz = ReflectionUtils.getSingleParameterTypeExtending(getClass());
44             this.constructor = clazz.getConstructor(ChangeSet.class);
45             return;
46         } catch (SecurityException e) {
47             Logger.defaultLogError(e);
48         } catch (NoSuchMethodException e) {
49             Logger.defaultLogError(e);
50         }
51         throw new IllegalArgumentException();
52
53     }
54
55     final public void graphChanged(ChangeEvent e) throws DatabaseException {
56
57         try {
58             if (!preEventRequest())
59                 return;
60
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());
71         }
72
73     }
74
75     /**
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.
79      * 
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}
84      * @since 1.8
85      */
86     public boolean preEventRequest() {
87         return true;
88     }
89
90     public abstract void onEvent(ReadGraph graph, MetadataI metadata, Result event) throws DatabaseException;
91
92 }