Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 08536cadb0d1c03738423bdf289da09a24cb3b0b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*****************************************************************************
 * Copyright (c) 2013, 2017 CEA LIST and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   CEA LIST - Initial API and implementation
 *   Eike Stepper (CEA) - bug 466520
 *****************************************************************************/
package org.eclipse.papyrus.cdo.internal.core.importer;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;

import org.eclipse.core.runtime.IPath;
import org.eclipse.emf.cdo.explorer.checkouts.CDOCheckout;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.DiagnosticChain;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.cdo.core.importer.IModelTransferConfiguration;
import org.eclipse.papyrus.cdo.core.importer.IModelTransferListener;
import org.eclipse.papyrus.cdo.core.importer.IModelTransferMapping;
import org.eclipse.papyrus.cdo.core.importer.IModelTransferMappingListener;
import org.eclipse.papyrus.cdo.core.importer.IModelTransferNode;
import org.eclipse.papyrus.cdo.core.importer.ModelTransferListenerAdapter;
import org.eclipse.papyrus.cdo.internal.core.Activator;
import org.eclipse.papyrus.cdo.internal.core.l10n.Messages;

import com.google.common.base.Objects;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

/**
 * This is the AbstractModelTransferMapping type. Enjoy.
 */
public abstract class AbstractModelTransferMapping implements IModelTransferMapping {

	private final IModelTransferConfiguration config;

	private final Map<IModelTransferNode, IPath> mappings = Maps.newHashMap();

	private CDOCheckout checkout;

	private final CopyOnWriteArrayList<IModelTransferMappingListener> listeners = new CopyOnWriteArrayList<IModelTransferMappingListener>();

	public AbstractModelTransferMapping(IModelTransferConfiguration config) {
		super();

		this.config = config;

		config.addModelTransferListener(createConfigurationListener());
	}

	@Override
	public IModelTransferConfiguration getConfiguration() {
		return config;
	}

	@Override
	public void mapTo(IModelTransferNode source, IPath path) {
		if (!Objects.equal(getMapping(source), path)) {
			mappings.put(source, path);

			fireMappingChanged(source);
		}
	}

	@Override
	public IPath getMapping(IModelTransferNode node) {
		return mappings.get(node);
	}

	@Override
	public CDOCheckout getCheckout() {
		return checkout;
	}

	@Override
	public void setCheckout(CDOCheckout checkout) {
		if (checkout != this.checkout) {
			this.checkout = checkout;

			fireCheckoutChanged();
		}
	}

	@Override
	public Diagnostic validate() {
		BasicDiagnostic result = new BasicDiagnostic();

		if (validateRepository(result)) {
			for (IModelTransferNode node : getConfiguration().getModelsToTransfer()) {
				validateMapping(node, result);
			}

			validateUniqueMappings(result);
		}

		fireProblemsEvent(result);

		return result;
	}

	protected abstract boolean validateMapping(IModelTransferNode node, DiagnosticChain diagnostics);

	protected void validateUniqueMappings(DiagnosticChain diagnostics) {
		Set<IPath> paths = Sets.newHashSet();

		for (IModelTransferNode next : getConfiguration().getModelsToTransfer()) {
			IPath mapping = getMapping(next);

			if ((mapping != null) && !paths.add(mapping)) {
				diagnostics.add(new BasicDiagnostic(Diagnostic.ERROR, Activator.PLUGIN_ID, 0, NLS.bind(Messages.AbstractModelTransferMapping_0, mapping), new Object[] { next }));
				break;
			}
		}
	}

	protected boolean validateRepository(DiagnosticChain diagnostics) {
		boolean result = true;

		if (getCheckout() == null) {
			diagnostics.add(new BasicDiagnostic(Diagnostic.ERROR, Activator.PLUGIN_ID, 0, Messages.AbstractModelTransferMapping_1, null));
			result = false;
		} else if (!getCheckout().isOpen()) {
			diagnostics.add(new BasicDiagnostic(Diagnostic.ERROR, Activator.PLUGIN_ID, 0, NLS.bind(Messages.AbstractModelTransferMapping_2, getCheckout().getLabel()), new Object[] { getCheckout() }));
			result = false;
		}

		return result;
	}

	@Override
	public void addModelTransferMappingListener(IModelTransferMappingListener listener) {
		listeners.addIfAbsent(listener);
	}

	@Override
	public void removeModelTransferMappingListener(IModelTransferMappingListener listener) {
		listeners.remove(listener);
	}

	protected void fireProblemsEvent(Diagnostic problems) {
		if (problems.getSeverity() > Diagnostic.OK) {
			for (IModelTransferMappingListener next : listeners) {
				try {
					next.modelTransferMappingProblemsOccurred(problems);
				} catch (Exception e) {
					Activator.log.error("Uncaught exception in model transfer mapping listener.", e); //$NON-NLS-1$
				}
			}
		}
	}

	protected void fireMappingChanged(IModelTransferNode node) {
		for (IModelTransferMappingListener next : listeners) {
			try {
				next.modelTransferMappingChanged(node);
			} catch (Exception e) {
				Activator.log.error("Uncaught exception in model transfer mapping listener.", e); //$NON-NLS-1$
			}
		}
	}

	protected void fireCheckoutChanged() {
		for (IModelTransferMappingListener next : listeners) {
			try {
				next.modelTransferRepositoryChanged(this);
			} catch (Exception e) {
				Activator.log.error("Uncaught exception in model transfer mapping listener.", e); //$NON-NLS-1$
			}
		}
	}

	private IModelTransferListener createConfigurationListener() {
		return new ModelTransferListenerAdapter() {

			@Override
			public void modelsToTransferChanged(IModelTransferConfiguration configuration) {
				computeDefaultMappings(configuration);
			}
		};
	}

	protected void computeDefaultMappings(IModelTransferConfiguration configuration) {
		// pass
	}
}

Back to the top