X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.layer0%2Fsrc%2Forg%2Fsimantics%2Fdb%2Flayer0%2Futil%2FTGRepresentationUtils.java;h=0bd71af863fa4330b94b01969aff6e736c7c46b4;hp=bf0ec04c59504ac73453cc516c9ab02afb8ef87b;hb=e19c37f84fd1ce2d946578f7c05f3e45444ba67a;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/TGRepresentationUtils.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/TGRepresentationUtils.java index bf0ec04c5..0bd71af86 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/TGRepresentationUtils.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/TGRepresentationUtils.java @@ -1,76 +1,81 @@ -/******************************************************************************* - * Copyright (c) 2016 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: - * Semantum Oy - initial API and implementation - *******************************************************************************/ -package org.simantics.db.layer0.util; - -import java.util.Collections; -import java.util.List; -import java.util.Map; - -import org.simantics.datatypes.literal.GUID; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.Statement; -import org.simantics.db.common.request.PossibleIndexRoot; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.adapter.impl.EntityInstances.QueryIndex; -import org.simantics.db.layer0.util.DomainProcessor3.ExclusionDecision; -import org.simantics.layer0.Layer0; -import org.simantics.scl.runtime.function.Function1; - -/** - * @author Antti Villberg - * @author Tuukka Lehtonen - * @since 1.22 - */ -public class TGRepresentationUtils { - - public static boolean findByIdentifier(ReadGraph graph, Resource targetIndex, Resource source) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); - GUID guid = graph.getPossibleRelatedValue(source, L0.identifier, GUID.BINDING); - if(guid != null) { - List exist = graph.syncRequest(new QueryIndex(targetIndex, L0.Entity, "GUID:" + guid.indexString())); - return !exist.isEmpty(); - } - return false; - } - - public static Function1 computeExclusionFunction(ReadGraph graph, Resource[] resources, Map hints) throws DatabaseException { - Resource targetResource = (Resource)hints.get(ClipboardUtils.HINT_TARGET_RESOURCE); - if(targetResource != null) { - Resource targetRoot = graph.syncRequest(new PossibleIndexRoot(targetResource)); - if(targetRoot != null) { - for(Resource r : resources) { - Resource sourceRoot = graph.syncRequest(new PossibleIndexRoot(r)); - // We are copying a complete index root => no need to change guids - if(r.equals(sourceRoot)) - continue; - if(targetRoot.equals(sourceRoot)) { - // Copy into same index root => guid rewrite is needed - return new GUIDExclusionFunction(graph); - } - // First try the root GUID - if(findByIdentifier(graph, targetRoot, r)) - return new GUIDExclusionFunction(graph); - - // The root is OK - check everything beneath - for(Resource part : ConsistsOfProcess.walk(graph, Collections.singleton(r), Collections.emptySet(), true)) { - if(findByIdentifier(graph, targetRoot, part)) - return new GUIDExclusionFunction(graph); - } - } - } - } - - return null; - } - -} +/******************************************************************************* + * Copyright (c) 2016 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: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.db.layer0.util; + +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.simantics.datatypes.literal.GUID; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.Statement; +import org.simantics.db.common.request.PossibleIndexRoot; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.adapter.impl.EntityInstances.QueryIndex; +import org.simantics.db.layer0.util.ConsistsOfProcess.InternalEntry; +import org.simantics.db.layer0.util.DomainProcessor3.ExclusionDecision; +import org.simantics.layer0.Layer0; +import org.simantics.scl.runtime.function.Function1; +import org.simantics.utils.datastructures.Pair; + +/** + * @author Antti Villberg + * @author Tuukka Lehtonen + * @since 1.22 + */ +public class TGRepresentationUtils { + + public static boolean findByIdentifier(ReadGraph graph, Resource targetIndex, Resource source) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + GUID guid = graph.getPossibleRelatedValue(source, L0.identifier, GUID.BINDING); + if(guid != null) { + List exist = graph.syncRequest(new QueryIndex(targetIndex, L0.Entity, "GUID:" + guid.indexString())); + return !exist.isEmpty(); + } + return false; + } + + public static Function1 computeExclusionFunction(ReadGraph graph, Resource[] resources, Map hints) throws DatabaseException { + Resource targetResource = (Resource)hints.get(ClipboardUtils.HINT_TARGET_RESOURCE); + if(targetResource != null) { + Resource targetRoot = graph.syncRequest(new PossibleIndexRoot(targetResource)); + if(targetRoot != null) { + for(Resource r : resources) { + Resource sourceRoot = graph.syncRequest(new PossibleIndexRoot(r)); + // We are copying a complete index root => no need to change guids + if(r.equals(sourceRoot)) + continue; + if(targetRoot.equals(sourceRoot)) { + // Copy into same index root => guid rewrite is needed + return new GUIDExclusionFunction(graph); + } + // First try the root GUID + if(findByIdentifier(graph, targetRoot, r)) + return new GUIDExclusionFunction(graph); + + // The root is OK - check everything beneath + Pair,Set> pair = ConsistsOfProcess.walk(graph, null, Collections.singleton(r), Collections.emptySet(), true); + List entries = pair.first; + for(InternalEntry entry : entries) { + if(findByIdentifier(graph, targetRoot, entry.resource)) + return new GUIDExclusionFunction(graph); + } + } + } + } + + return null; + } + +}