]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.migration.ui/src/org/simantics/migration/ui/MigrationAnalysis.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.migration.ui / src / org / simantics / migration / ui / MigrationAnalysis.java
1 package org.simantics.migration.ui;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5
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;
16
17 import gnu.trove.map.hash.THashMap;
18
19 public class MigrationAnalysis {
20     
21     public class Update implements Comparable<Update> {
22         Resource updateResource;
23         Resource parentVersion;
24         Resource childVersion;
25         String versionName;
26         
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;
33         }
34
35         @Override
36         public int compareTo(Update o) {
37             return versionName.compareTo(o.versionName);
38         }
39
40         public void execute(final Resource target) throws DatabaseException {
41             Update parentUpdate = updates.get(parentVersion);
42             if(parentUpdate != null)
43                 parentUpdate.execute(target);
44             
45             Collection<Runnable> migrateActions = Simantics.getSession().syncRequest(new Read<Collection<Runnable>>() {
46                 @Override
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));
53                     return result;
54                 }                
55             });
56             for(Runnable action : migrateActions)
57                 action.run();
58         }
59         
60         public void updateTargetVersion(final Resource target) throws DatabaseException {
61             Simantics.getSession().syncRequest(new WriteRequest() {                        
62                 @Override
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);
68                 }
69             });
70         }
71     }
72     
73     Resource parentVersion;
74     THashMap<Resource, Update> updates = new THashMap<Resource, Update>();
75
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);
81     }
82
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);
91             }
92         }
93     }
94     
95     public Collection<Update> getUpdates() {
96         return updates.values();
97     }
98 }