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