]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/UpdateComponentUids.java
Fixed all line endings of the repository
[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
9 /**
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.
14  * 
15  * @author Hannu Niemistö
16  * @author Tuukka Lehtonen
17  */
18 public class UpdateComponentUids<T extends ComponentBase<T>> {
19
20     public static interface ComponentUpdateProgressMonitor {
21         void componentsDone(int components, int componentCount);
22     }
23
24     private IProgressMonitor monitor;
25     private ComponentUpdateProgressMonitor componentMonitor;
26     private ReadGraph graph;
27         private int componentCount;
28     private int counter;
29
30     private UpdateComponentUids(IProgressMonitor monitor, ReadGraph graph, int componentCount) {
31         this.monitor = monitor;
32         this.componentMonitor = monitor instanceof ComponentUpdateProgressMonitor ? (ComponentUpdateProgressMonitor) monitor : null;
33         this.graph = graph;
34         this.componentCount = componentCount;
35     }
36
37     private void update(T component, Variable variable) throws DatabaseException {
38         component.uid = variable.getRVI(graph).toString();
39
40         // Handle progress monitoring and cancellation
41         counter++;
42         if (monitor != null) {
43             if (componentMonitor != null)
44                 componentMonitor.componentsDone(counter, componentCount);
45             if (monitor.isCanceled())
46                 throw new CancelTransactionException();
47         }
48
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);
54         }
55     }
56
57     public static <T extends ComponentBase<T>> void update(ReadGraph graph, T component, Variable variable) throws DatabaseException {
58         update(null, graph, component, variable);
59     }
60
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);
63     }
64
65     public static <T extends ComponentBase<T>> int countComponents(T component) {
66         return new Counter<T>().count(component);
67     }
68
69     private static class Counter<T extends ComponentBase<T>> {
70         int counter;
71         int count(T component) {
72             ++counter;
73             for (T child : component.getChildren())
74                 count(child);
75             return counter;
76         }
77     }
78
79 }