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