]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/NamespaceMigrationStep.java
Merge "Remove unnecessary getComparableKey from HashMapBinding"
[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 java.util.ArrayList;
15
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.simantics.databoard.Bindings;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.Session;
21 import org.simantics.db.common.utils.ListUtils;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.graph.query.Path;
24 import org.simantics.graph.query.TransferableGraphConversion;
25 import org.simantics.graph.query.UriUtils;
26 import org.simantics.graph.refactoring.GraphRefactoringException;
27 import org.simantics.graph.refactoring.GraphRefactoringUtils;
28 import org.simantics.graph.refactoring.MappingSpecification;
29 import org.simantics.graph.refactoring.MappingSpecification.MappingRule;
30 import org.simantics.graph.representation.Identity;
31 import org.simantics.graph.representation.Internal;
32 import org.simantics.graph.representation.TransferableGraph1;
33 import org.simantics.graph.store.IdentityStore;
34 import org.simantics.layer0.Layer0;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import gnu.trove.set.hash.TIntHashSet;
39
40 public class NamespaceMigrationStep implements MigrationStep {
41     private static final Logger LOGGER = LoggerFactory.getLogger(NamespaceMigrationStep.class);
42
43     final ArrayList<MappingRule> rules;
44     
45         public NamespaceMigrationStep(ReadGraph graph, Resource step) throws DatabaseException {
46                 rules = new ArrayList<MappingRule>();
47                 Layer0 L0 = Layer0.getInstance(graph);
48                 for(Resource prefix : ListUtils.toList(graph, step)) {
49                         String base = graph.getPossibleRelatedValue(prefix, L0.NamespaceMigrationStep_Prefix_base, Bindings.STRING);
50                         String from = graph.getPossibleRelatedValue(prefix, L0.NamespaceMigrationStep_Prefix_from, Bindings.STRING);
51                         String to = graph.getPossibleRelatedValue(prefix, L0.NamespaceMigrationStep_Prefix_to, Bindings.STRING);
52                         if(from != null && to != null) {
53                                 Path fromURI = UriUtils.uriToPath(base+from);
54                                 Path toURI = UriUtils.uriToPath(base+to);
55                                 if(fromURI != null && toURI != null)
56                                         rules.add(new MappingRule(fromURI, toURI));
57                                 else
58                                     LOGGER.error("Namespace migration uri formation error: base " + base + " from " + from + " to " + to);
59                         }
60                 }
61         }
62
63     @Override
64     public void applyTo(IProgressMonitor monitor, Session session, MigrationState state) throws DatabaseException {
65         
66         TransferableGraph1 tg = MigrationUtils.getTG(session, state); 
67                 
68         try {
69             MappingSpecification mappingSpec = new MappingSpecification(rules);
70             boolean fixed = GraphRefactoringUtils.fixIncorrectRoot(tg.identities);
71             LOGGER.info("fixed=" + fixed);
72             IdentityStore idStore = TransferableGraphConversion.extractIdentities(tg);
73             // Mark internal identities new
74             for(Identity id : tg.identities)
75                 if(id.definition instanceof Internal)
76                     idStore.markNew(id.resource);
77 //            idStore.printChildMap();
78 //            System.err.println("ids: " + idStore);
79 //            System.err.println("rc: " + tg.resourceCount);
80 //            System.err.println("idStore: " + idStore.toArray().length);
81             TIntHashSet parentsAffected = new TIntHashSet(); 
82             GraphRefactoringUtils.refactor(tg, idStore, mappingSpec, parentsAffected);
83 //
84             tg.resourceCount = idStore.getResourceCount();
85             
86             tg.identities = idStore.toArray();
87
88             if(fixed)
89                 GraphRefactoringUtils.unfixIncorrectRoot(tg.identities);
90
91             LOGGER.info("rc2: " + tg.resourceCount);
92 //            System.err.println("idStore2: " + idStore.toArray().length);
93
94         } catch (GraphRefactoringException e) {
95             e.printStackTrace();
96         }
97         
98     }
99     
100 }