]> gerrit.simantics Code Review - simantics/platform.git/blob
22059cc21a0b493e97cb230144792687b44611f6
[simantics/platform.git] /
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.diagram.synchronization.graph;
13
14 import org.simantics.db.WriteGraph;
15 import org.simantics.db.common.request.WriteRequest;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.request.Write;
18 import org.simantics.diagram.internal.DebugPolicy;
19 import org.simantics.diagram.internal.timing.GTask;
20 import org.simantics.diagram.internal.timing.Timing;
21 import org.simantics.diagram.synchronization.ErrorHandler;
22 import org.simantics.diagram.synchronization.IModification;
23 import org.simantics.utils.logging.TimeLogger;
24
25 /**
26  * A graph database write request that composes several {@link IModification}
27  * instances into a single {@link Write} request.
28  * 
29  * @author Tuukka Lehtonen
30  * 
31  * @see IModification
32  */
33 public class ModificationProcessRequest extends WriteRequest {
34
35     ErrorHandler    errorHandler;
36     IModification[] modifications;
37
38     public ModificationProcessRequest(ErrorHandler errorHandler, IModification[] modifications) {
39         this.errorHandler = errorHandler;
40         this.modifications = modifications;
41     }
42
43     @Override
44     public void perform(final WriteGraph g) throws DatabaseException {
45         g.markUndoPoint();
46         if (DebugPolicy.DEBUG_GRAPH_WRITEBACK)
47             System.out.println(Thread.currentThread() + " Performing " + modifications.length + " modifications");
48         Timing.timed("" + modifications.length + " MODIFICATIONS", new GTask() {
49             @Override
50             public void run() throws DatabaseException {
51                 for (IModification m : modifications) {
52                     if (m != null) {
53                         modification(g, m);
54                     }
55                 }
56             }
57         });
58     }
59
60     public void modification(final WriteGraph g, final IModification m) throws DatabaseException {
61         Timing.timed(m.toString(), new GTask() {
62             @Override
63             public void run() throws DatabaseException {
64                 synchronized (m) {
65                     try {
66                         if (DebugPolicy.DEBUG_GRAPH_WRITEBACK_MODIFICATION)
67                             System.out.println(Thread.currentThread() + "  Performing modification: " + m);
68                         m.perform(g);
69                     } catch (Exception e) {
70                         m.setException(e);
71                         errorHandler.error(e.getMessage(), e);
72                     } catch (Error e) {
73                         m.setException(e);
74                         errorHandler.error(e.getMessage(), e);
75                     } finally {
76                         m.markComplete();
77                         m.notifyAll();
78                     }
79                 }
80             }
81         });
82     }
83
84 }