--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 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
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.mapping;\r
+\r
+import gnu.trove.map.hash.THashMap;\r
+\r
+import java.util.Map;\r
+\r
+import org.simantics.Simantics;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.datatypes.literal.GUID;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.synchronization.ISynchronizationContext;\r
+import org.simantics.diagram.synchronization.SynchronizationException;\r
+import org.simantics.diagram.synchronization.SynchronizationHints;\r
+import org.simantics.diagram.synchronization.graph.BasicResources;\r
+import org.simantics.diagram.synchronization.graph.CopyAdvisorUtil;\r
+import org.simantics.diagram.synchronization.graph.GraphCopyAdvisor;\r
+import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ComponentUtils;\r
+import org.simantics.modeling.services.ComponentNamingUtil;\r
+import org.simantics.modeling.services.NamingException;\r
+import org.simantics.project.IProject;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ComponentCopyAdvisor extends GraphCopyAdvisor {\r
+\r
+ @Override\r
+ public Evaluation canCopy(ISynchronizationContext context, WriteGraph graph, Resource source,\r
+ Resource sourceContainer, Resource targetContainer) throws DatabaseException {\r
+ BasicResources br = context.get(GraphSynchronizationHints.BASIC_RESOURCES);\r
+ return (graph.isInstanceOf(source, br.STR.Component) || graph.isInstanceOf(source, br.STR.Connection))\r
+ ? Evaluation.SUPPORTED : Evaluation.NOT_SUPPORTED;\r
+ }\r
+\r
+ @Override\r
+ public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,\r
+ Resource targetContainer) throws DatabaseException {\r
+ return copy(context, graph, source, sourceContainer, targetContainer, new THashMap<Object, Object>());\r
+ }\r
+\r
+ @Override\r
+ public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,\r
+ Resource targetContainer, Map<Object, Object> map) throws DatabaseException {\r
+ StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
+ Resource copy = null;\r
+ if (graph.isInstanceOf(source, STR.Connection)) {\r
+ // Configuration connections are not named, can't use TG copy for\r
+ // them at the moment.\r
+ copy = CopyAdvisorUtil.copy2(graph, source, null, map);\r
+ } else {\r
+// Resource model = graph.syncRequest(new PossibleModel(targetContainer));\r
+// copy = CopyAdvisorUtil.copy4(graph, source, model);\r
+ copy = CopyAdvisorUtil.copy2(graph, source, null, map);\r
+ }\r
+\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ if (graph.hasStatement(sourceContainer, L0.ConsistsOf, source))\r
+ graph.claim(targetContainer, L0.ConsistsOf, copy);\r
+\r
+ graph.claimLiteral(copy, L0.identifier, L0.identifier_Inverse, L0.GUID, GUID.random(), GUID.BINDING);\r
+ if (context.get(SynchronizationHints.NO_RENAME) == null)\r
+ renameComponent(context, graph, source, copy, sourceContainer, targetContainer);\r
+ return copy;\r
+ }\r
+\r
+ public static String renameComponent(ISynchronizationContext context, WriteGraph graph, Resource source,\r
+ Resource copy, Resource sourceContainer, Resource targetContainer) throws DatabaseException {\r
+ return renameComponent(context, graph, source, copy, sourceContainer, targetContainer,\r
+ Layer0.getInstance(graph).HasName);\r
+ }\r
+\r
+ public static String renameComponent(ISynchronizationContext context, WriteGraph graph, Resource source,\r
+ Resource copy, Resource sourceContainer, Resource targetContainer, Resource nameProperty)\r
+ throws DatabaseException {\r
+ BasicResources br = context.get(GraphSynchronizationHints.BASIC_RESOURCES);\r
+\r
+ Resource sourceComponentType = graph.getPossibleType(source, br.STR.Component);\r
+ if (sourceComponentType == null)\r
+ return null;\r
+\r
+ // Try to obtain a ComponentNamingStrategy for renaming the copied object.\r
+ IProject project = context.get(SynchronizationHints.PROJECT);\r
+ if (project == null)\r
+ project = Simantics.getProject();\r
+\r
+ // Try to give a valid or at least unique name for the new component.\r
+ //Resource configurationRoot = ComponentUtils.getComponentConfigurationRoot(graph, copy);\r
+ //Resource composite = ComponentUtils.tryGetComponentContainer(graph, copy);\r
+ Resource configurationRoot = ComponentUtils.getCompositeConfigurationRoot(graph, targetContainer);\r
+ Resource composite = targetContainer;\r
+\r
+ if (configurationRoot != null) {\r
+ try {\r
+ String name = ComponentNamingUtil.findFreshInstanceName(graph, project, configurationRoot, composite, sourceComponentType);\r
+ graph.claimLiteral(copy, nameProperty, name, Bindings.STRING);\r
+ return name;\r
+ } catch (NamingException e) {\r
+ // This will produce duplicate names in the model,\r
+ // should not happen if we don't run out of names.\r
+ throw new SynchronizationException(e);\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ public Object cut(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,\r
+ Resource targetContainer) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+\r
+ if (sourceContainer.equals(targetContainer))\r
+ return null;\r
+\r
+ // This handles e.g. connections, which are not part of container\r
+ if(graph.hasStatement(sourceContainer, L0.ConsistsOf, source)) {\r
+ graph.deny(sourceContainer, L0.ConsistsOf, source);\r
+ graph.claim(targetContainer, L0.ConsistsOf, source);\r
+ }\r
+\r
+ return source;\r
+ }\r
+\r
+}\r