]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/UpdateComponentUids.java
Merge "Refactoring of simulator toolkit"
[simantics/platform.git] / bundles / org.simantics.structural.synchronization.client / src / org / simantics / structural / synchronization / base / UpdateComponentUids.java
1 package org.simantics.structural.synchronization.base;
2
3 import org.eclipse.core.runtime.IProgressMonitor;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.exception.CancelTransactionException;
6 import org.simantics.db.exception.DatabaseException;
7 import org.simantics.db.layer0.variable.Variable;
8 import org.simantics.structural.synchronization.utils.ComponentBase;
9
10 import gnu.trove.map.hash.THashMap;
11
12 /**
13  * Utility for updating UIDs in the given component to correspond
14  * the RVIs of the given variable. This operation is needed when
15  * the mapping is part of a model that is imported to a new database
16  * and resource ids have been changed.
17  *
18  * @author Hannu Niemistö
19  * @author Tuukka Lehtonen
20  */
21 public class UpdateComponentUids<T extends ComponentBase<T>> {
22
23     public static interface ComponentUpdateProgressMonitor {
24         void componentsDone(int components, int componentCount);
25     }
26
27     private THashMap<String, String> oldToNewUids;
28     private IProgressMonitor monitor;
29     private ComponentUpdateProgressMonitor componentMonitor;
30     private ReadGraph graph;
31     private int componentCount;
32     private int counter;
33
34     private UpdateComponentUids(IProgressMonitor monitor, ReadGraph graph, int componentCount) {
35         this.monitor = monitor;
36         this.componentMonitor = monitor instanceof ComponentUpdateProgressMonitor ? (ComponentUpdateProgressMonitor) monitor : null;
37         this.graph = graph;
38         this.componentCount = componentCount;
39         this.oldToNewUids = new THashMap<>(componentCount);
40     }
41
42     private void update(T component, Variable variable) throws DatabaseException {
43         String newUid = variable.getRVI(graph).toString();
44         oldToNewUids.put(component.uid, newUid);
45         component.uid = newUid;
46
47         // Handle progress monitoring and cancellation
48         counter++;
49         if (monitor != null) {
50             if (componentMonitor != null)
51                 componentMonitor.componentsDone(counter, componentCount);
52             if (monitor.isCanceled())
53                 throw new CancelTransactionException();
54         }
55
56         for(Variable childVariable : variable.getChildren(graph)) {
57             String childName = childVariable.getName(graph);
58             T childComponent = component.getChild(childName);
59             if(childComponent != null)
60                 update(childComponent, childVariable);
61         }
62     }
63
64     public static <T extends ComponentBase<T>> THashMap<String, String> update(ReadGraph graph, T component, Variable variable) throws DatabaseException {
65         return update(null, graph, component, variable);
66     }
67
68     public static <T extends ComponentBase<T>> THashMap<String, String> update(IProgressMonitor monitor, ReadGraph graph, T component, Variable variable) throws DatabaseException {
69         UpdateComponentUids<T> updateComponentUids = new UpdateComponentUids<T>(monitor, graph, countComponents(component));
70         updateComponentUids.update(component, variable);
71         return updateComponentUids.oldToNewUids;
72     }
73
74     public static <T extends ComponentBase<T>> int countComponents(T component) {
75         return new Counter<T>().count(component);
76     }
77
78     private static class Counter<T extends ComponentBase<T>> {
79         int counter;
80         int count(T component) {
81             ++counter;
82             for (T child : component.getChildren())
83                 count(child);
84             return counter;
85         }
86     }
87
88 }