1 /*******************************************************************************
\r
2 * Copyright (c) 2012 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.layer0.migration;
\r
14 import gnu.trove.set.hash.TIntHashSet;
\r
16 import java.util.ArrayList;
\r
18 import org.eclipse.core.runtime.IProgressMonitor;
\r
19 import org.simantics.databoard.Bindings;
\r
20 import org.simantics.db.ReadGraph;
\r
21 import org.simantics.db.Resource;
\r
22 import org.simantics.db.Session;
\r
23 import org.simantics.db.common.utils.ListUtils;
\r
24 import org.simantics.db.exception.DatabaseException;
\r
25 import org.simantics.graph.query.Path;
\r
26 import org.simantics.graph.query.TransferableGraphConversion;
\r
27 import org.simantics.graph.query.UriUtils;
\r
28 import org.simantics.graph.refactoring.GraphRefactoringException;
\r
29 import org.simantics.graph.refactoring.GraphRefactoringUtils;
\r
30 import org.simantics.graph.refactoring.MappingSpecification;
\r
31 import org.simantics.graph.refactoring.MappingSpecification.MappingRule;
\r
32 import org.simantics.graph.representation.TransferableGraph1;
\r
33 import org.simantics.graph.store.IdentityStore;
\r
34 import org.simantics.layer0.Layer0;
\r
36 public class NamespaceMigrationStep implements MigrationStep {
\r
38 final ArrayList<MappingRule> rules;
\r
40 public NamespaceMigrationStep(ReadGraph graph, Resource step) throws DatabaseException {
\r
41 rules = new ArrayList<MappingRule>();
\r
42 Layer0 L0 = Layer0.getInstance(graph);
\r
43 for(Resource prefix : ListUtils.toList(graph, step)) {
\r
44 String base = graph.getPossibleRelatedValue(prefix, L0.NamespaceMigrationStep_Prefix_base, Bindings.STRING);
\r
45 String from = graph.getPossibleRelatedValue(prefix, L0.NamespaceMigrationStep_Prefix_from, Bindings.STRING);
\r
46 String to = graph.getPossibleRelatedValue(prefix, L0.NamespaceMigrationStep_Prefix_to, Bindings.STRING);
\r
47 if(from != null && to != null) {
\r
48 Path fromURI = UriUtils.uriToPath(base+from);
\r
49 Path toURI = UriUtils.uriToPath(base+to);
\r
50 if(fromURI != null && toURI != null)
\r
51 rules.add(new MappingRule(fromURI, toURI));
\r
53 System.err.println("Namespace migration uri formation error: base " + base + " from " + from + " to " + to);
\r
59 public void applyTo(IProgressMonitor monitor, Session session, MigrationState state) throws DatabaseException {
\r
61 TransferableGraph1 tg = MigrationUtils.getTG(session, state);
\r
64 MappingSpecification mappingSpec = new MappingSpecification(rules);
\r
65 boolean fixed = GraphRefactoringUtils.fixIncorrectRoot(tg.identities);
\r
66 System.err.println("fixed=" + fixed);
\r
67 IdentityStore idStore = TransferableGraphConversion.extractIdentities(tg);
\r
68 idStore.printChildMap();
\r
69 // System.err.println("ids: " + idStore);
\r
70 // System.err.println("rc: " + tg.resourceCount);
\r
71 // System.err.println("idStore: " + idStore.toArray().length);
\r
72 TIntHashSet parentsAffected = new TIntHashSet();
\r
73 GraphRefactoringUtils.refactor(tg, idStore, mappingSpec, parentsAffected);
\r
75 tg.resourceCount = idStore.getResourceCount();
\r
77 tg.identities = idStore.toArray();
\r
80 GraphRefactoringUtils.unfixIncorrectRoot(tg.identities);
\r
82 System.err.println("rc2: " + tg.resourceCount);
\r
83 // System.err.println("idStore2: " + idStore.toArray().length);
\r
85 } catch (GraphRefactoringException e) {
\r
86 e.printStackTrace();
\r