1 /*******************************************************************************
2 * Copyright (c) 2016 Association for Decentralized Information Management
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
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.layer0.util;
14 import java.util.Collections;
15 import java.util.List;
19 import org.simantics.datatypes.literal.GUID;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.Statement;
23 import org.simantics.db.common.request.PossibleIndexRoot;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.adapter.impl.EntityInstances.QueryIndex;
26 import org.simantics.db.layer0.util.ConsistsOfProcess.ConsistsOfProcessEntry;
27 import org.simantics.db.layer0.util.DomainProcessor3.ExclusionDecision;
28 import org.simantics.db.layer0.util.TransferableGraphConfiguration2.SeedSpec;
29 import org.simantics.layer0.Layer0;
30 import org.simantics.scl.runtime.function.Function1;
31 import org.simantics.utils.datastructures.Pair;
34 * @author Antti Villberg
35 * @author Tuukka Lehtonen
38 public class TGRepresentationUtils {
40 public static boolean findByIdentifier(ReadGraph graph, Resource targetIndex, Resource source) throws DatabaseException {
41 Layer0 L0 = Layer0.getInstance(graph);
42 GUID guid = graph.getPossibleRelatedValue(source, L0.identifier, GUID.BINDING);
44 List<Resource> exist = graph.syncRequest(new QueryIndex(targetIndex, L0.Entity, "GUID:" + guid.indexString()));
45 return !exist.isEmpty();
50 public static Function1<Statement, ExclusionDecision> computeExclusionFunction(ReadGraph graph, Resource[] resources, Map<String,Object> hints) throws DatabaseException {
51 Resource targetResource = (Resource)hints.get(ClipboardUtils.HINT_TARGET_RESOURCE);
52 if(targetResource != null) {
53 Resource targetRoot = graph.syncRequest(new PossibleIndexRoot(targetResource));
54 if(targetRoot != null) {
55 for(Resource r : resources) {
56 Resource sourceRoot = graph.syncRequest(new PossibleIndexRoot(r));
57 // We are copying a complete index root => no need to change guids
58 if(r.equals(sourceRoot))
60 if(targetRoot.equals(sourceRoot)) {
61 // Copy into same index root => guid rewrite is needed
62 return new GUIDExclusionFunction(graph);
64 // First try the root GUID
65 if(findByIdentifier(graph, targetRoot, r))
66 return new GUIDExclusionFunction(graph);
68 // The root is OK - check everything beneath
69 Pair<List<ConsistsOfProcessEntry>,Set<Resource>> pair = ConsistsOfProcess.walk(graph, Collections.singleton(SeedSpec.internal(r)), true);
70 List<ConsistsOfProcessEntry> entries = pair.first;
71 for(ConsistsOfProcessEntry entry : entries) {
72 if(findByIdentifier(graph, targetRoot, entry.resource))
73 return new GUIDExclusionFunction(graph);