1 package org.simantics.structural.synchronization.client;
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;
10 import gnu.trove.map.hash.THashMap;
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.
18 * @author Hannu Niemistö
19 * @author Tuukka Lehtonen
21 public class UpdateComponentUids<T extends ComponentBase<T>> {
23 public static interface ComponentUpdateProgressMonitor {
24 void componentsDone(int components, int componentCount);
27 private THashMap<String, String> oldToNewUids;
28 private IProgressMonitor monitor;
29 private ComponentUpdateProgressMonitor componentMonitor;
30 private ReadGraph graph;
31 private int componentCount;
34 private UpdateComponentUids(IProgressMonitor monitor, ReadGraph graph, int componentCount) {
35 this.monitor = monitor;
36 this.componentMonitor = monitor instanceof ComponentUpdateProgressMonitor ? (ComponentUpdateProgressMonitor) monitor : null;
38 this.componentCount = componentCount;
39 this.oldToNewUids = new THashMap<>(componentCount);
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;
47 // Handle progress monitoring and cancellation
49 if (monitor != null) {
50 if (componentMonitor != null)
51 componentMonitor.componentsDone(counter, componentCount);
52 if (monitor.isCanceled())
53 throw new CancelTransactionException();
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);
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);
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;
74 public static <T extends ComponentBase<T>> int countComponents(T component) {
75 return new Counter<T>().count(component);
78 private static class Counter<T extends ComponentBase<T>> {
80 int count(T component) {
82 for (T child : component.getChildren())