--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2016 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.layer0.util;\r
+\r
+import java.util.Collections;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+import org.simantics.datatypes.literal.GUID;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Statement;\r
+import org.simantics.db.common.request.PossibleIndexRoot;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.impl.EntityInstances.QueryIndex;\r
+import org.simantics.db.layer0.util.DomainProcessor3.ExclusionDecision;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.scl.runtime.function.Function1;\r
+\r
+/**\r
+ * @author Antti Villberg\r
+ * @author Tuukka Lehtonen\r
+ * @since 1.22\r
+ */\r
+public class TGRepresentationUtils {\r
+\r
+ public static boolean findByIdentifier(ReadGraph graph, Resource targetIndex, Resource source) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ GUID guid = graph.getPossibleRelatedValue(source, L0.identifier, GUID.BINDING);\r
+ if(guid != null) {\r
+ List<Resource> exist = graph.syncRequest(new QueryIndex(targetIndex, L0.Entity, "GUID:" + guid.indexString()));\r
+ return !exist.isEmpty();\r
+ }\r
+ return false;\r
+ }\r
+\r
+ public static Function1<Statement, ExclusionDecision> computeExclusionFunction(ReadGraph graph, Resource[] resources, Map<String,Object> hints) throws DatabaseException {\r
+ Resource targetResource = (Resource)hints.get(ClipboardUtils.HINT_TARGET_RESOURCE);\r
+ if(targetResource != null) {\r
+ Resource targetRoot = graph.syncRequest(new PossibleIndexRoot(targetResource));\r
+ if(targetRoot != null) {\r
+ for(Resource r : resources) {\r
+ Resource sourceRoot = graph.syncRequest(new PossibleIndexRoot(r));\r
+ // We are copying a complete index root => no need to change guids\r
+ if(r.equals(sourceRoot))\r
+ continue;\r
+ if(targetRoot.equals(sourceRoot)) {\r
+ // Copy into same index root => guid rewrite is needed\r
+ return new GUIDExclusionFunction(graph);\r
+ }\r
+ // First try the root GUID\r
+ if(findByIdentifier(graph, targetRoot, r))\r
+ return new GUIDExclusionFunction(graph);\r
+\r
+ // The root is OK - check everything beneath\r
+ for(Resource part : ConsistsOfProcess.walk(graph, Collections.singleton(r), Collections.emptySet(), true)) {\r
+ if(findByIdentifier(graph, targetRoot, part))\r
+ return new GUIDExclusionFunction(graph);\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ return null;\r
+ }\r
+\r
+}\r