X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.layer0%2Fsrc%2Forg%2Fsimantics%2Fdb%2Flayer0%2Fmigration%2FNamespaceMigrationStep.java;h=a0d4f9da5e202f87a893aa76556c942b83c81e1b;hb=ccb253462b54a0e036f0906ec8ac11a9021a968c;hp=d6da4e9061fd13e8c14ffd900597a025f1dd5bb3;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/NamespaceMigrationStep.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/NamespaceMigrationStep.java index d6da4e906..a0d4f9da5 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/NamespaceMigrationStep.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/NamespaceMigrationStep.java @@ -1,91 +1,100 @@ -/******************************************************************************* - * Copyright (c) 2012 Association for Decentralized Information Management in - * Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.db.layer0.migration; - -import gnu.trove.set.hash.TIntHashSet; - -import java.util.ArrayList; - -import org.eclipse.core.runtime.IProgressMonitor; -import org.simantics.databoard.Bindings; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.Session; -import org.simantics.db.common.utils.ListUtils; -import org.simantics.db.exception.DatabaseException; -import org.simantics.graph.query.Path; -import org.simantics.graph.query.TransferableGraphConversion; -import org.simantics.graph.query.UriUtils; -import org.simantics.graph.refactoring.GraphRefactoringException; -import org.simantics.graph.refactoring.GraphRefactoringUtils; -import org.simantics.graph.refactoring.MappingSpecification; -import org.simantics.graph.refactoring.MappingSpecification.MappingRule; -import org.simantics.graph.representation.TransferableGraph1; -import org.simantics.graph.store.IdentityStore; -import org.simantics.layer0.Layer0; - -public class NamespaceMigrationStep implements MigrationStep { - - final ArrayList rules; - - public NamespaceMigrationStep(ReadGraph graph, Resource step) throws DatabaseException { - rules = new ArrayList(); - Layer0 L0 = Layer0.getInstance(graph); - for(Resource prefix : ListUtils.toList(graph, step)) { - String base = graph.getPossibleRelatedValue(prefix, L0.NamespaceMigrationStep_Prefix_base, Bindings.STRING); - String from = graph.getPossibleRelatedValue(prefix, L0.NamespaceMigrationStep_Prefix_from, Bindings.STRING); - String to = graph.getPossibleRelatedValue(prefix, L0.NamespaceMigrationStep_Prefix_to, Bindings.STRING); - if(from != null && to != null) { - Path fromURI = UriUtils.uriToPath(base+from); - Path toURI = UriUtils.uriToPath(base+to); - if(fromURI != null && toURI != null) - rules.add(new MappingRule(fromURI, toURI)); - else - System.err.println("Namespace migration uri formation error: base " + base + " from " + from + " to " + to); - } - } - } - - @Override - public void applyTo(IProgressMonitor monitor, Session session, MigrationState state) throws DatabaseException { - - TransferableGraph1 tg = MigrationUtils.getTG(session, state); - - try { - MappingSpecification mappingSpec = new MappingSpecification(rules); - boolean fixed = GraphRefactoringUtils.fixIncorrectRoot(tg.identities); - System.err.println("fixed=" + fixed); - IdentityStore idStore = TransferableGraphConversion.extractIdentities(tg); - idStore.printChildMap(); -// System.err.println("ids: " + idStore); -// System.err.println("rc: " + tg.resourceCount); -// System.err.println("idStore: " + idStore.toArray().length); - TIntHashSet parentsAffected = new TIntHashSet(); - GraphRefactoringUtils.refactor(tg, idStore, mappingSpec, parentsAffected); -// - tg.resourceCount = idStore.getResourceCount(); - - tg.identities = idStore.toArray(); - - if(fixed) - GraphRefactoringUtils.unfixIncorrectRoot(tg.identities); - - System.err.println("rc2: " + tg.resourceCount); -// System.err.println("idStore2: " + idStore.toArray().length); - - } catch (GraphRefactoringException e) { - e.printStackTrace(); - } - - } - -} +/******************************************************************************* + * Copyright (c) 2012 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.db.layer0.migration; + +import java.util.ArrayList; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.common.utils.ListUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.graph.query.Path; +import org.simantics.graph.query.TransferableGraphConversion; +import org.simantics.graph.query.UriUtils; +import org.simantics.graph.refactoring.GraphRefactoringException; +import org.simantics.graph.refactoring.GraphRefactoringUtils; +import org.simantics.graph.refactoring.MappingSpecification; +import org.simantics.graph.refactoring.MappingSpecification.MappingRule; +import org.simantics.graph.representation.Identity; +import org.simantics.graph.representation.Internal; +import org.simantics.graph.representation.TransferableGraph1; +import org.simantics.graph.store.IdentityStore; +import org.simantics.layer0.Layer0; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import gnu.trove.set.hash.TIntHashSet; + +public class NamespaceMigrationStep implements MigrationStep { + private static final Logger LOGGER = LoggerFactory.getLogger(NamespaceMigrationStep.class); + + final ArrayList rules; + + public NamespaceMigrationStep(ReadGraph graph, Resource step) throws DatabaseException { + rules = new ArrayList(); + Layer0 L0 = Layer0.getInstance(graph); + for(Resource prefix : ListUtils.toList(graph, step)) { + String base = graph.getPossibleRelatedValue(prefix, L0.NamespaceMigrationStep_Prefix_base, Bindings.STRING); + String from = graph.getPossibleRelatedValue(prefix, L0.NamespaceMigrationStep_Prefix_from, Bindings.STRING); + String to = graph.getPossibleRelatedValue(prefix, L0.NamespaceMigrationStep_Prefix_to, Bindings.STRING); + if(from != null && to != null) { + Path fromURI = UriUtils.uriToPath(base+from); + Path toURI = UriUtils.uriToPath(base+to); + if(fromURI != null && toURI != null) + rules.add(new MappingRule(fromURI, toURI)); + else + LOGGER.error("Namespace migration uri formation error: base " + base + " from " + from + " to " + to); + } + } + } + + @Override + public void applyTo(IProgressMonitor monitor, Session session, MigrationState state) throws DatabaseException { + + TransferableGraph1 tg = MigrationUtils.getTG(session, state); + + try { + MappingSpecification mappingSpec = new MappingSpecification(rules); + boolean fixed = GraphRefactoringUtils.fixIncorrectRoot(tg.identities); + LOGGER.info("fixed=" + fixed); + IdentityStore idStore = TransferableGraphConversion.extractIdentities(tg); + // Mark internal identities new + for(Identity id : tg.identities) + if(id.definition instanceof Internal) + idStore.markNew(id.resource); +// idStore.printChildMap(); +// System.err.println("ids: " + idStore); +// System.err.println("rc: " + tg.resourceCount); +// System.err.println("idStore: " + idStore.toArray().length); + TIntHashSet parentsAffected = new TIntHashSet(); + GraphRefactoringUtils.refactor(tg, idStore, mappingSpec, parentsAffected); +// + tg.resourceCount = idStore.getResourceCount(); + + tg.identities = idStore.toArray(); + + if(fixed) + GraphRefactoringUtils.unfixIncorrectRoot(tg.identities); + + LOGGER.info("rc2: " + tg.resourceCount); +// System.err.println("idStore2: " + idStore.toArray().length); + + } catch (GraphRefactoringException e) { + e.printStackTrace(); + } + + } + +}