--- /dev/null
+/*******************************************************************************\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 gnu.trove.set.hash.TIntHashSet;\r
+\r
+import java.util.ArrayList;\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.common.utils.ListUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.graph.query.Path;\r
+import org.simantics.graph.query.TransferableGraphConversion;\r
+import org.simantics.graph.query.UriUtils;\r
+import org.simantics.graph.refactoring.GraphRefactoringException;\r
+import org.simantics.graph.refactoring.GraphRefactoringUtils;\r
+import org.simantics.graph.refactoring.MappingSpecification;\r
+import org.simantics.graph.refactoring.MappingSpecification.MappingRule;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.graph.store.IdentityStore;\r
+import org.simantics.layer0.Layer0;\r
+\r
+public class NamespaceMigrationStep implements MigrationStep {\r
+ \r
+ final ArrayList<MappingRule> rules;\r
+ \r
+ public NamespaceMigrationStep(ReadGraph graph, Resource step) throws DatabaseException {\r
+ rules = new ArrayList<MappingRule>();\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ for(Resource prefix : ListUtils.toList(graph, step)) {\r
+ String base = graph.getPossibleRelatedValue(prefix, L0.NamespaceMigrationStep_Prefix_base, Bindings.STRING);\r
+ String from = graph.getPossibleRelatedValue(prefix, L0.NamespaceMigrationStep_Prefix_from, Bindings.STRING);\r
+ String to = graph.getPossibleRelatedValue(prefix, L0.NamespaceMigrationStep_Prefix_to, Bindings.STRING);\r
+ if(from != null && to != null) {\r
+ Path fromURI = UriUtils.uriToPath(base+from);\r
+ Path toURI = UriUtils.uriToPath(base+to);\r
+ if(fromURI != null && toURI != null)\r
+ rules.add(new MappingRule(fromURI, toURI));\r
+ else\r
+ System.err.println("Namespace migration uri formation error: base " + base + " from " + from + " to " + to);\r
+ }\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void applyTo(IProgressMonitor monitor, Session session, MigrationState state) throws DatabaseException {\r
+ \r
+ TransferableGraph1 tg = MigrationUtils.getTG(session, state); \r
+ \r
+ try {\r
+ MappingSpecification mappingSpec = new MappingSpecification(rules);\r
+ boolean fixed = GraphRefactoringUtils.fixIncorrectRoot(tg.identities);\r
+ System.err.println("fixed=" + fixed);\r
+ IdentityStore idStore = TransferableGraphConversion.extractIdentities(tg);\r
+ idStore.printChildMap();\r
+// System.err.println("ids: " + idStore);\r
+// System.err.println("rc: " + tg.resourceCount);\r
+// System.err.println("idStore: " + idStore.toArray().length);\r
+ TIntHashSet parentsAffected = new TIntHashSet(); \r
+ GraphRefactoringUtils.refactor(tg, idStore, mappingSpec, parentsAffected);\r
+//\r
+ tg.resourceCount = idStore.getResourceCount();\r
+ \r
+ tg.identities = idStore.toArray();\r
+\r
+ if(fixed)\r
+ GraphRefactoringUtils.unfixIncorrectRoot(tg.identities);\r
+\r
+ System.err.println("rc2: " + tg.resourceCount);\r
+// System.err.println("idStore2: " + idStore.toArray().length);\r
+\r
+ } catch (GraphRefactoringException e) {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ }\r
+ \r
+}\r