]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/OrderedSetToListMigrationStep.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / migration / OrderedSetToListMigrationStep.java
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/OrderedSetToListMigrationStep.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/OrderedSetToListMigrationStep.java
new file mode 100644 (file)
index 0000000..b7180b7
--- /dev/null
@@ -0,0 +1,89 @@
+/*******************************************************************************\r
+ * Copyright (c) 2012 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.layer0.migration;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.PossibleObjectWithType;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.common.utils.ListUtils;\r
+import org.simantics.db.common.utils.OrderedSetUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.Instances;\r
+import org.simantics.layer0.Layer0;\r
+\r
+public class OrderedSetToListMigrationStep implements MigrationStep {\r
+    \r
+    final ArrayList<OrderedSetToListMapping> rules;\r
+    \r
+    private class OrderedSetToListMapping {\r
+        public Resource entityType, orderedSet, orderedSetRelation, list, listRelation;\r
+    }\r
+    \r
+    public OrderedSetToListMigrationStep(ReadGraph graph, Resource step) throws DatabaseException {\r
+        rules = new ArrayList<OrderedSetToListMapping>();\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        for(Resource change : ListUtils.toList(graph, step)) {\r
+            String entityType = graph.getPossibleRelatedValue(change, L0.OrderedSetToListMigrationStep_entityType, Bindings.STRING);\r
+            String orderedSet = graph.getPossibleRelatedValue(change, L0.OrderedSetToListMigrationStep_orderedSetType, Bindings.STRING);\r
+            String orderedSetRelation = graph.getPossibleRelatedValue(change, L0.OrderedSetToListMigrationStep_orderedSetRelation, Bindings.STRING);\r
+            String list = graph.getPossibleRelatedValue(change, L0.OrderedSetToListMigrationStep_listType, Bindings.STRING);\r
+            String listRelation = graph.getPossibleRelatedValue(change, L0.OrderedSetToListMigrationStep_listRelation, Bindings.STRING);\r
+            \r
+            if(orderedSet != null && orderedSetRelation != null && list != null && listRelation != null) {\r
+                OrderedSetToListMapping mapping = new OrderedSetToListMapping();\r
+                mapping.entityType = graph.getResource(entityType);\r
+                mapping.orderedSet = graph.getResource(orderedSet);\r
+                mapping.orderedSetRelation = graph.getResource(orderedSetRelation);\r
+                mapping.list = graph.getResource(list);\r
+                mapping.listRelation = graph.getResource(listRelation);\r
+                rules.add(mapping);\r
+            }\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public void applyTo(IProgressMonitor monitor, Session session, MigrationState state) throws DatabaseException {\r
+        final Resource resource = MigrationUtils.getResource(monitor, session, state);\r
+        \r
+        if(resource != null) {\r
+            session.syncRequest(new WriteRequest() {\r
+                \r
+                @Override\r
+                public void perform(WriteGraph graph) throws DatabaseException {\r
+                    for(OrderedSetToListMapping mapping : rules) {\r
+                        Instances entityFinder = graph.adapt(mapping.entityType, Instances.class);\r
+                        for(Resource entity : entityFinder.find(graph, resource)) {\r
+                            Resource orderedSet = graph.syncRequest(new PossibleObjectWithType(entity, mapping.orderedSetRelation, mapping.orderedSet));\r
+                            if(orderedSet != null) {\r
+                                Collection<Resource> list = OrderedSetUtils.toList(graph, orderedSet);\r
+                                graph.deny(entity, mapping.orderedSetRelation, orderedSet);\r
+                                graph.claim(entity, mapping.listRelation, ListUtils.create(graph, mapping.list, list));\r
+                            }\r
+                        }\r
+\r
+                    }                    \r
+                }\r
+            });\r
+        }\r
+        \r
+    }\r
+\r
+\r
+}\r