1 package org.simantics.migration.ui;
3 import java.util.ArrayList;
4 import java.util.Collection;
6 import org.simantics.Simantics;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.WriteGraph;
10 import org.simantics.db.common.request.WriteRequest;
11 import org.simantics.db.common.utils.NameUtils;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.adapter.ActionFactory;
14 import org.simantics.db.request.Read;
15 import org.simantics.layer0.SoftwareConfigurationResources;
17 import gnu.trove.map.hash.THashMap;
19 public class MigrationAnalysis {
21 public class Update implements Comparable<Update> {
22 Resource updateResource;
23 Resource parentVersion;
24 Resource childVersion;
27 public Update(Resource updateResource, Resource parentVersion,
28 Resource childVersion, String versionName) {
29 this.updateResource = updateResource;
30 this.parentVersion = parentVersion;
31 this.childVersion = childVersion;
32 this.versionName = versionName;
36 public int compareTo(Update o) {
37 return versionName.compareTo(o.versionName);
40 public void execute(final Resource target) throws DatabaseException {
41 Update parentUpdate = updates.get(parentVersion);
42 if(parentUpdate != null)
43 parentUpdate.execute(target);
45 Collection<Runnable> migrateActions = Simantics.getSession().syncRequest(new Read<Collection<Runnable>>() {
47 public Collection<Runnable> perform(ReadGraph graph)
48 throws DatabaseException {
49 SoftwareConfigurationResources SC = SoftwareConfigurationResources.getInstance(graph);
50 ArrayList<Runnable> result = new ArrayList<Runnable>();
51 for(Resource action : graph.getObjects(updateResource, SC.Update_HasMigrationAction))
52 result.add(graph.adapt(action, ActionFactory.class).create(target));
56 for(Runnable action : migrateActions)
60 public void updateTargetVersion(final Resource target) throws DatabaseException {
61 Simantics.getSession().syncRequest(new WriteRequest() {
63 public void perform(WriteGraph graph) throws DatabaseException {
64 graph.markUndoPoint();
65 SoftwareConfigurationResources SC = SoftwareConfigurationResources.getInstance(graph);
66 graph.deny(target, SC.IsCompatibleWith, MigrationAnalysis.this.parentVersion);
67 graph.claim(target, SC.IsCompatibleWith, childVersion);
73 Resource parentVersion;
74 THashMap<Resource, Update> updates = new THashMap<Resource, Update>();
76 public MigrationAnalysis(ReadGraph g, Resource parentVersion) throws DatabaseException {
77 this.parentVersion = parentVersion;
78 updates.put(parentVersion, null);
79 findUpdatesFrom(g, parentVersion);
80 updates.remove(parentVersion);
83 private void findUpdatesFrom(ReadGraph g, Resource version) throws DatabaseException {
84 SoftwareConfigurationResources SC = SoftwareConfigurationResources.getInstance(g);
85 for(Resource update : g.getObjects(version, SC.Version_HasUpdate)) {
86 Resource to = g.getSingleObject(update, SC.Update_To);
87 if(!updates.contains(to)) {
88 String name = NameUtils.getSafeLabel(g, to);
89 updates.put(to, new Update(update, version, to, name));
90 findUpdatesFrom(g, to);
95 public Collection<Update> getUpdates() {
96 return updates.values();