1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.mapping;
15 import java.util.function.BiFunction;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Statement;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.common.utils.NameUtils;
22 import org.simantics.db.common.utils.OrderedSetUtils;
23 import org.simantics.db.exception.CancelTransactionException;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.request.PossibleModel;
26 import org.simantics.db.layer0.util.Layer0Utils;
27 import org.simantics.diagram.stubs.DiagramResource;
28 import org.simantics.diagram.stubs.G2DResource;
29 import org.simantics.diagram.synchronization.ISynchronizationContext;
30 import org.simantics.diagram.synchronization.StatementEvaluation;
31 import org.simantics.diagram.synchronization.graph.BasicResources;
32 import org.simantics.diagram.synchronization.graph.CopyAdvisorUtil;
33 import org.simantics.diagram.synchronization.graph.GraphCopyAdvisor;
34 import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;
35 import org.simantics.layer0.Layer0;
37 import gnu.trove.map.hash.THashMap;
40 * @author Tuukka Lehtonen
42 public class ElementCopyAdvisor extends GraphCopyAdvisor {
48 * This is necessary to have DIA.Flag type copied over to the copied flag.
49 * Diagram mapping will have problems and potentially break the
50 * configuration if the type is not the same as in the source.
52 BiFunction<ReadGraph, Statement, StatementEvaluation> statementAdvisor =
53 new BiFunction<ReadGraph, Statement, StatementEvaluation>() {
55 public StatementEvaluation apply(ReadGraph graph, Statement stm) {
56 if (DIA.HasFlagType.equals(stm.getPredicate()))
57 return StatementEvaluation.INCLUDE;
58 if (G2D.HasFontStyle.equals(stm.getPredicate()))
59 return StatementEvaluation.INCLUDE;
60 return StatementEvaluation.USE_DEFAULT;
65 public Evaluation canCopy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourcecontainer, Resource targetContainer) throws DatabaseException {
66 BasicResources br = context.get(GraphSynchronizationHints.BASIC_RESOURCES);
67 return graph.isInstanceOf(source, br.DIA.Element) ? Evaluation.SUPPORTED : Evaluation.NOT_SUPPORTED;
71 public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer, Resource targetContainer) throws DatabaseException {
72 return copy(context, graph, source, sourceContainer, targetContainer, new THashMap<Object, Object>());
76 public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,
77 Resource targetContainer, Map<Object, Object> map) throws DatabaseException {
78 Resource model = graph.syncRequest(new PossibleModel(targetContainer));
80 // throw new IllegalArgumentException("no model found for copy target container " + NameUtils.getSafeName(graph, targetContainer, true));
82 Layer0 L0 = Layer0.getInstance(graph);
83 G2D = G2DResource.getInstance(graph);
84 DIA = DiagramResource.getInstance(graph);
86 Resource copy = (model != null) ?
87 CopyAdvisorUtil.copy3(graph, source, model, statementAdvisor, map) :
88 CopyAdvisorUtil.copy2(graph, source, statementAdvisor, map);
90 if (graph.hasStatement(sourceContainer, L0.ConsistsOf, source))
91 graph.claim(targetContainer, L0.ConsistsOf, copy);
93 Layer0Utils.claimNewIdentifier(graph, copy, false);
99 public Object cut(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,
100 Resource targetContainer) throws DatabaseException {
102 Layer0 L0 = Layer0.getInstance(graph);
103 DiagramResource DIA = DiagramResource.getInstance(graph);
105 if (sourceContainer.equals(targetContainer))
108 // disconnect from source diagram
109 if (!OrderedSetUtils.remove(graph, sourceContainer, source))
110 // Unlink failed for some reason.
111 throw new CancelTransactionException("Failed to unlink element "
112 + NameUtils.getSafeName(graph, source) + " from source diagram "
113 + NameUtils.getSafeName(graph, sourceContainer));
115 // connect to target diagram
116 boolean prepend = graph.isInstanceOf(source, DIA.Connection);
119 add = OrderedSetUtils.addFirst(graph, targetContainer, source);
121 add = OrderedSetUtils.add(graph, targetContainer, source);
123 // Link failed for some reason.
124 throw new CancelTransactionException("Failed to link element "
125 + NameUtils.getSafeName(graph, source) + " to target diagram "
126 + NameUtils.getSafeName(graph, targetContainer));
128 // This handles e.g. connections, which are not part of container
129 if(graph.hasStatement(sourceContainer, L0.ConsistsOf, source)) {
130 graph.deny(sourceContainer, L0.ConsistsOf, source);
131 graph.claim(targetContainer, L0.ConsistsOf, source);