1 package org.simantics.structural.synchronization.base;
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;
10 * Utility for updating UIDs in the given component to correspond
11 * the RVIs of the given variable. This operation is needed when
12 * the mapping is part of a model that is imported to a new database
13 * and resource ids have been changed.
15 * @author Hannu Niemistö
16 * @author Tuukka Lehtonen
18 public class UpdateComponentUids<T extends ComponentBase<T>> {
20 public static interface ComponentUpdateProgressMonitor {
21 void componentsDone(int components, int componentCount);
24 private IProgressMonitor monitor;
25 private ComponentUpdateProgressMonitor componentMonitor;
26 private ReadGraph graph;
27 private int componentCount;
30 private UpdateComponentUids(IProgressMonitor monitor, ReadGraph graph, int componentCount) {
31 this.monitor = monitor;
32 this.componentMonitor = monitor instanceof ComponentUpdateProgressMonitor ? (ComponentUpdateProgressMonitor) monitor : null;
34 this.componentCount = componentCount;
37 private void update(T component, Variable variable) throws DatabaseException {
38 component.uid = variable.getRVI(graph).toString();
40 // Handle progress monitoring and cancellation
42 if (monitor != null) {
43 if (componentMonitor != null)
44 componentMonitor.componentsDone(counter, componentCount);
45 if (monitor.isCanceled())
46 throw new CancelTransactionException();
49 for(Variable childVariable : variable.getChildren(graph)) {
50 String childName = childVariable.getName(graph);
51 T childComponent = component.getChild(childName);
52 if(childComponent != null)
53 update(childComponent, childVariable);
57 public static <T extends ComponentBase<T>> void update(ReadGraph graph, T component, Variable variable) throws DatabaseException {
58 update(null, graph, component, variable);
61 public static <T extends ComponentBase<T>> void update(IProgressMonitor monitor, ReadGraph graph, T component, Variable variable) throws DatabaseException {
62 new UpdateComponentUids<T>(monitor, graph, countComponents(component)).update(component, variable);
65 public static <T extends ComponentBase<T>> int countComponents(T component) {
66 return new Counter<T>().count(component);
69 private static class Counter<T extends ComponentBase<T>> {
71 int count(T component) {
73 for (T child : component.getChildren())