b58aef5ff395a13522e5ac3172079f11622cad9e
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / mapping / ComponentCopyAdvisor.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.mapping;
13
14 import gnu.trove.map.hash.THashMap;
15
16 import java.util.Map;
17
18 import org.simantics.Simantics;
19 import org.simantics.databoard.Bindings;
20 import org.simantics.datatypes.literal.GUID;
21 import org.simantics.db.Resource;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.diagram.synchronization.ISynchronizationContext;
25 import org.simantics.diagram.synchronization.SynchronizationException;
26 import org.simantics.diagram.synchronization.SynchronizationHints;
27 import org.simantics.diagram.synchronization.graph.BasicResources;
28 import org.simantics.diagram.synchronization.graph.CopyAdvisorUtil;
29 import org.simantics.diagram.synchronization.graph.GraphCopyAdvisor;
30 import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;
31 import org.simantics.layer0.Layer0;
32 import org.simantics.modeling.ComponentUtils;
33 import org.simantics.modeling.services.ComponentNamingUtil;
34 import org.simantics.modeling.services.NamingException;
35 import org.simantics.project.IProject;
36 import org.simantics.structural.stubs.StructuralResource2;
37
38 /**
39  * @author Tuukka Lehtonen
40  */
41 public class ComponentCopyAdvisor extends GraphCopyAdvisor {
42
43     @Override
44     public Evaluation canCopy(ISynchronizationContext context, WriteGraph graph, Resource source,
45             Resource sourceContainer, Resource targetContainer) throws DatabaseException {
46         BasicResources br = context.get(GraphSynchronizationHints.BASIC_RESOURCES);
47         return (graph.isInstanceOf(source, br.STR.Component) || graph.isInstanceOf(source, br.STR.Connection))
48         ? Evaluation.SUPPORTED : Evaluation.NOT_SUPPORTED;
49     }
50
51     @Override
52     public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,
53             Resource targetContainer) throws DatabaseException {
54         return copy(context, graph, source, sourceContainer, targetContainer, new THashMap<Object, Object>());
55     }
56
57     @Override
58     public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,
59             Resource targetContainer, Map<Object, Object> map) throws DatabaseException {
60         StructuralResource2 STR = StructuralResource2.getInstance(graph);
61         Resource copy = null;
62         if (graph.isInstanceOf(source, STR.Connection)) {
63             // Configuration connections are not named, can't use TG copy for
64             // them at the moment.
65             copy = CopyAdvisorUtil.copy2(graph, source, null, map);
66         } else {
67 //            Resource model = graph.syncRequest(new PossibleModel(targetContainer));
68 //            copy = CopyAdvisorUtil.copy4(graph, source, model);
69             copy = CopyAdvisorUtil.copy2(graph, source, null, map);
70         }
71
72         Layer0 L0 = Layer0.getInstance(graph);
73         if (graph.hasStatement(sourceContainer, L0.ConsistsOf, source))
74             graph.claim(targetContainer, L0.ConsistsOf, copy);
75
76         graph.claimLiteral(copy, L0.identifier, L0.identifier_Inverse, L0.GUID, GUID.random(), GUID.BINDING);
77         if (context.get(SynchronizationHints.NO_RENAME) == null)
78             renameComponent(context, graph, source, copy, sourceContainer, targetContainer);
79         return copy;
80     }
81
82     public static String renameComponent(ISynchronizationContext context, WriteGraph graph, Resource source,
83             Resource copy, Resource sourceContainer, Resource targetContainer) throws DatabaseException {
84         return renameComponent(context, graph, source, copy, sourceContainer, targetContainer,
85                 Layer0.getInstance(graph).HasName);
86     }
87
88     public static String renameComponent(ISynchronizationContext context, WriteGraph graph, Resource source,
89             Resource copy, Resource sourceContainer, Resource targetContainer, Resource nameProperty)
90             throws DatabaseException {
91         BasicResources br = context.get(GraphSynchronizationHints.BASIC_RESOURCES);
92
93         Resource sourceComponentType = graph.getPossibleType(source, br.STR.Component);
94         if (sourceComponentType == null)
95             return null;
96
97         // Try to obtain a ComponentNamingStrategy for renaming the copied object.
98         IProject project = context.get(SynchronizationHints.PROJECT);
99         if (project == null)
100             project = Simantics.getProject();
101
102         // Try to give a valid or at least unique name for the new component.
103         //Resource configurationRoot = ComponentUtils.getComponentConfigurationRoot(graph, copy);
104         //Resource composite = ComponentUtils.tryGetComponentContainer(graph, copy);
105         Resource configurationRoot = ComponentUtils.getCompositeConfigurationRoot(graph, targetContainer);
106         Resource composite = targetContainer;
107
108         if (configurationRoot != null) {
109             try {
110                 String name = ComponentNamingUtil.findFreshInstanceName(graph, project, configurationRoot, composite, sourceComponentType);
111                 graph.claimLiteral(copy, nameProperty, name, Bindings.STRING);
112                 return name;
113             } catch (NamingException e) {
114                 // This will produce duplicate names in the model,
115                 // should not happen if we don't run out of names.
116                 throw new SynchronizationException(e);
117             }
118         }
119         return null;
120     }
121
122     @Override
123     public Object cut(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,
124             Resource targetContainer) throws DatabaseException {
125         Layer0 L0 = Layer0.getInstance(graph);
126
127         if (sourceContainer.equals(targetContainer))
128             return null;
129
130         // This handles e.g. connections, which are not part of container
131         if(graph.hasStatement(sourceContainer, L0.ConsistsOf, source)) {
132             graph.deny(sourceContainer, L0.ConsistsOf, source);
133             graph.claim(targetContainer, L0.ConsistsOf, source);
134         }
135
136         return source;
137     }
138
139 }