]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/UpdateComponentUids.java
5ace8a2c345374035754e3ca8fa90de8d24a51ef
[simantics/platform.git] / bundles / org.simantics.structural.synchronization.client / src / org / simantics / structural / synchronization / base / UpdateComponentUids.java
1 package org.simantics.structural.synchronization.base;\r
2 \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
8 \r
9 /**\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
14  * \r
15  * @author Hannu Niemistö\r
16  * @author Tuukka Lehtonen\r
17  */\r
18 public class UpdateComponentUids<T extends ComponentBase<T>> {\r
19 \r
20     public static interface ComponentUpdateProgressMonitor {\r
21         void componentsDone(int components, int componentCount);\r
22     }\r
23 \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
29 \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
33         this.graph = graph;\r
34         this.componentCount = componentCount;\r
35     }\r
36 \r
37     private void update(T component, Variable variable) throws DatabaseException {\r
38         component.uid = variable.getRVI(graph).toString();\r
39 \r
40         // Handle progress monitoring and cancellation\r
41         counter++;\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
47         }\r
48 \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
54         }\r
55     }\r
56 \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
59     }\r
60 \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
63     }\r
64 \r
65     public static <T extends ComponentBase<T>> int countComponents(T component) {\r
66         return new Counter<T>().count(component);\r
67     }\r
68 \r
69     private static class Counter<T extends ComponentBase<T>> {\r
70         int counter;\r
71         int count(T component) {\r
72             ++counter;\r
73             for (T child : component.getChildren())\r
74                 count(child);\r
75             return counter;\r
76         }\r
77     }\r
78 \r
79 }\r