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