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