]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/TGRepresentationUtils.java
bf0ec04c59504ac73453cc516c9ab02afb8ef87b
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / TGRepresentationUtils.java
1 /*******************************************************************************\r
2  * Copyright (c) 2016 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     Semantum Oy - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.layer0.util;\r
13 \r
14 import java.util.Collections;\r
15 import java.util.List;\r
16 import java.util.Map;\r
17 \r
18 import org.simantics.datatypes.literal.GUID;\r
19 import org.simantics.db.ReadGraph;\r
20 import org.simantics.db.Resource;\r
21 import org.simantics.db.Statement;\r
22 import org.simantics.db.common.request.PossibleIndexRoot;\r
23 import org.simantics.db.exception.DatabaseException;\r
24 import org.simantics.db.layer0.adapter.impl.EntityInstances.QueryIndex;\r
25 import org.simantics.db.layer0.util.DomainProcessor3.ExclusionDecision;\r
26 import org.simantics.layer0.Layer0;\r
27 import org.simantics.scl.runtime.function.Function1;\r
28 \r
29 /**\r
30  * @author Antti Villberg\r
31  * @author Tuukka Lehtonen\r
32  * @since 1.22\r
33  */\r
34 public class TGRepresentationUtils {\r
35 \r
36     public static boolean findByIdentifier(ReadGraph graph, Resource targetIndex, Resource source) throws DatabaseException {\r
37         Layer0 L0 = Layer0.getInstance(graph);\r
38         GUID guid = graph.getPossibleRelatedValue(source, L0.identifier, GUID.BINDING);\r
39         if(guid != null) {\r
40             List<Resource> exist = graph.syncRequest(new QueryIndex(targetIndex, L0.Entity, "GUID:" + guid.indexString()));\r
41             return !exist.isEmpty();\r
42         }\r
43         return false;\r
44     }\r
45 \r
46     public static Function1<Statement, ExclusionDecision> computeExclusionFunction(ReadGraph graph, Resource[] resources, Map<String,Object> hints) throws DatabaseException {\r
47         Resource targetResource = (Resource)hints.get(ClipboardUtils.HINT_TARGET_RESOURCE);\r
48         if(targetResource != null) {\r
49             Resource targetRoot = graph.syncRequest(new PossibleIndexRoot(targetResource));\r
50             if(targetRoot != null) {\r
51                 for(Resource r : resources) {\r
52                     Resource sourceRoot = graph.syncRequest(new PossibleIndexRoot(r));\r
53                     // We are copying a complete index root => no need to change guids\r
54                     if(r.equals(sourceRoot))\r
55                         continue;\r
56                     if(targetRoot.equals(sourceRoot)) {\r
57                         // Copy into same index root => guid rewrite is needed\r
58                         return new GUIDExclusionFunction(graph);\r
59                     }\r
60                     // First try the root GUID\r
61                     if(findByIdentifier(graph, targetRoot, r))\r
62                         return new GUIDExclusionFunction(graph);\r
63 \r
64                     // The root is OK - check everything beneath\r
65                     for(Resource part : ConsistsOfProcess.walk(graph, Collections.singleton(r), Collections.emptySet(), true)) {\r
66                         if(findByIdentifier(graph, targetRoot, part))\r
67                             return new GUIDExclusionFunction(graph);\r
68                     }\r
69                 }\r
70             }\r
71         }\r
72 \r
73         return null;\r
74     }\r
75 \r
76 }\r