Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ecc06ee491153de3065284c9337d9828cb3a4d03 (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
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 * 
 * 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
 *****************************************************************************/
package org.eclipse.papyrus.cdo.internal.core.exporter;

import java.util.Iterator;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.DiagnosticChain;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.papyrus.cdo.core.exporter.IModelExportMapping;
import org.eclipse.papyrus.cdo.core.exporter.IModelExporter;
import org.eclipse.papyrus.cdo.core.importer.IModelTransferConfiguration;
import org.eclipse.papyrus.cdo.core.importer.IModelTransferNode;
import org.eclipse.papyrus.cdo.core.importer.IModelTransferOperation;
import org.eclipse.papyrus.cdo.internal.core.Activator;
import org.eclipse.papyrus.cdo.internal.core.CDOProxyResolvingResourceSet;
import org.eclipse.papyrus.cdo.internal.core.IInternalPapyrusRepository;
import org.eclipse.papyrus.cdo.internal.core.l10n.Messages;

/**
 * This is the ModelExporter type. Enjoy.
 */
public class ModelExporter implements IModelExporter {

	public ModelExporter() {
		super();
	}

	public Diagnostic exportModels(final IModelExportMapping mapping) {
		BasicDiagnostic result = new BasicDiagnostic();

		add(result, mapping.getConfiguration().validate());
		add(result, mapping.validate());

		if(result.getSeverity() < Diagnostic.ERROR) {
			add(result, mapping.getConfiguration().getOperationContext().run(new IModelTransferOperation() {

				public Diagnostic run(IProgressMonitor monitor) {
					return doExport(mapping, monitor);
				}
			}));
		}

		return result;
	}

	protected Diagnostic doExport(IModelExportMapping mapping, IProgressMonitor monitor) {
		BasicDiagnostic result = new BasicDiagnostic();
		IModelTransferConfiguration configuration = mapping.getConfiguration();

		// 1/resource for loading, 1/resource for exporting,
		// 1 for proxy resolution, 1 for resource saving, and 1 for clean-up
		SubMonitor sub = SubMonitor.convert(monitor, Messages.ModelExporter_0, 2 * configuration.getModelsToTransfer().size() + 3);

		IInternalPapyrusRepository repository = (IInternalPapyrusRepository)mapping.getRepository();
		ResourceSet destination = new ResourceSetImpl();
		ResourceSet source = repository.createTransaction(new CDOProxyResolvingResourceSet());
		CDOView view = repository.getCDOView(source);

		try {
			// load all models to be exported and resolve their cross-references so that CDO-style
			// cross-resource proxies will be resolved
			for(IModelTransferNode model : configuration.getModelsToTransfer()) {
				add(result, loadModel(model, view, sub.newChild(1)));
			}
			EcoreUtil.resolveAll(source);
			sub.worked(1);

			for(IModelTransferNode model : configuration.getModelsToTransfer()) {
				add(result, exportModel(model, view, mapping.getMapping(model), destination, sub.newChild(1)));
			}

			for(IModelTransferNode model : configuration.getModelsToTransfer()) {
				add(result, saveModel(model, view, mapping.getMapping(model), destination));
			}
			sub.worked(1);
		} finally {
			// don't clean up the configuration's resource set because it is not owned by the configuration
			cleanUp(destination);
			repository.close(source);
			cleanUp(source);
			sub.worked(1);
		}

		sub.done();

		return result;
	}

	protected Diagnostic loadModel(IModelTransferNode model, CDOView view, IProgressMonitor monitor) {
		BasicDiagnostic result = new BasicDiagnostic();

		SubMonitor sub = SubMonitor.convert(monitor, model.getName(), model.getResourceURIs().size());

		for(URI next : model.getResourceURIs()) {
			Resource source = view.getResourceSet().getResource(next, true);
			for(Iterator<?> iter = source.getContents().iterator(); iter.hasNext(); iter.next()) {
				// just iterate them to load the contents; we will walk over the content trees later to resolve proxies
			}
			sub.worked(1);
		}

		sub.done();

		return result;
	}

	protected Diagnostic exportModel(IModelTransferNode model, CDOView view, IPath toPath, ResourceSet rset, IProgressMonitor monitor) {
		BasicDiagnostic result = new BasicDiagnostic();

		IPath basePath = toPath.removeFileExtension();

		SubMonitor sub = SubMonitor.convert(monitor, model.getName(), model.getResourceURIs().size());

		for(URI next : model.getResourceURIs()) {
			Resource source = view.getResourceSet().getResource(next, true);
			Resource destination = rset.createResource(URI.createPlatformResourceURI(basePath.addFileExtension(next.fileExtension()).toString(), true));
			add(result, exportResource(source, destination));
			sub.worked(1);
		}

		sub.done();

		return result;
	}

	protected Diagnostic exportResource(Resource source, Resource destination) {
		destination.getContents().addAll(source.getContents());

		return Diagnostic.OK_INSTANCE;
	}

	protected Diagnostic saveModel(IModelTransferNode model, CDOView view, IPath toPath, ResourceSet rset) {
		BasicDiagnostic result = new BasicDiagnostic();

		IPath basePath = toPath.removeFileExtension();

		for(URI next : model.getResourceURIs()) {
			Resource destination = rset.getResource(URI.createPlatformResourceURI(basePath.addFileExtension(next.fileExtension()).toString(), true), false);
			try {
				destination.save(null);
			} catch (Exception e) {
				add(result, new BasicDiagnostic(IStatus.ERROR, Activator.PLUGIN_ID, 0, Messages.ModelExporter_1, new Object[]{ e }));
			}
		}

		return result;
	}

	private void cleanUp(ResourceSet resourceSet) {
		for(Resource next : resourceSet.getResources()) {
			next.unload();
			next.eAdapters().clear();
		}
		resourceSet.getResources().clear();
	}

	private static void add(DiagnosticChain diagnostics, Diagnostic diagnostic) {
		if(diagnostic.getSeverity() > Diagnostic.OK) {
			diagnostics.merge(diagnostic);
		}
	}
}

Back to the top